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