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