annotate clang/lib/Lex/PPLexerChange.cpp @ 200:40b1cab18437

swap CurLexer
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Fri, 04 Jun 2021 20:14:29 +0900
parents f935e5e0dbe7
children dd44ba33042e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
150
anatofuz
parents:
diff changeset
1 //===--- PPLexerChange.cpp - Handle changing lexers in the preprocessor ---===//
anatofuz
parents:
diff changeset
2 //
anatofuz
parents:
diff changeset
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
anatofuz
parents:
diff changeset
4 // See https://llvm.org/LICENSE.txt for license information.
anatofuz
parents:
diff changeset
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
anatofuz
parents:
diff changeset
6 //
anatofuz
parents:
diff changeset
7 //===----------------------------------------------------------------------===//
anatofuz
parents:
diff changeset
8 //
anatofuz
parents:
diff changeset
9 // This file implements pieces of the Preprocessor interface that manage the
anatofuz
parents:
diff changeset
10 // current lexer stack.
anatofuz
parents:
diff changeset
11 //
anatofuz
parents:
diff changeset
12 //===----------------------------------------------------------------------===//
anatofuz
parents:
diff changeset
13
anatofuz
parents:
diff changeset
14 #include "clang/Lex/Preprocessor.h"
anatofuz
parents:
diff changeset
15 #include "clang/Lex/PreprocessorOptions.h"
anatofuz
parents:
diff changeset
16 #include "clang/Basic/FileManager.h"
anatofuz
parents:
diff changeset
17 #include "clang/Basic/SourceManager.h"
anatofuz
parents:
diff changeset
18 #include "clang/Lex/HeaderSearch.h"
anatofuz
parents:
diff changeset
19 #include "clang/Lex/LexDiagnostic.h"
anatofuz
parents:
diff changeset
20 #include "clang/Lex/MacroInfo.h"
anatofuz
parents:
diff changeset
21 #include "llvm/ADT/StringSwitch.h"
anatofuz
parents:
diff changeset
22 #include "llvm/Support/FileSystem.h"
anatofuz
parents:
diff changeset
23 #include "llvm/Support/MemoryBuffer.h"
anatofuz
parents:
diff changeset
24 #include "llvm/Support/Path.h"
anatofuz
parents:
diff changeset
25 using namespace clang;
anatofuz
parents:
diff changeset
26
anatofuz
parents:
diff changeset
27 //===----------------------------------------------------------------------===//
anatofuz
parents:
diff changeset
28 // Miscellaneous Methods.
anatofuz
parents:
diff changeset
29 //===----------------------------------------------------------------------===//
anatofuz
parents:
diff changeset
30
anatofuz
parents:
diff changeset
31 /// isInPrimaryFile - Return true if we're in the top-level file, not in a
anatofuz
parents:
diff changeset
32 /// \#include. This looks through macro expansions and active _Pragma lexers.
anatofuz
parents:
diff changeset
33 bool Preprocessor::isInPrimaryFile() const {
anatofuz
parents:
diff changeset
34 if (IsFileLexer())
anatofuz
parents:
diff changeset
35 return IncludeMacroStack.empty();
anatofuz
parents:
diff changeset
36
anatofuz
parents:
diff changeset
37 // If there are any stacked lexers, we're in a #include.
anatofuz
parents:
diff changeset
38 assert(IsFileLexer(IncludeMacroStack[0]) &&
anatofuz
parents:
diff changeset
39 "Top level include stack isn't our primary lexer?");
anatofuz
parents:
diff changeset
40 return std::none_of(
anatofuz
parents:
diff changeset
41 IncludeMacroStack.begin() + 1, IncludeMacroStack.end(),
anatofuz
parents:
diff changeset
42 [&](const IncludeStackInfo &ISI) -> bool { return IsFileLexer(ISI); });
anatofuz
parents:
diff changeset
43 }
anatofuz
parents:
diff changeset
44
anatofuz
parents:
diff changeset
45 /// getCurrentLexer - Return the current file lexer being lexed from. Note
anatofuz
parents:
diff changeset
46 /// that this ignores any potentially active macro expansions and _Pragma
anatofuz
parents:
diff changeset
47 /// expansions going on at the time.
anatofuz
parents:
diff changeset
48 PreprocessorLexer *Preprocessor::getCurrentFileLexer() const {
anatofuz
parents:
diff changeset
49 if (IsFileLexer())
anatofuz
parents:
diff changeset
50 return CurPPLexer;
anatofuz
parents:
diff changeset
51
anatofuz
parents:
diff changeset
52 // Look for a stacked lexer.
anatofuz
parents:
diff changeset
53 for (const IncludeStackInfo &ISI : llvm::reverse(IncludeMacroStack)) {
anatofuz
parents:
diff changeset
54 if (IsFileLexer(ISI))
anatofuz
parents:
diff changeset
55 return ISI.ThePPLexer;
anatofuz
parents:
diff changeset
56 }
anatofuz
parents:
diff changeset
57 return nullptr;
anatofuz
parents:
diff changeset
58 }
anatofuz
parents:
diff changeset
59
anatofuz
parents:
diff changeset
60
anatofuz
parents:
diff changeset
61 //===----------------------------------------------------------------------===//
anatofuz
parents:
diff changeset
62 // Methods for Entering and Callbacks for leaving various contexts
anatofuz
parents:
diff changeset
63 //===----------------------------------------------------------------------===//
anatofuz
parents:
diff changeset
64
anatofuz
parents:
diff changeset
65 /// EnterSourceFile - Add a source file to the top of the include stack and
anatofuz
parents:
diff changeset
66 /// start lexing tokens from it instead of the current buffer.
anatofuz
parents:
diff changeset
67 bool Preprocessor::EnterSourceFile(FileID FID, const DirectoryLookup *CurDir,
anatofuz
parents:
diff changeset
68 SourceLocation Loc) {
anatofuz
parents:
diff changeset
69 assert(!CurTokenLexer && "Cannot #include a file inside a macro!");
anatofuz
parents:
diff changeset
70 ++NumEnteredSourceFiles;
anatofuz
parents:
diff changeset
71
anatofuz
parents:
diff changeset
72 if (MaxIncludeStackDepth < IncludeMacroStack.size())
anatofuz
parents:
diff changeset
73 MaxIncludeStackDepth = IncludeMacroStack.size();
anatofuz
parents:
diff changeset
74
anatofuz
parents:
diff changeset
75 // Get the MemoryBuffer for this FID, if it fails, we fail.
anatofuz
parents:
diff changeset
76 bool Invalid = false;
anatofuz
parents:
diff changeset
77 const llvm::MemoryBuffer *InputFile =
anatofuz
parents:
diff changeset
78 getSourceManager().getBuffer(FID, Loc, &Invalid);
anatofuz
parents:
diff changeset
79 if (Invalid) {
anatofuz
parents:
diff changeset
80 SourceLocation FileStart = SourceMgr.getLocForStartOfFile(FID);
anatofuz
parents:
diff changeset
81 Diag(Loc, diag::err_pp_error_opening_file)
anatofuz
parents:
diff changeset
82 << std::string(SourceMgr.getBufferName(FileStart)) << "";
anatofuz
parents:
diff changeset
83 return true;
anatofuz
parents:
diff changeset
84 }
anatofuz
parents:
diff changeset
85
anatofuz
parents:
diff changeset
86 if (isCodeCompletionEnabled() &&
anatofuz
parents:
diff changeset
87 SourceMgr.getFileEntryForID(FID) == CodeCompletionFile) {
anatofuz
parents:
diff changeset
88 CodeCompletionFileLoc = SourceMgr.getLocForStartOfFile(FID);
anatofuz
parents:
diff changeset
89 CodeCompletionLoc =
anatofuz
parents:
diff changeset
90 CodeCompletionFileLoc.getLocWithOffset(CodeCompletionOffset);
anatofuz
parents:
diff changeset
91 }
anatofuz
parents:
diff changeset
92
anatofuz
parents:
diff changeset
93 EnterSourceFileWithLexer(new Lexer(FID, InputFile, *this), CurDir);
anatofuz
parents:
diff changeset
94 return false;
anatofuz
parents:
diff changeset
95 }
anatofuz
parents:
diff changeset
96
anatofuz
parents:
diff changeset
97 /// EnterSourceFileWithLexer - Add a source file to the top of the include stack
anatofuz
parents:
diff changeset
98 /// and start lexing tokens from it instead of the current buffer.
anatofuz
parents:
diff changeset
99 void Preprocessor::EnterSourceFileWithLexer(Lexer *TheLexer,
anatofuz
parents:
diff changeset
100 const DirectoryLookup *CurDir) {
anatofuz
parents:
diff changeset
101
anatofuz
parents:
diff changeset
102 // Add the current lexer to the include stack.
anatofuz
parents:
diff changeset
103 if (CurPPLexer || CurTokenLexer)
anatofuz
parents:
diff changeset
104 PushIncludeMacroStack();
anatofuz
parents:
diff changeset
105
anatofuz
parents:
diff changeset
106 CurLexer.reset(TheLexer);
anatofuz
parents:
diff changeset
107 CurPPLexer = TheLexer;
anatofuz
parents:
diff changeset
108 CurDirLookup = CurDir;
anatofuz
parents:
diff changeset
109 CurLexerSubmodule = nullptr;
anatofuz
parents:
diff changeset
110 if (CurLexerKind != CLK_LexAfterModuleImport)
anatofuz
parents:
diff changeset
111 CurLexerKind = CLK_Lexer;
anatofuz
parents:
diff changeset
112
anatofuz
parents:
diff changeset
113 // Notify the client, if desired, that we are in a new source file.
anatofuz
parents:
diff changeset
114 if (Callbacks && !CurLexer->Is_PragmaLexer) {
anatofuz
parents:
diff changeset
115 SrcMgr::CharacteristicKind FileType =
anatofuz
parents:
diff changeset
116 SourceMgr.getFileCharacteristic(CurLexer->getFileLoc());
anatofuz
parents:
diff changeset
117
anatofuz
parents:
diff changeset
118 Callbacks->FileChanged(CurLexer->getFileLoc(),
anatofuz
parents:
diff changeset
119 PPCallbacks::EnterFile, FileType);
anatofuz
parents:
diff changeset
120 }
anatofuz
parents:
diff changeset
121 }
anatofuz
parents:
diff changeset
122
anatofuz
parents:
diff changeset
123 /// EnterMacro - Add a Macro to the top of the include stack and start lexing
anatofuz
parents:
diff changeset
124 /// tokens from it instead of the current buffer.
anatofuz
parents:
diff changeset
125 void Preprocessor::EnterMacro(Token &Tok, SourceLocation ILEnd,
anatofuz
parents:
diff changeset
126 MacroInfo *Macro, MacroArgs *Args) {
anatofuz
parents:
diff changeset
127 std::unique_ptr<TokenLexer> TokLexer;
anatofuz
parents:
diff changeset
128 if (NumCachedTokenLexers == 0) {
anatofuz
parents:
diff changeset
129 TokLexer = std::make_unique<TokenLexer>(Tok, ILEnd, Macro, Args, *this);
anatofuz
parents:
diff changeset
130 } else {
anatofuz
parents:
diff changeset
131 TokLexer = std::move(TokenLexerCache[--NumCachedTokenLexers]);
anatofuz
parents:
diff changeset
132 TokLexer->Init(Tok, ILEnd, Macro, Args);
anatofuz
parents:
diff changeset
133 }
anatofuz
parents:
diff changeset
134
anatofuz
parents:
diff changeset
135 PushIncludeMacroStack();
anatofuz
parents:
diff changeset
136 CurDirLookup = nullptr;
anatofuz
parents:
diff changeset
137 CurTokenLexer = std::move(TokLexer);
anatofuz
parents:
diff changeset
138 if (CurLexerKind != CLK_LexAfterModuleImport)
anatofuz
parents:
diff changeset
139 CurLexerKind = CLK_TokenLexer;
anatofuz
parents:
diff changeset
140 }
anatofuz
parents:
diff changeset
141
anatofuz
parents:
diff changeset
142 /// EnterTokenStream - Add a "macro" context to the top of the include stack,
anatofuz
parents:
diff changeset
143 /// which will cause the lexer to start returning the specified tokens.
anatofuz
parents:
diff changeset
144 ///
anatofuz
parents:
diff changeset
145 /// If DisableMacroExpansion is true, tokens lexed from the token stream will
anatofuz
parents:
diff changeset
146 /// not be subject to further macro expansion. Otherwise, these tokens will
anatofuz
parents:
diff changeset
147 /// be re-macro-expanded when/if expansion is enabled.
anatofuz
parents:
diff changeset
148 ///
anatofuz
parents:
diff changeset
149 /// If OwnsTokens is false, this method assumes that the specified stream of
anatofuz
parents:
diff changeset
150 /// tokens has a permanent owner somewhere, so they do not need to be copied.
anatofuz
parents:
diff changeset
151 /// If it is true, it assumes the array of tokens is allocated with new[] and
anatofuz
parents:
diff changeset
152 /// must be freed.
anatofuz
parents:
diff changeset
153 ///
anatofuz
parents:
diff changeset
154 void Preprocessor::EnterTokenStream(const Token *Toks, unsigned NumToks,
anatofuz
parents:
diff changeset
155 bool DisableMacroExpansion, bool OwnsTokens,
anatofuz
parents:
diff changeset
156 bool IsReinject) {
anatofuz
parents:
diff changeset
157 if (CurLexerKind == CLK_CachingLexer) {
anatofuz
parents:
diff changeset
158 if (CachedLexPos < CachedTokens.size()) {
anatofuz
parents:
diff changeset
159 assert(IsReinject && "new tokens in the middle of cached stream");
anatofuz
parents:
diff changeset
160 // We're entering tokens into the middle of our cached token stream. We
anatofuz
parents:
diff changeset
161 // can't represent that, so just insert the tokens into the buffer.
anatofuz
parents:
diff changeset
162 CachedTokens.insert(CachedTokens.begin() + CachedLexPos,
anatofuz
parents:
diff changeset
163 Toks, Toks + NumToks);
anatofuz
parents:
diff changeset
164 if (OwnsTokens)
anatofuz
parents:
diff changeset
165 delete [] Toks;
anatofuz
parents:
diff changeset
166 return;
anatofuz
parents:
diff changeset
167 }
anatofuz
parents:
diff changeset
168
anatofuz
parents:
diff changeset
169 // New tokens are at the end of the cached token sequnece; insert the
anatofuz
parents:
diff changeset
170 // token stream underneath the caching lexer.
anatofuz
parents:
diff changeset
171 ExitCachingLexMode();
anatofuz
parents:
diff changeset
172 EnterTokenStream(Toks, NumToks, DisableMacroExpansion, OwnsTokens,
anatofuz
parents:
diff changeset
173 IsReinject);
anatofuz
parents:
diff changeset
174 EnterCachingLexMode();
anatofuz
parents:
diff changeset
175 return;
anatofuz
parents:
diff changeset
176 }
anatofuz
parents:
diff changeset
177
anatofuz
parents:
diff changeset
178 // Create a macro expander to expand from the specified token stream.
anatofuz
parents:
diff changeset
179 std::unique_ptr<TokenLexer> TokLexer;
anatofuz
parents:
diff changeset
180 if (NumCachedTokenLexers == 0) {
anatofuz
parents:
diff changeset
181 TokLexer = std::make_unique<TokenLexer>(
anatofuz
parents:
diff changeset
182 Toks, NumToks, DisableMacroExpansion, OwnsTokens, IsReinject, *this);
anatofuz
parents:
diff changeset
183 } else {
anatofuz
parents:
diff changeset
184 TokLexer = std::move(TokenLexerCache[--NumCachedTokenLexers]);
anatofuz
parents:
diff changeset
185 TokLexer->Init(Toks, NumToks, DisableMacroExpansion, OwnsTokens,
anatofuz
parents:
diff changeset
186 IsReinject);
anatofuz
parents:
diff changeset
187 }
anatofuz
parents:
diff changeset
188
anatofuz
parents:
diff changeset
189 // Save our current state.
anatofuz
parents:
diff changeset
190 PushIncludeMacroStack();
anatofuz
parents:
diff changeset
191 CurDirLookup = nullptr;
anatofuz
parents:
diff changeset
192 CurTokenLexer = std::move(TokLexer);
anatofuz
parents:
diff changeset
193 if (CurLexerKind != CLK_LexAfterModuleImport)
anatofuz
parents:
diff changeset
194 CurLexerKind = CLK_TokenLexer;
anatofuz
parents:
diff changeset
195 }
anatofuz
parents:
diff changeset
196
anatofuz
parents:
diff changeset
197 /// Compute the relative path that names the given file relative to
anatofuz
parents:
diff changeset
198 /// the given directory.
anatofuz
parents:
diff changeset
199 static void computeRelativePath(FileManager &FM, const DirectoryEntry *Dir,
anatofuz
parents:
diff changeset
200 const FileEntry *File,
anatofuz
parents:
diff changeset
201 SmallString<128> &Result) {
anatofuz
parents:
diff changeset
202 Result.clear();
anatofuz
parents:
diff changeset
203
anatofuz
parents:
diff changeset
204 StringRef FilePath = File->getDir()->getName();
anatofuz
parents:
diff changeset
205 StringRef Path = FilePath;
anatofuz
parents:
diff changeset
206 while (!Path.empty()) {
anatofuz
parents:
diff changeset
207 if (auto CurDir = FM.getDirectory(Path)) {
anatofuz
parents:
diff changeset
208 if (*CurDir == Dir) {
anatofuz
parents:
diff changeset
209 Result = FilePath.substr(Path.size());
anatofuz
parents:
diff changeset
210 llvm::sys::path::append(Result,
anatofuz
parents:
diff changeset
211 llvm::sys::path::filename(File->getName()));
anatofuz
parents:
diff changeset
212 return;
anatofuz
parents:
diff changeset
213 }
anatofuz
parents:
diff changeset
214 }
anatofuz
parents:
diff changeset
215
anatofuz
parents:
diff changeset
216 Path = llvm::sys::path::parent_path(Path);
anatofuz
parents:
diff changeset
217 }
anatofuz
parents:
diff changeset
218
anatofuz
parents:
diff changeset
219 Result = File->getName();
anatofuz
parents:
diff changeset
220 }
anatofuz
parents:
diff changeset
221
anatofuz
parents:
diff changeset
222 void Preprocessor::PropagateLineStartLeadingSpaceInfo(Token &Result) {
anatofuz
parents:
diff changeset
223 if (CurTokenLexer) {
anatofuz
parents:
diff changeset
224 CurTokenLexer->PropagateLineStartLeadingSpaceInfo(Result);
anatofuz
parents:
diff changeset
225 return;
anatofuz
parents:
diff changeset
226 }
anatofuz
parents:
diff changeset
227 if (CurLexer) {
anatofuz
parents:
diff changeset
228 CurLexer->PropagateLineStartLeadingSpaceInfo(Result);
anatofuz
parents:
diff changeset
229 return;
anatofuz
parents:
diff changeset
230 }
anatofuz
parents:
diff changeset
231 // FIXME: Handle other kinds of lexers? It generally shouldn't matter,
anatofuz
parents:
diff changeset
232 // but it might if they're empty?
anatofuz
parents:
diff changeset
233 }
anatofuz
parents:
diff changeset
234
anatofuz
parents:
diff changeset
235 /// Determine the location to use as the end of the buffer for a lexer.
anatofuz
parents:
diff changeset
236 ///
anatofuz
parents:
diff changeset
237 /// If the file ends with a newline, form the EOF token on the newline itself,
anatofuz
parents:
diff changeset
238 /// rather than "on the line following it", which doesn't exist. This makes
anatofuz
parents:
diff changeset
239 /// diagnostics relating to the end of file include the last file that the user
anatofuz
parents:
diff changeset
240 /// actually typed, which is goodness.
anatofuz
parents:
diff changeset
241 const char *Preprocessor::getCurLexerEndPos() {
anatofuz
parents:
diff changeset
242 const char *EndPos = CurLexer->BufferEnd;
anatofuz
parents:
diff changeset
243 if (EndPos != CurLexer->BufferStart &&
anatofuz
parents:
diff changeset
244 (EndPos[-1] == '\n' || EndPos[-1] == '\r')) {
anatofuz
parents:
diff changeset
245 --EndPos;
anatofuz
parents:
diff changeset
246
anatofuz
parents:
diff changeset
247 // Handle \n\r and \r\n:
anatofuz
parents:
diff changeset
248 if (EndPos != CurLexer->BufferStart &&
anatofuz
parents:
diff changeset
249 (EndPos[-1] == '\n' || EndPos[-1] == '\r') &&
anatofuz
parents:
diff changeset
250 EndPos[-1] != EndPos[0])
anatofuz
parents:
diff changeset
251 --EndPos;
anatofuz
parents:
diff changeset
252 }
anatofuz
parents:
diff changeset
253
anatofuz
parents:
diff changeset
254 return EndPos;
anatofuz
parents:
diff changeset
255 }
anatofuz
parents:
diff changeset
256
anatofuz
parents:
diff changeset
257 static void collectAllSubModulesWithUmbrellaHeader(
anatofuz
parents:
diff changeset
258 const Module &Mod, SmallVectorImpl<const Module *> &SubMods) {
anatofuz
parents:
diff changeset
259 if (Mod.getUmbrellaHeader())
anatofuz
parents:
diff changeset
260 SubMods.push_back(&Mod);
anatofuz
parents:
diff changeset
261 for (auto *M : Mod.submodules())
anatofuz
parents:
diff changeset
262 collectAllSubModulesWithUmbrellaHeader(*M, SubMods);
anatofuz
parents:
diff changeset
263 }
anatofuz
parents:
diff changeset
264
anatofuz
parents:
diff changeset
265 void Preprocessor::diagnoseMissingHeaderInUmbrellaDir(const Module &Mod) {
anatofuz
parents:
diff changeset
266 assert(Mod.getUmbrellaHeader() && "Module must use umbrella header");
anatofuz
parents:
diff changeset
267 SourceLocation StartLoc =
anatofuz
parents:
diff changeset
268 SourceMgr.getLocForStartOfFile(SourceMgr.getMainFileID());
anatofuz
parents:
diff changeset
269 if (getDiagnostics().isIgnored(diag::warn_uncovered_module_header, StartLoc))
anatofuz
parents:
diff changeset
270 return;
anatofuz
parents:
diff changeset
271
anatofuz
parents:
diff changeset
272 ModuleMap &ModMap = getHeaderSearchInfo().getModuleMap();
anatofuz
parents:
diff changeset
273 const DirectoryEntry *Dir = Mod.getUmbrellaDir().Entry;
anatofuz
parents:
diff changeset
274 llvm::vfs::FileSystem &FS = FileMgr.getVirtualFileSystem();
anatofuz
parents:
diff changeset
275 std::error_code EC;
anatofuz
parents:
diff changeset
276 for (llvm::vfs::recursive_directory_iterator Entry(FS, Dir->getName(), EC),
anatofuz
parents:
diff changeset
277 End;
anatofuz
parents:
diff changeset
278 Entry != End && !EC; Entry.increment(EC)) {
anatofuz
parents:
diff changeset
279 using llvm::StringSwitch;
anatofuz
parents:
diff changeset
280
anatofuz
parents:
diff changeset
281 // Check whether this entry has an extension typically associated with
anatofuz
parents:
diff changeset
282 // headers.
anatofuz
parents:
diff changeset
283 if (!StringSwitch<bool>(llvm::sys::path::extension(Entry->path()))
anatofuz
parents:
diff changeset
284 .Cases(".h", ".H", ".hh", ".hpp", true)
anatofuz
parents:
diff changeset
285 .Default(false))
anatofuz
parents:
diff changeset
286 continue;
anatofuz
parents:
diff changeset
287
anatofuz
parents:
diff changeset
288 if (auto Header = getFileManager().getFile(Entry->path()))
anatofuz
parents:
diff changeset
289 if (!getSourceManager().hasFileInfo(*Header)) {
anatofuz
parents:
diff changeset
290 if (!ModMap.isHeaderInUnavailableModule(*Header)) {
anatofuz
parents:
diff changeset
291 // Find the relative path that would access this header.
anatofuz
parents:
diff changeset
292 SmallString<128> RelativePath;
anatofuz
parents:
diff changeset
293 computeRelativePath(FileMgr, Dir, *Header, RelativePath);
anatofuz
parents:
diff changeset
294 Diag(StartLoc, diag::warn_uncovered_module_header)
anatofuz
parents:
diff changeset
295 << Mod.getFullModuleName() << RelativePath;
anatofuz
parents:
diff changeset
296 }
anatofuz
parents:
diff changeset
297 }
anatofuz
parents:
diff changeset
298 }
anatofuz
parents:
diff changeset
299 }
anatofuz
parents:
diff changeset
300
anatofuz
parents:
diff changeset
301 /// HandleEndOfFile - This callback is invoked when the lexer hits the end of
anatofuz
parents:
diff changeset
302 /// the current file. This either returns the EOF token or pops a level off
anatofuz
parents:
diff changeset
303 /// the include stack and keeps going.
anatofuz
parents:
diff changeset
304 bool Preprocessor::HandleEndOfFile(Token &Result, bool isEndOfMacro) {
anatofuz
parents:
diff changeset
305 assert(!CurTokenLexer &&
anatofuz
parents:
diff changeset
306 "Ending a file when currently in a macro!");
anatofuz
parents:
diff changeset
307
anatofuz
parents:
diff changeset
308 // If we have an unclosed module region from a pragma at the end of a
anatofuz
parents:
diff changeset
309 // module, complain and close it now.
anatofuz
parents:
diff changeset
310 const bool LeavingSubmodule = CurLexer && CurLexerSubmodule;
anatofuz
parents:
diff changeset
311 if ((LeavingSubmodule || IncludeMacroStack.empty()) &&
anatofuz
parents:
diff changeset
312 !BuildingSubmoduleStack.empty() &&
anatofuz
parents:
diff changeset
313 BuildingSubmoduleStack.back().IsPragma) {
anatofuz
parents:
diff changeset
314 Diag(BuildingSubmoduleStack.back().ImportLoc,
anatofuz
parents:
diff changeset
315 diag::err_pp_module_begin_without_module_end);
anatofuz
parents:
diff changeset
316 Module *M = LeaveSubmodule(/*ForPragma*/true);
anatofuz
parents:
diff changeset
317
anatofuz
parents:
diff changeset
318 Result.startToken();
anatofuz
parents:
diff changeset
319 const char *EndPos = getCurLexerEndPos();
anatofuz
parents:
diff changeset
320 CurLexer->BufferPtr = EndPos;
anatofuz
parents:
diff changeset
321 CurLexer->FormTokenWithChars(Result, EndPos, tok::annot_module_end);
anatofuz
parents:
diff changeset
322 Result.setAnnotationEndLoc(Result.getLocation());
anatofuz
parents:
diff changeset
323 Result.setAnnotationValue(M);
anatofuz
parents:
diff changeset
324 return true;
anatofuz
parents:
diff changeset
325 }
anatofuz
parents:
diff changeset
326
anatofuz
parents:
diff changeset
327 // See if this file had a controlling macro.
anatofuz
parents:
diff changeset
328 if (CurPPLexer) { // Not ending a macro, ignore it.
anatofuz
parents:
diff changeset
329 if (const IdentifierInfo *ControllingMacro =
anatofuz
parents:
diff changeset
330 CurPPLexer->MIOpt.GetControllingMacroAtEndOfFile()) {
anatofuz
parents:
diff changeset
331 // Okay, this has a controlling macro, remember in HeaderFileInfo.
anatofuz
parents:
diff changeset
332 if (const FileEntry *FE = CurPPLexer->getFileEntry()) {
anatofuz
parents:
diff changeset
333 HeaderInfo.SetFileControllingMacro(FE, ControllingMacro);
anatofuz
parents:
diff changeset
334 if (MacroInfo *MI =
anatofuz
parents:
diff changeset
335 getMacroInfo(const_cast<IdentifierInfo*>(ControllingMacro)))
anatofuz
parents:
diff changeset
336 MI->setUsedForHeaderGuard(true);
anatofuz
parents:
diff changeset
337 if (const IdentifierInfo *DefinedMacro =
anatofuz
parents:
diff changeset
338 CurPPLexer->MIOpt.GetDefinedMacro()) {
anatofuz
parents:
diff changeset
339 if (!isMacroDefined(ControllingMacro) &&
anatofuz
parents:
diff changeset
340 DefinedMacro != ControllingMacro &&
anatofuz
parents:
diff changeset
341 HeaderInfo.FirstTimeLexingFile(FE)) {
anatofuz
parents:
diff changeset
342
anatofuz
parents:
diff changeset
343 // If the edit distance between the two macros is more than 50%,
anatofuz
parents:
diff changeset
344 // DefinedMacro may not be header guard, or can be header guard of
anatofuz
parents:
diff changeset
345 // another header file. Therefore, it maybe defining something
anatofuz
parents:
diff changeset
346 // completely different. This can be observed in the wild when
anatofuz
parents:
diff changeset
347 // handling feature macros or header guards in different files.
anatofuz
parents:
diff changeset
348
anatofuz
parents:
diff changeset
349 const StringRef ControllingMacroName = ControllingMacro->getName();
anatofuz
parents:
diff changeset
350 const StringRef DefinedMacroName = DefinedMacro->getName();
anatofuz
parents:
diff changeset
351 const size_t MaxHalfLength = std::max(ControllingMacroName.size(),
anatofuz
parents:
diff changeset
352 DefinedMacroName.size()) / 2;
anatofuz
parents:
diff changeset
353 const unsigned ED = ControllingMacroName.edit_distance(
anatofuz
parents:
diff changeset
354 DefinedMacroName, true, MaxHalfLength);
anatofuz
parents:
diff changeset
355 if (ED <= MaxHalfLength) {
anatofuz
parents:
diff changeset
356 // Emit a warning for a bad header guard.
anatofuz
parents:
diff changeset
357 Diag(CurPPLexer->MIOpt.GetMacroLocation(),
anatofuz
parents:
diff changeset
358 diag::warn_header_guard)
anatofuz
parents:
diff changeset
359 << CurPPLexer->MIOpt.GetMacroLocation() << ControllingMacro;
anatofuz
parents:
diff changeset
360 Diag(CurPPLexer->MIOpt.GetDefinedLocation(),
anatofuz
parents:
diff changeset
361 diag::note_header_guard)
anatofuz
parents:
diff changeset
362 << CurPPLexer->MIOpt.GetDefinedLocation() << DefinedMacro
anatofuz
parents:
diff changeset
363 << ControllingMacro
anatofuz
parents:
diff changeset
364 << FixItHint::CreateReplacement(
anatofuz
parents:
diff changeset
365 CurPPLexer->MIOpt.GetDefinedLocation(),
anatofuz
parents:
diff changeset
366 ControllingMacro->getName());
anatofuz
parents:
diff changeset
367 }
anatofuz
parents:
diff changeset
368 }
anatofuz
parents:
diff changeset
369 }
anatofuz
parents:
diff changeset
370 }
anatofuz
parents:
diff changeset
371 }
anatofuz
parents:
diff changeset
372 }
anatofuz
parents:
diff changeset
373
anatofuz
parents:
diff changeset
374 // Complain about reaching a true EOF within arc_cf_code_audited.
anatofuz
parents:
diff changeset
375 // We don't want to complain about reaching the end of a macro
anatofuz
parents:
diff changeset
376 // instantiation or a _Pragma.
anatofuz
parents:
diff changeset
377 if (PragmaARCCFCodeAuditedInfo.second.isValid() && !isEndOfMacro &&
anatofuz
parents:
diff changeset
378 !(CurLexer && CurLexer->Is_PragmaLexer)) {
anatofuz
parents:
diff changeset
379 Diag(PragmaARCCFCodeAuditedInfo.second,
anatofuz
parents:
diff changeset
380 diag::err_pp_eof_in_arc_cf_code_audited);
anatofuz
parents:
diff changeset
381
anatofuz
parents:
diff changeset
382 // Recover by leaving immediately.
anatofuz
parents:
diff changeset
383 PragmaARCCFCodeAuditedInfo = {nullptr, SourceLocation()};
anatofuz
parents:
diff changeset
384 }
anatofuz
parents:
diff changeset
385
anatofuz
parents:
diff changeset
386 // Complain about reaching a true EOF within assume_nonnull.
anatofuz
parents:
diff changeset
387 // We don't want to complain about reaching the end of a macro
anatofuz
parents:
diff changeset
388 // instantiation or a _Pragma.
anatofuz
parents:
diff changeset
389 if (PragmaAssumeNonNullLoc.isValid() &&
anatofuz
parents:
diff changeset
390 !isEndOfMacro && !(CurLexer && CurLexer->Is_PragmaLexer)) {
anatofuz
parents:
diff changeset
391 Diag(PragmaAssumeNonNullLoc, diag::err_pp_eof_in_assume_nonnull);
anatofuz
parents:
diff changeset
392
anatofuz
parents:
diff changeset
393 // Recover by leaving immediately.
anatofuz
parents:
diff changeset
394 PragmaAssumeNonNullLoc = SourceLocation();
anatofuz
parents:
diff changeset
395 }
anatofuz
parents:
diff changeset
396
anatofuz
parents:
diff changeset
397 bool LeavingPCHThroughHeader = false;
anatofuz
parents:
diff changeset
398
anatofuz
parents:
diff changeset
399 // If this is a #include'd file, pop it off the include stack and continue
anatofuz
parents:
diff changeset
400 // lexing the #includer file.
anatofuz
parents:
diff changeset
401 if (!IncludeMacroStack.empty()) {
anatofuz
parents:
diff changeset
402
anatofuz
parents:
diff changeset
403 // If we lexed the code-completion file, act as if we reached EOF.
anatofuz
parents:
diff changeset
404 if (isCodeCompletionEnabled() && CurPPLexer &&
anatofuz
parents:
diff changeset
405 SourceMgr.getLocForStartOfFile(CurPPLexer->getFileID()) ==
anatofuz
parents:
diff changeset
406 CodeCompletionFileLoc) {
anatofuz
parents:
diff changeset
407 assert(CurLexer && "Got EOF but no current lexer set!");
anatofuz
parents:
diff changeset
408 Result.startToken();
anatofuz
parents:
diff changeset
409 CurLexer->FormTokenWithChars(Result, CurLexer->BufferEnd, tok::eof);
anatofuz
parents:
diff changeset
410 CurLexer.reset();
anatofuz
parents:
diff changeset
411
anatofuz
parents:
diff changeset
412 CurPPLexer = nullptr;
anatofuz
parents:
diff changeset
413 recomputeCurLexerKind();
anatofuz
parents:
diff changeset
414 return true;
anatofuz
parents:
diff changeset
415 }
anatofuz
parents:
diff changeset
416
anatofuz
parents:
diff changeset
417 if (!isEndOfMacro && CurPPLexer &&
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
418 (SourceMgr.getIncludeLoc(CurPPLexer->getFileID()).isValid() ||
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
419 // Predefines file doesn't have a valid include location.
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
420 (PredefinesFileID.isValid() &&
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
421 CurPPLexer->getFileID() == PredefinesFileID))) {
150
anatofuz
parents:
diff changeset
422 // Notify SourceManager to record the number of FileIDs that were created
anatofuz
parents:
diff changeset
423 // during lexing of the #include'd file.
anatofuz
parents:
diff changeset
424 unsigned NumFIDs =
anatofuz
parents:
diff changeset
425 SourceMgr.local_sloc_entry_size() -
anatofuz
parents:
diff changeset
426 CurPPLexer->getInitialNumSLocEntries() + 1/*#include'd file*/;
anatofuz
parents:
diff changeset
427 SourceMgr.setNumCreatedFIDsForFileID(CurPPLexer->getFileID(), NumFIDs);
anatofuz
parents:
diff changeset
428 }
anatofuz
parents:
diff changeset
429
anatofuz
parents:
diff changeset
430 bool ExitedFromPredefinesFile = false;
anatofuz
parents:
diff changeset
431 FileID ExitedFID;
anatofuz
parents:
diff changeset
432 if (!isEndOfMacro && CurPPLexer) {
anatofuz
parents:
diff changeset
433 ExitedFID = CurPPLexer->getFileID();
anatofuz
parents:
diff changeset
434
anatofuz
parents:
diff changeset
435 assert(PredefinesFileID.isValid() &&
anatofuz
parents:
diff changeset
436 "HandleEndOfFile is called before PredefinesFileId is set");
anatofuz
parents:
diff changeset
437 ExitedFromPredefinesFile = (PredefinesFileID == ExitedFID);
anatofuz
parents:
diff changeset
438 }
anatofuz
parents:
diff changeset
439
anatofuz
parents:
diff changeset
440 if (LeavingSubmodule) {
anatofuz
parents:
diff changeset
441 // We're done with this submodule.
anatofuz
parents:
diff changeset
442 Module *M = LeaveSubmodule(/*ForPragma*/false);
anatofuz
parents:
diff changeset
443
anatofuz
parents:
diff changeset
444 // Notify the parser that we've left the module.
anatofuz
parents:
diff changeset
445 const char *EndPos = getCurLexerEndPos();
anatofuz
parents:
diff changeset
446 Result.startToken();
anatofuz
parents:
diff changeset
447 CurLexer->BufferPtr = EndPos;
anatofuz
parents:
diff changeset
448 CurLexer->FormTokenWithChars(Result, EndPos, tok::annot_module_end);
anatofuz
parents:
diff changeset
449 Result.setAnnotationEndLoc(Result.getLocation());
anatofuz
parents:
diff changeset
450 Result.setAnnotationValue(M);
anatofuz
parents:
diff changeset
451 }
anatofuz
parents:
diff changeset
452
anatofuz
parents:
diff changeset
453 bool FoundPCHThroughHeader = false;
anatofuz
parents:
diff changeset
454 if (CurPPLexer && creatingPCHWithThroughHeader() &&
anatofuz
parents:
diff changeset
455 isPCHThroughHeader(
anatofuz
parents:
diff changeset
456 SourceMgr.getFileEntryForID(CurPPLexer->getFileID())))
anatofuz
parents:
diff changeset
457 FoundPCHThroughHeader = true;
anatofuz
parents:
diff changeset
458
anatofuz
parents:
diff changeset
459 // We're done with the #included file.
anatofuz
parents:
diff changeset
460 RemoveTopOfLexerStack();
anatofuz
parents:
diff changeset
461
anatofuz
parents:
diff changeset
462 // Propagate info about start-of-line/leading white-space/etc.
anatofuz
parents:
diff changeset
463 PropagateLineStartLeadingSpaceInfo(Result);
anatofuz
parents:
diff changeset
464
anatofuz
parents:
diff changeset
465 // Notify the client, if desired, that we are in a new source file.
anatofuz
parents:
diff changeset
466 if (Callbacks && !isEndOfMacro && CurPPLexer) {
anatofuz
parents:
diff changeset
467 SrcMgr::CharacteristicKind FileType =
anatofuz
parents:
diff changeset
468 SourceMgr.getFileCharacteristic(CurPPLexer->getSourceLocation());
anatofuz
parents:
diff changeset
469 Callbacks->FileChanged(CurPPLexer->getSourceLocation(),
anatofuz
parents:
diff changeset
470 PPCallbacks::ExitFile, FileType, ExitedFID);
anatofuz
parents:
diff changeset
471 }
anatofuz
parents:
diff changeset
472
152
e8a9b4f4d755 pull from 146
anatofuz
parents: 150
diff changeset
473 #ifndef noCbC
200
40b1cab18437 swap CurLexer
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 174
diff changeset
474 if (SavedTokenFlag && IncludeMacroStack.size() == SavedDepth){ // dead code?
152
e8a9b4f4d755 pull from 146
anatofuz
parents: 150
diff changeset
475 Result = SavedToken;
e8a9b4f4d755 pull from 146
anatofuz
parents: 150
diff changeset
476 SavedTokenFlag = false;
200
40b1cab18437 swap CurLexer
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 174
diff changeset
477 if (CurLexer->ParsingPreprocessorDirective) {
40b1cab18437 swap CurLexer
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 174
diff changeset
478 // Done parsing the "line".
40b1cab18437 swap CurLexer
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 174
diff changeset
479 CurLexer->ParsingPreprocessorDirective = false;
152
e8a9b4f4d755 pull from 146
anatofuz
parents: 150
diff changeset
480
200
40b1cab18437 swap CurLexer
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 174
diff changeset
481 // Restore comment saving mode, in case it was disabled for directive.
40b1cab18437 swap CurLexer
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 174
diff changeset
482 CurLexer->resetExtendedTokenMode();
152
e8a9b4f4d755 pull from 146
anatofuz
parents: 150
diff changeset
483
200
40b1cab18437 swap CurLexer
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 174
diff changeset
484 // Since we consumed a newline, we are back at the start of a line.
40b1cab18437 swap CurLexer
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 174
diff changeset
485 CurLexer->IsAtStartOfLine = true;
40b1cab18437 swap CurLexer
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 174
diff changeset
486 CurLexer->IsAtPhysicalStartOfLine = true;
40b1cab18437 swap CurLexer
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 174
diff changeset
487 }
152
e8a9b4f4d755 pull from 146
anatofuz
parents: 150
diff changeset
488 return true;
e8a9b4f4d755 pull from 146
anatofuz
parents: 150
diff changeset
489 }
e8a9b4f4d755 pull from 146
anatofuz
parents: 150
diff changeset
490 #endif
e8a9b4f4d755 pull from 146
anatofuz
parents: 150
diff changeset
491
150
anatofuz
parents:
diff changeset
492 // Restore conditional stack from the preamble right after exiting from the
anatofuz
parents:
diff changeset
493 // predefines file.
anatofuz
parents:
diff changeset
494 if (ExitedFromPredefinesFile)
anatofuz
parents:
diff changeset
495 replayPreambleConditionalStack();
anatofuz
parents:
diff changeset
496
anatofuz
parents:
diff changeset
497 if (!isEndOfMacro && CurPPLexer && FoundPCHThroughHeader &&
anatofuz
parents:
diff changeset
498 (isInPrimaryFile() ||
anatofuz
parents:
diff changeset
499 CurPPLexer->getFileID() == getPredefinesFileID())) {
anatofuz
parents:
diff changeset
500 // Leaving the through header. Continue directly to end of main file
anatofuz
parents:
diff changeset
501 // processing.
anatofuz
parents:
diff changeset
502 LeavingPCHThroughHeader = true;
anatofuz
parents:
diff changeset
503 } else {
anatofuz
parents:
diff changeset
504 // Client should lex another token unless we generated an EOM.
anatofuz
parents:
diff changeset
505 return LeavingSubmodule;
anatofuz
parents:
diff changeset
506 }
anatofuz
parents:
diff changeset
507 }
anatofuz
parents:
diff changeset
508
anatofuz
parents:
diff changeset
509 // If this is the end of the main file, form an EOF token.
anatofuz
parents:
diff changeset
510 assert(CurLexer && "Got EOF but no current lexer set!");
anatofuz
parents:
diff changeset
511 const char *EndPos = getCurLexerEndPos();
anatofuz
parents:
diff changeset
512 Result.startToken();
anatofuz
parents:
diff changeset
513 CurLexer->BufferPtr = EndPos;
anatofuz
parents:
diff changeset
514 CurLexer->FormTokenWithChars(Result, EndPos, tok::eof);
anatofuz
parents:
diff changeset
515
anatofuz
parents:
diff changeset
516 if (isCodeCompletionEnabled()) {
anatofuz
parents:
diff changeset
517 // Inserting the code-completion point increases the source buffer by 1,
anatofuz
parents:
diff changeset
518 // but the main FileID was created before inserting the point.
anatofuz
parents:
diff changeset
519 // Compensate by reducing the EOF location by 1, otherwise the location
anatofuz
parents:
diff changeset
520 // will point to the next FileID.
anatofuz
parents:
diff changeset
521 // FIXME: This is hacky, the code-completion point should probably be
anatofuz
parents:
diff changeset
522 // inserted before the main FileID is created.
anatofuz
parents:
diff changeset
523 if (CurLexer->getFileLoc() == CodeCompletionFileLoc)
anatofuz
parents:
diff changeset
524 Result.setLocation(Result.getLocation().getLocWithOffset(-1));
anatofuz
parents:
diff changeset
525 }
anatofuz
parents:
diff changeset
526
anatofuz
parents:
diff changeset
527 if (creatingPCHWithThroughHeader() && !LeavingPCHThroughHeader) {
anatofuz
parents:
diff changeset
528 // Reached the end of the compilation without finding the through header.
anatofuz
parents:
diff changeset
529 Diag(CurLexer->getFileLoc(), diag::err_pp_through_header_not_seen)
anatofuz
parents:
diff changeset
530 << PPOpts->PCHThroughHeader << 0;
anatofuz
parents:
diff changeset
531 }
anatofuz
parents:
diff changeset
532
anatofuz
parents:
diff changeset
533 if (!isIncrementalProcessingEnabled())
anatofuz
parents:
diff changeset
534 // We're done with lexing.
anatofuz
parents:
diff changeset
535 CurLexer.reset();
anatofuz
parents:
diff changeset
536
anatofuz
parents:
diff changeset
537 if (!isIncrementalProcessingEnabled())
anatofuz
parents:
diff changeset
538 CurPPLexer = nullptr;
anatofuz
parents:
diff changeset
539
anatofuz
parents:
diff changeset
540 if (TUKind == TU_Complete) {
anatofuz
parents:
diff changeset
541 // This is the end of the top-level file. 'WarnUnusedMacroLocs' has
anatofuz
parents:
diff changeset
542 // collected all macro locations that we need to warn because they are not
anatofuz
parents:
diff changeset
543 // used.
anatofuz
parents:
diff changeset
544 for (WarnUnusedMacroLocsTy::iterator
anatofuz
parents:
diff changeset
545 I=WarnUnusedMacroLocs.begin(), E=WarnUnusedMacroLocs.end();
anatofuz
parents:
diff changeset
546 I!=E; ++I)
anatofuz
parents:
diff changeset
547 Diag(*I, diag::pp_macro_not_used);
anatofuz
parents:
diff changeset
548 }
anatofuz
parents:
diff changeset
549
anatofuz
parents:
diff changeset
550 // If we are building a module that has an umbrella header, make sure that
anatofuz
parents:
diff changeset
551 // each of the headers within the directory, including all submodules, is
anatofuz
parents:
diff changeset
552 // covered by the umbrella header was actually included by the umbrella
anatofuz
parents:
diff changeset
553 // header.
anatofuz
parents:
diff changeset
554 if (Module *Mod = getCurrentModule()) {
anatofuz
parents:
diff changeset
555 llvm::SmallVector<const Module *, 4> AllMods;
anatofuz
parents:
diff changeset
556 collectAllSubModulesWithUmbrellaHeader(*Mod, AllMods);
anatofuz
parents:
diff changeset
557 for (auto *M : AllMods)
anatofuz
parents:
diff changeset
558 diagnoseMissingHeaderInUmbrellaDir(*M);
anatofuz
parents:
diff changeset
559 }
anatofuz
parents:
diff changeset
560
anatofuz
parents:
diff changeset
561 return true;
anatofuz
parents:
diff changeset
562 }
anatofuz
parents:
diff changeset
563
anatofuz
parents:
diff changeset
564 /// HandleEndOfTokenLexer - This callback is invoked when the current TokenLexer
anatofuz
parents:
diff changeset
565 /// hits the end of its token stream.
anatofuz
parents:
diff changeset
566 bool Preprocessor::HandleEndOfTokenLexer(Token &Result) {
anatofuz
parents:
diff changeset
567 assert(CurTokenLexer && !CurPPLexer &&
anatofuz
parents:
diff changeset
568 "Ending a macro when currently in a #include file!");
anatofuz
parents:
diff changeset
569
anatofuz
parents:
diff changeset
570 if (!MacroExpandingLexersStack.empty() &&
anatofuz
parents:
diff changeset
571 MacroExpandingLexersStack.back().first == CurTokenLexer.get())
anatofuz
parents:
diff changeset
572 removeCachedMacroExpandedTokensOfLastLexer();
anatofuz
parents:
diff changeset
573
anatofuz
parents:
diff changeset
574 // Delete or cache the now-dead macro expander.
anatofuz
parents:
diff changeset
575 if (NumCachedTokenLexers == TokenLexerCacheSize)
anatofuz
parents:
diff changeset
576 CurTokenLexer.reset();
anatofuz
parents:
diff changeset
577 else
anatofuz
parents:
diff changeset
578 TokenLexerCache[NumCachedTokenLexers++] = std::move(CurTokenLexer);
anatofuz
parents:
diff changeset
579
anatofuz
parents:
diff changeset
580 // Handle this like a #include file being popped off the stack.
anatofuz
parents:
diff changeset
581 return HandleEndOfFile(Result, true);
anatofuz
parents:
diff changeset
582 }
anatofuz
parents:
diff changeset
583
anatofuz
parents:
diff changeset
584 /// RemoveTopOfLexerStack - Pop the current lexer/macro exp off the top of the
anatofuz
parents:
diff changeset
585 /// lexer stack. This should only be used in situations where the current
anatofuz
parents:
diff changeset
586 /// state of the top-of-stack lexer is unknown.
anatofuz
parents:
diff changeset
587 void Preprocessor::RemoveTopOfLexerStack() {
anatofuz
parents:
diff changeset
588 assert(!IncludeMacroStack.empty() && "Ran out of stack entries to load");
anatofuz
parents:
diff changeset
589
anatofuz
parents:
diff changeset
590 if (CurTokenLexer) {
anatofuz
parents:
diff changeset
591 // Delete or cache the now-dead macro expander.
anatofuz
parents:
diff changeset
592 if (NumCachedTokenLexers == TokenLexerCacheSize)
anatofuz
parents:
diff changeset
593 CurTokenLexer.reset();
anatofuz
parents:
diff changeset
594 else
anatofuz
parents:
diff changeset
595 TokenLexerCache[NumCachedTokenLexers++] = std::move(CurTokenLexer);
anatofuz
parents:
diff changeset
596 }
anatofuz
parents:
diff changeset
597
anatofuz
parents:
diff changeset
598 PopIncludeMacroStack();
anatofuz
parents:
diff changeset
599 }
anatofuz
parents:
diff changeset
600
anatofuz
parents:
diff changeset
601 /// HandleMicrosoftCommentPaste - When the macro expander pastes together a
anatofuz
parents:
diff changeset
602 /// comment (/##/) in microsoft mode, this method handles updating the current
anatofuz
parents:
diff changeset
603 /// state, returning the token on the next source line.
anatofuz
parents:
diff changeset
604 void Preprocessor::HandleMicrosoftCommentPaste(Token &Tok) {
anatofuz
parents:
diff changeset
605 assert(CurTokenLexer && !CurPPLexer &&
anatofuz
parents:
diff changeset
606 "Pasted comment can only be formed from macro");
anatofuz
parents:
diff changeset
607 // We handle this by scanning for the closest real lexer, switching it to
anatofuz
parents:
diff changeset
608 // raw mode and preprocessor mode. This will cause it to return \n as an
anatofuz
parents:
diff changeset
609 // explicit EOD token.
anatofuz
parents:
diff changeset
610 PreprocessorLexer *FoundLexer = nullptr;
anatofuz
parents:
diff changeset
611 bool LexerWasInPPMode = false;
anatofuz
parents:
diff changeset
612 for (const IncludeStackInfo &ISI : llvm::reverse(IncludeMacroStack)) {
anatofuz
parents:
diff changeset
613 if (ISI.ThePPLexer == nullptr) continue; // Scan for a real lexer.
anatofuz
parents:
diff changeset
614
anatofuz
parents:
diff changeset
615 // Once we find a real lexer, mark it as raw mode (disabling macro
anatofuz
parents:
diff changeset
616 // expansions) and preprocessor mode (return EOD). We know that the lexer
anatofuz
parents:
diff changeset
617 // was *not* in raw mode before, because the macro that the comment came
anatofuz
parents:
diff changeset
618 // from was expanded. However, it could have already been in preprocessor
anatofuz
parents:
diff changeset
619 // mode (#if COMMENT) in which case we have to return it to that mode and
anatofuz
parents:
diff changeset
620 // return EOD.
anatofuz
parents:
diff changeset
621 FoundLexer = ISI.ThePPLexer;
anatofuz
parents:
diff changeset
622 FoundLexer->LexingRawMode = true;
anatofuz
parents:
diff changeset
623 LexerWasInPPMode = FoundLexer->ParsingPreprocessorDirective;
anatofuz
parents:
diff changeset
624 FoundLexer->ParsingPreprocessorDirective = true;
anatofuz
parents:
diff changeset
625 break;
anatofuz
parents:
diff changeset
626 }
anatofuz
parents:
diff changeset
627
anatofuz
parents:
diff changeset
628 // Okay, we either found and switched over the lexer, or we didn't find a
anatofuz
parents:
diff changeset
629 // lexer. In either case, finish off the macro the comment came from, getting
anatofuz
parents:
diff changeset
630 // the next token.
anatofuz
parents:
diff changeset
631 if (!HandleEndOfTokenLexer(Tok)) Lex(Tok);
anatofuz
parents:
diff changeset
632
anatofuz
parents:
diff changeset
633 // Discarding comments as long as we don't have EOF or EOD. This 'comments
anatofuz
parents:
diff changeset
634 // out' the rest of the line, including any tokens that came from other macros
anatofuz
parents:
diff changeset
635 // that were active, as in:
anatofuz
parents:
diff changeset
636 // #define submacro a COMMENT b
anatofuz
parents:
diff changeset
637 // submacro c
anatofuz
parents:
diff changeset
638 // which should lex to 'a' only: 'b' and 'c' should be removed.
anatofuz
parents:
diff changeset
639 while (Tok.isNot(tok::eod) && Tok.isNot(tok::eof))
anatofuz
parents:
diff changeset
640 Lex(Tok);
anatofuz
parents:
diff changeset
641
anatofuz
parents:
diff changeset
642 // If we got an eod token, then we successfully found the end of the line.
anatofuz
parents:
diff changeset
643 if (Tok.is(tok::eod)) {
anatofuz
parents:
diff changeset
644 assert(FoundLexer && "Can't get end of line without an active lexer");
anatofuz
parents:
diff changeset
645 // Restore the lexer back to normal mode instead of raw mode.
anatofuz
parents:
diff changeset
646 FoundLexer->LexingRawMode = false;
anatofuz
parents:
diff changeset
647
anatofuz
parents:
diff changeset
648 // If the lexer was already in preprocessor mode, just return the EOD token
anatofuz
parents:
diff changeset
649 // to finish the preprocessor line.
anatofuz
parents:
diff changeset
650 if (LexerWasInPPMode) return;
anatofuz
parents:
diff changeset
651
anatofuz
parents:
diff changeset
652 // Otherwise, switch out of PP mode and return the next lexed token.
anatofuz
parents:
diff changeset
653 FoundLexer->ParsingPreprocessorDirective = false;
anatofuz
parents:
diff changeset
654 return Lex(Tok);
anatofuz
parents:
diff changeset
655 }
anatofuz
parents:
diff changeset
656
anatofuz
parents:
diff changeset
657 // If we got an EOF token, then we reached the end of the token stream but
anatofuz
parents:
diff changeset
658 // didn't find an explicit \n. This can only happen if there was no lexer
anatofuz
parents:
diff changeset
659 // active (an active lexer would return EOD at EOF if there was no \n in
anatofuz
parents:
diff changeset
660 // preprocessor directive mode), so just return EOF as our token.
anatofuz
parents:
diff changeset
661 assert(!FoundLexer && "Lexer should return EOD before EOF in PP mode");
anatofuz
parents:
diff changeset
662 }
anatofuz
parents:
diff changeset
663
anatofuz
parents:
diff changeset
664 void Preprocessor::EnterSubmodule(Module *M, SourceLocation ImportLoc,
anatofuz
parents:
diff changeset
665 bool ForPragma) {
anatofuz
parents:
diff changeset
666 if (!getLangOpts().ModulesLocalVisibility) {
anatofuz
parents:
diff changeset
667 // Just track that we entered this submodule.
anatofuz
parents:
diff changeset
668 BuildingSubmoduleStack.push_back(
anatofuz
parents:
diff changeset
669 BuildingSubmoduleInfo(M, ImportLoc, ForPragma, CurSubmoduleState,
anatofuz
parents:
diff changeset
670 PendingModuleMacroNames.size()));
anatofuz
parents:
diff changeset
671 if (Callbacks)
anatofuz
parents:
diff changeset
672 Callbacks->EnteredSubmodule(M, ImportLoc, ForPragma);
anatofuz
parents:
diff changeset
673 return;
anatofuz
parents:
diff changeset
674 }
anatofuz
parents:
diff changeset
675
anatofuz
parents:
diff changeset
676 // Resolve as much of the module definition as we can now, before we enter
anatofuz
parents:
diff changeset
677 // one of its headers.
anatofuz
parents:
diff changeset
678 // FIXME: Can we enable Complain here?
anatofuz
parents:
diff changeset
679 // FIXME: Can we do this when local visibility is disabled?
anatofuz
parents:
diff changeset
680 ModuleMap &ModMap = getHeaderSearchInfo().getModuleMap();
anatofuz
parents:
diff changeset
681 ModMap.resolveExports(M, /*Complain=*/false);
anatofuz
parents:
diff changeset
682 ModMap.resolveUses(M, /*Complain=*/false);
anatofuz
parents:
diff changeset
683 ModMap.resolveConflicts(M, /*Complain=*/false);
anatofuz
parents:
diff changeset
684
anatofuz
parents:
diff changeset
685 // If this is the first time we've entered this module, set up its state.
anatofuz
parents:
diff changeset
686 auto R = Submodules.insert(std::make_pair(M, SubmoduleState()));
anatofuz
parents:
diff changeset
687 auto &State = R.first->second;
anatofuz
parents:
diff changeset
688 bool FirstTime = R.second;
anatofuz
parents:
diff changeset
689 if (FirstTime) {
anatofuz
parents:
diff changeset
690 // Determine the set of starting macros for this submodule; take these
anatofuz
parents:
diff changeset
691 // from the "null" module (the predefines buffer).
anatofuz
parents:
diff changeset
692 //
anatofuz
parents:
diff changeset
693 // FIXME: If we have local visibility but not modules enabled, the
anatofuz
parents:
diff changeset
694 // NullSubmoduleState is polluted by #defines in the top-level source
anatofuz
parents:
diff changeset
695 // file.
anatofuz
parents:
diff changeset
696 auto &StartingMacros = NullSubmoduleState.Macros;
anatofuz
parents:
diff changeset
697
anatofuz
parents:
diff changeset
698 // Restore to the starting state.
anatofuz
parents:
diff changeset
699 // FIXME: Do this lazily, when each macro name is first referenced.
anatofuz
parents:
diff changeset
700 for (auto &Macro : StartingMacros) {
anatofuz
parents:
diff changeset
701 // Skip uninteresting macros.
anatofuz
parents:
diff changeset
702 if (!Macro.second.getLatest() &&
anatofuz
parents:
diff changeset
703 Macro.second.getOverriddenMacros().empty())
anatofuz
parents:
diff changeset
704 continue;
anatofuz
parents:
diff changeset
705
anatofuz
parents:
diff changeset
706 MacroState MS(Macro.second.getLatest());
anatofuz
parents:
diff changeset
707 MS.setOverriddenMacros(*this, Macro.second.getOverriddenMacros());
anatofuz
parents:
diff changeset
708 State.Macros.insert(std::make_pair(Macro.first, std::move(MS)));
anatofuz
parents:
diff changeset
709 }
anatofuz
parents:
diff changeset
710 }
anatofuz
parents:
diff changeset
711
anatofuz
parents:
diff changeset
712 // Track that we entered this module.
anatofuz
parents:
diff changeset
713 BuildingSubmoduleStack.push_back(
anatofuz
parents:
diff changeset
714 BuildingSubmoduleInfo(M, ImportLoc, ForPragma, CurSubmoduleState,
anatofuz
parents:
diff changeset
715 PendingModuleMacroNames.size()));
anatofuz
parents:
diff changeset
716
anatofuz
parents:
diff changeset
717 if (Callbacks)
anatofuz
parents:
diff changeset
718 Callbacks->EnteredSubmodule(M, ImportLoc, ForPragma);
anatofuz
parents:
diff changeset
719
anatofuz
parents:
diff changeset
720 // Switch to this submodule as the current submodule.
anatofuz
parents:
diff changeset
721 CurSubmoduleState = &State;
anatofuz
parents:
diff changeset
722
anatofuz
parents:
diff changeset
723 // This module is visible to itself.
anatofuz
parents:
diff changeset
724 if (FirstTime)
anatofuz
parents:
diff changeset
725 makeModuleVisible(M, ImportLoc);
anatofuz
parents:
diff changeset
726 }
anatofuz
parents:
diff changeset
727
anatofuz
parents:
diff changeset
728 bool Preprocessor::needModuleMacros() const {
anatofuz
parents:
diff changeset
729 // If we're not within a submodule, we never need to create ModuleMacros.
anatofuz
parents:
diff changeset
730 if (BuildingSubmoduleStack.empty())
anatofuz
parents:
diff changeset
731 return false;
anatofuz
parents:
diff changeset
732 // If we are tracking module macro visibility even for textually-included
anatofuz
parents:
diff changeset
733 // headers, we need ModuleMacros.
anatofuz
parents:
diff changeset
734 if (getLangOpts().ModulesLocalVisibility)
anatofuz
parents:
diff changeset
735 return true;
anatofuz
parents:
diff changeset
736 // Otherwise, we only need module macros if we're actually compiling a module
anatofuz
parents:
diff changeset
737 // interface.
anatofuz
parents:
diff changeset
738 return getLangOpts().isCompilingModule();
anatofuz
parents:
diff changeset
739 }
anatofuz
parents:
diff changeset
740
anatofuz
parents:
diff changeset
741 Module *Preprocessor::LeaveSubmodule(bool ForPragma) {
anatofuz
parents:
diff changeset
742 if (BuildingSubmoduleStack.empty() ||
anatofuz
parents:
diff changeset
743 BuildingSubmoduleStack.back().IsPragma != ForPragma) {
anatofuz
parents:
diff changeset
744 assert(ForPragma && "non-pragma module enter/leave mismatch");
anatofuz
parents:
diff changeset
745 return nullptr;
anatofuz
parents:
diff changeset
746 }
anatofuz
parents:
diff changeset
747
anatofuz
parents:
diff changeset
748 auto &Info = BuildingSubmoduleStack.back();
anatofuz
parents:
diff changeset
749
anatofuz
parents:
diff changeset
750 Module *LeavingMod = Info.M;
anatofuz
parents:
diff changeset
751 SourceLocation ImportLoc = Info.ImportLoc;
anatofuz
parents:
diff changeset
752
anatofuz
parents:
diff changeset
753 if (!needModuleMacros() ||
anatofuz
parents:
diff changeset
754 (!getLangOpts().ModulesLocalVisibility &&
anatofuz
parents:
diff changeset
755 LeavingMod->getTopLevelModuleName() != getLangOpts().CurrentModule)) {
anatofuz
parents:
diff changeset
756 // If we don't need module macros, or this is not a module for which we
anatofuz
parents:
diff changeset
757 // are tracking macro visibility, don't build any, and preserve the list
anatofuz
parents:
diff changeset
758 // of pending names for the surrounding submodule.
anatofuz
parents:
diff changeset
759 BuildingSubmoduleStack.pop_back();
anatofuz
parents:
diff changeset
760
anatofuz
parents:
diff changeset
761 if (Callbacks)
anatofuz
parents:
diff changeset
762 Callbacks->LeftSubmodule(LeavingMod, ImportLoc, ForPragma);
anatofuz
parents:
diff changeset
763
anatofuz
parents:
diff changeset
764 makeModuleVisible(LeavingMod, ImportLoc);
anatofuz
parents:
diff changeset
765 return LeavingMod;
anatofuz
parents:
diff changeset
766 }
anatofuz
parents:
diff changeset
767
anatofuz
parents:
diff changeset
768 // Create ModuleMacros for any macros defined in this submodule.
anatofuz
parents:
diff changeset
769 llvm::SmallPtrSet<const IdentifierInfo*, 8> VisitedMacros;
anatofuz
parents:
diff changeset
770 for (unsigned I = Info.OuterPendingModuleMacroNames;
anatofuz
parents:
diff changeset
771 I != PendingModuleMacroNames.size(); ++I) {
anatofuz
parents:
diff changeset
772 auto *II = const_cast<IdentifierInfo*>(PendingModuleMacroNames[I]);
anatofuz
parents:
diff changeset
773 if (!VisitedMacros.insert(II).second)
anatofuz
parents:
diff changeset
774 continue;
anatofuz
parents:
diff changeset
775
anatofuz
parents:
diff changeset
776 auto MacroIt = CurSubmoduleState->Macros.find(II);
anatofuz
parents:
diff changeset
777 if (MacroIt == CurSubmoduleState->Macros.end())
anatofuz
parents:
diff changeset
778 continue;
anatofuz
parents:
diff changeset
779 auto &Macro = MacroIt->second;
anatofuz
parents:
diff changeset
780
anatofuz
parents:
diff changeset
781 // Find the starting point for the MacroDirective chain in this submodule.
anatofuz
parents:
diff changeset
782 MacroDirective *OldMD = nullptr;
anatofuz
parents:
diff changeset
783 auto *OldState = Info.OuterSubmoduleState;
anatofuz
parents:
diff changeset
784 if (getLangOpts().ModulesLocalVisibility)
anatofuz
parents:
diff changeset
785 OldState = &NullSubmoduleState;
anatofuz
parents:
diff changeset
786 if (OldState && OldState != CurSubmoduleState) {
anatofuz
parents:
diff changeset
787 // FIXME: It'd be better to start at the state from when we most recently
anatofuz
parents:
diff changeset
788 // entered this submodule, but it doesn't really matter.
anatofuz
parents:
diff changeset
789 auto &OldMacros = OldState->Macros;
anatofuz
parents:
diff changeset
790 auto OldMacroIt = OldMacros.find(II);
anatofuz
parents:
diff changeset
791 if (OldMacroIt == OldMacros.end())
anatofuz
parents:
diff changeset
792 OldMD = nullptr;
anatofuz
parents:
diff changeset
793 else
anatofuz
parents:
diff changeset
794 OldMD = OldMacroIt->second.getLatest();
anatofuz
parents:
diff changeset
795 }
anatofuz
parents:
diff changeset
796
anatofuz
parents:
diff changeset
797 // This module may have exported a new macro. If so, create a ModuleMacro
anatofuz
parents:
diff changeset
798 // representing that fact.
anatofuz
parents:
diff changeset
799 bool ExplicitlyPublic = false;
anatofuz
parents:
diff changeset
800 for (auto *MD = Macro.getLatest(); MD != OldMD; MD = MD->getPrevious()) {
anatofuz
parents:
diff changeset
801 assert(MD && "broken macro directive chain");
anatofuz
parents:
diff changeset
802
anatofuz
parents:
diff changeset
803 if (auto *VisMD = dyn_cast<VisibilityMacroDirective>(MD)) {
anatofuz
parents:
diff changeset
804 // The latest visibility directive for a name in a submodule affects
anatofuz
parents:
diff changeset
805 // all the directives that come before it.
anatofuz
parents:
diff changeset
806 if (VisMD->isPublic())
anatofuz
parents:
diff changeset
807 ExplicitlyPublic = true;
anatofuz
parents:
diff changeset
808 else if (!ExplicitlyPublic)
anatofuz
parents:
diff changeset
809 // Private with no following public directive: not exported.
anatofuz
parents:
diff changeset
810 break;
anatofuz
parents:
diff changeset
811 } else {
anatofuz
parents:
diff changeset
812 MacroInfo *Def = nullptr;
anatofuz
parents:
diff changeset
813 if (DefMacroDirective *DefMD = dyn_cast<DefMacroDirective>(MD))
anatofuz
parents:
diff changeset
814 Def = DefMD->getInfo();
anatofuz
parents:
diff changeset
815
anatofuz
parents:
diff changeset
816 // FIXME: Issue a warning if multiple headers for the same submodule
anatofuz
parents:
diff changeset
817 // define a macro, rather than silently ignoring all but the first.
anatofuz
parents:
diff changeset
818 bool IsNew;
anatofuz
parents:
diff changeset
819 // Don't bother creating a module macro if it would represent a #undef
anatofuz
parents:
diff changeset
820 // that doesn't override anything.
anatofuz
parents:
diff changeset
821 if (Def || !Macro.getOverriddenMacros().empty())
anatofuz
parents:
diff changeset
822 addModuleMacro(LeavingMod, II, Def,
anatofuz
parents:
diff changeset
823 Macro.getOverriddenMacros(), IsNew);
anatofuz
parents:
diff changeset
824
anatofuz
parents:
diff changeset
825 if (!getLangOpts().ModulesLocalVisibility) {
anatofuz
parents:
diff changeset
826 // This macro is exposed to the rest of this compilation as a
anatofuz
parents:
diff changeset
827 // ModuleMacro; we don't need to track its MacroDirective any more.
anatofuz
parents:
diff changeset
828 Macro.setLatest(nullptr);
anatofuz
parents:
diff changeset
829 Macro.setOverriddenMacros(*this, {});
anatofuz
parents:
diff changeset
830 }
anatofuz
parents:
diff changeset
831 break;
anatofuz
parents:
diff changeset
832 }
anatofuz
parents:
diff changeset
833 }
anatofuz
parents:
diff changeset
834 }
anatofuz
parents:
diff changeset
835 PendingModuleMacroNames.resize(Info.OuterPendingModuleMacroNames);
anatofuz
parents:
diff changeset
836
anatofuz
parents:
diff changeset
837 // FIXME: Before we leave this submodule, we should parse all the other
anatofuz
parents:
diff changeset
838 // headers within it. Otherwise, we're left with an inconsistent state
anatofuz
parents:
diff changeset
839 // where we've made the module visible but don't yet have its complete
anatofuz
parents:
diff changeset
840 // contents.
anatofuz
parents:
diff changeset
841
anatofuz
parents:
diff changeset
842 // Put back the outer module's state, if we're tracking it.
anatofuz
parents:
diff changeset
843 if (getLangOpts().ModulesLocalVisibility)
anatofuz
parents:
diff changeset
844 CurSubmoduleState = Info.OuterSubmoduleState;
anatofuz
parents:
diff changeset
845
anatofuz
parents:
diff changeset
846 BuildingSubmoduleStack.pop_back();
anatofuz
parents:
diff changeset
847
anatofuz
parents:
diff changeset
848 if (Callbacks)
anatofuz
parents:
diff changeset
849 Callbacks->LeftSubmodule(LeavingMod, ImportLoc, ForPragma);
anatofuz
parents:
diff changeset
850
anatofuz
parents:
diff changeset
851 // A nested #include makes the included submodule visible.
anatofuz
parents:
diff changeset
852 makeModuleVisible(LeavingMod, ImportLoc);
anatofuz
parents:
diff changeset
853 return LeavingMod;
anatofuz
parents:
diff changeset
854 }