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