1 //===- Preprocess.cpp - C Language Family Preprocessor Implementation -----===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 //  This file implements the Preprocessor interface.
11 //
12 //===----------------------------------------------------------------------===//
13 //
14 // Options to support:
15 //   -H       - Print the name of each header file used.
16 //   -d[DNI] - Dump various things.
17 //   -fworking-directory - #line's with preprocessor's working dir.
18 //   -fpreprocessed
19 //   -dependency-file,-M,-MM,-MF,-MG,-MP,-MT,-MQ,-MD,-MMD
20 //   -W*
21 //   -w
22 //
23 // Messages to emit:
24 //   "Multiple include guards may be useful for:\n"
25 //
26 //===----------------------------------------------------------------------===//
27 
28 #include "clang/Lex/Preprocessor.h"
29 #include "clang/Basic/FileManager.h"
30 #include "clang/Basic/FileSystemStatCache.h"
31 #include "clang/Basic/IdentifierTable.h"
32 #include "clang/Basic/LLVM.h"
33 #include "clang/Basic/LangOptions.h"
34 #include "clang/Basic/Module.h"
35 #include "clang/Basic/SourceLocation.h"
36 #include "clang/Basic/SourceManager.h"
37 #include "clang/Basic/TargetInfo.h"
38 #include "clang/Lex/CodeCompletionHandler.h"
39 #include "clang/Lex/ExternalPreprocessorSource.h"
40 #include "clang/Lex/HeaderSearch.h"
41 #include "clang/Lex/LexDiagnostic.h"
42 #include "clang/Lex/Lexer.h"
43 #include "clang/Lex/LiteralSupport.h"
44 #include "clang/Lex/MacroArgs.h"
45 #include "clang/Lex/MacroInfo.h"
46 #include "clang/Lex/ModuleLoader.h"
47 #include "clang/Lex/PTHLexer.h"
48 #include "clang/Lex/PTHManager.h"
49 #include "clang/Lex/Pragma.h"
50 #include "clang/Lex/PreprocessingRecord.h"
51 #include "clang/Lex/PreprocessorLexer.h"
52 #include "clang/Lex/PreprocessorOptions.h"
53 #include "clang/Lex/ScratchBuffer.h"
54 #include "clang/Lex/Token.h"
55 #include "clang/Lex/TokenLexer.h"
56 #include "llvm/ADT/APInt.h"
57 #include "llvm/ADT/ArrayRef.h"
58 #include "llvm/ADT/DenseMap.h"
59 #include "llvm/ADT/SmallString.h"
60 #include "llvm/ADT/SmallVector.h"
61 #include "llvm/ADT/STLExtras.h"
62 #include "llvm/ADT/StringRef.h"
63 #include "llvm/ADT/StringSwitch.h"
64 #include "llvm/Support/Capacity.h"
65 #include "llvm/Support/ErrorHandling.h"
66 #include "llvm/Support/MemoryBuffer.h"
67 #include "llvm/Support/raw_ostream.h"
68 #include <algorithm>
69 #include <cassert>
70 #include <memory>
71 #include <string>
72 #include <utility>
73 #include <vector>
74 
75 using namespace clang;
76 
77 LLVM_INSTANTIATE_REGISTRY(PragmaHandlerRegistry)
78 
79 ExternalPreprocessorSource::~ExternalPreprocessorSource() = default;
80 
81 Preprocessor::Preprocessor(std::shared_ptr<PreprocessorOptions> PPOpts,
82                            DiagnosticsEngine &diags, LangOptions &opts,
83                            SourceManager &SM, MemoryBufferCache &PCMCache,
84                            HeaderSearch &Headers, ModuleLoader &TheModuleLoader,
85                            IdentifierInfoLookup *IILookup, bool OwnsHeaders,
86                            TranslationUnitKind TUKind)
87     : PPOpts(std::move(PPOpts)), Diags(&diags), LangOpts(opts),
88       FileMgr(Headers.getFileMgr()), SourceMgr(SM), PCMCache(PCMCache),
89       ScratchBuf(new ScratchBuffer(SourceMgr)), HeaderInfo(Headers),
90       TheModuleLoader(TheModuleLoader), ExternalSource(nullptr),
91       // As the language options may have not been loaded yet (when
92       // deserializing an ASTUnit), adding keywords to the identifier table is
93       // deferred to Preprocessor::Initialize().
94       Identifiers(IILookup), PragmaHandlers(new PragmaNamespace(StringRef())),
95       TUKind(TUKind), SkipMainFilePreamble(0, true),
96       CurSubmoduleState(&NullSubmoduleState) {
97   OwnsHeaderSearch = OwnsHeaders;
98 
99   // Default to discarding comments.
100   KeepComments = false;
101   KeepMacroComments = false;
102   SuppressIncludeNotFoundError = false;
103 
104   // Macro expansion is enabled.
105   DisableMacroExpansion = false;
106   MacroExpansionInDirectivesOverride = false;
107   InMacroArgs = false;
108   InMacroArgPreExpansion = false;
109   NumCachedTokenLexers = 0;
110   PragmasEnabled = true;
111   ParsingIfOrElifDirective = false;
112   PreprocessedOutput = false;
113 
114   // We haven't read anything from the external source.
115   ReadMacrosFromExternalSource = false;
116 
117   // "Poison" __VA_ARGS__, __VA_OPT__ which can only appear in the expansion of
118   // a macro. They get unpoisoned where it is allowed.
119   (Ident__VA_ARGS__ = getIdentifierInfo("__VA_ARGS__"))->setIsPoisoned();
120   SetPoisonReason(Ident__VA_ARGS__,diag::ext_pp_bad_vaargs_use);
121   if (getLangOpts().CPlusPlus2a) {
122     (Ident__VA_OPT__ = getIdentifierInfo("__VA_OPT__"))->setIsPoisoned();
123     SetPoisonReason(Ident__VA_OPT__,diag::ext_pp_bad_vaopt_use);
124   } else {
125     Ident__VA_OPT__ = nullptr;
126   }
127 
128   // Initialize the pragma handlers.
129   RegisterBuiltinPragmas();
130 
131   // Initialize builtin macros like __LINE__ and friends.
132   RegisterBuiltinMacros();
133 
134   if(LangOpts.Borland) {
135     Ident__exception_info        = getIdentifierInfo("_exception_info");
136     Ident___exception_info       = getIdentifierInfo("__exception_info");
137     Ident_GetExceptionInfo       = getIdentifierInfo("GetExceptionInformation");
138     Ident__exception_code        = getIdentifierInfo("_exception_code");
139     Ident___exception_code       = getIdentifierInfo("__exception_code");
140     Ident_GetExceptionCode       = getIdentifierInfo("GetExceptionCode");
141     Ident__abnormal_termination  = getIdentifierInfo("_abnormal_termination");
142     Ident___abnormal_termination = getIdentifierInfo("__abnormal_termination");
143     Ident_AbnormalTermination    = getIdentifierInfo("AbnormalTermination");
144   } else {
145     Ident__exception_info = Ident__exception_code = nullptr;
146     Ident__abnormal_termination = Ident___exception_info = nullptr;
147     Ident___exception_code = Ident___abnormal_termination = nullptr;
148     Ident_GetExceptionInfo = Ident_GetExceptionCode = nullptr;
149     Ident_AbnormalTermination = nullptr;
150   }
151 
152   if (this->PPOpts->GeneratePreamble)
153     PreambleConditionalStack.startRecording();
154 }
155 
156 Preprocessor::~Preprocessor() {
157   assert(BacktrackPositions.empty() && "EnableBacktrack/Backtrack imbalance!");
158 
159   IncludeMacroStack.clear();
160 
161   // Destroy any macro definitions.
162   while (MacroInfoChain *I = MIChainHead) {
163     MIChainHead = I->Next;
164     I->~MacroInfoChain();
165   }
166 
167   // Free any cached macro expanders.
168   // This populates MacroArgCache, so all TokenLexers need to be destroyed
169   // before the code below that frees up the MacroArgCache list.
170   std::fill(TokenLexerCache, TokenLexerCache + NumCachedTokenLexers, nullptr);
171   CurTokenLexer.reset();
172 
173   // Free any cached MacroArgs.
174   for (MacroArgs *ArgList = MacroArgCache; ArgList;)
175     ArgList = ArgList->deallocate();
176 
177   // Delete the header search info, if we own it.
178   if (OwnsHeaderSearch)
179     delete &HeaderInfo;
180 }
181 
182 void Preprocessor::Initialize(const TargetInfo &Target,
183                               const TargetInfo *AuxTarget) {
184   assert((!this->Target || this->Target == &Target) &&
185          "Invalid override of target information");
186   this->Target = &Target;
187 
188   assert((!this->AuxTarget || this->AuxTarget == AuxTarget) &&
189          "Invalid override of aux target information.");
190   this->AuxTarget = AuxTarget;
191 
192   // Initialize information about built-ins.
193   BuiltinInfo.InitializeTarget(Target, AuxTarget);
194   HeaderInfo.setTarget(Target);
195 
196   // Populate the identifier table with info about keywords for the current language.
197   Identifiers.AddKeywords(LangOpts);
198 }
199 
200 void Preprocessor::InitializeForModelFile() {
201   NumEnteredSourceFiles = 0;
202 
203   // Reset pragmas
204   PragmaHandlersBackup = std::move(PragmaHandlers);
205   PragmaHandlers = llvm::make_unique<PragmaNamespace>(StringRef());
206   RegisterBuiltinPragmas();
207 
208   // Reset PredefinesFileID
209   PredefinesFileID = FileID();
210 }
211 
212 void Preprocessor::FinalizeForModelFile() {
213   NumEnteredSourceFiles = 1;
214 
215   PragmaHandlers = std::move(PragmaHandlersBackup);
216 }
217 
218 void Preprocessor::setPTHManager(PTHManager* pm) {
219   PTH.reset(pm);
220   FileMgr.addStatCache(PTH->createStatCache());
221 }
222 
223 void Preprocessor::DumpToken(const Token &Tok, bool DumpFlags) const {
224   llvm::errs() << tok::getTokenName(Tok.getKind()) << " '"
225                << getSpelling(Tok) << "'";
226 
227   if (!DumpFlags) return;
228 
229   llvm::errs() << "\t";
230   if (Tok.isAtStartOfLine())
231     llvm::errs() << " [StartOfLine]";
232   if (Tok.hasLeadingSpace())
233     llvm::errs() << " [LeadingSpace]";
234   if (Tok.isExpandDisabled())
235     llvm::errs() << " [ExpandDisabled]";
236   if (Tok.needsCleaning()) {
237     const char *Start = SourceMgr.getCharacterData(Tok.getLocation());
238     llvm::errs() << " [UnClean='" << StringRef(Start, Tok.getLength())
239                  << "']";
240   }
241 
242   llvm::errs() << "\tLoc=<";
243   DumpLocation(Tok.getLocation());
244   llvm::errs() << ">";
245 }
246 
247 void Preprocessor::DumpLocation(SourceLocation Loc) const {
248   Loc.dump(SourceMgr);
249 }
250 
251 void Preprocessor::DumpMacro(const MacroInfo &MI) const {
252   llvm::errs() << "MACRO: ";
253   for (unsigned i = 0, e = MI.getNumTokens(); i != e; ++i) {
254     DumpToken(MI.getReplacementToken(i));
255     llvm::errs() << "  ";
256   }
257   llvm::errs() << "\n";
258 }
259 
260 void Preprocessor::PrintStats() {
261   llvm::errs() << "\n*** Preprocessor Stats:\n";
262   llvm::errs() << NumDirectives << " directives found:\n";
263   llvm::errs() << "  " << NumDefined << " #define.\n";
264   llvm::errs() << "  " << NumUndefined << " #undef.\n";
265   llvm::errs() << "  #include/#include_next/#import:\n";
266   llvm::errs() << "    " << NumEnteredSourceFiles << " source files entered.\n";
267   llvm::errs() << "    " << MaxIncludeStackDepth << " max include stack depth\n";
268   llvm::errs() << "  " << NumIf << " #if/#ifndef/#ifdef.\n";
269   llvm::errs() << "  " << NumElse << " #else/#elif.\n";
270   llvm::errs() << "  " << NumEndif << " #endif.\n";
271   llvm::errs() << "  " << NumPragma << " #pragma.\n";
272   llvm::errs() << NumSkipped << " #if/#ifndef#ifdef regions skipped\n";
273 
274   llvm::errs() << NumMacroExpanded << "/" << NumFnMacroExpanded << "/"
275              << NumBuiltinMacroExpanded << " obj/fn/builtin macros expanded, "
276              << NumFastMacroExpanded << " on the fast path.\n";
277   llvm::errs() << (NumFastTokenPaste+NumTokenPaste)
278              << " token paste (##) operations performed, "
279              << NumFastTokenPaste << " on the fast path.\n";
280 
281   llvm::errs() << "\nPreprocessor Memory: " << getTotalMemory() << "B total";
282 
283   llvm::errs() << "\n  BumpPtr: " << BP.getTotalMemory();
284   llvm::errs() << "\n  Macro Expanded Tokens: "
285                << llvm::capacity_in_bytes(MacroExpandedTokens);
286   llvm::errs() << "\n  Predefines Buffer: " << Predefines.capacity();
287   // FIXME: List information for all submodules.
288   llvm::errs() << "\n  Macros: "
289                << llvm::capacity_in_bytes(CurSubmoduleState->Macros);
290   llvm::errs() << "\n  #pragma push_macro Info: "
291                << llvm::capacity_in_bytes(PragmaPushMacroInfo);
292   llvm::errs() << "\n  Poison Reasons: "
293                << llvm::capacity_in_bytes(PoisonReasons);
294   llvm::errs() << "\n  Comment Handlers: "
295                << llvm::capacity_in_bytes(CommentHandlers) << "\n";
296 }
297 
298 Preprocessor::macro_iterator
299 Preprocessor::macro_begin(bool IncludeExternalMacros) const {
300   if (IncludeExternalMacros && ExternalSource &&
301       !ReadMacrosFromExternalSource) {
302     ReadMacrosFromExternalSource = true;
303     ExternalSource->ReadDefinedMacros();
304   }
305 
306   // Make sure we cover all macros in visible modules.
307   for (const ModuleMacro &Macro : ModuleMacros)
308     CurSubmoduleState->Macros.insert(std::make_pair(Macro.II, MacroState()));
309 
310   return CurSubmoduleState->Macros.begin();
311 }
312 
313 size_t Preprocessor::getTotalMemory() const {
314   return BP.getTotalMemory()
315     + llvm::capacity_in_bytes(MacroExpandedTokens)
316     + Predefines.capacity() /* Predefines buffer. */
317     // FIXME: Include sizes from all submodules, and include MacroInfo sizes,
318     // and ModuleMacros.
319     + llvm::capacity_in_bytes(CurSubmoduleState->Macros)
320     + llvm::capacity_in_bytes(PragmaPushMacroInfo)
321     + llvm::capacity_in_bytes(PoisonReasons)
322     + llvm::capacity_in_bytes(CommentHandlers);
323 }
324 
325 Preprocessor::macro_iterator
326 Preprocessor::macro_end(bool IncludeExternalMacros) const {
327   if (IncludeExternalMacros && ExternalSource &&
328       !ReadMacrosFromExternalSource) {
329     ReadMacrosFromExternalSource = true;
330     ExternalSource->ReadDefinedMacros();
331   }
332 
333   return CurSubmoduleState->Macros.end();
334 }
335 
336 /// Compares macro tokens with a specified token value sequence.
337 static bool MacroDefinitionEquals(const MacroInfo *MI,
338                                   ArrayRef<TokenValue> Tokens) {
339   return Tokens.size() == MI->getNumTokens() &&
340       std::equal(Tokens.begin(), Tokens.end(), MI->tokens_begin());
341 }
342 
343 StringRef Preprocessor::getLastMacroWithSpelling(
344                                     SourceLocation Loc,
345                                     ArrayRef<TokenValue> Tokens) const {
346   SourceLocation BestLocation;
347   StringRef BestSpelling;
348   for (Preprocessor::macro_iterator I = macro_begin(), E = macro_end();
349        I != E; ++I) {
350     const MacroDirective::DefInfo
351       Def = I->second.findDirectiveAtLoc(Loc, SourceMgr);
352     if (!Def || !Def.getMacroInfo())
353       continue;
354     if (!Def.getMacroInfo()->isObjectLike())
355       continue;
356     if (!MacroDefinitionEquals(Def.getMacroInfo(), Tokens))
357       continue;
358     SourceLocation Location = Def.getLocation();
359     // Choose the macro defined latest.
360     if (BestLocation.isInvalid() ||
361         (Location.isValid() &&
362          SourceMgr.isBeforeInTranslationUnit(BestLocation, Location))) {
363       BestLocation = Location;
364       BestSpelling = I->first->getName();
365     }
366   }
367   return BestSpelling;
368 }
369 
370 void Preprocessor::recomputeCurLexerKind() {
371   if (CurLexer)
372     CurLexerKind = CLK_Lexer;
373   else if (CurPTHLexer)
374     CurLexerKind = CLK_PTHLexer;
375   else if (CurTokenLexer)
376     CurLexerKind = CLK_TokenLexer;
377   else
378     CurLexerKind = CLK_CachingLexer;
379 }
380 
381 bool Preprocessor::SetCodeCompletionPoint(const FileEntry *File,
382                                           unsigned CompleteLine,
383                                           unsigned CompleteColumn) {
384   assert(File);
385   assert(CompleteLine && CompleteColumn && "Starts from 1:1");
386   assert(!CodeCompletionFile && "Already set");
387 
388   using llvm::MemoryBuffer;
389 
390   // Load the actual file's contents.
391   bool Invalid = false;
392   const MemoryBuffer *Buffer = SourceMgr.getMemoryBufferForFile(File, &Invalid);
393   if (Invalid)
394     return true;
395 
396   // Find the byte position of the truncation point.
397   const char *Position = Buffer->getBufferStart();
398   for (unsigned Line = 1; Line < CompleteLine; ++Line) {
399     for (; *Position; ++Position) {
400       if (*Position != '\r' && *Position != '\n')
401         continue;
402 
403       // Eat \r\n or \n\r as a single line.
404       if ((Position[1] == '\r' || Position[1] == '\n') &&
405           Position[0] != Position[1])
406         ++Position;
407       ++Position;
408       break;
409     }
410   }
411 
412   Position += CompleteColumn - 1;
413 
414   // If pointing inside the preamble, adjust the position at the beginning of
415   // the file after the preamble.
416   if (SkipMainFilePreamble.first &&
417       SourceMgr.getFileEntryForID(SourceMgr.getMainFileID()) == File) {
418     if (Position - Buffer->getBufferStart() < SkipMainFilePreamble.first)
419       Position = Buffer->getBufferStart() + SkipMainFilePreamble.first;
420   }
421 
422   if (Position > Buffer->getBufferEnd())
423     Position = Buffer->getBufferEnd();
424 
425   CodeCompletionFile = File;
426   CodeCompletionOffset = Position - Buffer->getBufferStart();
427 
428   auto NewBuffer = llvm::WritableMemoryBuffer::getNewUninitMemBuffer(
429       Buffer->getBufferSize() + 1, Buffer->getBufferIdentifier());
430   char *NewBuf = NewBuffer->getBufferStart();
431   char *NewPos = std::copy(Buffer->getBufferStart(), Position, NewBuf);
432   *NewPos = '\0';
433   std::copy(Position, Buffer->getBufferEnd(), NewPos+1);
434   SourceMgr.overrideFileContents(File, std::move(NewBuffer));
435 
436   return false;
437 }
438 
439 void Preprocessor::CodeCompleteNaturalLanguage() {
440   if (CodeComplete)
441     CodeComplete->CodeCompleteNaturalLanguage();
442   setCodeCompletionReached();
443 }
444 
445 /// getSpelling - This method is used to get the spelling of a token into a
446 /// SmallVector. Note that the returned StringRef may not point to the
447 /// supplied buffer if a copy can be avoided.
448 StringRef Preprocessor::getSpelling(const Token &Tok,
449                                           SmallVectorImpl<char> &Buffer,
450                                           bool *Invalid) const {
451   // NOTE: this has to be checked *before* testing for an IdentifierInfo.
452   if (Tok.isNot(tok::raw_identifier) && !Tok.hasUCN()) {
453     // Try the fast path.
454     if (const IdentifierInfo *II = Tok.getIdentifierInfo())
455       return II->getName();
456   }
457 
458   // Resize the buffer if we need to copy into it.
459   if (Tok.needsCleaning())
460     Buffer.resize(Tok.getLength());
461 
462   const char *Ptr = Buffer.data();
463   unsigned Len = getSpelling(Tok, Ptr, Invalid);
464   return StringRef(Ptr, Len);
465 }
466 
467 /// CreateString - Plop the specified string into a scratch buffer and return a
468 /// location for it.  If specified, the source location provides a source
469 /// location for the token.
470 void Preprocessor::CreateString(StringRef Str, Token &Tok,
471                                 SourceLocation ExpansionLocStart,
472                                 SourceLocation ExpansionLocEnd) {
473   Tok.setLength(Str.size());
474 
475   const char *DestPtr;
476   SourceLocation Loc = ScratchBuf->getToken(Str.data(), Str.size(), DestPtr);
477 
478   if (ExpansionLocStart.isValid())
479     Loc = SourceMgr.createExpansionLoc(Loc, ExpansionLocStart,
480                                        ExpansionLocEnd, Str.size());
481   Tok.setLocation(Loc);
482 
483   // If this is a raw identifier or a literal token, set the pointer data.
484   if (Tok.is(tok::raw_identifier))
485     Tok.setRawIdentifierData(DestPtr);
486   else if (Tok.isLiteral())
487     Tok.setLiteralData(DestPtr);
488 }
489 
490 SourceLocation Preprocessor::SplitToken(SourceLocation Loc, unsigned Length) {
491   auto &SM = getSourceManager();
492   SourceLocation SpellingLoc = SM.getSpellingLoc(Loc);
493   std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(SpellingLoc);
494   bool Invalid = false;
495   StringRef Buffer = SM.getBufferData(LocInfo.first, &Invalid);
496   if (Invalid)
497     return SourceLocation();
498 
499   // FIXME: We could consider re-using spelling for tokens we see repeatedly.
500   const char *DestPtr;
501   SourceLocation Spelling =
502       ScratchBuf->getToken(Buffer.data() + LocInfo.second, Length, DestPtr);
503   return SM.createTokenSplitLoc(Spelling, Loc, Loc.getLocWithOffset(Length));
504 }
505 
506 Module *Preprocessor::getCurrentModule() {
507   if (!getLangOpts().isCompilingModule())
508     return nullptr;
509 
510   return getHeaderSearchInfo().lookupModule(getLangOpts().CurrentModule);
511 }
512 
513 //===----------------------------------------------------------------------===//
514 // Preprocessor Initialization Methods
515 //===----------------------------------------------------------------------===//
516 
517 /// EnterMainSourceFile - Enter the specified FileID as the main source file,
518 /// which implicitly adds the builtin defines etc.
519 void Preprocessor::EnterMainSourceFile() {
520   // We do not allow the preprocessor to reenter the main file.  Doing so will
521   // cause FileID's to accumulate information from both runs (e.g. #line
522   // information) and predefined macros aren't guaranteed to be set properly.
523   assert(NumEnteredSourceFiles == 0 && "Cannot reenter the main file!");
524   FileID MainFileID = SourceMgr.getMainFileID();
525 
526   // If MainFileID is loaded it means we loaded an AST file, no need to enter
527   // a main file.
528   if (!SourceMgr.isLoadedFileID(MainFileID)) {
529     // Enter the main file source buffer.
530     EnterSourceFile(MainFileID, nullptr, SourceLocation());
531 
532     // If we've been asked to skip bytes in the main file (e.g., as part of a
533     // precompiled preamble), do so now.
534     if (SkipMainFilePreamble.first > 0)
535       CurLexer->SetByteOffset(SkipMainFilePreamble.first,
536                               SkipMainFilePreamble.second);
537 
538     // Tell the header info that the main file was entered.  If the file is later
539     // #imported, it won't be re-entered.
540     if (const FileEntry *FE = SourceMgr.getFileEntryForID(MainFileID))
541       HeaderInfo.IncrementIncludeCount(FE);
542   }
543 
544   // Preprocess Predefines to populate the initial preprocessor state.
545   std::unique_ptr<llvm::MemoryBuffer> SB =
546     llvm::MemoryBuffer::getMemBufferCopy(Predefines, "<built-in>");
547   assert(SB && "Cannot create predefined source buffer");
548   FileID FID = SourceMgr.createFileID(std::move(SB));
549   assert(FID.isValid() && "Could not create FileID for predefines?");
550   setPredefinesFileID(FID);
551 
552   // Start parsing the predefines.
553   EnterSourceFile(FID, nullptr, SourceLocation());
554 }
555 
556 void Preprocessor::replayPreambleConditionalStack() {
557   // Restore the conditional stack from the preamble, if there is one.
558   if (PreambleConditionalStack.isReplaying()) {
559     assert(CurPPLexer &&
560            "CurPPLexer is null when calling replayPreambleConditionalStack.");
561     CurPPLexer->setConditionalLevels(PreambleConditionalStack.getStack());
562     PreambleConditionalStack.doneReplaying();
563     if (PreambleConditionalStack.reachedEOFWhileSkipping())
564       SkipExcludedConditionalBlock(
565           PreambleConditionalStack.SkipInfo->HashTokenLoc,
566           PreambleConditionalStack.SkipInfo->IfTokenLoc,
567           PreambleConditionalStack.SkipInfo->FoundNonSkipPortion,
568           PreambleConditionalStack.SkipInfo->FoundElse,
569           PreambleConditionalStack.SkipInfo->ElseLoc);
570   }
571 }
572 
573 void Preprocessor::EndSourceFile() {
574   // Notify the client that we reached the end of the source file.
575   if (Callbacks)
576     Callbacks->EndOfMainFile();
577 }
578 
579 //===----------------------------------------------------------------------===//
580 // Lexer Event Handling.
581 //===----------------------------------------------------------------------===//
582 
583 /// LookUpIdentifierInfo - Given a tok::raw_identifier token, look up the
584 /// identifier information for the token and install it into the token,
585 /// updating the token kind accordingly.
586 IdentifierInfo *Preprocessor::LookUpIdentifierInfo(Token &Identifier) const {
587   assert(!Identifier.getRawIdentifier().empty() && "No raw identifier data!");
588 
589   // Look up this token, see if it is a macro, or if it is a language keyword.
590   IdentifierInfo *II;
591   if (!Identifier.needsCleaning() && !Identifier.hasUCN()) {
592     // No cleaning needed, just use the characters from the lexed buffer.
593     II = getIdentifierInfo(Identifier.getRawIdentifier());
594   } else {
595     // Cleaning needed, alloca a buffer, clean into it, then use the buffer.
596     SmallString<64> IdentifierBuffer;
597     StringRef CleanedStr = getSpelling(Identifier, IdentifierBuffer);
598 
599     if (Identifier.hasUCN()) {
600       SmallString<64> UCNIdentifierBuffer;
601       expandUCNs(UCNIdentifierBuffer, CleanedStr);
602       II = getIdentifierInfo(UCNIdentifierBuffer);
603     } else {
604       II = getIdentifierInfo(CleanedStr);
605     }
606   }
607 
608   // Update the token info (identifier info and appropriate token kind).
609   Identifier.setIdentifierInfo(II);
610   if (getLangOpts().MSVCCompat && II->isCPlusPlusOperatorKeyword() &&
611       getSourceManager().isInSystemHeader(Identifier.getLocation()))
612     Identifier.setKind(tok::identifier);
613   else
614     Identifier.setKind(II->getTokenID());
615 
616   return II;
617 }
618 
619 void Preprocessor::SetPoisonReason(IdentifierInfo *II, unsigned DiagID) {
620   PoisonReasons[II] = DiagID;
621 }
622 
623 void Preprocessor::PoisonSEHIdentifiers(bool Poison) {
624   assert(Ident__exception_code && Ident__exception_info);
625   assert(Ident___exception_code && Ident___exception_info);
626   Ident__exception_code->setIsPoisoned(Poison);
627   Ident___exception_code->setIsPoisoned(Poison);
628   Ident_GetExceptionCode->setIsPoisoned(Poison);
629   Ident__exception_info->setIsPoisoned(Poison);
630   Ident___exception_info->setIsPoisoned(Poison);
631   Ident_GetExceptionInfo->setIsPoisoned(Poison);
632   Ident__abnormal_termination->setIsPoisoned(Poison);
633   Ident___abnormal_termination->setIsPoisoned(Poison);
634   Ident_AbnormalTermination->setIsPoisoned(Poison);
635 }
636 
637 void Preprocessor::HandlePoisonedIdentifier(Token & Identifier) {
638   assert(Identifier.getIdentifierInfo() &&
639          "Can't handle identifiers without identifier info!");
640   llvm::DenseMap<IdentifierInfo*,unsigned>::const_iterator it =
641     PoisonReasons.find(Identifier.getIdentifierInfo());
642   if(it == PoisonReasons.end())
643     Diag(Identifier, diag::err_pp_used_poisoned_id);
644   else
645     Diag(Identifier,it->second) << Identifier.getIdentifierInfo();
646 }
647 
648 /// Returns a diagnostic message kind for reporting a future keyword as
649 /// appropriate for the identifier and specified language.
650 static diag::kind getFutureCompatDiagKind(const IdentifierInfo &II,
651                                           const LangOptions &LangOpts) {
652   assert(II.isFutureCompatKeyword() && "diagnostic should not be needed");
653 
654   if (LangOpts.CPlusPlus)
655     return llvm::StringSwitch<diag::kind>(II.getName())
656 #define CXX11_KEYWORD(NAME, FLAGS)                                             \
657         .Case(#NAME, diag::warn_cxx11_keyword)
658 #define CXX2A_KEYWORD(NAME, FLAGS)                                             \
659         .Case(#NAME, diag::warn_cxx2a_keyword)
660 #include "clang/Basic/TokenKinds.def"
661         ;
662 
663   llvm_unreachable(
664       "Keyword not known to come from a newer Standard or proposed Standard");
665 }
666 
667 void Preprocessor::updateOutOfDateIdentifier(IdentifierInfo &II) const {
668   assert(II.isOutOfDate() && "not out of date");
669   getExternalSource()->updateOutOfDateIdentifier(II);
670 }
671 
672 /// HandleIdentifier - This callback is invoked when the lexer reads an
673 /// identifier.  This callback looks up the identifier in the map and/or
674 /// potentially macro expands it or turns it into a named token (like 'for').
675 ///
676 /// Note that callers of this method are guarded by checking the
677 /// IdentifierInfo's 'isHandleIdentifierCase' bit.  If this method changes, the
678 /// IdentifierInfo methods that compute these properties will need to change to
679 /// match.
680 bool Preprocessor::HandleIdentifier(Token &Identifier) {
681   assert(Identifier.getIdentifierInfo() &&
682          "Can't handle identifiers without identifier info!");
683 
684   IdentifierInfo &II = *Identifier.getIdentifierInfo();
685 
686   // If the information about this identifier is out of date, update it from
687   // the external source.
688   // We have to treat __VA_ARGS__ in a special way, since it gets
689   // serialized with isPoisoned = true, but our preprocessor may have
690   // unpoisoned it if we're defining a C99 macro.
691   if (II.isOutOfDate()) {
692     bool CurrentIsPoisoned = false;
693     const bool IsSpecialVariadicMacro =
694         &II == Ident__VA_ARGS__ || &II == Ident__VA_OPT__;
695     if (IsSpecialVariadicMacro)
696       CurrentIsPoisoned = II.isPoisoned();
697 
698     updateOutOfDateIdentifier(II);
699     Identifier.setKind(II.getTokenID());
700 
701     if (IsSpecialVariadicMacro)
702       II.setIsPoisoned(CurrentIsPoisoned);
703   }
704 
705   // If this identifier was poisoned, and if it was not produced from a macro
706   // expansion, emit an error.
707   if (II.isPoisoned() && CurPPLexer) {
708     HandlePoisonedIdentifier(Identifier);
709   }
710 
711   // If this is a macro to be expanded, do it.
712   if (MacroDefinition MD = getMacroDefinition(&II)) {
713     auto *MI = MD.getMacroInfo();
714     assert(MI && "macro definition with no macro info?");
715     if (!DisableMacroExpansion) {
716       if (!Identifier.isExpandDisabled() && MI->isEnabled()) {
717         // C99 6.10.3p10: If the preprocessing token immediately after the
718         // macro name isn't a '(', this macro should not be expanded.
719         if (!MI->isFunctionLike() || isNextPPTokenLParen())
720           return HandleMacroExpandedIdentifier(Identifier, MD);
721       } else {
722         // C99 6.10.3.4p2 says that a disabled macro may never again be
723         // expanded, even if it's in a context where it could be expanded in the
724         // future.
725         Identifier.setFlag(Token::DisableExpand);
726         if (MI->isObjectLike() || isNextPPTokenLParen())
727           Diag(Identifier, diag::pp_disabled_macro_expansion);
728       }
729     }
730   }
731 
732   // If this identifier is a keyword in a newer Standard or proposed Standard,
733   // produce a warning. Don't warn if we're not considering macro expansion,
734   // since this identifier might be the name of a macro.
735   // FIXME: This warning is disabled in cases where it shouldn't be, like
736   //   "#define constexpr constexpr", "int constexpr;"
737   if (II.isFutureCompatKeyword() && !DisableMacroExpansion) {
738     Diag(Identifier, getFutureCompatDiagKind(II, getLangOpts()))
739         << II.getName();
740     // Don't diagnose this keyword again in this translation unit.
741     II.setIsFutureCompatKeyword(false);
742   }
743 
744   // If this is an extension token, diagnose its use.
745   // We avoid diagnosing tokens that originate from macro definitions.
746   // FIXME: This warning is disabled in cases where it shouldn't be,
747   // like "#define TY typeof", "TY(1) x".
748   if (II.isExtensionToken() && !DisableMacroExpansion)
749     Diag(Identifier, diag::ext_token_used);
750 
751   // If this is the 'import' contextual keyword following an '@', note
752   // that the next token indicates a module name.
753   //
754   // Note that we do not treat 'import' as a contextual
755   // keyword when we're in a caching lexer, because caching lexers only get
756   // used in contexts where import declarations are disallowed.
757   //
758   // Likewise if this is the C++ Modules TS import keyword.
759   if (((LastTokenWasAt && II.isModulesImport()) ||
760        Identifier.is(tok::kw_import)) &&
761       !InMacroArgs && !DisableMacroExpansion &&
762       (getLangOpts().Modules || getLangOpts().DebuggerSupport) &&
763       CurLexerKind != CLK_CachingLexer) {
764     ModuleImportLoc = Identifier.getLocation();
765     ModuleImportPath.clear();
766     ModuleImportExpectsIdentifier = true;
767     CurLexerKind = CLK_LexAfterModuleImport;
768   }
769   return true;
770 }
771 
772 void Preprocessor::Lex(Token &Result) {
773   // We loop here until a lex function returns a token; this avoids recursion.
774   bool ReturnedToken;
775   do {
776     switch (CurLexerKind) {
777     case CLK_Lexer:
778       ReturnedToken = CurLexer->Lex(Result);
779       break;
780     case CLK_PTHLexer:
781       ReturnedToken = CurPTHLexer->Lex(Result);
782       break;
783     case CLK_TokenLexer:
784       ReturnedToken = CurTokenLexer->Lex(Result);
785       break;
786     case CLK_CachingLexer:
787       CachingLex(Result);
788       ReturnedToken = true;
789       break;
790     case CLK_LexAfterModuleImport:
791       LexAfterModuleImport(Result);
792       ReturnedToken = true;
793       break;
794     }
795   } while (!ReturnedToken);
796 
797   if (Result.is(tok::code_completion) && Result.getIdentifierInfo()) {
798     // Remember the identifier before code completion token.
799     setCodeCompletionIdentifierInfo(Result.getIdentifierInfo());
800     // Set IdenfitierInfo to null to avoid confusing code that handles both
801     // identifiers and completion tokens.
802     Result.setIdentifierInfo(nullptr);
803   }
804 
805   LastTokenWasAt = Result.is(tok::at);
806 }
807 
808 /// Lex a token following the 'import' contextual keyword.
809 ///
810 void Preprocessor::LexAfterModuleImport(Token &Result) {
811   // Figure out what kind of lexer we actually have.
812   recomputeCurLexerKind();
813 
814   // Lex the next token.
815   Lex(Result);
816 
817   // The token sequence
818   //
819   //   import identifier (. identifier)*
820   //
821   // indicates a module import directive. We already saw the 'import'
822   // contextual keyword, so now we're looking for the identifiers.
823   if (ModuleImportExpectsIdentifier && Result.getKind() == tok::identifier) {
824     // We expected to see an identifier here, and we did; continue handling
825     // identifiers.
826     ModuleImportPath.push_back(std::make_pair(Result.getIdentifierInfo(),
827                                               Result.getLocation()));
828     ModuleImportExpectsIdentifier = false;
829     CurLexerKind = CLK_LexAfterModuleImport;
830     return;
831   }
832 
833   // If we're expecting a '.' or a ';', and we got a '.', then wait until we
834   // see the next identifier. (We can also see a '[[' that begins an
835   // attribute-specifier-seq here under the C++ Modules TS.)
836   if (!ModuleImportExpectsIdentifier && Result.getKind() == tok::period) {
837     ModuleImportExpectsIdentifier = true;
838     CurLexerKind = CLK_LexAfterModuleImport;
839     return;
840   }
841 
842   // If we have a non-empty module path, load the named module.
843   if (!ModuleImportPath.empty()) {
844     // Under the Modules TS, the dot is just part of the module name, and not
845     // a real hierarachy separator. Flatten such module names now.
846     //
847     // FIXME: Is this the right level to be performing this transformation?
848     std::string FlatModuleName;
849     if (getLangOpts().ModulesTS) {
850       for (auto &Piece : ModuleImportPath) {
851         if (!FlatModuleName.empty())
852           FlatModuleName += ".";
853         FlatModuleName += Piece.first->getName();
854       }
855       SourceLocation FirstPathLoc = ModuleImportPath[0].second;
856       ModuleImportPath.clear();
857       ModuleImportPath.push_back(
858           std::make_pair(getIdentifierInfo(FlatModuleName), FirstPathLoc));
859     }
860 
861     Module *Imported = nullptr;
862     if (getLangOpts().Modules) {
863       Imported = TheModuleLoader.loadModule(ModuleImportLoc,
864                                             ModuleImportPath,
865                                             Module::Hidden,
866                                             /*IsIncludeDirective=*/false);
867       if (Imported)
868         makeModuleVisible(Imported, ModuleImportLoc);
869     }
870     if (Callbacks && (getLangOpts().Modules || getLangOpts().DebuggerSupport))
871       Callbacks->moduleImport(ModuleImportLoc, ModuleImportPath, Imported);
872   }
873 }
874 
875 void Preprocessor::makeModuleVisible(Module *M, SourceLocation Loc) {
876   CurSubmoduleState->VisibleModules.setVisible(
877       M, Loc, [](Module *) {},
878       [&](ArrayRef<Module *> Path, Module *Conflict, StringRef Message) {
879         // FIXME: Include the path in the diagnostic.
880         // FIXME: Include the import location for the conflicting module.
881         Diag(ModuleImportLoc, diag::warn_module_conflict)
882             << Path[0]->getFullModuleName()
883             << Conflict->getFullModuleName()
884             << Message;
885       });
886 
887   // Add this module to the imports list of the currently-built submodule.
888   if (!BuildingSubmoduleStack.empty() && M != BuildingSubmoduleStack.back().M)
889     BuildingSubmoduleStack.back().M->Imports.insert(M);
890 }
891 
892 bool Preprocessor::FinishLexStringLiteral(Token &Result, std::string &String,
893                                           const char *DiagnosticTag,
894                                           bool AllowMacroExpansion) {
895   // We need at least one string literal.
896   if (Result.isNot(tok::string_literal)) {
897     Diag(Result, diag::err_expected_string_literal)
898       << /*Source='in...'*/0 << DiagnosticTag;
899     return false;
900   }
901 
902   // Lex string literal tokens, optionally with macro expansion.
903   SmallVector<Token, 4> StrToks;
904   do {
905     StrToks.push_back(Result);
906 
907     if (Result.hasUDSuffix())
908       Diag(Result, diag::err_invalid_string_udl);
909 
910     if (AllowMacroExpansion)
911       Lex(Result);
912     else
913       LexUnexpandedToken(Result);
914   } while (Result.is(tok::string_literal));
915 
916   // Concatenate and parse the strings.
917   StringLiteralParser Literal(StrToks, *this);
918   assert(Literal.isAscii() && "Didn't allow wide strings in");
919 
920   if (Literal.hadError)
921     return false;
922 
923   if (Literal.Pascal) {
924     Diag(StrToks[0].getLocation(), diag::err_expected_string_literal)
925       << /*Source='in...'*/0 << DiagnosticTag;
926     return false;
927   }
928 
929   String = Literal.GetString();
930   return true;
931 }
932 
933 bool Preprocessor::parseSimpleIntegerLiteral(Token &Tok, uint64_t &Value) {
934   assert(Tok.is(tok::numeric_constant));
935   SmallString<8> IntegerBuffer;
936   bool NumberInvalid = false;
937   StringRef Spelling = getSpelling(Tok, IntegerBuffer, &NumberInvalid);
938   if (NumberInvalid)
939     return false;
940   NumericLiteralParser Literal(Spelling, Tok.getLocation(), *this);
941   if (Literal.hadError || !Literal.isIntegerLiteral() || Literal.hasUDSuffix())
942     return false;
943   llvm::APInt APVal(64, 0);
944   if (Literal.GetIntegerValue(APVal))
945     return false;
946   Lex(Tok);
947   Value = APVal.getLimitedValue();
948   return true;
949 }
950 
951 void Preprocessor::addCommentHandler(CommentHandler *Handler) {
952   assert(Handler && "NULL comment handler");
953   assert(std::find(CommentHandlers.begin(), CommentHandlers.end(), Handler) ==
954          CommentHandlers.end() && "Comment handler already registered");
955   CommentHandlers.push_back(Handler);
956 }
957 
958 void Preprocessor::removeCommentHandler(CommentHandler *Handler) {
959   std::vector<CommentHandler *>::iterator Pos =
960       std::find(CommentHandlers.begin(), CommentHandlers.end(), Handler);
961   assert(Pos != CommentHandlers.end() && "Comment handler not registered");
962   CommentHandlers.erase(Pos);
963 }
964 
965 bool Preprocessor::HandleComment(Token &result, SourceRange Comment) {
966   bool AnyPendingTokens = false;
967   for (std::vector<CommentHandler *>::iterator H = CommentHandlers.begin(),
968        HEnd = CommentHandlers.end();
969        H != HEnd; ++H) {
970     if ((*H)->HandleComment(*this, Comment))
971       AnyPendingTokens = true;
972   }
973   if (!AnyPendingTokens || getCommentRetentionState())
974     return false;
975   Lex(result);
976   return true;
977 }
978 
979 ModuleLoader::~ModuleLoader() = default;
980 
981 CommentHandler::~CommentHandler() = default;
982 
983 CodeCompletionHandler::~CodeCompletionHandler() = default;
984 
985 void Preprocessor::createPreprocessingRecord() {
986   if (Record)
987     return;
988 
989   Record = new PreprocessingRecord(getSourceManager());
990   addPPCallbacks(std::unique_ptr<PPCallbacks>(Record));
991 }
992