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