PPDirectives.cpp revision ac6b06df32e907cac388a92310650b5895c11241
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/HeaderSearch.h"
16#include "clang/Lex/MacroInfo.h"
17#include "clang/Basic/Diagnostic.h"
18#include "clang/Basic/SourceManager.h"
19using namespace clang;
20
21//===----------------------------------------------------------------------===//
22// Utility Methods for Preprocessor Directive Handling.
23//===----------------------------------------------------------------------===//
24
25/// DiscardUntilEndOfDirective - Read and discard all tokens remaining on the
26/// current line until the tok::eom token is found.
27void Preprocessor::DiscardUntilEndOfDirective() {
28  Token Tmp;
29  do {
30    LexUnexpandedToken(Tmp);
31  } while (Tmp.isNot(tok::eom));
32}
33
34/// isCXXNamedOperator - Returns "true" if the token is a named operator in C++.
35static bool isCXXNamedOperator(const std::string &Spelling) {
36  return Spelling == "and" || Spelling == "bitand" || Spelling == "bitor" ||
37    Spelling == "compl" || Spelling == "not" || Spelling == "not_eq" ||
38    Spelling == "or" || Spelling == "xor";
39}
40
41/// ReadMacroName - Lex and validate a macro name, which occurs after a
42/// #define or #undef.  This sets the token kind to eom and discards the rest
43/// of the macro line if the macro name is invalid.  isDefineUndef is 1 if
44/// this is due to a a #define, 2 if #undef directive, 0 if it is something
45/// else (e.g. #ifdef).
46void Preprocessor::ReadMacroName(Token &MacroNameTok, char isDefineUndef) {
47  // Read the token, don't allow macro expansion on it.
48  LexUnexpandedToken(MacroNameTok);
49
50  // Missing macro name?
51  if (MacroNameTok.is(tok::eom))
52    return Diag(MacroNameTok, diag::err_pp_missing_macro_name);
53
54  IdentifierInfo *II = MacroNameTok.getIdentifierInfo();
55  if (II == 0) {
56    std::string Spelling = getSpelling(MacroNameTok);
57    if (isCXXNamedOperator(Spelling))
58      // C++ 2.5p2: Alternative tokens behave the same as its primary token
59      // except for their spellings.
60      Diag(MacroNameTok, diag::err_pp_operator_used_as_macro_name, Spelling);
61    else
62      Diag(MacroNameTok, diag::err_pp_macro_not_identifier);
63    // Fall through on error.
64  } else if (isDefineUndef && II->getPPKeywordID() == tok::pp_defined) {
65    // Error if defining "defined": C99 6.10.8.4.
66    Diag(MacroNameTok, diag::err_defined_macro_name);
67  } else if (isDefineUndef && II->hasMacroDefinition() &&
68             getMacroInfo(II)->isBuiltinMacro()) {
69    // Error if defining "__LINE__" and other builtins: C99 6.10.8.4.
70    if (isDefineUndef == 1)
71      Diag(MacroNameTok, diag::pp_redef_builtin_macro);
72    else
73      Diag(MacroNameTok, diag::pp_undef_builtin_macro);
74  } else {
75    // Okay, we got a good identifier node.  Return it.
76    return;
77  }
78
79  // Invalid macro name, read and discard the rest of the line.  Then set the
80  // token kind to tok::eom.
81  MacroNameTok.setKind(tok::eom);
82  return DiscardUntilEndOfDirective();
83}
84
85/// CheckEndOfDirective - Ensure that the next token is a tok::eom token.  If
86/// not, emit a diagnostic and consume up until the eom.
87void Preprocessor::CheckEndOfDirective(const char *DirType) {
88  Token Tmp;
89  // Lex unexpanded tokens: macros might expand to zero tokens, causing us to
90  // miss diagnosing invalid lines.
91  LexUnexpandedToken(Tmp);
92
93  // There should be no tokens after the directive, but we allow them as an
94  // extension.
95  while (Tmp.is(tok::comment))  // Skip comments in -C mode.
96    LexUnexpandedToken(Tmp);
97
98  if (Tmp.isNot(tok::eom)) {
99    Diag(Tmp, diag::ext_pp_extra_tokens_at_eol, DirType);
100    DiscardUntilEndOfDirective();
101  }
102}
103
104
105
106/// SkipExcludedConditionalBlock - We just read a #if or related directive and
107/// decided that the subsequent tokens are in the #if'd out portion of the
108/// file.  Lex the rest of the file, until we see an #endif.  If
109/// FoundNonSkipPortion is true, then we have already emitted code for part of
110/// this #if directive, so #else/#elif blocks should never be entered. If ElseOk
111/// is true, then #else directives are ok, if not, then we have already seen one
112/// so a #else directive is a duplicate.  When this returns, the caller can lex
113/// the first valid token.
114void Preprocessor::SkipExcludedConditionalBlock(SourceLocation IfTokenLoc,
115                                                bool FoundNonSkipPortion,
116                                                bool FoundElse) {
117  ++NumSkipped;
118  assert(CurTokenLexer == 0 && CurLexer &&
119         "Lexing a macro, not a file?");
120
121  CurPPLexer->pushConditionalLevel(IfTokenLoc, /*isSkipping*/false,
122                                 FoundNonSkipPortion, FoundElse);
123
124  // Enter raw mode to disable identifier lookup (and thus macro expansion),
125  // disabling warnings, etc.
126  CurPPLexer->LexingRawMode = true;
127  Token Tok;
128  while (1) {
129    CurLexer->Lex(Tok);
130
131    // If this is the end of the buffer, we have an error.
132    if (Tok.is(tok::eof)) {
133      // Emit errors for each unterminated conditional on the stack, including
134      // the current one.
135      while (!CurPPLexer->ConditionalStack.empty()) {
136        Diag(CurPPLexer->ConditionalStack.back().IfLoc,
137             diag::err_pp_unterminated_conditional);
138        CurPPLexer->ConditionalStack.pop_back();
139      }
140
141      // Just return and let the caller lex after this #include.
142      break;
143    }
144
145    // If this token is not a preprocessor directive, just skip it.
146    if (Tok.isNot(tok::hash) || !Tok.isAtStartOfLine())
147      continue;
148
149    // We just parsed a # character at the start of a line, so we're in
150    // directive mode.  Tell the lexer this so any newlines we see will be
151    // converted into an EOM token (this terminates the macro).
152    CurPPLexer->ParsingPreprocessorDirective = true;
153    if (CurLexer) CurLexer->SetCommentRetentionState(false);
154
155
156    // Read the next token, the directive flavor.
157    LexUnexpandedToken(Tok);
158
159    // If this isn't an identifier directive (e.g. is "# 1\n" or "#\n", or
160    // something bogus), skip it.
161    if (Tok.isNot(tok::identifier)) {
162      CurPPLexer->ParsingPreprocessorDirective = false;
163      // Restore comment saving mode.
164      if (CurLexer) CurLexer->SetCommentRetentionState(KeepComments);
165      continue;
166    }
167
168    // If the first letter isn't i or e, it isn't intesting to us.  We know that
169    // this is safe in the face of spelling differences, because there is no way
170    // to spell an i/e in a strange way that is another letter.  Skipping this
171    // allows us to avoid looking up the identifier info for #define/#undef and
172    // other common directives.
173    const char *RawCharData = SourceMgr.getCharacterData(Tok.getLocation());
174    char FirstChar = RawCharData[0];
175    if (FirstChar >= 'a' && FirstChar <= 'z' &&
176        FirstChar != 'i' && FirstChar != 'e') {
177      CurPPLexer->ParsingPreprocessorDirective = false;
178      // Restore comment saving mode.
179      if (CurLexer) CurLexer->SetCommentRetentionState(KeepComments);
180      continue;
181    }
182
183    // Get the identifier name without trigraphs or embedded newlines.  Note
184    // that we can't use Tok.getIdentifierInfo() because its lookup is disabled
185    // when skipping.
186    // TODO: could do this with zero copies in the no-clean case by using
187    // strncmp below.
188    char Directive[20];
189    unsigned IdLen;
190    if (!Tok.needsCleaning() && Tok.getLength() < 20) {
191      IdLen = Tok.getLength();
192      memcpy(Directive, RawCharData, IdLen);
193      Directive[IdLen] = 0;
194    } else {
195      std::string DirectiveStr = getSpelling(Tok);
196      IdLen = DirectiveStr.size();
197      if (IdLen >= 20) {
198        CurPPLexer->ParsingPreprocessorDirective = false;
199        // Restore comment saving mode.
200        if (CurLexer) CurLexer->SetCommentRetentionState(KeepComments);
201        continue;
202      }
203      memcpy(Directive, &DirectiveStr[0], IdLen);
204      Directive[IdLen] = 0;
205    }
206
207    if (FirstChar == 'i' && Directive[1] == 'f') {
208      if ((IdLen == 2) ||   // "if"
209          (IdLen == 5 && !strcmp(Directive+2, "def")) ||   // "ifdef"
210          (IdLen == 6 && !strcmp(Directive+2, "ndef"))) {  // "ifndef"
211        // We know the entire #if/#ifdef/#ifndef block will be skipped, don't
212        // bother parsing the condition.
213        DiscardUntilEndOfDirective();
214        CurPPLexer->pushConditionalLevel(Tok.getLocation(), /*wasskipping*/true,
215                                       /*foundnonskip*/false,
216                                       /*fnddelse*/false);
217      }
218    } else if (FirstChar == 'e') {
219      if (IdLen == 5 && !strcmp(Directive+1, "ndif")) {  // "endif"
220        CheckEndOfDirective("#endif");
221        PPConditionalInfo CondInfo;
222        CondInfo.WasSkipping = true; // Silence bogus warning.
223        bool InCond = CurPPLexer->popConditionalLevel(CondInfo);
224        InCond = InCond;  // Silence warning in no-asserts mode.
225        assert(!InCond && "Can't be skipping if not in a conditional!");
226
227        // If we popped the outermost skipping block, we're done skipping!
228        if (!CondInfo.WasSkipping)
229          break;
230      } else if (IdLen == 4 && !strcmp(Directive+1, "lse")) { // "else".
231        // #else directive in a skipping conditional.  If not in some other
232        // skipping conditional, and if #else hasn't already been seen, enter it
233        // as a non-skipping conditional.
234        CheckEndOfDirective("#else");
235        PPConditionalInfo &CondInfo = CurPPLexer->peekConditionalLevel();
236
237        // If this is a #else with a #else before it, report the error.
238        if (CondInfo.FoundElse) Diag(Tok, diag::pp_err_else_after_else);
239
240        // Note that we've seen a #else in this conditional.
241        CondInfo.FoundElse = true;
242
243        // If the conditional is at the top level, and the #if block wasn't
244        // entered, enter the #else block now.
245        if (!CondInfo.WasSkipping && !CondInfo.FoundNonSkip) {
246          CondInfo.FoundNonSkip = true;
247          break;
248        }
249      } else if (IdLen == 4 && !strcmp(Directive+1, "lif")) {  // "elif".
250        PPConditionalInfo &CondInfo = CurPPLexer->peekConditionalLevel();
251
252        bool ShouldEnter;
253        // If this is in a skipping block or if we're already handled this #if
254        // block, don't bother parsing the condition.
255        if (CondInfo.WasSkipping || CondInfo.FoundNonSkip) {
256          DiscardUntilEndOfDirective();
257          ShouldEnter = false;
258        } else {
259          // Restore the value of LexingRawMode so that identifiers are
260          // looked up, etc, inside the #elif expression.
261          assert(CurPPLexer->LexingRawMode && "We have to be skipping here!");
262          CurPPLexer->LexingRawMode = false;
263          IdentifierInfo *IfNDefMacro = 0;
264          ShouldEnter = EvaluateDirectiveExpression(IfNDefMacro);
265          CurPPLexer->LexingRawMode = true;
266        }
267
268        // If this is a #elif with a #else before it, report the error.
269        if (CondInfo.FoundElse) Diag(Tok, diag::pp_err_elif_after_else);
270
271        // If this condition is true, enter it!
272        if (ShouldEnter) {
273          CondInfo.FoundNonSkip = true;
274          break;
275        }
276      }
277    }
278
279    CurPPLexer->ParsingPreprocessorDirective = false;
280    // Restore comment saving mode.
281    if (CurLexer) CurLexer->SetCommentRetentionState(KeepComments);
282  }
283
284  // Finally, if we are out of the conditional (saw an #endif or ran off the end
285  // of the file, just stop skipping and return to lexing whatever came after
286  // the #if block.
287  CurPPLexer->LexingRawMode = false;
288}
289
290/// LookupFile - Given a "foo" or <foo> reference, look up the indicated file,
291/// return null on failure.  isAngled indicates whether the file reference is
292/// for system #include's or not (i.e. using <> instead of "").
293const FileEntry *Preprocessor::LookupFile(const char *FilenameStart,
294                                          const char *FilenameEnd,
295                                          bool isAngled,
296                                          const DirectoryLookup *FromDir,
297                                          const DirectoryLookup *&CurDir) {
298  // If the header lookup mechanism may be relative to the current file, pass in
299  // info about where the current file is.
300  const FileEntry *CurFileEnt = 0;
301  if (!FromDir) {
302    SourceLocation FileLoc = getCurrentFileLexer()->getFileLoc();
303    CurFileEnt = SourceMgr.getFileEntryForLoc(FileLoc);
304  }
305
306  // Do a standard file entry lookup.
307  CurDir = CurDirLookup;
308  const FileEntry *FE =
309  HeaderInfo.LookupFile(FilenameStart, FilenameEnd,
310                        isAngled, FromDir, CurDir, CurFileEnt);
311  if (FE) return FE;
312
313  // Otherwise, see if this is a subframework header.  If so, this is relative
314  // to one of the headers on the #include stack.  Walk the list of the current
315  // headers on the #include stack and pass them to HeaderInfo.
316  if (CurLexer && !CurLexer->Is_PragmaLexer) {
317    if ((CurFileEnt = SourceMgr.getFileEntryForLoc(CurLexer->getFileLoc())))
318      if ((FE = HeaderInfo.LookupSubframeworkHeader(FilenameStart, FilenameEnd,
319                                                    CurFileEnt)))
320        return FE;
321  }
322
323  for (unsigned i = 0, e = IncludeMacroStack.size(); i != e; ++i) {
324    IncludeStackInfo &ISEntry = IncludeMacroStack[e-i-1];
325    if (ISEntry.TheLexer && !ISEntry.TheLexer->Is_PragmaLexer) {
326      if ((CurFileEnt =
327           SourceMgr.getFileEntryForLoc(ISEntry.TheLexer->getFileLoc())))
328        if ((FE = HeaderInfo.LookupSubframeworkHeader(FilenameStart,
329                                                      FilenameEnd, CurFileEnt)))
330          return FE;
331    }
332  }
333
334  // Otherwise, we really couldn't find the file.
335  return 0;
336}
337
338
339//===----------------------------------------------------------------------===//
340// Preprocessor Directive Handling.
341//===----------------------------------------------------------------------===//
342
343/// HandleDirective - This callback is invoked when the lexer sees a # token
344/// at the start of a line.  This consumes the directive, modifies the
345/// lexer/preprocessor state, and advances the lexer(s) so that the next token
346/// read is the correct one.
347void Preprocessor::HandleDirective(Token &Result) {
348  // FIXME: Traditional: # with whitespace before it not recognized by K&R?
349
350  // We just parsed a # character at the start of a line, so we're in directive
351  // mode.  Tell the lexer this so any newlines we see will be converted into an
352  // EOM token (which terminates the directive).
353  CurPPLexer->ParsingPreprocessorDirective = true;
354
355  ++NumDirectives;
356
357  // We are about to read a token.  For the multiple-include optimization FA to
358  // work, we have to remember if we had read any tokens *before* this
359  // pp-directive.
360  bool ReadAnyTokensBeforeDirective = CurPPLexer->MIOpt.getHasReadAnyTokensVal();
361
362  // Read the next token, the directive flavor.  This isn't expanded due to
363  // C99 6.10.3p8.
364  LexUnexpandedToken(Result);
365
366  // C99 6.10.3p11: Is this preprocessor directive in macro invocation?  e.g.:
367  //   #define A(x) #x
368  //   A(abc
369  //     #warning blah
370  //   def)
371  // If so, the user is relying on non-portable behavior, emit a diagnostic.
372  if (InMacroArgs)
373    Diag(Result, diag::ext_embedded_directive);
374
375TryAgain:
376  switch (Result.getKind()) {
377  case tok::eom:
378    return;   // null directive.
379  case tok::comment:
380    // Handle stuff like "# /*foo*/ define X" in -E -C mode.
381    LexUnexpandedToken(Result);
382    goto TryAgain;
383
384  case tok::numeric_constant:
385    // FIXME: implement # 7 line numbers!
386    DiscardUntilEndOfDirective();
387    return;
388  default:
389    IdentifierInfo *II = Result.getIdentifierInfo();
390    if (II == 0) break;  // Not an identifier.
391
392    // Ask what the preprocessor keyword ID is.
393    switch (II->getPPKeywordID()) {
394    default: break;
395    // C99 6.10.1 - Conditional Inclusion.
396    case tok::pp_if:
397      return HandleIfDirective(Result, ReadAnyTokensBeforeDirective);
398    case tok::pp_ifdef:
399      return HandleIfdefDirective(Result, false, true/*not valid for miopt*/);
400    case tok::pp_ifndef:
401      return HandleIfdefDirective(Result, true, ReadAnyTokensBeforeDirective);
402    case tok::pp_elif:
403      return HandleElifDirective(Result);
404    case tok::pp_else:
405      return HandleElseDirective(Result);
406    case tok::pp_endif:
407      return HandleEndifDirective(Result);
408
409    // C99 6.10.2 - Source File Inclusion.
410    case tok::pp_include:
411      return HandleIncludeDirective(Result);            // Handle #include.
412
413    // C99 6.10.3 - Macro Replacement.
414    case tok::pp_define:
415      return HandleDefineDirective(Result);
416    case tok::pp_undef:
417      return HandleUndefDirective(Result);
418
419    // C99 6.10.4 - Line Control.
420    case tok::pp_line:
421      // FIXME: implement #line
422      DiscardUntilEndOfDirective();
423      return;
424
425    // C99 6.10.5 - Error Directive.
426    case tok::pp_error:
427      return HandleUserDiagnosticDirective(Result, false);
428
429    // C99 6.10.6 - Pragma Directive.
430    case tok::pp_pragma:
431      return HandlePragmaDirective();
432
433    // GNU Extensions.
434    case tok::pp_import:
435      return HandleImportDirective(Result);
436    case tok::pp_include_next:
437      return HandleIncludeNextDirective(Result);
438
439    case tok::pp_warning:
440      Diag(Result, diag::ext_pp_warning_directive);
441      return HandleUserDiagnosticDirective(Result, true);
442    case tok::pp_ident:
443      return HandleIdentSCCSDirective(Result);
444    case tok::pp_sccs:
445      return HandleIdentSCCSDirective(Result);
446    case tok::pp_assert:
447      //isExtension = true;  // FIXME: implement #assert
448      break;
449    case tok::pp_unassert:
450      //isExtension = true;  // FIXME: implement #unassert
451      break;
452    }
453    break;
454  }
455
456  // If we reached here, the preprocessing token is not valid!
457  Diag(Result, diag::err_pp_invalid_directive);
458
459  // Read the rest of the PP line.
460  DiscardUntilEndOfDirective();
461
462  // Okay, we're done parsing the directive.
463}
464
465void Preprocessor::HandleUserDiagnosticDirective(Token &Tok,
466                                                 bool isWarning) {
467  // Read the rest of the line raw.  We do this because we don't want macros
468  // to be expanded and we don't require that the tokens be valid preprocessing
469  // tokens.  For example, this is allowed: "#warning `   'foo".  GCC does
470  // collapse multiple consequtive white space between tokens, but this isn't
471  // specified by the standard.
472  std::string Message = CurLexer->ReadToEndOfLine();
473
474  unsigned DiagID = isWarning ? diag::pp_hash_warning : diag::err_pp_hash_error;
475  return Diag(Tok, DiagID, Message);
476}
477
478/// HandleIdentSCCSDirective - Handle a #ident/#sccs directive.
479///
480void Preprocessor::HandleIdentSCCSDirective(Token &Tok) {
481  // Yes, this directive is an extension.
482  Diag(Tok, diag::ext_pp_ident_directive);
483
484  // Read the string argument.
485  Token StrTok;
486  Lex(StrTok);
487
488  // If the token kind isn't a string, it's a malformed directive.
489  if (StrTok.isNot(tok::string_literal) &&
490      StrTok.isNot(tok::wide_string_literal))
491    return Diag(StrTok, diag::err_pp_malformed_ident);
492
493  // Verify that there is nothing after the string, other than EOM.
494  CheckEndOfDirective("#ident");
495
496  if (Callbacks)
497    Callbacks->Ident(Tok.getLocation(), getSpelling(StrTok));
498}
499
500//===----------------------------------------------------------------------===//
501// Preprocessor Include Directive Handling.
502//===----------------------------------------------------------------------===//
503
504/// GetIncludeFilenameSpelling - Turn the specified lexer token into a fully
505/// checked and spelled filename, e.g. as an operand of #include. This returns
506/// true if the input filename was in <>'s or false if it were in ""'s.  The
507/// caller is expected to provide a buffer that is large enough to hold the
508/// spelling of the filename, but is also expected to handle the case when
509/// this method decides to use a different buffer.
510bool Preprocessor::GetIncludeFilenameSpelling(SourceLocation Loc,
511                                              const char *&BufStart,
512                                              const char *&BufEnd) {
513  // Get the text form of the filename.
514  assert(BufStart != BufEnd && "Can't have tokens with empty spellings!");
515
516  // Make sure the filename is <x> or "x".
517  bool isAngled;
518  if (BufStart[0] == '<') {
519    if (BufEnd[-1] != '>') {
520      Diag(Loc, diag::err_pp_expects_filename);
521      BufStart = 0;
522      return true;
523    }
524    isAngled = true;
525  } else if (BufStart[0] == '"') {
526    if (BufEnd[-1] != '"') {
527      Diag(Loc, diag::err_pp_expects_filename);
528      BufStart = 0;
529      return true;
530    }
531    isAngled = false;
532  } else {
533    Diag(Loc, diag::err_pp_expects_filename);
534    BufStart = 0;
535    return true;
536  }
537
538  // Diagnose #include "" as invalid.
539  if (BufEnd-BufStart <= 2) {
540    Diag(Loc, diag::err_pp_empty_filename);
541    BufStart = 0;
542    return "";
543  }
544
545  // Skip the brackets.
546  ++BufStart;
547  --BufEnd;
548  return isAngled;
549}
550
551/// ConcatenateIncludeName - Handle cases where the #include name is expanded
552/// from a macro as multiple tokens, which need to be glued together.  This
553/// occurs for code like:
554///    #define FOO <a/b.h>
555///    #include FOO
556/// because in this case, "<a/b.h>" is returned as 7 tokens, not one.
557///
558/// This code concatenates and consumes tokens up to the '>' token.  It returns
559/// false if the > was found, otherwise it returns true if it finds and consumes
560/// the EOM marker.
561static bool ConcatenateIncludeName(llvm::SmallVector<char, 128> &FilenameBuffer,
562                                   Preprocessor &PP) {
563  Token CurTok;
564
565  PP.Lex(CurTok);
566  while (CurTok.isNot(tok::eom)) {
567    // Append the spelling of this token to the buffer. If there was a space
568    // before it, add it now.
569    if (CurTok.hasLeadingSpace())
570      FilenameBuffer.push_back(' ');
571
572    // Get the spelling of the token, directly into FilenameBuffer if possible.
573    unsigned PreAppendSize = FilenameBuffer.size();
574    FilenameBuffer.resize(PreAppendSize+CurTok.getLength());
575
576    const char *BufPtr = &FilenameBuffer[PreAppendSize];
577    unsigned ActualLen = PP.getSpelling(CurTok, BufPtr);
578
579    // If the token was spelled somewhere else, copy it into FilenameBuffer.
580    if (BufPtr != &FilenameBuffer[PreAppendSize])
581      memcpy(&FilenameBuffer[PreAppendSize], BufPtr, ActualLen);
582
583    // Resize FilenameBuffer to the correct size.
584    if (CurTok.getLength() != ActualLen)
585      FilenameBuffer.resize(PreAppendSize+ActualLen);
586
587    // If we found the '>' marker, return success.
588    if (CurTok.is(tok::greater))
589      return false;
590
591    PP.Lex(CurTok);
592  }
593
594  // If we hit the eom marker, emit an error and return true so that the caller
595  // knows the EOM has been read.
596  PP.Diag(CurTok.getLocation(), diag::err_pp_expects_filename);
597  return true;
598}
599
600/// HandleIncludeDirective - The "#include" tokens have just been read, read the
601/// file to be included from the lexer, then include it!  This is a common
602/// routine with functionality shared between #include, #include_next and
603/// #import.  LookupFrom is set when this is a #include_next directive, it
604/// specifies the file to start searching from.
605void Preprocessor::HandleIncludeDirective(Token &IncludeTok,
606                                          const DirectoryLookup *LookupFrom,
607                                          bool isImport) {
608
609  Token FilenameTok;
610  CurPPLexer->LexIncludeFilename(FilenameTok);
611
612  // Reserve a buffer to get the spelling.
613  llvm::SmallVector<char, 128> FilenameBuffer;
614  const char *FilenameStart, *FilenameEnd;
615
616  switch (FilenameTok.getKind()) {
617  case tok::eom:
618    // If the token kind is EOM, the error has already been diagnosed.
619    return;
620
621  case tok::angle_string_literal:
622  case tok::string_literal: {
623    FilenameBuffer.resize(FilenameTok.getLength());
624    FilenameStart = &FilenameBuffer[0];
625    unsigned Len = getSpelling(FilenameTok, FilenameStart);
626    FilenameEnd = FilenameStart+Len;
627    break;
628  }
629
630  case tok::less:
631    // This could be a <foo/bar.h> file coming from a macro expansion.  In this
632    // case, glue the tokens together into FilenameBuffer and interpret those.
633    FilenameBuffer.push_back('<');
634    if (ConcatenateIncludeName(FilenameBuffer, *this))
635      return;   // Found <eom> but no ">"?  Diagnostic already emitted.
636    FilenameStart = &FilenameBuffer[0];
637    FilenameEnd = &FilenameBuffer[FilenameBuffer.size()];
638    break;
639  default:
640    Diag(FilenameTok.getLocation(), diag::err_pp_expects_filename);
641    DiscardUntilEndOfDirective();
642    return;
643  }
644
645  bool isAngled = GetIncludeFilenameSpelling(FilenameTok.getLocation(),
646                                             FilenameStart, FilenameEnd);
647  // If GetIncludeFilenameSpelling set the start ptr to null, there was an
648  // error.
649  if (FilenameStart == 0) {
650    DiscardUntilEndOfDirective();
651    return;
652  }
653
654  // Verify that there is nothing after the filename, other than EOM.  Use the
655  // preprocessor to lex this in case lexing the filename entered a macro.
656  CheckEndOfDirective("#include");
657
658  // Check that we don't have infinite #include recursion.
659  if (IncludeMacroStack.size() == MaxAllowedIncludeStackDepth-1)
660    return Diag(FilenameTok, diag::err_pp_include_too_deep);
661
662  // Search include directories.
663  const DirectoryLookup *CurDir;
664  const FileEntry *File = LookupFile(FilenameStart, FilenameEnd,
665                                     isAngled, LookupFrom, CurDir);
666  if (File == 0)
667    return Diag(FilenameTok, diag::err_pp_file_not_found,
668                std::string(FilenameStart, FilenameEnd));
669
670  // Ask HeaderInfo if we should enter this #include file.  If not, #including
671  // this file will have no effect.
672  if (!HeaderInfo.ShouldEnterIncludeFile(File, isImport))
673    return;
674
675  // The #included file will be considered to be a system header if either it is
676  // in a system include directory, or if the #includer is a system include
677  // header.
678  SrcMgr::CharacteristicKind FileCharacter =
679    std::max(HeaderInfo.getFileDirFlavor(File),
680          SourceMgr.getFileCharacteristic(getCurrentFileLexer()->getFileLoc()));
681
682  // Look up the file, create a File ID for it.
683  unsigned FileID = SourceMgr.createFileID(File, FilenameTok.getLocation(),
684                                           FileCharacter);
685  if (FileID == 0)
686    return Diag(FilenameTok, diag::err_pp_file_not_found,
687                std::string(FilenameStart, FilenameEnd));
688
689  // Finally, if all is good, enter the new file!
690  EnterSourceFile(FileID, CurDir);
691}
692
693/// HandleIncludeNextDirective - Implements #include_next.
694///
695void Preprocessor::HandleIncludeNextDirective(Token &IncludeNextTok) {
696  Diag(IncludeNextTok, diag::ext_pp_include_next_directive);
697
698  // #include_next is like #include, except that we start searching after
699  // the current found directory.  If we can't do this, issue a
700  // diagnostic.
701  const DirectoryLookup *Lookup = CurDirLookup;
702  if (isInPrimaryFile()) {
703    Lookup = 0;
704    Diag(IncludeNextTok, diag::pp_include_next_in_primary);
705  } else if (Lookup == 0) {
706    Diag(IncludeNextTok, diag::pp_include_next_absolute_path);
707  } else {
708    // Start looking up in the next directory.
709    ++Lookup;
710  }
711
712  return HandleIncludeDirective(IncludeNextTok, Lookup);
713}
714
715/// HandleImportDirective - Implements #import.
716///
717void Preprocessor::HandleImportDirective(Token &ImportTok) {
718  Diag(ImportTok, diag::ext_pp_import_directive);
719
720  return HandleIncludeDirective(ImportTok, 0, true);
721}
722
723//===----------------------------------------------------------------------===//
724// Preprocessor Macro Directive Handling.
725//===----------------------------------------------------------------------===//
726
727/// ReadMacroDefinitionArgList - The ( starting an argument list of a macro
728/// definition has just been read.  Lex the rest of the arguments and the
729/// closing ), updating MI with what we learn.  Return true if an error occurs
730/// parsing the arg list.
731bool Preprocessor::ReadMacroDefinitionArgList(MacroInfo *MI) {
732  llvm::SmallVector<IdentifierInfo*, 32> Arguments;
733
734  Token Tok;
735  while (1) {
736    LexUnexpandedToken(Tok);
737    switch (Tok.getKind()) {
738    case tok::r_paren:
739      // Found the end of the argument list.
740      if (Arguments.empty()) {  // #define FOO()
741        MI->setArgumentList(Arguments.begin(), Arguments.end());
742        return false;
743      }
744      // Otherwise we have #define FOO(A,)
745      Diag(Tok, diag::err_pp_expected_ident_in_arg_list);
746      return true;
747    case tok::ellipsis:  // #define X(... -> C99 varargs
748      // Warn if use of C99 feature in non-C99 mode.
749      if (!Features.C99) Diag(Tok, diag::ext_variadic_macro);
750
751      // Lex the token after the identifier.
752      LexUnexpandedToken(Tok);
753      if (Tok.isNot(tok::r_paren)) {
754        Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
755        return true;
756      }
757      // Add the __VA_ARGS__ identifier as an argument.
758      Arguments.push_back(Ident__VA_ARGS__);
759      MI->setIsC99Varargs();
760      MI->setArgumentList(Arguments.begin(), Arguments.end());
761      return false;
762    case tok::eom:  // #define X(
763      Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
764      return true;
765    default:
766      // Handle keywords and identifiers here to accept things like
767      // #define Foo(for) for.
768      IdentifierInfo *II = Tok.getIdentifierInfo();
769      if (II == 0) {
770        // #define X(1
771        Diag(Tok, diag::err_pp_invalid_tok_in_arg_list);
772        return true;
773      }
774
775      // If this is already used as an argument, it is used multiple times (e.g.
776      // #define X(A,A.
777      if (std::find(Arguments.begin(), Arguments.end(), II) !=
778          Arguments.end()) {  // C99 6.10.3p6
779        Diag(Tok, diag::err_pp_duplicate_name_in_arg_list, II->getName());
780        return true;
781      }
782
783      // Add the argument to the macro info.
784      Arguments.push_back(II);
785
786      // Lex the token after the identifier.
787      LexUnexpandedToken(Tok);
788
789      switch (Tok.getKind()) {
790      default:          // #define X(A B
791        Diag(Tok, diag::err_pp_expected_comma_in_arg_list);
792        return true;
793      case tok::r_paren: // #define X(A)
794        MI->setArgumentList(Arguments.begin(), Arguments.end());
795        return false;
796      case tok::comma:  // #define X(A,
797        break;
798      case tok::ellipsis:  // #define X(A... -> GCC extension
799        // Diagnose extension.
800        Diag(Tok, diag::ext_named_variadic_macro);
801
802        // Lex the token after the identifier.
803        LexUnexpandedToken(Tok);
804        if (Tok.isNot(tok::r_paren)) {
805          Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
806          return true;
807        }
808
809        MI->setIsGNUVarargs();
810        MI->setArgumentList(Arguments.begin(), Arguments.end());
811        return false;
812      }
813    }
814  }
815}
816
817/// HandleDefineDirective - Implements #define.  This consumes the entire macro
818/// line then lets the caller lex the next real token.
819void Preprocessor::HandleDefineDirective(Token &DefineTok) {
820  ++NumDefined;
821
822  Token MacroNameTok;
823  ReadMacroName(MacroNameTok, 1);
824
825  // Error reading macro name?  If so, diagnostic already issued.
826  if (MacroNameTok.is(tok::eom))
827    return;
828
829  // If we are supposed to keep comments in #defines, reenable comment saving
830  // mode.
831  if (CurLexer) CurLexer->SetCommentRetentionState(KeepMacroComments);
832
833  // Create the new macro.
834  MacroInfo *MI = new MacroInfo(MacroNameTok.getLocation());
835
836  Token Tok;
837  LexUnexpandedToken(Tok);
838
839  // If this is a function-like macro definition, parse the argument list,
840  // marking each of the identifiers as being used as macro arguments.  Also,
841  // check other constraints on the first token of the macro body.
842  if (Tok.is(tok::eom)) {
843    // If there is no body to this macro, we have no special handling here.
844  } else if (Tok.is(tok::l_paren) && !Tok.hasLeadingSpace()) {
845    // This is a function-like macro definition.  Read the argument list.
846    MI->setIsFunctionLike();
847    if (ReadMacroDefinitionArgList(MI)) {
848      // Forget about MI.
849      delete MI;
850      // Throw away the rest of the line.
851      if (CurPPLexer->ParsingPreprocessorDirective)
852        DiscardUntilEndOfDirective();
853      return;
854    }
855
856    // Read the first token after the arg list for down below.
857    LexUnexpandedToken(Tok);
858  } else if (!Tok.hasLeadingSpace()) {
859    // C99 requires whitespace between the macro definition and the body.  Emit
860    // a diagnostic for something like "#define X+".
861    if (Features.C99) {
862      Diag(Tok, diag::ext_c99_whitespace_required_after_macro_name);
863    } else {
864      // FIXME: C90/C++ do not get this diagnostic, but it does get a similar
865      // one in some cases!
866    }
867  } else {
868    // This is a normal token with leading space.  Clear the leading space
869    // marker on the first token to get proper expansion.
870    Tok.clearFlag(Token::LeadingSpace);
871  }
872
873  // If this is a definition of a variadic C99 function-like macro, not using
874  // the GNU named varargs extension, enabled __VA_ARGS__.
875
876  // "Poison" __VA_ARGS__, which can only appear in the expansion of a macro.
877  // This gets unpoisoned where it is allowed.
878  assert(Ident__VA_ARGS__->isPoisoned() && "__VA_ARGS__ should be poisoned!");
879  if (MI->isC99Varargs())
880    Ident__VA_ARGS__->setIsPoisoned(false);
881
882  // Read the rest of the macro body.
883  if (MI->isObjectLike()) {
884    // Object-like macros are very simple, just read their body.
885    while (Tok.isNot(tok::eom)) {
886      MI->AddTokenToBody(Tok);
887      // Get the next token of the macro.
888      LexUnexpandedToken(Tok);
889    }
890
891  } else {
892    // Otherwise, read the body of a function-like macro.  This has to validate
893    // the # (stringize) operator.
894    while (Tok.isNot(tok::eom)) {
895      MI->AddTokenToBody(Tok);
896
897      // Check C99 6.10.3.2p1: ensure that # operators are followed by macro
898      // parameters in function-like macro expansions.
899      if (Tok.isNot(tok::hash)) {
900        // Get the next token of the macro.
901        LexUnexpandedToken(Tok);
902        continue;
903      }
904
905      // Get the next token of the macro.
906      LexUnexpandedToken(Tok);
907
908      // Not a macro arg identifier?
909      if (!Tok.getIdentifierInfo() ||
910          MI->getArgumentNum(Tok.getIdentifierInfo()) == -1) {
911        Diag(Tok, diag::err_pp_stringize_not_parameter);
912        delete MI;
913
914        // Disable __VA_ARGS__ again.
915        Ident__VA_ARGS__->setIsPoisoned(true);
916        return;
917      }
918
919      // Things look ok, add the param name token to the macro.
920      MI->AddTokenToBody(Tok);
921
922      // Get the next token of the macro.
923      LexUnexpandedToken(Tok);
924    }
925  }
926
927
928  // Disable __VA_ARGS__ again.
929  Ident__VA_ARGS__->setIsPoisoned(true);
930
931  // Check that there is no paste (##) operator at the begining or end of the
932  // replacement list.
933  unsigned NumTokens = MI->getNumTokens();
934  if (NumTokens != 0) {
935    if (MI->getReplacementToken(0).is(tok::hashhash)) {
936      Diag(MI->getReplacementToken(0), diag::err_paste_at_start);
937      delete MI;
938      return;
939    }
940    if (MI->getReplacementToken(NumTokens-1).is(tok::hashhash)) {
941      Diag(MI->getReplacementToken(NumTokens-1), diag::err_paste_at_end);
942      delete MI;
943      return;
944    }
945  }
946
947  // If this is the primary source file, remember that this macro hasn't been
948  // used yet.
949  if (isInPrimaryFile())
950    MI->setIsUsed(false);
951
952  // Finally, if this identifier already had a macro defined for it, verify that
953  // the macro bodies are identical and free the old definition.
954  if (MacroInfo *OtherMI = getMacroInfo(MacroNameTok.getIdentifierInfo())) {
955    if (!OtherMI->isUsed())
956      Diag(OtherMI->getDefinitionLoc(), diag::pp_macro_not_used);
957
958    // Macros must be identical.  This means all tokes and whitespace separation
959    // must be the same.  C99 6.10.3.2.
960    if (!MI->isIdenticalTo(*OtherMI, *this)) {
961      Diag(MI->getDefinitionLoc(), diag::ext_pp_macro_redef,
962           MacroNameTok.getIdentifierInfo()->getName());
963      Diag(OtherMI->getDefinitionLoc(), diag::ext_pp_macro_redef2);
964    }
965    delete OtherMI;
966  }
967
968  setMacroInfo(MacroNameTok.getIdentifierInfo(), MI);
969}
970
971/// HandleUndefDirective - Implements #undef.
972///
973void Preprocessor::HandleUndefDirective(Token &UndefTok) {
974  ++NumUndefined;
975
976  Token MacroNameTok;
977  ReadMacroName(MacroNameTok, 2);
978
979  // Error reading macro name?  If so, diagnostic already issued.
980  if (MacroNameTok.is(tok::eom))
981    return;
982
983  // Check to see if this is the last token on the #undef line.
984  CheckEndOfDirective("#undef");
985
986  // Okay, we finally have a valid identifier to undef.
987  MacroInfo *MI = getMacroInfo(MacroNameTok.getIdentifierInfo());
988
989  // If the macro is not defined, this is a noop undef, just return.
990  if (MI == 0) return;
991
992  if (!MI->isUsed())
993    Diag(MI->getDefinitionLoc(), diag::pp_macro_not_used);
994
995  // Free macro definition.
996  delete MI;
997  setMacroInfo(MacroNameTok.getIdentifierInfo(), 0);
998}
999
1000
1001//===----------------------------------------------------------------------===//
1002// Preprocessor Conditional Directive Handling.
1003//===----------------------------------------------------------------------===//
1004
1005/// HandleIfdefDirective - Implements the #ifdef/#ifndef directive.  isIfndef is
1006/// true when this is a #ifndef directive.  ReadAnyTokensBeforeDirective is true
1007/// if any tokens have been returned or pp-directives activated before this
1008/// #ifndef has been lexed.
1009///
1010void Preprocessor::HandleIfdefDirective(Token &Result, bool isIfndef,
1011                                        bool ReadAnyTokensBeforeDirective) {
1012  ++NumIf;
1013  Token DirectiveTok = Result;
1014
1015  Token MacroNameTok;
1016  ReadMacroName(MacroNameTok);
1017
1018  // Error reading macro name?  If so, diagnostic already issued.
1019  if (MacroNameTok.is(tok::eom)) {
1020    // Skip code until we get to #endif.  This helps with recovery by not
1021    // emitting an error when the #endif is reached.
1022    SkipExcludedConditionalBlock(DirectiveTok.getLocation(),
1023                                 /*Foundnonskip*/false, /*FoundElse*/false);
1024    return;
1025  }
1026
1027  // Check to see if this is the last token on the #if[n]def line.
1028  CheckEndOfDirective(isIfndef ? "#ifndef" : "#ifdef");
1029
1030  if (CurPPLexer->getConditionalStackDepth() == 0) {
1031    // If the start of a top-level #ifdef, inform MIOpt.
1032    if (!ReadAnyTokensBeforeDirective) {
1033      assert(isIfndef && "#ifdef shouldn't reach here");
1034      CurPPLexer->MIOpt.EnterTopLevelIFNDEF(MacroNameTok.getIdentifierInfo());
1035    } else
1036      CurPPLexer->MIOpt.EnterTopLevelConditional();
1037  }
1038
1039  IdentifierInfo *MII = MacroNameTok.getIdentifierInfo();
1040  MacroInfo *MI = getMacroInfo(MII);
1041
1042  // If there is a macro, process it.
1043  if (MI)  // Mark it used.
1044    MI->setIsUsed(true);
1045
1046  // Should we include the stuff contained by this directive?
1047  if (!MI == isIfndef) {
1048    // Yes, remember that we are inside a conditional, then lex the next token.
1049    CurPPLexer->pushConditionalLevel(DirectiveTok.getLocation(), /*wasskip*/false,
1050                                   /*foundnonskip*/true, /*foundelse*/false);
1051  } else {
1052    // No, skip the contents of this block and return the first token after it.
1053    SkipExcludedConditionalBlock(DirectiveTok.getLocation(),
1054                                 /*Foundnonskip*/false,
1055                                 /*FoundElse*/false);
1056  }
1057}
1058
1059/// HandleIfDirective - Implements the #if directive.
1060///
1061void Preprocessor::HandleIfDirective(Token &IfToken,
1062                                     bool ReadAnyTokensBeforeDirective) {
1063  ++NumIf;
1064
1065  // Parse and evaluation the conditional expression.
1066  IdentifierInfo *IfNDefMacro = 0;
1067  bool ConditionalTrue = EvaluateDirectiveExpression(IfNDefMacro);
1068
1069
1070  // If this condition is equivalent to #ifndef X, and if this is the first
1071  // directive seen, handle it for the multiple-include optimization.
1072  if (CurPPLexer->getConditionalStackDepth() == 0) {
1073    if (!ReadAnyTokensBeforeDirective && IfNDefMacro)
1074      CurPPLexer->MIOpt.EnterTopLevelIFNDEF(IfNDefMacro);
1075    else
1076      CurPPLexer->MIOpt.EnterTopLevelConditional();
1077  }
1078
1079  // Should we include the stuff contained by this directive?
1080  if (ConditionalTrue) {
1081    // Yes, remember that we are inside a conditional, then lex the next token.
1082    CurPPLexer->pushConditionalLevel(IfToken.getLocation(), /*wasskip*/false,
1083                                   /*foundnonskip*/true, /*foundelse*/false);
1084  } else {
1085    // No, skip the contents of this block and return the first token after it.
1086    SkipExcludedConditionalBlock(IfToken.getLocation(), /*Foundnonskip*/false,
1087                                 /*FoundElse*/false);
1088  }
1089}
1090
1091/// HandleEndifDirective - Implements the #endif directive.
1092///
1093void Preprocessor::HandleEndifDirective(Token &EndifToken) {
1094  ++NumEndif;
1095
1096  // Check that this is the whole directive.
1097  CheckEndOfDirective("#endif");
1098
1099  PPConditionalInfo CondInfo;
1100  if (CurPPLexer->popConditionalLevel(CondInfo)) {
1101    // No conditionals on the stack: this is an #endif without an #if.
1102    return Diag(EndifToken, diag::err_pp_endif_without_if);
1103  }
1104
1105  // If this the end of a top-level #endif, inform MIOpt.
1106  if (CurPPLexer->getConditionalStackDepth() == 0)
1107    CurPPLexer->MIOpt.ExitTopLevelConditional();
1108
1109  assert(!CondInfo.WasSkipping && !CurPPLexer->LexingRawMode &&
1110         "This code should only be reachable in the non-skipping case!");
1111}
1112
1113
1114void Preprocessor::HandleElseDirective(Token &Result) {
1115  ++NumElse;
1116
1117  // #else directive in a non-skipping conditional... start skipping.
1118  CheckEndOfDirective("#else");
1119
1120  PPConditionalInfo CI;
1121  if (CurPPLexer->popConditionalLevel(CI))
1122    return Diag(Result, diag::pp_err_else_without_if);
1123
1124  // If this is a top-level #else, inform the MIOpt.
1125  if (CurPPLexer->getConditionalStackDepth() == 0)
1126    CurPPLexer->MIOpt.EnterTopLevelConditional();
1127
1128  // If this is a #else with a #else before it, report the error.
1129  if (CI.FoundElse) Diag(Result, diag::pp_err_else_after_else);
1130
1131  // Finally, skip the rest of the contents of this block and return the first
1132  // token after it.
1133  return SkipExcludedConditionalBlock(CI.IfLoc, /*Foundnonskip*/true,
1134                                      /*FoundElse*/true);
1135}
1136
1137void Preprocessor::HandleElifDirective(Token &ElifToken) {
1138  ++NumElse;
1139
1140  // #elif directive in a non-skipping conditional... start skipping.
1141  // We don't care what the condition is, because we will always skip it (since
1142  // the block immediately before it was included).
1143  DiscardUntilEndOfDirective();
1144
1145  PPConditionalInfo CI;
1146  if (CurPPLexer->popConditionalLevel(CI))
1147    return Diag(ElifToken, diag::pp_err_elif_without_if);
1148
1149  // If this is a top-level #elif, inform the MIOpt.
1150  if (CurPPLexer->getConditionalStackDepth() == 0)
1151    CurPPLexer->MIOpt.EnterTopLevelConditional();
1152
1153  // If this is a #elif with a #else before it, report the error.
1154  if (CI.FoundElse) Diag(ElifToken, diag::pp_err_elif_after_else);
1155
1156  // Finally, skip the rest of the contents of this block and return the first
1157  // token after it.
1158  return SkipExcludedConditionalBlock(CI.IfLoc, /*Foundnonskip*/true,
1159                                      /*FoundElse*/CI.FoundElse);
1160}
1161
1162