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