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