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