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