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