1 //===--- PPDirectives.cpp - Directive Handling for Preprocessor -----------===//
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 /// \file
11 /// \brief Implements # directive processing for the Preprocessor.
12 ///
13 //===----------------------------------------------------------------------===//
14 
15 #include "clang/Lex/Preprocessor.h"
16 #include "clang/Basic/FileManager.h"
17 #include "clang/Basic/SourceManager.h"
18 #include "clang/Lex/CodeCompletionHandler.h"
19 #include "clang/Lex/HeaderSearch.h"
20 #include "clang/Lex/HeaderSearchOptions.h"
21 #include "clang/Lex/LexDiagnostic.h"
22 #include "clang/Lex/LiteralSupport.h"
23 #include "clang/Lex/MacroInfo.h"
24 #include "clang/Lex/ModuleLoader.h"
25 #include "clang/Lex/Pragma.h"
26 #include "llvm/ADT/APInt.h"
27 #include "llvm/Support/ErrorHandling.h"
28 #include "llvm/Support/Path.h"
29 #include "llvm/Support/SaveAndRestore.h"
30 using namespace clang;
31 
32 //===----------------------------------------------------------------------===//
33 // Utility Methods for Preprocessor Directive Handling.
34 //===----------------------------------------------------------------------===//
35 
36 MacroInfo *Preprocessor::AllocateMacroInfo() {
37   MacroInfoChain *MIChain = BP.Allocate<MacroInfoChain>();
38   MIChain->Next = MIChainHead;
39   MIChainHead = MIChain;
40   return &MIChain->MI;
41 }
42 
43 MacroInfo *Preprocessor::AllocateMacroInfo(SourceLocation L) {
44   MacroInfo *MI = AllocateMacroInfo();
45   new (MI) MacroInfo(L);
46   return MI;
47 }
48 
49 MacroInfo *Preprocessor::AllocateDeserializedMacroInfo(SourceLocation L,
50                                                        unsigned SubModuleID) {
51   static_assert(llvm::AlignOf<MacroInfo>::Alignment >= sizeof(SubModuleID),
52                 "alignment for MacroInfo is less than the ID");
53   DeserializedMacroInfoChain *MIChain =
54       BP.Allocate<DeserializedMacroInfoChain>();
55   MIChain->Next = DeserialMIChainHead;
56   DeserialMIChainHead = MIChain;
57 
58   MacroInfo *MI = &MIChain->MI;
59   new (MI) MacroInfo(L);
60   MI->FromASTFile = true;
61   MI->setOwningModuleID(SubModuleID);
62   return MI;
63 }
64 
65 DefMacroDirective *Preprocessor::AllocateDefMacroDirective(MacroInfo *MI,
66                                                            SourceLocation Loc) {
67   return new (BP) DefMacroDirective(MI, Loc);
68 }
69 
70 UndefMacroDirective *
71 Preprocessor::AllocateUndefMacroDirective(SourceLocation UndefLoc) {
72   return new (BP) UndefMacroDirective(UndefLoc);
73 }
74 
75 VisibilityMacroDirective *
76 Preprocessor::AllocateVisibilityMacroDirective(SourceLocation Loc,
77                                                bool isPublic) {
78   return new (BP) VisibilityMacroDirective(Loc, isPublic);
79 }
80 
81 /// \brief Read and discard all tokens remaining on the current line until
82 /// the tok::eod token is found.
83 void Preprocessor::DiscardUntilEndOfDirective() {
84   Token Tmp;
85   do {
86     LexUnexpandedToken(Tmp);
87     assert(Tmp.isNot(tok::eof) && "EOF seen while discarding directive tokens");
88   } while (Tmp.isNot(tok::eod));
89 }
90 
91 /// \brief Enumerates possible cases of #define/#undef a reserved identifier.
92 enum MacroDiag {
93   MD_NoWarn,        //> Not a reserved identifier
94   MD_KeywordDef,    //> Macro hides keyword, enabled by default
95   MD_ReservedMacro  //> #define of #undef reserved id, disabled by default
96 };
97 
98 /// \brief Checks if the specified identifier is reserved in the specified
99 /// language.
100 /// This function does not check if the identifier is a keyword.
101 static bool isReservedId(StringRef Text, const LangOptions &Lang) {
102   // C++ [macro.names], C11 7.1.3:
103   // All identifiers that begin with an underscore and either an uppercase
104   // letter or another underscore are always reserved for any use.
105   if (Text.size() >= 2 && Text[0] == '_' &&
106       (isUppercase(Text[1]) || Text[1] == '_'))
107       return true;
108   // C++ [global.names]
109   // Each name that contains a double underscore ... is reserved to the
110   // implementation for any use.
111   if (Lang.CPlusPlus) {
112     if (Text.find("__") != StringRef::npos)
113       return true;
114   }
115   return false;
116 }
117 
118 static MacroDiag shouldWarnOnMacroDef(Preprocessor &PP, IdentifierInfo *II) {
119   const LangOptions &Lang = PP.getLangOpts();
120   StringRef Text = II->getName();
121   if (isReservedId(Text, Lang))
122     return MD_ReservedMacro;
123   if (II->isKeyword(Lang))
124     return MD_KeywordDef;
125   if (Lang.CPlusPlus11 && (Text.equals("override") || Text.equals("final")))
126     return MD_KeywordDef;
127   return MD_NoWarn;
128 }
129 
130 static MacroDiag shouldWarnOnMacroUndef(Preprocessor &PP, IdentifierInfo *II) {
131   const LangOptions &Lang = PP.getLangOpts();
132   StringRef Text = II->getName();
133   // Do not warn on keyword undef.  It is generally harmless and widely used.
134   if (isReservedId(Text, Lang))
135     return MD_ReservedMacro;
136   return MD_NoWarn;
137 }
138 
139 bool Preprocessor::CheckMacroName(Token &MacroNameTok, MacroUse isDefineUndef,
140                                   bool *ShadowFlag) {
141   // Missing macro name?
142   if (MacroNameTok.is(tok::eod))
143     return Diag(MacroNameTok, diag::err_pp_missing_macro_name);
144 
145   IdentifierInfo *II = MacroNameTok.getIdentifierInfo();
146   if (!II) {
147     bool Invalid = false;
148     std::string Spelling = getSpelling(MacroNameTok, &Invalid);
149     if (Invalid)
150       return Diag(MacroNameTok, diag::err_pp_macro_not_identifier);
151     II = getIdentifierInfo(Spelling);
152 
153     if (!II->isCPlusPlusOperatorKeyword())
154       return Diag(MacroNameTok, diag::err_pp_macro_not_identifier);
155 
156     // C++ 2.5p2: Alternative tokens behave the same as its primary token
157     // except for their spellings.
158     Diag(MacroNameTok, getLangOpts().MicrosoftExt
159                            ? diag::ext_pp_operator_used_as_macro_name
160                            : diag::err_pp_operator_used_as_macro_name)
161         << II << MacroNameTok.getKind();
162 
163     // Allow #defining |and| and friends for Microsoft compatibility or
164     // recovery when legacy C headers are included in C++.
165     MacroNameTok.setIdentifierInfo(II);
166   }
167 
168   if ((isDefineUndef != MU_Other) && II->getPPKeywordID() == tok::pp_defined) {
169     // Error if defining "defined": C99 6.10.8/4, C++ [cpp.predefined]p4.
170     return Diag(MacroNameTok, diag::err_defined_macro_name);
171   }
172 
173   if (isDefineUndef == MU_Undef) {
174     auto *MI = getMacroInfo(II);
175     if (MI && MI->isBuiltinMacro()) {
176       // Warn if undefining "__LINE__" and other builtins, per C99 6.10.8/4
177       // and C++ [cpp.predefined]p4], but allow it as an extension.
178       Diag(MacroNameTok, diag::ext_pp_undef_builtin_macro);
179     }
180   }
181 
182   // If defining/undefining reserved identifier or a keyword, we need to issue
183   // a warning.
184   SourceLocation MacroNameLoc = MacroNameTok.getLocation();
185   if (ShadowFlag)
186     *ShadowFlag = false;
187   if (!SourceMgr.isInSystemHeader(MacroNameLoc) &&
188       (strcmp(SourceMgr.getBufferName(MacroNameLoc), "<built-in>") != 0)) {
189     MacroDiag D = MD_NoWarn;
190     if (isDefineUndef == MU_Define) {
191       D = shouldWarnOnMacroDef(*this, II);
192     }
193     else if (isDefineUndef == MU_Undef)
194       D = shouldWarnOnMacroUndef(*this, II);
195     if (D == MD_KeywordDef) {
196       // We do not want to warn on some patterns widely used in configuration
197       // scripts.  This requires analyzing next tokens, so do not issue warnings
198       // now, only inform caller.
199       if (ShadowFlag)
200         *ShadowFlag = true;
201     }
202     if (D == MD_ReservedMacro)
203       Diag(MacroNameTok, diag::warn_pp_macro_is_reserved_id);
204   }
205 
206   // Okay, we got a good identifier.
207   return false;
208 }
209 
210 /// \brief Lex and validate a macro name, which occurs after a
211 /// \#define or \#undef.
212 ///
213 /// This sets the token kind to eod and discards the rest of the macro line if
214 /// the macro name is invalid.
215 ///
216 /// \param MacroNameTok Token that is expected to be a macro name.
217 /// \param isDefineUndef Context in which macro is used.
218 /// \param ShadowFlag Points to a flag that is set if macro shadows a keyword.
219 void Preprocessor::ReadMacroName(Token &MacroNameTok, MacroUse isDefineUndef,
220                                  bool *ShadowFlag) {
221   // Read the token, don't allow macro expansion on it.
222   LexUnexpandedToken(MacroNameTok);
223 
224   if (MacroNameTok.is(tok::code_completion)) {
225     if (CodeComplete)
226       CodeComplete->CodeCompleteMacroName(isDefineUndef == MU_Define);
227     setCodeCompletionReached();
228     LexUnexpandedToken(MacroNameTok);
229   }
230 
231   if (!CheckMacroName(MacroNameTok, isDefineUndef, ShadowFlag))
232     return;
233 
234   // Invalid macro name, read and discard the rest of the line and set the
235   // token kind to tok::eod if necessary.
236   if (MacroNameTok.isNot(tok::eod)) {
237     MacroNameTok.setKind(tok::eod);
238     DiscardUntilEndOfDirective();
239   }
240 }
241 
242 /// \brief Ensure that the next token is a tok::eod token.
243 ///
244 /// If not, emit a diagnostic and consume up until the eod.  If EnableMacros is
245 /// true, then we consider macros that expand to zero tokens as being ok.
246 void Preprocessor::CheckEndOfDirective(const char *DirType, bool EnableMacros) {
247   Token Tmp;
248   // Lex unexpanded tokens for most directives: macros might expand to zero
249   // tokens, causing us to miss diagnosing invalid lines.  Some directives (like
250   // #line) allow empty macros.
251   if (EnableMacros)
252     Lex(Tmp);
253   else
254     LexUnexpandedToken(Tmp);
255 
256   // There should be no tokens after the directive, but we allow them as an
257   // extension.
258   while (Tmp.is(tok::comment))  // Skip comments in -C mode.
259     LexUnexpandedToken(Tmp);
260 
261   if (Tmp.isNot(tok::eod)) {
262     // Add a fixit in GNU/C99/C++ mode.  Don't offer a fixit for strict-C89,
263     // or if this is a macro-style preprocessing directive, because it is more
264     // trouble than it is worth to insert /**/ and check that there is no /**/
265     // in the range also.
266     FixItHint Hint;
267     if ((LangOpts.GNUMode || LangOpts.C99 || LangOpts.CPlusPlus) &&
268         !CurTokenLexer)
269       Hint = FixItHint::CreateInsertion(Tmp.getLocation(),"//");
270     Diag(Tmp, diag::ext_pp_extra_tokens_at_eol) << DirType << Hint;
271     DiscardUntilEndOfDirective();
272   }
273 }
274 
275 
276 
277 /// SkipExcludedConditionalBlock - We just read a \#if or related directive and
278 /// decided that the subsequent tokens are in the \#if'd out portion of the
279 /// file.  Lex the rest of the file, until we see an \#endif.  If
280 /// FoundNonSkipPortion is true, then we have already emitted code for part of
281 /// this \#if directive, so \#else/\#elif blocks should never be entered.
282 /// If ElseOk is true, then \#else directives are ok, if not, then we have
283 /// already seen one so a \#else directive is a duplicate.  When this returns,
284 /// the caller can lex the first valid token.
285 void Preprocessor::SkipExcludedConditionalBlock(SourceLocation IfTokenLoc,
286                                                 bool FoundNonSkipPortion,
287                                                 bool FoundElse,
288                                                 SourceLocation ElseLoc) {
289   ++NumSkipped;
290   assert(!CurTokenLexer && CurPPLexer && "Lexing a macro, not a file?");
291 
292   CurPPLexer->pushConditionalLevel(IfTokenLoc, /*isSkipping*/false,
293                                  FoundNonSkipPortion, FoundElse);
294 
295   if (CurPTHLexer) {
296     PTHSkipExcludedConditionalBlock();
297     return;
298   }
299 
300   // Enter raw mode to disable identifier lookup (and thus macro expansion),
301   // disabling warnings, etc.
302   CurPPLexer->LexingRawMode = true;
303   Token Tok;
304   while (1) {
305     CurLexer->Lex(Tok);
306 
307     if (Tok.is(tok::code_completion)) {
308       if (CodeComplete)
309         CodeComplete->CodeCompleteInConditionalExclusion();
310       setCodeCompletionReached();
311       continue;
312     }
313 
314     // If this is the end of the buffer, we have an error.
315     if (Tok.is(tok::eof)) {
316       // Emit errors for each unterminated conditional on the stack, including
317       // the current one.
318       while (!CurPPLexer->ConditionalStack.empty()) {
319         if (CurLexer->getFileLoc() != CodeCompletionFileLoc)
320           Diag(CurPPLexer->ConditionalStack.back().IfLoc,
321                diag::err_pp_unterminated_conditional);
322         CurPPLexer->ConditionalStack.pop_back();
323       }
324 
325       // Just return and let the caller lex after this #include.
326       break;
327     }
328 
329     // If this token is not a preprocessor directive, just skip it.
330     if (Tok.isNot(tok::hash) || !Tok.isAtStartOfLine())
331       continue;
332 
333     // We just parsed a # character at the start of a line, so we're in
334     // directive mode.  Tell the lexer this so any newlines we see will be
335     // converted into an EOD token (this terminates the macro).
336     CurPPLexer->ParsingPreprocessorDirective = true;
337     if (CurLexer) CurLexer->SetKeepWhitespaceMode(false);
338 
339 
340     // Read the next token, the directive flavor.
341     LexUnexpandedToken(Tok);
342 
343     // If this isn't an identifier directive (e.g. is "# 1\n" or "#\n", or
344     // something bogus), skip it.
345     if (Tok.isNot(tok::raw_identifier)) {
346       CurPPLexer->ParsingPreprocessorDirective = false;
347       // Restore comment saving mode.
348       if (CurLexer) CurLexer->resetExtendedTokenMode();
349       continue;
350     }
351 
352     // If the first letter isn't i or e, it isn't intesting to us.  We know that
353     // this is safe in the face of spelling differences, because there is no way
354     // to spell an i/e in a strange way that is another letter.  Skipping this
355     // allows us to avoid looking up the identifier info for #define/#undef and
356     // other common directives.
357     StringRef RI = Tok.getRawIdentifier();
358 
359     char FirstChar = RI[0];
360     if (FirstChar >= 'a' && FirstChar <= 'z' &&
361         FirstChar != 'i' && FirstChar != 'e') {
362       CurPPLexer->ParsingPreprocessorDirective = false;
363       // Restore comment saving mode.
364       if (CurLexer) CurLexer->resetExtendedTokenMode();
365       continue;
366     }
367 
368     // Get the identifier name without trigraphs or embedded newlines.  Note
369     // that we can't use Tok.getIdentifierInfo() because its lookup is disabled
370     // when skipping.
371     char DirectiveBuf[20];
372     StringRef Directive;
373     if (!Tok.needsCleaning() && RI.size() < 20) {
374       Directive = RI;
375     } else {
376       std::string DirectiveStr = getSpelling(Tok);
377       unsigned IdLen = DirectiveStr.size();
378       if (IdLen >= 20) {
379         CurPPLexer->ParsingPreprocessorDirective = false;
380         // Restore comment saving mode.
381         if (CurLexer) CurLexer->resetExtendedTokenMode();
382         continue;
383       }
384       memcpy(DirectiveBuf, &DirectiveStr[0], IdLen);
385       Directive = StringRef(DirectiveBuf, IdLen);
386     }
387 
388     if (Directive.startswith("if")) {
389       StringRef Sub = Directive.substr(2);
390       if (Sub.empty() ||   // "if"
391           Sub == "def" ||   // "ifdef"
392           Sub == "ndef") {  // "ifndef"
393         // We know the entire #if/#ifdef/#ifndef block will be skipped, don't
394         // bother parsing the condition.
395         DiscardUntilEndOfDirective();
396         CurPPLexer->pushConditionalLevel(Tok.getLocation(), /*wasskipping*/true,
397                                        /*foundnonskip*/false,
398                                        /*foundelse*/false);
399       }
400     } else if (Directive[0] == 'e') {
401       StringRef Sub = Directive.substr(1);
402       if (Sub == "ndif") {  // "endif"
403         PPConditionalInfo CondInfo;
404         CondInfo.WasSkipping = true; // Silence bogus warning.
405         bool InCond = CurPPLexer->popConditionalLevel(CondInfo);
406         (void)InCond;  // Silence warning in no-asserts mode.
407         assert(!InCond && "Can't be skipping if not in a conditional!");
408 
409         // If we popped the outermost skipping block, we're done skipping!
410         if (!CondInfo.WasSkipping) {
411           // Restore the value of LexingRawMode so that trailing comments
412           // are handled correctly, if we've reached the outermost block.
413           CurPPLexer->LexingRawMode = false;
414           CheckEndOfDirective("endif");
415           CurPPLexer->LexingRawMode = true;
416           if (Callbacks)
417             Callbacks->Endif(Tok.getLocation(), CondInfo.IfLoc);
418           break;
419         } else {
420           DiscardUntilEndOfDirective();
421         }
422       } else if (Sub == "lse") { // "else".
423         // #else directive in a skipping conditional.  If not in some other
424         // skipping conditional, and if #else hasn't already been seen, enter it
425         // as a non-skipping conditional.
426         PPConditionalInfo &CondInfo = CurPPLexer->peekConditionalLevel();
427 
428         // If this is a #else with a #else before it, report the error.
429         if (CondInfo.FoundElse) Diag(Tok, diag::pp_err_else_after_else);
430 
431         // Note that we've seen a #else in this conditional.
432         CondInfo.FoundElse = true;
433 
434         // If the conditional is at the top level, and the #if block wasn't
435         // entered, enter the #else block now.
436         if (!CondInfo.WasSkipping && !CondInfo.FoundNonSkip) {
437           CondInfo.FoundNonSkip = true;
438           // Restore the value of LexingRawMode so that trailing comments
439           // are handled correctly.
440           CurPPLexer->LexingRawMode = false;
441           CheckEndOfDirective("else");
442           CurPPLexer->LexingRawMode = true;
443           if (Callbacks)
444             Callbacks->Else(Tok.getLocation(), CondInfo.IfLoc);
445           break;
446         } else {
447           DiscardUntilEndOfDirective();  // C99 6.10p4.
448         }
449       } else if (Sub == "lif") {  // "elif".
450         PPConditionalInfo &CondInfo = CurPPLexer->peekConditionalLevel();
451 
452         // If this is a #elif with a #else before it, report the error.
453         if (CondInfo.FoundElse) Diag(Tok, diag::pp_err_elif_after_else);
454 
455         // If this is in a skipping block or if we're already handled this #if
456         // block, don't bother parsing the condition.
457         if (CondInfo.WasSkipping || CondInfo.FoundNonSkip) {
458           DiscardUntilEndOfDirective();
459         } else {
460           const SourceLocation CondBegin = CurPPLexer->getSourceLocation();
461           // Restore the value of LexingRawMode so that identifiers are
462           // looked up, etc, inside the #elif expression.
463           assert(CurPPLexer->LexingRawMode && "We have to be skipping here!");
464           CurPPLexer->LexingRawMode = false;
465           IdentifierInfo *IfNDefMacro = nullptr;
466           const bool CondValue = EvaluateDirectiveExpression(IfNDefMacro);
467           CurPPLexer->LexingRawMode = true;
468           if (Callbacks) {
469             const SourceLocation CondEnd = CurPPLexer->getSourceLocation();
470             Callbacks->Elif(Tok.getLocation(),
471                             SourceRange(CondBegin, CondEnd),
472                             (CondValue ? PPCallbacks::CVK_True : PPCallbacks::CVK_False), CondInfo.IfLoc);
473           }
474           // If this condition is true, enter it!
475           if (CondValue) {
476             CondInfo.FoundNonSkip = true;
477             break;
478           }
479         }
480       }
481     }
482 
483     CurPPLexer->ParsingPreprocessorDirective = false;
484     // Restore comment saving mode.
485     if (CurLexer) CurLexer->resetExtendedTokenMode();
486   }
487 
488   // Finally, if we are out of the conditional (saw an #endif or ran off the end
489   // of the file, just stop skipping and return to lexing whatever came after
490   // the #if block.
491   CurPPLexer->LexingRawMode = false;
492 
493   if (Callbacks) {
494     SourceLocation BeginLoc = ElseLoc.isValid() ? ElseLoc : IfTokenLoc;
495     Callbacks->SourceRangeSkipped(SourceRange(BeginLoc, Tok.getLocation()));
496   }
497 }
498 
499 void Preprocessor::PTHSkipExcludedConditionalBlock() {
500 
501   while (1) {
502     assert(CurPTHLexer);
503     assert(CurPTHLexer->LexingRawMode == false);
504 
505     // Skip to the next '#else', '#elif', or #endif.
506     if (CurPTHLexer->SkipBlock()) {
507       // We have reached an #endif.  Both the '#' and 'endif' tokens
508       // have been consumed by the PTHLexer.  Just pop off the condition level.
509       PPConditionalInfo CondInfo;
510       bool InCond = CurPTHLexer->popConditionalLevel(CondInfo);
511       (void)InCond;  // Silence warning in no-asserts mode.
512       assert(!InCond && "Can't be skipping if not in a conditional!");
513       break;
514     }
515 
516     // We have reached a '#else' or '#elif'.  Lex the next token to get
517     // the directive flavor.
518     Token Tok;
519     LexUnexpandedToken(Tok);
520 
521     // We can actually look up the IdentifierInfo here since we aren't in
522     // raw mode.
523     tok::PPKeywordKind K = Tok.getIdentifierInfo()->getPPKeywordID();
524 
525     if (K == tok::pp_else) {
526       // #else: Enter the else condition.  We aren't in a nested condition
527       //  since we skip those. We're always in the one matching the last
528       //  blocked we skipped.
529       PPConditionalInfo &CondInfo = CurPTHLexer->peekConditionalLevel();
530       // Note that we've seen a #else in this conditional.
531       CondInfo.FoundElse = true;
532 
533       // If the #if block wasn't entered then enter the #else block now.
534       if (!CondInfo.FoundNonSkip) {
535         CondInfo.FoundNonSkip = true;
536 
537         // Scan until the eod token.
538         CurPTHLexer->ParsingPreprocessorDirective = true;
539         DiscardUntilEndOfDirective();
540         CurPTHLexer->ParsingPreprocessorDirective = false;
541 
542         break;
543       }
544 
545       // Otherwise skip this block.
546       continue;
547     }
548 
549     assert(K == tok::pp_elif);
550     PPConditionalInfo &CondInfo = CurPTHLexer->peekConditionalLevel();
551 
552     // If this is a #elif with a #else before it, report the error.
553     if (CondInfo.FoundElse)
554       Diag(Tok, diag::pp_err_elif_after_else);
555 
556     // If this is in a skipping block or if we're already handled this #if
557     // block, don't bother parsing the condition.  We just skip this block.
558     if (CondInfo.FoundNonSkip)
559       continue;
560 
561     // Evaluate the condition of the #elif.
562     IdentifierInfo *IfNDefMacro = nullptr;
563     CurPTHLexer->ParsingPreprocessorDirective = true;
564     bool ShouldEnter = EvaluateDirectiveExpression(IfNDefMacro);
565     CurPTHLexer->ParsingPreprocessorDirective = false;
566 
567     // If this condition is true, enter it!
568     if (ShouldEnter) {
569       CondInfo.FoundNonSkip = true;
570       break;
571     }
572 
573     // Otherwise, skip this block and go to the next one.
574     continue;
575   }
576 }
577 
578 Module *Preprocessor::getModuleForLocation(SourceLocation Loc) {
579   ModuleMap &ModMap = HeaderInfo.getModuleMap();
580   if (SourceMgr.isInMainFile(Loc)) {
581     if (Module *CurMod = getCurrentModule())
582       return CurMod;                               // Compiling a module.
583     return HeaderInfo.getModuleMap().SourceModule; // Compiling a source.
584   }
585   // Try to determine the module of the include directive.
586   // FIXME: Look into directly passing the FileEntry from LookupFile instead.
587   FileID IDOfIncl = SourceMgr.getFileID(SourceMgr.getExpansionLoc(Loc));
588   if (const FileEntry *EntryOfIncl = SourceMgr.getFileEntryForID(IDOfIncl)) {
589     // The include comes from a file.
590     return ModMap.findModuleForHeader(EntryOfIncl).getModule();
591   } else {
592     // The include does not come from a file,
593     // so it is probably a module compilation.
594     return getCurrentModule();
595   }
596 }
597 
598 Module *Preprocessor::getModuleContainingLocation(SourceLocation Loc) {
599   return HeaderInfo.getModuleMap().inferModuleFromLocation(
600       FullSourceLoc(Loc, SourceMgr));
601 }
602 
603 const FileEntry *Preprocessor::LookupFile(
604     SourceLocation FilenameLoc,
605     StringRef Filename,
606     bool isAngled,
607     const DirectoryLookup *FromDir,
608     const FileEntry *FromFile,
609     const DirectoryLookup *&CurDir,
610     SmallVectorImpl<char> *SearchPath,
611     SmallVectorImpl<char> *RelativePath,
612     ModuleMap::KnownHeader *SuggestedModule,
613     bool SkipCache) {
614   Module *RequestingModule = getModuleForLocation(FilenameLoc);
615 
616   // If the header lookup mechanism may be relative to the current inclusion
617   // stack, record the parent #includes.
618   SmallVector<std::pair<const FileEntry *, const DirectoryEntry *>, 16>
619       Includers;
620   if (!FromDir && !FromFile) {
621     FileID FID = getCurrentFileLexer()->getFileID();
622     const FileEntry *FileEnt = SourceMgr.getFileEntryForID(FID);
623 
624     // If there is no file entry associated with this file, it must be the
625     // predefines buffer or the module includes buffer. Any other file is not
626     // lexed with a normal lexer, so it won't be scanned for preprocessor
627     // directives.
628     //
629     // If we have the predefines buffer, resolve #include references (which come
630     // from the -include command line argument) from the current working
631     // directory instead of relative to the main file.
632     //
633     // If we have the module includes buffer, resolve #include references (which
634     // come from header declarations in the module map) relative to the module
635     // map file.
636     if (!FileEnt) {
637       if (FID == SourceMgr.getMainFileID() && MainFileDir)
638         Includers.push_back(std::make_pair(nullptr, MainFileDir));
639       else if ((FileEnt =
640                     SourceMgr.getFileEntryForID(SourceMgr.getMainFileID())))
641         Includers.push_back(std::make_pair(FileEnt, FileMgr.getDirectory(".")));
642     } else {
643       Includers.push_back(std::make_pair(FileEnt, FileEnt->getDir()));
644     }
645 
646     // MSVC searches the current include stack from top to bottom for
647     // headers included by quoted include directives.
648     // See: http://msdn.microsoft.com/en-us/library/36k2cdd4.aspx
649     if (LangOpts.MSVCCompat && !isAngled) {
650       for (unsigned i = 0, e = IncludeMacroStack.size(); i != e; ++i) {
651         IncludeStackInfo &ISEntry = IncludeMacroStack[e - i - 1];
652         if (IsFileLexer(ISEntry))
653           if ((FileEnt = SourceMgr.getFileEntryForID(
654                    ISEntry.ThePPLexer->getFileID())))
655             Includers.push_back(std::make_pair(FileEnt, FileEnt->getDir()));
656       }
657     }
658   }
659 
660   CurDir = CurDirLookup;
661 
662   if (FromFile) {
663     // We're supposed to start looking from after a particular file. Search
664     // the include path until we find that file or run out of files.
665     const DirectoryLookup *TmpCurDir = CurDir;
666     const DirectoryLookup *TmpFromDir = nullptr;
667     while (const FileEntry *FE = HeaderInfo.LookupFile(
668                Filename, FilenameLoc, isAngled, TmpFromDir, TmpCurDir,
669                Includers, SearchPath, RelativePath, RequestingModule,
670                SuggestedModule, SkipCache)) {
671       // Keep looking as if this file did a #include_next.
672       TmpFromDir = TmpCurDir;
673       ++TmpFromDir;
674       if (FE == FromFile) {
675         // Found it.
676         FromDir = TmpFromDir;
677         CurDir = TmpCurDir;
678         break;
679       }
680     }
681   }
682 
683   // Do a standard file entry lookup.
684   const FileEntry *FE = HeaderInfo.LookupFile(
685       Filename, FilenameLoc, isAngled, FromDir, CurDir, Includers, SearchPath,
686       RelativePath, RequestingModule, SuggestedModule, SkipCache);
687   if (FE) {
688     if (SuggestedModule && !LangOpts.AsmPreprocessor)
689       HeaderInfo.getModuleMap().diagnoseHeaderInclusion(
690           RequestingModule, FilenameLoc, Filename, FE);
691     return FE;
692   }
693 
694   const FileEntry *CurFileEnt;
695   // Otherwise, see if this is a subframework header.  If so, this is relative
696   // to one of the headers on the #include stack.  Walk the list of the current
697   // headers on the #include stack and pass them to HeaderInfo.
698   if (IsFileLexer()) {
699     if ((CurFileEnt = SourceMgr.getFileEntryForID(CurPPLexer->getFileID()))) {
700       if ((FE = HeaderInfo.LookupSubframeworkHeader(Filename, CurFileEnt,
701                                                     SearchPath, RelativePath,
702                                                     RequestingModule,
703                                                     SuggestedModule))) {
704         if (SuggestedModule && !LangOpts.AsmPreprocessor)
705           HeaderInfo.getModuleMap().diagnoseHeaderInclusion(
706               RequestingModule, FilenameLoc, Filename, FE);
707         return FE;
708       }
709     }
710   }
711 
712   for (unsigned i = 0, e = IncludeMacroStack.size(); i != e; ++i) {
713     IncludeStackInfo &ISEntry = IncludeMacroStack[e-i-1];
714     if (IsFileLexer(ISEntry)) {
715       if ((CurFileEnt =
716            SourceMgr.getFileEntryForID(ISEntry.ThePPLexer->getFileID()))) {
717         if ((FE = HeaderInfo.LookupSubframeworkHeader(
718                 Filename, CurFileEnt, SearchPath, RelativePath,
719                 RequestingModule, SuggestedModule))) {
720           if (SuggestedModule && !LangOpts.AsmPreprocessor)
721             HeaderInfo.getModuleMap().diagnoseHeaderInclusion(
722                 RequestingModule, FilenameLoc, Filename, FE);
723           return FE;
724         }
725       }
726     }
727   }
728 
729   // Otherwise, we really couldn't find the file.
730   return nullptr;
731 }
732 
733 
734 //===----------------------------------------------------------------------===//
735 // Preprocessor Directive Handling.
736 //===----------------------------------------------------------------------===//
737 
738 class Preprocessor::ResetMacroExpansionHelper {
739 public:
740   ResetMacroExpansionHelper(Preprocessor *pp)
741     : PP(pp), save(pp->DisableMacroExpansion) {
742     if (pp->MacroExpansionInDirectivesOverride)
743       pp->DisableMacroExpansion = false;
744   }
745   ~ResetMacroExpansionHelper() {
746     PP->DisableMacroExpansion = save;
747   }
748 private:
749   Preprocessor *PP;
750   bool save;
751 };
752 
753 /// HandleDirective - This callback is invoked when the lexer sees a # token
754 /// at the start of a line.  This consumes the directive, modifies the
755 /// lexer/preprocessor state, and advances the lexer(s) so that the next token
756 /// read is the correct one.
757 void Preprocessor::HandleDirective(Token &Result) {
758   // FIXME: Traditional: # with whitespace before it not recognized by K&R?
759 
760   // We just parsed a # character at the start of a line, so we're in directive
761   // mode.  Tell the lexer this so any newlines we see will be converted into an
762   // EOD token (which terminates the directive).
763   CurPPLexer->ParsingPreprocessorDirective = true;
764   if (CurLexer) CurLexer->SetKeepWhitespaceMode(false);
765 
766   bool ImmediatelyAfterTopLevelIfndef =
767       CurPPLexer->MIOpt.getImmediatelyAfterTopLevelIfndef();
768   CurPPLexer->MIOpt.resetImmediatelyAfterTopLevelIfndef();
769 
770   ++NumDirectives;
771 
772   // We are about to read a token.  For the multiple-include optimization FA to
773   // work, we have to remember if we had read any tokens *before* this
774   // pp-directive.
775   bool ReadAnyTokensBeforeDirective =CurPPLexer->MIOpt.getHasReadAnyTokensVal();
776 
777   // Save the '#' token in case we need to return it later.
778   Token SavedHash = Result;
779 
780   // Read the next token, the directive flavor.  This isn't expanded due to
781   // C99 6.10.3p8.
782   LexUnexpandedToken(Result);
783 
784   // C99 6.10.3p11: Is this preprocessor directive in macro invocation?  e.g.:
785   //   #define A(x) #x
786   //   A(abc
787   //     #warning blah
788   //   def)
789   // If so, the user is relying on undefined behavior, emit a diagnostic. Do
790   // not support this for #include-like directives, since that can result in
791   // terrible diagnostics, and does not work in GCC.
792   if (InMacroArgs) {
793     if (IdentifierInfo *II = Result.getIdentifierInfo()) {
794       switch (II->getPPKeywordID()) {
795       case tok::pp_include:
796       case tok::pp_import:
797       case tok::pp_include_next:
798       case tok::pp___include_macros:
799       case tok::pp_pragma:
800         Diag(Result, diag::err_embedded_directive) << II->getName();
801         DiscardUntilEndOfDirective();
802         return;
803       default:
804         break;
805       }
806     }
807     Diag(Result, diag::ext_embedded_directive);
808   }
809 
810   // Temporarily enable macro expansion if set so
811   // and reset to previous state when returning from this function.
812   ResetMacroExpansionHelper helper(this);
813 
814   switch (Result.getKind()) {
815   case tok::eod:
816     return;   // null directive.
817   case tok::code_completion:
818     if (CodeComplete)
819       CodeComplete->CodeCompleteDirective(
820                                     CurPPLexer->getConditionalStackDepth() > 0);
821     setCodeCompletionReached();
822     return;
823   case tok::numeric_constant:  // # 7  GNU line marker directive.
824     if (getLangOpts().AsmPreprocessor)
825       break;  // # 4 is not a preprocessor directive in .S files.
826     return HandleDigitDirective(Result);
827   default:
828     IdentifierInfo *II = Result.getIdentifierInfo();
829     if (!II) break; // Not an identifier.
830 
831     // Ask what the preprocessor keyword ID is.
832     switch (II->getPPKeywordID()) {
833     default: break;
834     // C99 6.10.1 - Conditional Inclusion.
835     case tok::pp_if:
836       return HandleIfDirective(Result, ReadAnyTokensBeforeDirective);
837     case tok::pp_ifdef:
838       return HandleIfdefDirective(Result, false, true/*not valid for miopt*/);
839     case tok::pp_ifndef:
840       return HandleIfdefDirective(Result, true, ReadAnyTokensBeforeDirective);
841     case tok::pp_elif:
842       return HandleElifDirective(Result);
843     case tok::pp_else:
844       return HandleElseDirective(Result);
845     case tok::pp_endif:
846       return HandleEndifDirective(Result);
847 
848     // C99 6.10.2 - Source File Inclusion.
849     case tok::pp_include:
850       // Handle #include.
851       return HandleIncludeDirective(SavedHash.getLocation(), Result);
852     case tok::pp___include_macros:
853       // Handle -imacros.
854       return HandleIncludeMacrosDirective(SavedHash.getLocation(), Result);
855 
856     // C99 6.10.3 - Macro Replacement.
857     case tok::pp_define:
858       return HandleDefineDirective(Result, ImmediatelyAfterTopLevelIfndef);
859     case tok::pp_undef:
860       return HandleUndefDirective(Result);
861 
862     // C99 6.10.4 - Line Control.
863     case tok::pp_line:
864       return HandleLineDirective(Result);
865 
866     // C99 6.10.5 - Error Directive.
867     case tok::pp_error:
868       return HandleUserDiagnosticDirective(Result, false);
869 
870     // C99 6.10.6 - Pragma Directive.
871     case tok::pp_pragma:
872       return HandlePragmaDirective(SavedHash.getLocation(), PIK_HashPragma);
873 
874     // GNU Extensions.
875     case tok::pp_import:
876       return HandleImportDirective(SavedHash.getLocation(), Result);
877     case tok::pp_include_next:
878       return HandleIncludeNextDirective(SavedHash.getLocation(), Result);
879 
880     case tok::pp_warning:
881       Diag(Result, diag::ext_pp_warning_directive);
882       return HandleUserDiagnosticDirective(Result, true);
883     case tok::pp_ident:
884       return HandleIdentSCCSDirective(Result);
885     case tok::pp_sccs:
886       return HandleIdentSCCSDirective(Result);
887     case tok::pp_assert:
888       //isExtension = true;  // FIXME: implement #assert
889       break;
890     case tok::pp_unassert:
891       //isExtension = true;  // FIXME: implement #unassert
892       break;
893 
894     case tok::pp___public_macro:
895       if (getLangOpts().Modules)
896         return HandleMacroPublicDirective(Result);
897       break;
898 
899     case tok::pp___private_macro:
900       if (getLangOpts().Modules)
901         return HandleMacroPrivateDirective(Result);
902       break;
903     }
904     break;
905   }
906 
907   // If this is a .S file, treat unknown # directives as non-preprocessor
908   // directives.  This is important because # may be a comment or introduce
909   // various pseudo-ops.  Just return the # token and push back the following
910   // token to be lexed next time.
911   if (getLangOpts().AsmPreprocessor) {
912     Token *Toks = new Token[2];
913     // Return the # and the token after it.
914     Toks[0] = SavedHash;
915     Toks[1] = Result;
916 
917     // If the second token is a hashhash token, then we need to translate it to
918     // unknown so the token lexer doesn't try to perform token pasting.
919     if (Result.is(tok::hashhash))
920       Toks[1].setKind(tok::unknown);
921 
922     // Enter this token stream so that we re-lex the tokens.  Make sure to
923     // enable macro expansion, in case the token after the # is an identifier
924     // that is expanded.
925     EnterTokenStream(Toks, 2, false, true);
926     return;
927   }
928 
929   // If we reached here, the preprocessing token is not valid!
930   Diag(Result, diag::err_pp_invalid_directive);
931 
932   // Read the rest of the PP line.
933   DiscardUntilEndOfDirective();
934 
935   // Okay, we're done parsing the directive.
936 }
937 
938 /// GetLineValue - Convert a numeric token into an unsigned value, emitting
939 /// Diagnostic DiagID if it is invalid, and returning the value in Val.
940 static bool GetLineValue(Token &DigitTok, unsigned &Val,
941                          unsigned DiagID, Preprocessor &PP,
942                          bool IsGNULineDirective=false) {
943   if (DigitTok.isNot(tok::numeric_constant)) {
944     PP.Diag(DigitTok, DiagID);
945 
946     if (DigitTok.isNot(tok::eod))
947       PP.DiscardUntilEndOfDirective();
948     return true;
949   }
950 
951   SmallString<64> IntegerBuffer;
952   IntegerBuffer.resize(DigitTok.getLength());
953   const char *DigitTokBegin = &IntegerBuffer[0];
954   bool Invalid = false;
955   unsigned ActualLength = PP.getSpelling(DigitTok, DigitTokBegin, &Invalid);
956   if (Invalid)
957     return true;
958 
959   // Verify that we have a simple digit-sequence, and compute the value.  This
960   // is always a simple digit string computed in decimal, so we do this manually
961   // here.
962   Val = 0;
963   for (unsigned i = 0; i != ActualLength; ++i) {
964     // C++1y [lex.fcon]p1:
965     //   Optional separating single quotes in a digit-sequence are ignored
966     if (DigitTokBegin[i] == '\'')
967       continue;
968 
969     if (!isDigit(DigitTokBegin[i])) {
970       PP.Diag(PP.AdvanceToTokenCharacter(DigitTok.getLocation(), i),
971               diag::err_pp_line_digit_sequence) << IsGNULineDirective;
972       PP.DiscardUntilEndOfDirective();
973       return true;
974     }
975 
976     unsigned NextVal = Val*10+(DigitTokBegin[i]-'0');
977     if (NextVal < Val) { // overflow.
978       PP.Diag(DigitTok, DiagID);
979       PP.DiscardUntilEndOfDirective();
980       return true;
981     }
982     Val = NextVal;
983   }
984 
985   if (DigitTokBegin[0] == '0' && Val)
986     PP.Diag(DigitTok.getLocation(), diag::warn_pp_line_decimal)
987       << IsGNULineDirective;
988 
989   return false;
990 }
991 
992 /// \brief Handle a \#line directive: C99 6.10.4.
993 ///
994 /// The two acceptable forms are:
995 /// \verbatim
996 ///   # line digit-sequence
997 ///   # line digit-sequence "s-char-sequence"
998 /// \endverbatim
999 void Preprocessor::HandleLineDirective(Token &Tok) {
1000   // Read the line # and string argument.  Per C99 6.10.4p5, these tokens are
1001   // expanded.
1002   Token DigitTok;
1003   Lex(DigitTok);
1004 
1005   // Validate the number and convert it to an unsigned.
1006   unsigned LineNo;
1007   if (GetLineValue(DigitTok, LineNo, diag::err_pp_line_requires_integer,*this))
1008     return;
1009 
1010   if (LineNo == 0)
1011     Diag(DigitTok, diag::ext_pp_line_zero);
1012 
1013   // Enforce C99 6.10.4p3: "The digit sequence shall not specify ... a
1014   // number greater than 2147483647".  C90 requires that the line # be <= 32767.
1015   unsigned LineLimit = 32768U;
1016   if (LangOpts.C99 || LangOpts.CPlusPlus11)
1017     LineLimit = 2147483648U;
1018   if (LineNo >= LineLimit)
1019     Diag(DigitTok, diag::ext_pp_line_too_big) << LineLimit;
1020   else if (LangOpts.CPlusPlus11 && LineNo >= 32768U)
1021     Diag(DigitTok, diag::warn_cxx98_compat_pp_line_too_big);
1022 
1023   int FilenameID = -1;
1024   Token StrTok;
1025   Lex(StrTok);
1026 
1027   // If the StrTok is "eod", then it wasn't present.  Otherwise, it must be a
1028   // string followed by eod.
1029   if (StrTok.is(tok::eod))
1030     ; // ok
1031   else if (StrTok.isNot(tok::string_literal)) {
1032     Diag(StrTok, diag::err_pp_line_invalid_filename);
1033     return DiscardUntilEndOfDirective();
1034   } else if (StrTok.hasUDSuffix()) {
1035     Diag(StrTok, diag::err_invalid_string_udl);
1036     return DiscardUntilEndOfDirective();
1037   } else {
1038     // Parse and validate the string, converting it into a unique ID.
1039     StringLiteralParser Literal(StrTok, *this);
1040     assert(Literal.isAscii() && "Didn't allow wide strings in");
1041     if (Literal.hadError)
1042       return DiscardUntilEndOfDirective();
1043     if (Literal.Pascal) {
1044       Diag(StrTok, diag::err_pp_linemarker_invalid_filename);
1045       return DiscardUntilEndOfDirective();
1046     }
1047     FilenameID = SourceMgr.getLineTableFilenameID(Literal.GetString());
1048 
1049     // Verify that there is nothing after the string, other than EOD.  Because
1050     // of C99 6.10.4p5, macros that expand to empty tokens are ok.
1051     CheckEndOfDirective("line", true);
1052   }
1053 
1054   SourceMgr.AddLineNote(DigitTok.getLocation(), LineNo, FilenameID);
1055 
1056   if (Callbacks)
1057     Callbacks->FileChanged(CurPPLexer->getSourceLocation(),
1058                            PPCallbacks::RenameFile,
1059                            SrcMgr::C_User);
1060 }
1061 
1062 /// ReadLineMarkerFlags - Parse and validate any flags at the end of a GNU line
1063 /// marker directive.
1064 static bool ReadLineMarkerFlags(bool &IsFileEntry, bool &IsFileExit,
1065                                 bool &IsSystemHeader, bool &IsExternCHeader,
1066                                 Preprocessor &PP) {
1067   unsigned FlagVal;
1068   Token FlagTok;
1069   PP.Lex(FlagTok);
1070   if (FlagTok.is(tok::eod)) return false;
1071   if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag, PP))
1072     return true;
1073 
1074   if (FlagVal == 1) {
1075     IsFileEntry = true;
1076 
1077     PP.Lex(FlagTok);
1078     if (FlagTok.is(tok::eod)) return false;
1079     if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag,PP))
1080       return true;
1081   } else if (FlagVal == 2) {
1082     IsFileExit = true;
1083 
1084     SourceManager &SM = PP.getSourceManager();
1085     // If we are leaving the current presumed file, check to make sure the
1086     // presumed include stack isn't empty!
1087     FileID CurFileID =
1088       SM.getDecomposedExpansionLoc(FlagTok.getLocation()).first;
1089     PresumedLoc PLoc = SM.getPresumedLoc(FlagTok.getLocation());
1090     if (PLoc.isInvalid())
1091       return true;
1092 
1093     // If there is no include loc (main file) or if the include loc is in a
1094     // different physical file, then we aren't in a "1" line marker flag region.
1095     SourceLocation IncLoc = PLoc.getIncludeLoc();
1096     if (IncLoc.isInvalid() ||
1097         SM.getDecomposedExpansionLoc(IncLoc).first != CurFileID) {
1098       PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_pop);
1099       PP.DiscardUntilEndOfDirective();
1100       return true;
1101     }
1102 
1103     PP.Lex(FlagTok);
1104     if (FlagTok.is(tok::eod)) return false;
1105     if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag,PP))
1106       return true;
1107   }
1108 
1109   // We must have 3 if there are still flags.
1110   if (FlagVal != 3) {
1111     PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_flag);
1112     PP.DiscardUntilEndOfDirective();
1113     return true;
1114   }
1115 
1116   IsSystemHeader = true;
1117 
1118   PP.Lex(FlagTok);
1119   if (FlagTok.is(tok::eod)) return false;
1120   if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag, PP))
1121     return true;
1122 
1123   // We must have 4 if there is yet another flag.
1124   if (FlagVal != 4) {
1125     PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_flag);
1126     PP.DiscardUntilEndOfDirective();
1127     return true;
1128   }
1129 
1130   IsExternCHeader = true;
1131 
1132   PP.Lex(FlagTok);
1133   if (FlagTok.is(tok::eod)) return false;
1134 
1135   // There are no more valid flags here.
1136   PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_flag);
1137   PP.DiscardUntilEndOfDirective();
1138   return true;
1139 }
1140 
1141 /// HandleDigitDirective - Handle a GNU line marker directive, whose syntax is
1142 /// one of the following forms:
1143 ///
1144 ///     # 42
1145 ///     # 42 "file" ('1' | '2')?
1146 ///     # 42 "file" ('1' | '2')? '3' '4'?
1147 ///
1148 void Preprocessor::HandleDigitDirective(Token &DigitTok) {
1149   // Validate the number and convert it to an unsigned.  GNU does not have a
1150   // line # limit other than it fit in 32-bits.
1151   unsigned LineNo;
1152   if (GetLineValue(DigitTok, LineNo, diag::err_pp_linemarker_requires_integer,
1153                    *this, true))
1154     return;
1155 
1156   Token StrTok;
1157   Lex(StrTok);
1158 
1159   bool IsFileEntry = false, IsFileExit = false;
1160   bool IsSystemHeader = false, IsExternCHeader = false;
1161   int FilenameID = -1;
1162 
1163   // If the StrTok is "eod", then it wasn't present.  Otherwise, it must be a
1164   // string followed by eod.
1165   if (StrTok.is(tok::eod))
1166     ; // ok
1167   else if (StrTok.isNot(tok::string_literal)) {
1168     Diag(StrTok, diag::err_pp_linemarker_invalid_filename);
1169     return DiscardUntilEndOfDirective();
1170   } else if (StrTok.hasUDSuffix()) {
1171     Diag(StrTok, diag::err_invalid_string_udl);
1172     return DiscardUntilEndOfDirective();
1173   } else {
1174     // Parse and validate the string, converting it into a unique ID.
1175     StringLiteralParser Literal(StrTok, *this);
1176     assert(Literal.isAscii() && "Didn't allow wide strings in");
1177     if (Literal.hadError)
1178       return DiscardUntilEndOfDirective();
1179     if (Literal.Pascal) {
1180       Diag(StrTok, diag::err_pp_linemarker_invalid_filename);
1181       return DiscardUntilEndOfDirective();
1182     }
1183     FilenameID = SourceMgr.getLineTableFilenameID(Literal.GetString());
1184 
1185     // If a filename was present, read any flags that are present.
1186     if (ReadLineMarkerFlags(IsFileEntry, IsFileExit,
1187                             IsSystemHeader, IsExternCHeader, *this))
1188       return;
1189   }
1190 
1191   // Create a line note with this information.
1192   SourceMgr.AddLineNote(DigitTok.getLocation(), LineNo, FilenameID,
1193                         IsFileEntry, IsFileExit,
1194                         IsSystemHeader, IsExternCHeader);
1195 
1196   // If the preprocessor has callbacks installed, notify them of the #line
1197   // change.  This is used so that the line marker comes out in -E mode for
1198   // example.
1199   if (Callbacks) {
1200     PPCallbacks::FileChangeReason Reason = PPCallbacks::RenameFile;
1201     if (IsFileEntry)
1202       Reason = PPCallbacks::EnterFile;
1203     else if (IsFileExit)
1204       Reason = PPCallbacks::ExitFile;
1205     SrcMgr::CharacteristicKind FileKind = SrcMgr::C_User;
1206     if (IsExternCHeader)
1207       FileKind = SrcMgr::C_ExternCSystem;
1208     else if (IsSystemHeader)
1209       FileKind = SrcMgr::C_System;
1210 
1211     Callbacks->FileChanged(CurPPLexer->getSourceLocation(), Reason, FileKind);
1212   }
1213 }
1214 
1215 
1216 /// HandleUserDiagnosticDirective - Handle a #warning or #error directive.
1217 ///
1218 void Preprocessor::HandleUserDiagnosticDirective(Token &Tok,
1219                                                  bool isWarning) {
1220   // PTH doesn't emit #warning or #error directives.
1221   if (CurPTHLexer)
1222     return CurPTHLexer->DiscardToEndOfLine();
1223 
1224   // Read the rest of the line raw.  We do this because we don't want macros
1225   // to be expanded and we don't require that the tokens be valid preprocessing
1226   // tokens.  For example, this is allowed: "#warning `   'foo".  GCC does
1227   // collapse multiple consequtive white space between tokens, but this isn't
1228   // specified by the standard.
1229   SmallString<128> Message;
1230   CurLexer->ReadToEndOfLine(&Message);
1231 
1232   // Find the first non-whitespace character, so that we can make the
1233   // diagnostic more succinct.
1234   StringRef Msg = StringRef(Message).ltrim(" ");
1235 
1236   if (isWarning)
1237     Diag(Tok, diag::pp_hash_warning) << Msg;
1238   else
1239     Diag(Tok, diag::err_pp_hash_error) << Msg;
1240 }
1241 
1242 /// HandleIdentSCCSDirective - Handle a #ident/#sccs directive.
1243 ///
1244 void Preprocessor::HandleIdentSCCSDirective(Token &Tok) {
1245   // Yes, this directive is an extension.
1246   Diag(Tok, diag::ext_pp_ident_directive);
1247 
1248   // Read the string argument.
1249   Token StrTok;
1250   Lex(StrTok);
1251 
1252   // If the token kind isn't a string, it's a malformed directive.
1253   if (StrTok.isNot(tok::string_literal) &&
1254       StrTok.isNot(tok::wide_string_literal)) {
1255     Diag(StrTok, diag::err_pp_malformed_ident);
1256     if (StrTok.isNot(tok::eod))
1257       DiscardUntilEndOfDirective();
1258     return;
1259   }
1260 
1261   if (StrTok.hasUDSuffix()) {
1262     Diag(StrTok, diag::err_invalid_string_udl);
1263     return DiscardUntilEndOfDirective();
1264   }
1265 
1266   // Verify that there is nothing after the string, other than EOD.
1267   CheckEndOfDirective("ident");
1268 
1269   if (Callbacks) {
1270     bool Invalid = false;
1271     std::string Str = getSpelling(StrTok, &Invalid);
1272     if (!Invalid)
1273       Callbacks->Ident(Tok.getLocation(), Str);
1274   }
1275 }
1276 
1277 /// \brief Handle a #public directive.
1278 void Preprocessor::HandleMacroPublicDirective(Token &Tok) {
1279   Token MacroNameTok;
1280   ReadMacroName(MacroNameTok, MU_Undef);
1281 
1282   // Error reading macro name?  If so, diagnostic already issued.
1283   if (MacroNameTok.is(tok::eod))
1284     return;
1285 
1286   // Check to see if this is the last token on the #__public_macro line.
1287   CheckEndOfDirective("__public_macro");
1288 
1289   IdentifierInfo *II = MacroNameTok.getIdentifierInfo();
1290   // Okay, we finally have a valid identifier to undef.
1291   MacroDirective *MD = getLocalMacroDirective(II);
1292 
1293   // If the macro is not defined, this is an error.
1294   if (!MD) {
1295     Diag(MacroNameTok, diag::err_pp_visibility_non_macro) << II;
1296     return;
1297   }
1298 
1299   // Note that this macro has now been exported.
1300   appendMacroDirective(II, AllocateVisibilityMacroDirective(
1301                                 MacroNameTok.getLocation(), /*IsPublic=*/true));
1302 }
1303 
1304 /// \brief Handle a #private directive.
1305 void Preprocessor::HandleMacroPrivateDirective(Token &Tok) {
1306   Token MacroNameTok;
1307   ReadMacroName(MacroNameTok, MU_Undef);
1308 
1309   // Error reading macro name?  If so, diagnostic already issued.
1310   if (MacroNameTok.is(tok::eod))
1311     return;
1312 
1313   // Check to see if this is the last token on the #__private_macro line.
1314   CheckEndOfDirective("__private_macro");
1315 
1316   IdentifierInfo *II = MacroNameTok.getIdentifierInfo();
1317   // Okay, we finally have a valid identifier to undef.
1318   MacroDirective *MD = getLocalMacroDirective(II);
1319 
1320   // If the macro is not defined, this is an error.
1321   if (!MD) {
1322     Diag(MacroNameTok, diag::err_pp_visibility_non_macro) << II;
1323     return;
1324   }
1325 
1326   // Note that this macro has now been marked private.
1327   appendMacroDirective(II, AllocateVisibilityMacroDirective(
1328                                MacroNameTok.getLocation(), /*IsPublic=*/false));
1329 }
1330 
1331 //===----------------------------------------------------------------------===//
1332 // Preprocessor Include Directive Handling.
1333 //===----------------------------------------------------------------------===//
1334 
1335 /// GetIncludeFilenameSpelling - Turn the specified lexer token into a fully
1336 /// checked and spelled filename, e.g. as an operand of \#include. This returns
1337 /// true if the input filename was in <>'s or false if it were in ""'s.  The
1338 /// caller is expected to provide a buffer that is large enough to hold the
1339 /// spelling of the filename, but is also expected to handle the case when
1340 /// this method decides to use a different buffer.
1341 bool Preprocessor::GetIncludeFilenameSpelling(SourceLocation Loc,
1342                                               StringRef &Buffer) {
1343   // Get the text form of the filename.
1344   assert(!Buffer.empty() && "Can't have tokens with empty spellings!");
1345 
1346   // Make sure the filename is <x> or "x".
1347   bool isAngled;
1348   if (Buffer[0] == '<') {
1349     if (Buffer.back() != '>') {
1350       Diag(Loc, diag::err_pp_expects_filename);
1351       Buffer = StringRef();
1352       return true;
1353     }
1354     isAngled = true;
1355   } else if (Buffer[0] == '"') {
1356     if (Buffer.back() != '"') {
1357       Diag(Loc, diag::err_pp_expects_filename);
1358       Buffer = StringRef();
1359       return true;
1360     }
1361     isAngled = false;
1362   } else {
1363     Diag(Loc, diag::err_pp_expects_filename);
1364     Buffer = StringRef();
1365     return true;
1366   }
1367 
1368   // Diagnose #include "" as invalid.
1369   if (Buffer.size() <= 2) {
1370     Diag(Loc, diag::err_pp_empty_filename);
1371     Buffer = StringRef();
1372     return true;
1373   }
1374 
1375   // Skip the brackets.
1376   Buffer = Buffer.substr(1, Buffer.size()-2);
1377   return isAngled;
1378 }
1379 
1380 // \brief Handle cases where the \#include name is expanded from a macro
1381 // as multiple tokens, which need to be glued together.
1382 //
1383 // This occurs for code like:
1384 // \code
1385 //    \#define FOO <a/b.h>
1386 //    \#include FOO
1387 // \endcode
1388 // because in this case, "<a/b.h>" is returned as 7 tokens, not one.
1389 //
1390 // This code concatenates and consumes tokens up to the '>' token.  It returns
1391 // false if the > was found, otherwise it returns true if it finds and consumes
1392 // the EOD marker.
1393 bool Preprocessor::ConcatenateIncludeName(SmallString<128> &FilenameBuffer,
1394                                           SourceLocation &End) {
1395   Token CurTok;
1396 
1397   Lex(CurTok);
1398   while (CurTok.isNot(tok::eod)) {
1399     End = CurTok.getLocation();
1400 
1401     // FIXME: Provide code completion for #includes.
1402     if (CurTok.is(tok::code_completion)) {
1403       setCodeCompletionReached();
1404       Lex(CurTok);
1405       continue;
1406     }
1407 
1408     // Append the spelling of this token to the buffer. If there was a space
1409     // before it, add it now.
1410     if (CurTok.hasLeadingSpace())
1411       FilenameBuffer.push_back(' ');
1412 
1413     // Get the spelling of the token, directly into FilenameBuffer if possible.
1414     unsigned PreAppendSize = FilenameBuffer.size();
1415     FilenameBuffer.resize(PreAppendSize+CurTok.getLength());
1416 
1417     const char *BufPtr = &FilenameBuffer[PreAppendSize];
1418     unsigned ActualLen = getSpelling(CurTok, BufPtr);
1419 
1420     // If the token was spelled somewhere else, copy it into FilenameBuffer.
1421     if (BufPtr != &FilenameBuffer[PreAppendSize])
1422       memcpy(&FilenameBuffer[PreAppendSize], BufPtr, ActualLen);
1423 
1424     // Resize FilenameBuffer to the correct size.
1425     if (CurTok.getLength() != ActualLen)
1426       FilenameBuffer.resize(PreAppendSize+ActualLen);
1427 
1428     // If we found the '>' marker, return success.
1429     if (CurTok.is(tok::greater))
1430       return false;
1431 
1432     Lex(CurTok);
1433   }
1434 
1435   // If we hit the eod marker, emit an error and return true so that the caller
1436   // knows the EOD has been read.
1437   Diag(CurTok.getLocation(), diag::err_pp_expects_filename);
1438   return true;
1439 }
1440 
1441 /// \brief Push a token onto the token stream containing an annotation.
1442 static void EnterAnnotationToken(Preprocessor &PP,
1443                                  SourceLocation Begin, SourceLocation End,
1444                                  tok::TokenKind Kind, void *AnnotationVal) {
1445   // FIXME: Produce this as the current token directly, rather than
1446   // allocating a new token for it.
1447   Token *Tok = new Token[1];
1448   Tok[0].startToken();
1449   Tok[0].setKind(Kind);
1450   Tok[0].setLocation(Begin);
1451   Tok[0].setAnnotationEndLoc(End);
1452   Tok[0].setAnnotationValue(AnnotationVal);
1453   PP.EnterTokenStream(Tok, 1, true, true);
1454 }
1455 
1456 /// \brief Produce a diagnostic informing the user that a #include or similar
1457 /// was implicitly treated as a module import.
1458 static void diagnoseAutoModuleImport(
1459     Preprocessor &PP, SourceLocation HashLoc, Token &IncludeTok,
1460     ArrayRef<std::pair<IdentifierInfo *, SourceLocation>> Path,
1461     SourceLocation PathEnd) {
1462   assert(PP.getLangOpts().ObjC2 && "no import syntax available");
1463 
1464   SmallString<128> PathString;
1465   for (unsigned I = 0, N = Path.size(); I != N; ++I) {
1466     if (I)
1467       PathString += '.';
1468     PathString += Path[I].first->getName();
1469   }
1470   int IncludeKind = 0;
1471 
1472   switch (IncludeTok.getIdentifierInfo()->getPPKeywordID()) {
1473   case tok::pp_include:
1474     IncludeKind = 0;
1475     break;
1476 
1477   case tok::pp_import:
1478     IncludeKind = 1;
1479     break;
1480 
1481   case tok::pp_include_next:
1482     IncludeKind = 2;
1483     break;
1484 
1485   case tok::pp___include_macros:
1486     IncludeKind = 3;
1487     break;
1488 
1489   default:
1490     llvm_unreachable("unknown include directive kind");
1491   }
1492 
1493   CharSourceRange ReplaceRange(SourceRange(HashLoc, PathEnd),
1494                                /*IsTokenRange=*/false);
1495   PP.Diag(HashLoc, diag::warn_auto_module_import)
1496       << IncludeKind << PathString
1497       << FixItHint::CreateReplacement(ReplaceRange,
1498                                       ("@import " + PathString + ";").str());
1499 }
1500 
1501 /// HandleIncludeDirective - The "\#include" tokens have just been read, read
1502 /// the file to be included from the lexer, then include it!  This is a common
1503 /// routine with functionality shared between \#include, \#include_next and
1504 /// \#import.  LookupFrom is set when this is a \#include_next directive, it
1505 /// specifies the file to start searching from.
1506 void Preprocessor::HandleIncludeDirective(SourceLocation HashLoc,
1507                                           Token &IncludeTok,
1508                                           const DirectoryLookup *LookupFrom,
1509                                           const FileEntry *LookupFromFile,
1510                                           bool isImport) {
1511 
1512   Token FilenameTok;
1513   CurPPLexer->LexIncludeFilename(FilenameTok);
1514 
1515   // Reserve a buffer to get the spelling.
1516   SmallString<128> FilenameBuffer;
1517   StringRef Filename;
1518   SourceLocation End;
1519   SourceLocation CharEnd; // the end of this directive, in characters
1520 
1521   switch (FilenameTok.getKind()) {
1522   case tok::eod:
1523     // If the token kind is EOD, the error has already been diagnosed.
1524     return;
1525 
1526   case tok::angle_string_literal:
1527   case tok::string_literal:
1528     Filename = getSpelling(FilenameTok, FilenameBuffer);
1529     End = FilenameTok.getLocation();
1530     CharEnd = End.getLocWithOffset(FilenameTok.getLength());
1531     break;
1532 
1533   case tok::less:
1534     // This could be a <foo/bar.h> file coming from a macro expansion.  In this
1535     // case, glue the tokens together into FilenameBuffer and interpret those.
1536     FilenameBuffer.push_back('<');
1537     if (ConcatenateIncludeName(FilenameBuffer, End))
1538       return;   // Found <eod> but no ">"?  Diagnostic already emitted.
1539     Filename = FilenameBuffer;
1540     CharEnd = End.getLocWithOffset(1);
1541     break;
1542   default:
1543     Diag(FilenameTok.getLocation(), diag::err_pp_expects_filename);
1544     DiscardUntilEndOfDirective();
1545     return;
1546   }
1547 
1548   CharSourceRange FilenameRange
1549     = CharSourceRange::getCharRange(FilenameTok.getLocation(), CharEnd);
1550   StringRef OriginalFilename = Filename;
1551   bool isAngled =
1552     GetIncludeFilenameSpelling(FilenameTok.getLocation(), Filename);
1553   // If GetIncludeFilenameSpelling set the start ptr to null, there was an
1554   // error.
1555   if (Filename.empty()) {
1556     DiscardUntilEndOfDirective();
1557     return;
1558   }
1559 
1560   // Verify that there is nothing after the filename, other than EOD.  Note that
1561   // we allow macros that expand to nothing after the filename, because this
1562   // falls into the category of "#include pp-tokens new-line" specified in
1563   // C99 6.10.2p4.
1564   CheckEndOfDirective(IncludeTok.getIdentifierInfo()->getNameStart(), true);
1565 
1566   // Check that we don't have infinite #include recursion.
1567   if (IncludeMacroStack.size() == MaxAllowedIncludeStackDepth-1) {
1568     Diag(FilenameTok, diag::err_pp_include_too_deep);
1569     return;
1570   }
1571 
1572   // Complain about attempts to #include files in an audit pragma.
1573   if (PragmaARCCFCodeAuditedLoc.isValid()) {
1574     Diag(HashLoc, diag::err_pp_include_in_arc_cf_code_audited);
1575     Diag(PragmaARCCFCodeAuditedLoc, diag::note_pragma_entered_here);
1576 
1577     // Immediately leave the pragma.
1578     PragmaARCCFCodeAuditedLoc = SourceLocation();
1579   }
1580 
1581   // Complain about attempts to #include files in an assume-nonnull pragma.
1582   if (PragmaAssumeNonNullLoc.isValid()) {
1583     Diag(HashLoc, diag::err_pp_include_in_assume_nonnull);
1584     Diag(PragmaAssumeNonNullLoc, diag::note_pragma_entered_here);
1585 
1586     // Immediately leave the pragma.
1587     PragmaAssumeNonNullLoc = SourceLocation();
1588   }
1589 
1590   if (HeaderInfo.HasIncludeAliasMap()) {
1591     // Map the filename with the brackets still attached.  If the name doesn't
1592     // map to anything, fall back on the filename we've already gotten the
1593     // spelling for.
1594     StringRef NewName = HeaderInfo.MapHeaderToIncludeAlias(OriginalFilename);
1595     if (!NewName.empty())
1596       Filename = NewName;
1597   }
1598 
1599   // Search include directories.
1600   const DirectoryLookup *CurDir;
1601   SmallString<1024> SearchPath;
1602   SmallString<1024> RelativePath;
1603   // We get the raw path only if we have 'Callbacks' to which we later pass
1604   // the path.
1605   ModuleMap::KnownHeader SuggestedModule;
1606   SourceLocation FilenameLoc = FilenameTok.getLocation();
1607   SmallString<128> NormalizedPath;
1608   if (LangOpts.MSVCCompat) {
1609     NormalizedPath = Filename.str();
1610 #ifndef LLVM_ON_WIN32
1611     llvm::sys::path::native(NormalizedPath);
1612 #endif
1613   }
1614   const FileEntry *File = LookupFile(
1615       FilenameLoc, LangOpts.MSVCCompat ? NormalizedPath.c_str() : Filename,
1616       isAngled, LookupFrom, LookupFromFile, CurDir,
1617       Callbacks ? &SearchPath : nullptr, Callbacks ? &RelativePath : nullptr,
1618       &SuggestedModule);
1619 
1620   if (!File) {
1621     if (Callbacks) {
1622       // Give the clients a chance to recover.
1623       SmallString<128> RecoveryPath;
1624       if (Callbacks->FileNotFound(Filename, RecoveryPath)) {
1625         if (const DirectoryEntry *DE = FileMgr.getDirectory(RecoveryPath)) {
1626           // Add the recovery path to the list of search paths.
1627           DirectoryLookup DL(DE, SrcMgr::C_User, false);
1628           HeaderInfo.AddSearchPath(DL, isAngled);
1629 
1630           // Try the lookup again, skipping the cache.
1631           File = LookupFile(
1632               FilenameLoc,
1633               LangOpts.MSVCCompat ? NormalizedPath.c_str() : Filename, isAngled,
1634               LookupFrom, LookupFromFile, CurDir, nullptr, nullptr,
1635               &SuggestedModule, /*SkipCache*/ true);
1636         }
1637       }
1638     }
1639 
1640     if (!SuppressIncludeNotFoundError) {
1641       // If the file could not be located and it was included via angle
1642       // brackets, we can attempt a lookup as though it were a quoted path to
1643       // provide the user with a possible fixit.
1644       if (isAngled) {
1645         File = LookupFile(
1646             FilenameLoc,
1647             LangOpts.MSVCCompat ? NormalizedPath.c_str() : Filename, false,
1648             LookupFrom, LookupFromFile, CurDir,
1649             Callbacks ? &SearchPath : nullptr,
1650             Callbacks ? &RelativePath : nullptr,
1651             &SuggestedModule);
1652         if (File) {
1653           SourceRange Range(FilenameTok.getLocation(), CharEnd);
1654           Diag(FilenameTok, diag::err_pp_file_not_found_not_fatal) <<
1655             Filename <<
1656             FixItHint::CreateReplacement(Range, "\"" + Filename.str() + "\"");
1657         }
1658       }
1659 
1660       // If the file is still not found, just go with the vanilla diagnostic
1661       if (!File)
1662         Diag(FilenameTok, diag::err_pp_file_not_found) << Filename;
1663     }
1664   }
1665 
1666   // Should we enter the source file? Set to false if either the source file is
1667   // known to have no effect beyond its effect on module visibility -- that is,
1668   // if it's got an include guard that is already defined or is a modular header
1669   // we've imported or already built.
1670   bool ShouldEnter = true;
1671 
1672   // Determine whether we should try to import the module for this #include, if
1673   // there is one. Don't do so if precompiled module support is disabled or we
1674   // are processing this module textually (because we're building the module).
1675   if (File && SuggestedModule && getLangOpts().Modules &&
1676       SuggestedModule.getModule()->getTopLevelModuleName() !=
1677           getLangOpts().CurrentModule &&
1678       SuggestedModule.getModule()->getTopLevelModuleName() !=
1679           getLangOpts().ImplementationOfModule) {
1680 
1681     // If this include corresponds to a module but that module is
1682     // unavailable, diagnose the situation and bail out.
1683     if (!SuggestedModule.getModule()->isAvailable()) {
1684       clang::Module::Requirement Requirement;
1685       clang::Module::UnresolvedHeaderDirective MissingHeader;
1686       Module *M = SuggestedModule.getModule();
1687       // Identify the cause.
1688       (void)M->isAvailable(getLangOpts(), getTargetInfo(), Requirement,
1689                            MissingHeader);
1690       if (MissingHeader.FileNameLoc.isValid()) {
1691         Diag(MissingHeader.FileNameLoc, diag::err_module_header_missing)
1692             << MissingHeader.IsUmbrella << MissingHeader.FileName;
1693       } else {
1694         Diag(M->DefinitionLoc, diag::err_module_unavailable)
1695             << M->getFullModuleName() << Requirement.second << Requirement.first;
1696       }
1697       Diag(FilenameTok.getLocation(),
1698            diag::note_implicit_top_level_module_import_here)
1699           << M->getTopLevelModuleName();
1700       return;
1701     }
1702 
1703     // Compute the module access path corresponding to this module.
1704     // FIXME: Should we have a second loadModule() overload to avoid this
1705     // extra lookup step?
1706     SmallVector<std::pair<IdentifierInfo *, SourceLocation>, 2> Path;
1707     for (Module *Mod = SuggestedModule.getModule(); Mod; Mod = Mod->Parent)
1708       Path.push_back(std::make_pair(getIdentifierInfo(Mod->Name),
1709                                     FilenameTok.getLocation()));
1710     std::reverse(Path.begin(), Path.end());
1711 
1712     // Warn that we're replacing the include/import with a module import.
1713     // We only do this in Objective-C, where we have a module-import syntax.
1714     if (getLangOpts().ObjC2)
1715       diagnoseAutoModuleImport(*this, HashLoc, IncludeTok, Path, CharEnd);
1716 
1717     // Load the module to import its macros. We'll make the declarations
1718     // visible when the parser gets here.
1719     // FIXME: Pass SuggestedModule in here rather than converting it to a path
1720     // and making the module loader convert it back again.
1721     ModuleLoadResult Imported = TheModuleLoader.loadModule(
1722         IncludeTok.getLocation(), Path, Module::Hidden,
1723         /*IsIncludeDirective=*/true);
1724     assert((Imported == nullptr || Imported == SuggestedModule.getModule()) &&
1725            "the imported module is different than the suggested one");
1726 
1727     if (Imported)
1728       ShouldEnter = false;
1729     else if (Imported.isMissingExpected()) {
1730       // We failed to find a submodule that we assumed would exist (because it
1731       // was in the directory of an umbrella header, for instance), but no
1732       // actual module exists for it (because the umbrella header is
1733       // incomplete).  Treat this as a textual inclusion.
1734       SuggestedModule = ModuleMap::KnownHeader();
1735     } else {
1736       // We hit an error processing the import. Bail out.
1737       if (hadModuleLoaderFatalFailure()) {
1738         // With a fatal failure in the module loader, we abort parsing.
1739         Token &Result = IncludeTok;
1740         if (CurLexer) {
1741           Result.startToken();
1742           CurLexer->FormTokenWithChars(Result, CurLexer->BufferEnd, tok::eof);
1743           CurLexer->cutOffLexing();
1744         } else {
1745           assert(CurPTHLexer && "#include but no current lexer set!");
1746           CurPTHLexer->getEOF(Result);
1747         }
1748       }
1749       return;
1750     }
1751   }
1752 
1753   if (Callbacks) {
1754     // Notify the callback object that we've seen an inclusion directive.
1755     Callbacks->InclusionDirective(
1756         HashLoc, IncludeTok,
1757         LangOpts.MSVCCompat ? NormalizedPath.c_str() : Filename, isAngled,
1758         FilenameRange, File, SearchPath, RelativePath,
1759         ShouldEnter ? nullptr : SuggestedModule.getModule());
1760   }
1761 
1762   if (!File)
1763     return;
1764 
1765   // The #included file will be considered to be a system header if either it is
1766   // in a system include directory, or if the #includer is a system include
1767   // header.
1768   SrcMgr::CharacteristicKind FileCharacter =
1769     std::max(HeaderInfo.getFileDirFlavor(File),
1770              SourceMgr.getFileCharacteristic(FilenameTok.getLocation()));
1771 
1772   // FIXME: If we have a suggested module, and we've already visited this file,
1773   // don't bother entering it again. We know it has no further effect.
1774 
1775   // Ask HeaderInfo if we should enter this #include file.  If not, #including
1776   // this file will have no effect.
1777   if (ShouldEnter &&
1778       !HeaderInfo.ShouldEnterIncludeFile(*this, File, isImport,
1779                                          SuggestedModule.getModule())) {
1780     ShouldEnter = false;
1781     if (Callbacks)
1782       Callbacks->FileSkipped(*File, FilenameTok, FileCharacter);
1783   }
1784 
1785   // If we don't need to enter the file, stop now.
1786   if (!ShouldEnter) {
1787     // If this is a module import, make it visible if needed.
1788     if (auto *M = SuggestedModule.getModule()) {
1789       makeModuleVisible(M, HashLoc);
1790 
1791       if (IncludeTok.getIdentifierInfo()->getPPKeywordID() !=
1792           tok::pp___include_macros)
1793         EnterAnnotationToken(*this, HashLoc, End, tok::annot_module_include, M);
1794     }
1795     return;
1796   }
1797 
1798   // Look up the file, create a File ID for it.
1799   SourceLocation IncludePos = End;
1800   // If the filename string was the result of macro expansions, set the include
1801   // position on the file where it will be included and after the expansions.
1802   if (IncludePos.isMacroID())
1803     IncludePos = SourceMgr.getExpansionRange(IncludePos).second;
1804   FileID FID = SourceMgr.createFileID(File, IncludePos, FileCharacter);
1805   assert(FID.isValid() && "Expected valid file ID");
1806 
1807   // If all is good, enter the new file!
1808   if (EnterSourceFile(FID, CurDir, FilenameTok.getLocation()))
1809     return;
1810 
1811   // Determine if we're switching to building a new submodule, and which one.
1812   if (auto *M = SuggestedModule.getModule()) {
1813     assert(!CurSubmodule && "should not have marked this as a module yet");
1814     CurSubmodule = M;
1815 
1816     // Let the macro handling code know that any future macros are within
1817     // the new submodule.
1818     EnterSubmodule(M, HashLoc);
1819 
1820     // Let the parser know that any future declarations are within the new
1821     // submodule.
1822     // FIXME: There's no point doing this if we're handling a #__include_macros
1823     // directive.
1824     EnterAnnotationToken(*this, HashLoc, End, tok::annot_module_begin, M);
1825   }
1826 }
1827 
1828 /// HandleIncludeNextDirective - Implements \#include_next.
1829 ///
1830 void Preprocessor::HandleIncludeNextDirective(SourceLocation HashLoc,
1831                                               Token &IncludeNextTok) {
1832   Diag(IncludeNextTok, diag::ext_pp_include_next_directive);
1833 
1834   // #include_next is like #include, except that we start searching after
1835   // the current found directory.  If we can't do this, issue a
1836   // diagnostic.
1837   const DirectoryLookup *Lookup = CurDirLookup;
1838   const FileEntry *LookupFromFile = nullptr;
1839   if (isInPrimaryFile()) {
1840     Lookup = nullptr;
1841     Diag(IncludeNextTok, diag::pp_include_next_in_primary);
1842   } else if (CurSubmodule) {
1843     // Start looking up in the directory *after* the one in which the current
1844     // file would be found, if any.
1845     assert(CurPPLexer && "#include_next directive in macro?");
1846     LookupFromFile = CurPPLexer->getFileEntry();
1847     Lookup = nullptr;
1848   } else if (!Lookup) {
1849     Diag(IncludeNextTok, diag::pp_include_next_absolute_path);
1850   } else {
1851     // Start looking up in the next directory.
1852     ++Lookup;
1853   }
1854 
1855   return HandleIncludeDirective(HashLoc, IncludeNextTok, Lookup,
1856                                 LookupFromFile);
1857 }
1858 
1859 /// HandleMicrosoftImportDirective - Implements \#import for Microsoft Mode
1860 void Preprocessor::HandleMicrosoftImportDirective(Token &Tok) {
1861   // The Microsoft #import directive takes a type library and generates header
1862   // files from it, and includes those.  This is beyond the scope of what clang
1863   // does, so we ignore it and error out.  However, #import can optionally have
1864   // trailing attributes that span multiple lines.  We're going to eat those
1865   // so we can continue processing from there.
1866   Diag(Tok, diag::err_pp_import_directive_ms );
1867 
1868   // Read tokens until we get to the end of the directive.  Note that the
1869   // directive can be split over multiple lines using the backslash character.
1870   DiscardUntilEndOfDirective();
1871 }
1872 
1873 /// HandleImportDirective - Implements \#import.
1874 ///
1875 void Preprocessor::HandleImportDirective(SourceLocation HashLoc,
1876                                          Token &ImportTok) {
1877   if (!LangOpts.ObjC1) {  // #import is standard for ObjC.
1878     if (LangOpts.MSVCCompat)
1879       return HandleMicrosoftImportDirective(ImportTok);
1880     Diag(ImportTok, diag::ext_pp_import_directive);
1881   }
1882   return HandleIncludeDirective(HashLoc, ImportTok, nullptr, nullptr, true);
1883 }
1884 
1885 /// HandleIncludeMacrosDirective - The -imacros command line option turns into a
1886 /// pseudo directive in the predefines buffer.  This handles it by sucking all
1887 /// tokens through the preprocessor and discarding them (only keeping the side
1888 /// effects on the preprocessor).
1889 void Preprocessor::HandleIncludeMacrosDirective(SourceLocation HashLoc,
1890                                                 Token &IncludeMacrosTok) {
1891   // This directive should only occur in the predefines buffer.  If not, emit an
1892   // error and reject it.
1893   SourceLocation Loc = IncludeMacrosTok.getLocation();
1894   if (strcmp(SourceMgr.getBufferName(Loc), "<built-in>") != 0) {
1895     Diag(IncludeMacrosTok.getLocation(),
1896          diag::pp_include_macros_out_of_predefines);
1897     DiscardUntilEndOfDirective();
1898     return;
1899   }
1900 
1901   // Treat this as a normal #include for checking purposes.  If this is
1902   // successful, it will push a new lexer onto the include stack.
1903   HandleIncludeDirective(HashLoc, IncludeMacrosTok);
1904 
1905   Token TmpTok;
1906   do {
1907     Lex(TmpTok);
1908     assert(TmpTok.isNot(tok::eof) && "Didn't find end of -imacros!");
1909   } while (TmpTok.isNot(tok::hashhash));
1910 }
1911 
1912 //===----------------------------------------------------------------------===//
1913 // Preprocessor Macro Directive Handling.
1914 //===----------------------------------------------------------------------===//
1915 
1916 /// ReadMacroDefinitionArgList - The ( starting an argument list of a macro
1917 /// definition has just been read.  Lex the rest of the arguments and the
1918 /// closing ), updating MI with what we learn.  Return true if an error occurs
1919 /// parsing the arg list.
1920 bool Preprocessor::ReadMacroDefinitionArgList(MacroInfo *MI, Token &Tok) {
1921   SmallVector<IdentifierInfo*, 32> Arguments;
1922 
1923   while (1) {
1924     LexUnexpandedToken(Tok);
1925     switch (Tok.getKind()) {
1926     case tok::r_paren:
1927       // Found the end of the argument list.
1928       if (Arguments.empty())  // #define FOO()
1929         return false;
1930       // Otherwise we have #define FOO(A,)
1931       Diag(Tok, diag::err_pp_expected_ident_in_arg_list);
1932       return true;
1933     case tok::ellipsis:  // #define X(... -> C99 varargs
1934       if (!LangOpts.C99)
1935         Diag(Tok, LangOpts.CPlusPlus11 ?
1936              diag::warn_cxx98_compat_variadic_macro :
1937              diag::ext_variadic_macro);
1938 
1939       // OpenCL v1.2 s6.9.e: variadic macros are not supported.
1940       if (LangOpts.OpenCL) {
1941         Diag(Tok, diag::err_pp_opencl_variadic_macros);
1942         return true;
1943       }
1944 
1945       // Lex the token after the identifier.
1946       LexUnexpandedToken(Tok);
1947       if (Tok.isNot(tok::r_paren)) {
1948         Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
1949         return true;
1950       }
1951       // Add the __VA_ARGS__ identifier as an argument.
1952       Arguments.push_back(Ident__VA_ARGS__);
1953       MI->setIsC99Varargs();
1954       MI->setArgumentList(Arguments, BP);
1955       return false;
1956     case tok::eod:  // #define X(
1957       Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
1958       return true;
1959     default:
1960       // Handle keywords and identifiers here to accept things like
1961       // #define Foo(for) for.
1962       IdentifierInfo *II = Tok.getIdentifierInfo();
1963       if (!II) {
1964         // #define X(1
1965         Diag(Tok, diag::err_pp_invalid_tok_in_arg_list);
1966         return true;
1967       }
1968 
1969       // If this is already used as an argument, it is used multiple times (e.g.
1970       // #define X(A,A.
1971       if (std::find(Arguments.begin(), Arguments.end(), II) !=
1972           Arguments.end()) {  // C99 6.10.3p6
1973         Diag(Tok, diag::err_pp_duplicate_name_in_arg_list) << II;
1974         return true;
1975       }
1976 
1977       // Add the argument to the macro info.
1978       Arguments.push_back(II);
1979 
1980       // Lex the token after the identifier.
1981       LexUnexpandedToken(Tok);
1982 
1983       switch (Tok.getKind()) {
1984       default:          // #define X(A B
1985         Diag(Tok, diag::err_pp_expected_comma_in_arg_list);
1986         return true;
1987       case tok::r_paren: // #define X(A)
1988         MI->setArgumentList(Arguments, BP);
1989         return false;
1990       case tok::comma:  // #define X(A,
1991         break;
1992       case tok::ellipsis:  // #define X(A... -> GCC extension
1993         // Diagnose extension.
1994         Diag(Tok, diag::ext_named_variadic_macro);
1995 
1996         // Lex the token after the identifier.
1997         LexUnexpandedToken(Tok);
1998         if (Tok.isNot(tok::r_paren)) {
1999           Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
2000           return true;
2001         }
2002 
2003         MI->setIsGNUVarargs();
2004         MI->setArgumentList(Arguments, BP);
2005         return false;
2006       }
2007     }
2008   }
2009 }
2010 
2011 static bool isConfigurationPattern(Token &MacroName, MacroInfo *MI,
2012                                    const LangOptions &LOptions) {
2013   if (MI->getNumTokens() == 1) {
2014     const Token &Value = MI->getReplacementToken(0);
2015 
2016     // Macro that is identity, like '#define inline inline' is a valid pattern.
2017     if (MacroName.getKind() == Value.getKind())
2018       return true;
2019 
2020     // Macro that maps a keyword to the same keyword decorated with leading/
2021     // trailing underscores is a valid pattern:
2022     //    #define inline __inline
2023     //    #define inline __inline__
2024     //    #define inline _inline (in MS compatibility mode)
2025     StringRef MacroText = MacroName.getIdentifierInfo()->getName();
2026     if (IdentifierInfo *II = Value.getIdentifierInfo()) {
2027       if (!II->isKeyword(LOptions))
2028         return false;
2029       StringRef ValueText = II->getName();
2030       StringRef TrimmedValue = ValueText;
2031       if (!ValueText.startswith("__")) {
2032         if (ValueText.startswith("_"))
2033           TrimmedValue = TrimmedValue.drop_front(1);
2034         else
2035           return false;
2036       } else {
2037         TrimmedValue = TrimmedValue.drop_front(2);
2038         if (TrimmedValue.endswith("__"))
2039           TrimmedValue = TrimmedValue.drop_back(2);
2040       }
2041       return TrimmedValue.equals(MacroText);
2042     } else {
2043       return false;
2044     }
2045   }
2046 
2047   // #define inline
2048   if (MacroName.isOneOf(tok::kw_extern, tok::kw_inline, tok::kw_static,
2049                         tok::kw_const) &&
2050       MI->getNumTokens() == 0) {
2051     return true;
2052   }
2053 
2054   return false;
2055 }
2056 
2057 /// HandleDefineDirective - Implements \#define.  This consumes the entire macro
2058 /// line then lets the caller lex the next real token.
2059 void Preprocessor::HandleDefineDirective(Token &DefineTok,
2060                                          bool ImmediatelyAfterHeaderGuard) {
2061   ++NumDefined;
2062 
2063   Token MacroNameTok;
2064   bool MacroShadowsKeyword;
2065   ReadMacroName(MacroNameTok, MU_Define, &MacroShadowsKeyword);
2066 
2067   // Error reading macro name?  If so, diagnostic already issued.
2068   if (MacroNameTok.is(tok::eod))
2069     return;
2070 
2071   Token LastTok = MacroNameTok;
2072 
2073   // If we are supposed to keep comments in #defines, reenable comment saving
2074   // mode.
2075   if (CurLexer) CurLexer->SetCommentRetentionState(KeepMacroComments);
2076 
2077   // Create the new macro.
2078   MacroInfo *MI = AllocateMacroInfo(MacroNameTok.getLocation());
2079 
2080   Token Tok;
2081   LexUnexpandedToken(Tok);
2082 
2083   // If this is a function-like macro definition, parse the argument list,
2084   // marking each of the identifiers as being used as macro arguments.  Also,
2085   // check other constraints on the first token of the macro body.
2086   if (Tok.is(tok::eod)) {
2087     if (ImmediatelyAfterHeaderGuard) {
2088       // Save this macro information since it may part of a header guard.
2089       CurPPLexer->MIOpt.SetDefinedMacro(MacroNameTok.getIdentifierInfo(),
2090                                         MacroNameTok.getLocation());
2091     }
2092     // If there is no body to this macro, we have no special handling here.
2093   } else if (Tok.hasLeadingSpace()) {
2094     // This is a normal token with leading space.  Clear the leading space
2095     // marker on the first token to get proper expansion.
2096     Tok.clearFlag(Token::LeadingSpace);
2097   } else if (Tok.is(tok::l_paren)) {
2098     // This is a function-like macro definition.  Read the argument list.
2099     MI->setIsFunctionLike();
2100     if (ReadMacroDefinitionArgList(MI, LastTok)) {
2101       // Throw away the rest of the line.
2102       if (CurPPLexer->ParsingPreprocessorDirective)
2103         DiscardUntilEndOfDirective();
2104       return;
2105     }
2106 
2107     // If this is a definition of a variadic C99 function-like macro, not using
2108     // the GNU named varargs extension, enabled __VA_ARGS__.
2109 
2110     // "Poison" __VA_ARGS__, which can only appear in the expansion of a macro.
2111     // This gets unpoisoned where it is allowed.
2112     assert(Ident__VA_ARGS__->isPoisoned() && "__VA_ARGS__ should be poisoned!");
2113     if (MI->isC99Varargs())
2114       Ident__VA_ARGS__->setIsPoisoned(false);
2115 
2116     // Read the first token after the arg list for down below.
2117     LexUnexpandedToken(Tok);
2118   } else if (LangOpts.C99 || LangOpts.CPlusPlus11) {
2119     // C99 requires whitespace between the macro definition and the body.  Emit
2120     // a diagnostic for something like "#define X+".
2121     Diag(Tok, diag::ext_c99_whitespace_required_after_macro_name);
2122   } else {
2123     // C90 6.8 TC1 says: "In the definition of an object-like macro, if the
2124     // first character of a replacement list is not a character required by
2125     // subclause 5.2.1, then there shall be white-space separation between the
2126     // identifier and the replacement list.".  5.2.1 lists this set:
2127     //   "A-Za-z0-9!"#%&'()*+,_./:;<=>?[\]^_{|}~" as well as whitespace, which
2128     // is irrelevant here.
2129     bool isInvalid = false;
2130     if (Tok.is(tok::at)) // @ is not in the list above.
2131       isInvalid = true;
2132     else if (Tok.is(tok::unknown)) {
2133       // If we have an unknown token, it is something strange like "`".  Since
2134       // all of valid characters would have lexed into a single character
2135       // token of some sort, we know this is not a valid case.
2136       isInvalid = true;
2137     }
2138     if (isInvalid)
2139       Diag(Tok, diag::ext_missing_whitespace_after_macro_name);
2140     else
2141       Diag(Tok, diag::warn_missing_whitespace_after_macro_name);
2142   }
2143 
2144   if (!Tok.is(tok::eod))
2145     LastTok = Tok;
2146 
2147   // Read the rest of the macro body.
2148   if (MI->isObjectLike()) {
2149     // Object-like macros are very simple, just read their body.
2150     while (Tok.isNot(tok::eod)) {
2151       LastTok = Tok;
2152       MI->AddTokenToBody(Tok);
2153       // Get the next token of the macro.
2154       LexUnexpandedToken(Tok);
2155     }
2156 
2157   } else {
2158     // Otherwise, read the body of a function-like macro.  While we are at it,
2159     // check C99 6.10.3.2p1: ensure that # operators are followed by macro
2160     // parameters in function-like macro expansions.
2161     while (Tok.isNot(tok::eod)) {
2162       LastTok = Tok;
2163 
2164       if (Tok.isNot(tok::hash) && Tok.isNot(tok::hashhash)) {
2165         MI->AddTokenToBody(Tok);
2166 
2167         // Get the next token of the macro.
2168         LexUnexpandedToken(Tok);
2169         continue;
2170       }
2171 
2172       // If we're in -traditional mode, then we should ignore stringification
2173       // and token pasting. Mark the tokens as unknown so as not to confuse
2174       // things.
2175       if (getLangOpts().TraditionalCPP) {
2176         Tok.setKind(tok::unknown);
2177         MI->AddTokenToBody(Tok);
2178 
2179         // Get the next token of the macro.
2180         LexUnexpandedToken(Tok);
2181         continue;
2182       }
2183 
2184       if (Tok.is(tok::hashhash)) {
2185 
2186         // If we see token pasting, check if it looks like the gcc comma
2187         // pasting extension.  We'll use this information to suppress
2188         // diagnostics later on.
2189 
2190         // Get the next token of the macro.
2191         LexUnexpandedToken(Tok);
2192 
2193         if (Tok.is(tok::eod)) {
2194           MI->AddTokenToBody(LastTok);
2195           break;
2196         }
2197 
2198         unsigned NumTokens = MI->getNumTokens();
2199         if (NumTokens && Tok.getIdentifierInfo() == Ident__VA_ARGS__ &&
2200             MI->getReplacementToken(NumTokens-1).is(tok::comma))
2201           MI->setHasCommaPasting();
2202 
2203         // Things look ok, add the '##' token to the macro.
2204         MI->AddTokenToBody(LastTok);
2205         continue;
2206       }
2207 
2208       // Get the next token of the macro.
2209       LexUnexpandedToken(Tok);
2210 
2211       // Check for a valid macro arg identifier.
2212       if (Tok.getIdentifierInfo() == nullptr ||
2213           MI->getArgumentNum(Tok.getIdentifierInfo()) == -1) {
2214 
2215         // If this is assembler-with-cpp mode, we accept random gibberish after
2216         // the '#' because '#' is often a comment character.  However, change
2217         // the kind of the token to tok::unknown so that the preprocessor isn't
2218         // confused.
2219         if (getLangOpts().AsmPreprocessor && Tok.isNot(tok::eod)) {
2220           LastTok.setKind(tok::unknown);
2221           MI->AddTokenToBody(LastTok);
2222           continue;
2223         } else {
2224           Diag(Tok, diag::err_pp_stringize_not_parameter);
2225 
2226           // Disable __VA_ARGS__ again.
2227           Ident__VA_ARGS__->setIsPoisoned(true);
2228           return;
2229         }
2230       }
2231 
2232       // Things look ok, add the '#' and param name tokens to the macro.
2233       MI->AddTokenToBody(LastTok);
2234       MI->AddTokenToBody(Tok);
2235       LastTok = Tok;
2236 
2237       // Get the next token of the macro.
2238       LexUnexpandedToken(Tok);
2239     }
2240   }
2241 
2242   if (MacroShadowsKeyword &&
2243       !isConfigurationPattern(MacroNameTok, MI, getLangOpts())) {
2244     Diag(MacroNameTok, diag::warn_pp_macro_hides_keyword);
2245   }
2246 
2247   // Disable __VA_ARGS__ again.
2248   Ident__VA_ARGS__->setIsPoisoned(true);
2249 
2250   // Check that there is no paste (##) operator at the beginning or end of the
2251   // replacement list.
2252   unsigned NumTokens = MI->getNumTokens();
2253   if (NumTokens != 0) {
2254     if (MI->getReplacementToken(0).is(tok::hashhash)) {
2255       Diag(MI->getReplacementToken(0), diag::err_paste_at_start);
2256       return;
2257     }
2258     if (MI->getReplacementToken(NumTokens-1).is(tok::hashhash)) {
2259       Diag(MI->getReplacementToken(NumTokens-1), diag::err_paste_at_end);
2260       return;
2261     }
2262   }
2263 
2264   MI->setDefinitionEndLoc(LastTok.getLocation());
2265 
2266   // Finally, if this identifier already had a macro defined for it, verify that
2267   // the macro bodies are identical, and issue diagnostics if they are not.
2268   if (const MacroInfo *OtherMI=getMacroInfo(MacroNameTok.getIdentifierInfo())) {
2269     // It is very common for system headers to have tons of macro redefinitions
2270     // and for warnings to be disabled in system headers.  If this is the case,
2271     // then don't bother calling MacroInfo::isIdenticalTo.
2272     if (!getDiagnostics().getSuppressSystemWarnings() ||
2273         !SourceMgr.isInSystemHeader(DefineTok.getLocation())) {
2274       if (!OtherMI->isUsed() && OtherMI->isWarnIfUnused())
2275         Diag(OtherMI->getDefinitionLoc(), diag::pp_macro_not_used);
2276 
2277       // Warn if defining "__LINE__" and other builtins, per C99 6.10.8/4 and
2278       // C++ [cpp.predefined]p4, but allow it as an extension.
2279       if (OtherMI->isBuiltinMacro())
2280         Diag(MacroNameTok, diag::ext_pp_redef_builtin_macro);
2281       // Macros must be identical.  This means all tokens and whitespace
2282       // separation must be the same.  C99 6.10.3p2.
2283       else if (!OtherMI->isAllowRedefinitionsWithoutWarning() &&
2284                !MI->isIdenticalTo(*OtherMI, *this, /*Syntactic=*/LangOpts.MicrosoftExt)) {
2285         Diag(MI->getDefinitionLoc(), diag::ext_pp_macro_redef)
2286           << MacroNameTok.getIdentifierInfo();
2287         Diag(OtherMI->getDefinitionLoc(), diag::note_previous_definition);
2288       }
2289     }
2290     if (OtherMI->isWarnIfUnused())
2291       WarnUnusedMacroLocs.erase(OtherMI->getDefinitionLoc());
2292   }
2293 
2294   DefMacroDirective *MD =
2295       appendDefMacroDirective(MacroNameTok.getIdentifierInfo(), MI);
2296 
2297   assert(!MI->isUsed());
2298   // If we need warning for not using the macro, add its location in the
2299   // warn-because-unused-macro set. If it gets used it will be removed from set.
2300   if (getSourceManager().isInMainFile(MI->getDefinitionLoc()) &&
2301       !Diags->isIgnored(diag::pp_macro_not_used, MI->getDefinitionLoc())) {
2302     MI->setIsWarnIfUnused(true);
2303     WarnUnusedMacroLocs.insert(MI->getDefinitionLoc());
2304   }
2305 
2306   // If the callbacks want to know, tell them about the macro definition.
2307   if (Callbacks)
2308     Callbacks->MacroDefined(MacroNameTok, MD);
2309 }
2310 
2311 /// HandleUndefDirective - Implements \#undef.
2312 ///
2313 void Preprocessor::HandleUndefDirective(Token &UndefTok) {
2314   ++NumUndefined;
2315 
2316   Token MacroNameTok;
2317   ReadMacroName(MacroNameTok, MU_Undef);
2318 
2319   // Error reading macro name?  If so, diagnostic already issued.
2320   if (MacroNameTok.is(tok::eod))
2321     return;
2322 
2323   // Check to see if this is the last token on the #undef line.
2324   CheckEndOfDirective("undef");
2325 
2326   // Okay, we have a valid identifier to undef.
2327   auto *II = MacroNameTok.getIdentifierInfo();
2328   auto MD = getMacroDefinition(II);
2329 
2330   // If the callbacks want to know, tell them about the macro #undef.
2331   // Note: no matter if the macro was defined or not.
2332   if (Callbacks)
2333     Callbacks->MacroUndefined(MacroNameTok, MD);
2334 
2335   // If the macro is not defined, this is a noop undef, just return.
2336   const MacroInfo *MI = MD.getMacroInfo();
2337   if (!MI)
2338     return;
2339 
2340   if (!MI->isUsed() && MI->isWarnIfUnused())
2341     Diag(MI->getDefinitionLoc(), diag::pp_macro_not_used);
2342 
2343   if (MI->isWarnIfUnused())
2344     WarnUnusedMacroLocs.erase(MI->getDefinitionLoc());
2345 
2346   appendMacroDirective(MacroNameTok.getIdentifierInfo(),
2347                        AllocateUndefMacroDirective(MacroNameTok.getLocation()));
2348 }
2349 
2350 
2351 //===----------------------------------------------------------------------===//
2352 // Preprocessor Conditional Directive Handling.
2353 //===----------------------------------------------------------------------===//
2354 
2355 /// HandleIfdefDirective - Implements the \#ifdef/\#ifndef directive.  isIfndef
2356 /// is true when this is a \#ifndef directive.  ReadAnyTokensBeforeDirective is
2357 /// true if any tokens have been returned or pp-directives activated before this
2358 /// \#ifndef has been lexed.
2359 ///
2360 void Preprocessor::HandleIfdefDirective(Token &Result, bool isIfndef,
2361                                         bool ReadAnyTokensBeforeDirective) {
2362   ++NumIf;
2363   Token DirectiveTok = Result;
2364 
2365   Token MacroNameTok;
2366   ReadMacroName(MacroNameTok);
2367 
2368   // Error reading macro name?  If so, diagnostic already issued.
2369   if (MacroNameTok.is(tok::eod)) {
2370     // Skip code until we get to #endif.  This helps with recovery by not
2371     // emitting an error when the #endif is reached.
2372     SkipExcludedConditionalBlock(DirectiveTok.getLocation(),
2373                                  /*Foundnonskip*/false, /*FoundElse*/false);
2374     return;
2375   }
2376 
2377   // Check to see if this is the last token on the #if[n]def line.
2378   CheckEndOfDirective(isIfndef ? "ifndef" : "ifdef");
2379 
2380   IdentifierInfo *MII = MacroNameTok.getIdentifierInfo();
2381   auto MD = getMacroDefinition(MII);
2382   MacroInfo *MI = MD.getMacroInfo();
2383 
2384   if (CurPPLexer->getConditionalStackDepth() == 0) {
2385     // If the start of a top-level #ifdef and if the macro is not defined,
2386     // inform MIOpt that this might be the start of a proper include guard.
2387     // Otherwise it is some other form of unknown conditional which we can't
2388     // handle.
2389     if (!ReadAnyTokensBeforeDirective && !MI) {
2390       assert(isIfndef && "#ifdef shouldn't reach here");
2391       CurPPLexer->MIOpt.EnterTopLevelIfndef(MII, MacroNameTok.getLocation());
2392     } else
2393       CurPPLexer->MIOpt.EnterTopLevelConditional();
2394   }
2395 
2396   // If there is a macro, process it.
2397   if (MI)  // Mark it used.
2398     markMacroAsUsed(MI);
2399 
2400   if (Callbacks) {
2401     if (isIfndef)
2402       Callbacks->Ifndef(DirectiveTok.getLocation(), MacroNameTok, MD);
2403     else
2404       Callbacks->Ifdef(DirectiveTok.getLocation(), MacroNameTok, MD);
2405   }
2406 
2407   // Should we include the stuff contained by this directive?
2408   if (!MI == isIfndef) {
2409     // Yes, remember that we are inside a conditional, then lex the next token.
2410     CurPPLexer->pushConditionalLevel(DirectiveTok.getLocation(),
2411                                      /*wasskip*/false, /*foundnonskip*/true,
2412                                      /*foundelse*/false);
2413   } else {
2414     // No, skip the contents of this block.
2415     SkipExcludedConditionalBlock(DirectiveTok.getLocation(),
2416                                  /*Foundnonskip*/false,
2417                                  /*FoundElse*/false);
2418   }
2419 }
2420 
2421 /// HandleIfDirective - Implements the \#if directive.
2422 ///
2423 void Preprocessor::HandleIfDirective(Token &IfToken,
2424                                      bool ReadAnyTokensBeforeDirective) {
2425   ++NumIf;
2426 
2427   // Parse and evaluate the conditional expression.
2428   IdentifierInfo *IfNDefMacro = nullptr;
2429   const SourceLocation ConditionalBegin = CurPPLexer->getSourceLocation();
2430   const bool ConditionalTrue = EvaluateDirectiveExpression(IfNDefMacro);
2431   const SourceLocation ConditionalEnd = CurPPLexer->getSourceLocation();
2432 
2433   // If this condition is equivalent to #ifndef X, and if this is the first
2434   // directive seen, handle it for the multiple-include optimization.
2435   if (CurPPLexer->getConditionalStackDepth() == 0) {
2436     if (!ReadAnyTokensBeforeDirective && IfNDefMacro && ConditionalTrue)
2437       // FIXME: Pass in the location of the macro name, not the 'if' token.
2438       CurPPLexer->MIOpt.EnterTopLevelIfndef(IfNDefMacro, IfToken.getLocation());
2439     else
2440       CurPPLexer->MIOpt.EnterTopLevelConditional();
2441   }
2442 
2443   if (Callbacks)
2444     Callbacks->If(IfToken.getLocation(),
2445                   SourceRange(ConditionalBegin, ConditionalEnd),
2446                   (ConditionalTrue ? PPCallbacks::CVK_True : PPCallbacks::CVK_False));
2447 
2448   // Should we include the stuff contained by this directive?
2449   if (ConditionalTrue) {
2450     // Yes, remember that we are inside a conditional, then lex the next token.
2451     CurPPLexer->pushConditionalLevel(IfToken.getLocation(), /*wasskip*/false,
2452                                    /*foundnonskip*/true, /*foundelse*/false);
2453   } else {
2454     // No, skip the contents of this block.
2455     SkipExcludedConditionalBlock(IfToken.getLocation(), /*Foundnonskip*/false,
2456                                  /*FoundElse*/false);
2457   }
2458 }
2459 
2460 /// HandleEndifDirective - Implements the \#endif directive.
2461 ///
2462 void Preprocessor::HandleEndifDirective(Token &EndifToken) {
2463   ++NumEndif;
2464 
2465   // Check that this is the whole directive.
2466   CheckEndOfDirective("endif");
2467 
2468   PPConditionalInfo CondInfo;
2469   if (CurPPLexer->popConditionalLevel(CondInfo)) {
2470     // No conditionals on the stack: this is an #endif without an #if.
2471     Diag(EndifToken, diag::err_pp_endif_without_if);
2472     return;
2473   }
2474 
2475   // If this the end of a top-level #endif, inform MIOpt.
2476   if (CurPPLexer->getConditionalStackDepth() == 0)
2477     CurPPLexer->MIOpt.ExitTopLevelConditional();
2478 
2479   assert(!CondInfo.WasSkipping && !CurPPLexer->LexingRawMode &&
2480          "This code should only be reachable in the non-skipping case!");
2481 
2482   if (Callbacks)
2483     Callbacks->Endif(EndifToken.getLocation(), CondInfo.IfLoc);
2484 }
2485 
2486 /// HandleElseDirective - Implements the \#else directive.
2487 ///
2488 void Preprocessor::HandleElseDirective(Token &Result) {
2489   ++NumElse;
2490 
2491   // #else directive in a non-skipping conditional... start skipping.
2492   CheckEndOfDirective("else");
2493 
2494   PPConditionalInfo CI;
2495   if (CurPPLexer->popConditionalLevel(CI)) {
2496     Diag(Result, diag::pp_err_else_without_if);
2497     return;
2498   }
2499 
2500   // If this is a top-level #else, inform the MIOpt.
2501   if (CurPPLexer->getConditionalStackDepth() == 0)
2502     CurPPLexer->MIOpt.EnterTopLevelConditional();
2503 
2504   // If this is a #else with a #else before it, report the error.
2505   if (CI.FoundElse) Diag(Result, diag::pp_err_else_after_else);
2506 
2507   if (Callbacks)
2508     Callbacks->Else(Result.getLocation(), CI.IfLoc);
2509 
2510   // Finally, skip the rest of the contents of this block.
2511   SkipExcludedConditionalBlock(CI.IfLoc, /*Foundnonskip*/true,
2512                                /*FoundElse*/true, Result.getLocation());
2513 }
2514 
2515 /// HandleElifDirective - Implements the \#elif directive.
2516 ///
2517 void Preprocessor::HandleElifDirective(Token &ElifToken) {
2518   ++NumElse;
2519 
2520   // #elif directive in a non-skipping conditional... start skipping.
2521   // We don't care what the condition is, because we will always skip it (since
2522   // the block immediately before it was included).
2523   const SourceLocation ConditionalBegin = CurPPLexer->getSourceLocation();
2524   DiscardUntilEndOfDirective();
2525   const SourceLocation ConditionalEnd = CurPPLexer->getSourceLocation();
2526 
2527   PPConditionalInfo CI;
2528   if (CurPPLexer->popConditionalLevel(CI)) {
2529     Diag(ElifToken, diag::pp_err_elif_without_if);
2530     return;
2531   }
2532 
2533   // If this is a top-level #elif, inform the MIOpt.
2534   if (CurPPLexer->getConditionalStackDepth() == 0)
2535     CurPPLexer->MIOpt.EnterTopLevelConditional();
2536 
2537   // If this is a #elif with a #else before it, report the error.
2538   if (CI.FoundElse) Diag(ElifToken, diag::pp_err_elif_after_else);
2539 
2540   if (Callbacks)
2541     Callbacks->Elif(ElifToken.getLocation(),
2542                     SourceRange(ConditionalBegin, ConditionalEnd),
2543                     PPCallbacks::CVK_NotEvaluated, CI.IfLoc);
2544 
2545   // Finally, skip the rest of the contents of this block.
2546   SkipExcludedConditionalBlock(CI.IfLoc, /*Foundnonskip*/true,
2547                                /*FoundElse*/CI.FoundElse,
2548                                ElifToken.getLocation());
2549 }
2550