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