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