1 //===--- PPDirectives.cpp - Directive Handling for Preprocessor -----------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 /// \brief Implements # directive processing for the Preprocessor.
13 //===----------------------------------------------------------------------===//
15 #include "clang/Lex/Preprocessor.h"
16 #include "clang/Basic/FileManager.h"
17 #include "clang/Basic/SourceManager.h"
18 #include "clang/Lex/CodeCompletionHandler.h"
19 #include "clang/Lex/HeaderSearch.h"
20 #include "clang/Lex/LexDiagnostic.h"
21 #include "clang/Lex/LiteralSupport.h"
22 #include "clang/Lex/MacroInfo.h"
23 #include "clang/Lex/ModuleLoader.h"
24 #include "clang/Lex/Pragma.h"
25 #include "llvm/ADT/APInt.h"
26 #include "llvm/Support/ErrorHandling.h"
27 #include "llvm/Support/SaveAndRestore.h"
28 using namespace clang;
30 //===----------------------------------------------------------------------===//
31 // Utility Methods for Preprocessor Directive Handling.
32 //===----------------------------------------------------------------------===//
34 MacroInfo *Preprocessor::AllocateMacroInfo() {
35 MacroInfoChain *MIChain;
39 MICache = MICache->Next;
42 MIChain = BP.Allocate<MacroInfoChain>();
45 MIChain->Next = MIChainHead;
48 MIChainHead->Prev = MIChain;
49 MIChainHead = MIChain;
51 return &(MIChain->MI);
54 MacroInfo *Preprocessor::AllocateMacroInfo(SourceLocation L) {
55 MacroInfo *MI = AllocateMacroInfo();
56 new (MI) MacroInfo(L);
60 MacroInfo *Preprocessor::AllocateDeserializedMacroInfo(SourceLocation L,
61 unsigned SubModuleID) {
62 LLVM_STATIC_ASSERT(llvm::AlignOf<MacroInfo>::Alignment >= sizeof(SubModuleID),
63 "alignment for MacroInfo is less than the ID");
65 (MacroInfo*)BP.Allocate(sizeof(MacroInfo) + sizeof(SubModuleID),
66 llvm::AlignOf<MacroInfo>::Alignment);
67 new (MI) MacroInfo(L);
68 MI->FromASTFile = true;
69 MI->setOwningModuleID(SubModuleID);
74 Preprocessor::AllocateDefMacroDirective(MacroInfo *MI, SourceLocation Loc,
76 DefMacroDirective *MD = BP.Allocate<DefMacroDirective>();
77 new (MD) DefMacroDirective(MI, Loc, isImported);
82 Preprocessor::AllocateUndefMacroDirective(SourceLocation UndefLoc) {
83 UndefMacroDirective *MD = BP.Allocate<UndefMacroDirective>();
84 new (MD) UndefMacroDirective(UndefLoc);
88 VisibilityMacroDirective *
89 Preprocessor::AllocateVisibilityMacroDirective(SourceLocation Loc,
91 VisibilityMacroDirective *MD = BP.Allocate<VisibilityMacroDirective>();
92 new (MD) VisibilityMacroDirective(Loc, isPublic);
96 /// \brief Release the specified MacroInfo to be reused for allocating
97 /// new MacroInfo objects.
98 void Preprocessor::ReleaseMacroInfo(MacroInfo *MI) {
99 MacroInfoChain *MIChain = (MacroInfoChain*) MI;
100 if (MacroInfoChain *Prev = MIChain->Prev) {
101 MacroInfoChain *Next = MIChain->Next;
107 assert(MIChainHead == MIChain);
108 MIChainHead = MIChain->Next;
109 MIChainHead->Prev = 0;
111 MIChain->Next = MICache;
117 /// \brief Read and discard all tokens remaining on the current line until
118 /// the tok::eod token is found.
119 void Preprocessor::DiscardUntilEndOfDirective() {
122 LexUnexpandedToken(Tmp);
123 assert(Tmp.isNot(tok::eof) && "EOF seen while discarding directive tokens");
124 } while (Tmp.isNot(tok::eod));
127 /// \brief Lex and validate a macro name, which occurs after a
128 /// \#define or \#undef.
130 /// This sets the token kind to eod and discards the rest
131 /// of the macro line if the macro name is invalid. \p isDefineUndef is 1 if
132 /// this is due to a a \#define, 2 if \#undef directive, 0 if it is something
133 /// else (e.g. \#ifdef).
134 void Preprocessor::ReadMacroName(Token &MacroNameTok, char isDefineUndef) {
135 // Read the token, don't allow macro expansion on it.
136 LexUnexpandedToken(MacroNameTok);
138 if (MacroNameTok.is(tok::code_completion)) {
140 CodeComplete->CodeCompleteMacroName(isDefineUndef == 1);
141 setCodeCompletionReached();
142 LexUnexpandedToken(MacroNameTok);
145 // Missing macro name?
146 if (MacroNameTok.is(tok::eod)) {
147 Diag(MacroNameTok, diag::err_pp_missing_macro_name);
151 IdentifierInfo *II = MacroNameTok.getIdentifierInfo();
153 bool Invalid = false;
154 std::string Spelling = getSpelling(MacroNameTok, &Invalid);
158 const IdentifierInfo &Info = Identifiers.get(Spelling);
160 // Allow #defining |and| and friends in microsoft mode.
161 if (Info.isCPlusPlusOperatorKeyword() && getLangOpts().MicrosoftMode) {
162 MacroNameTok.setIdentifierInfo(getIdentifierInfo(Spelling));
166 if (Info.isCPlusPlusOperatorKeyword())
167 // C++ 2.5p2: Alternative tokens behave the same as its primary token
168 // except for their spellings.
169 Diag(MacroNameTok, diag::err_pp_operator_used_as_macro_name) << Spelling;
171 Diag(MacroNameTok, diag::err_pp_macro_not_identifier);
172 // Fall through on error.
173 } else if (isDefineUndef && II->getPPKeywordID() == tok::pp_defined) {
174 // Error if defining "defined": C99 6.10.8/4, C++ [cpp.predefined]p4.
175 Diag(MacroNameTok, diag::err_defined_macro_name);
176 } else if (isDefineUndef == 2 && II->hasMacroDefinition() &&
177 getMacroInfo(II)->isBuiltinMacro()) {
178 // Warn if undefining "__LINE__" and other builtins, per C99 6.10.8/4
179 // and C++ [cpp.predefined]p4], but allow it as an extension.
180 Diag(MacroNameTok, diag::ext_pp_undef_builtin_macro);
183 // Okay, we got a good identifier node. Return it.
187 // Invalid macro name, read and discard the rest of the line. Then set the
188 // token kind to tok::eod.
189 MacroNameTok.setKind(tok::eod);
190 return DiscardUntilEndOfDirective();
193 /// \brief Ensure that the next token is a tok::eod token.
195 /// If not, emit a diagnostic and consume up until the eod. If EnableMacros is
196 /// true, then we consider macros that expand to zero tokens as being ok.
197 void Preprocessor::CheckEndOfDirective(const char *DirType, bool EnableMacros) {
199 // Lex unexpanded tokens for most directives: macros might expand to zero
200 // tokens, causing us to miss diagnosing invalid lines. Some directives (like
201 // #line) allow empty macros.
205 LexUnexpandedToken(Tmp);
207 // There should be no tokens after the directive, but we allow them as an
209 while (Tmp.is(tok::comment)) // Skip comments in -C mode.
210 LexUnexpandedToken(Tmp);
212 if (Tmp.isNot(tok::eod)) {
213 // Add a fixit in GNU/C99/C++ mode. Don't offer a fixit for strict-C89,
214 // or if this is a macro-style preprocessing directive, because it is more
215 // trouble than it is worth to insert /**/ and check that there is no /**/
216 // in the range also.
218 if ((LangOpts.GNUMode || LangOpts.C99 || LangOpts.CPlusPlus) &&
220 Hint = FixItHint::CreateInsertion(Tmp.getLocation(),"//");
221 Diag(Tmp, diag::ext_pp_extra_tokens_at_eol) << DirType << Hint;
222 DiscardUntilEndOfDirective();
228 /// SkipExcludedConditionalBlock - We just read a \#if or related directive and
229 /// decided that the subsequent tokens are in the \#if'd out portion of the
230 /// file. Lex the rest of the file, until we see an \#endif. If
231 /// FoundNonSkipPortion is true, then we have already emitted code for part of
232 /// this \#if directive, so \#else/\#elif blocks should never be entered.
233 /// If ElseOk is true, then \#else directives are ok, if not, then we have
234 /// already seen one so a \#else directive is a duplicate. When this returns,
235 /// the caller can lex the first valid token.
236 void Preprocessor::SkipExcludedConditionalBlock(SourceLocation IfTokenLoc,
237 bool FoundNonSkipPortion,
239 SourceLocation ElseLoc) {
241 assert(CurTokenLexer == 0 && CurPPLexer && "Lexing a macro, not a file?");
243 CurPPLexer->pushConditionalLevel(IfTokenLoc, /*isSkipping*/false,
244 FoundNonSkipPortion, FoundElse);
247 PTHSkipExcludedConditionalBlock();
251 // Enter raw mode to disable identifier lookup (and thus macro expansion),
252 // disabling warnings, etc.
253 CurPPLexer->LexingRawMode = true;
258 if (Tok.is(tok::code_completion)) {
260 CodeComplete->CodeCompleteInConditionalExclusion();
261 setCodeCompletionReached();
265 // If this is the end of the buffer, we have an error.
266 if (Tok.is(tok::eof)) {
267 // Emit errors for each unterminated conditional on the stack, including
269 while (!CurPPLexer->ConditionalStack.empty()) {
270 if (CurLexer->getFileLoc() != CodeCompletionFileLoc)
271 Diag(CurPPLexer->ConditionalStack.back().IfLoc,
272 diag::err_pp_unterminated_conditional);
273 CurPPLexer->ConditionalStack.pop_back();
276 // Just return and let the caller lex after this #include.
280 // If this token is not a preprocessor directive, just skip it.
281 if (Tok.isNot(tok::hash) || !Tok.isAtStartOfLine())
284 // We just parsed a # character at the start of a line, so we're in
285 // directive mode. Tell the lexer this so any newlines we see will be
286 // converted into an EOD token (this terminates the macro).
287 CurPPLexer->ParsingPreprocessorDirective = true;
288 if (CurLexer) CurLexer->SetKeepWhitespaceMode(false);
291 // Read the next token, the directive flavor.
292 LexUnexpandedToken(Tok);
294 // If this isn't an identifier directive (e.g. is "# 1\n" or "#\n", or
295 // something bogus), skip it.
296 if (Tok.isNot(tok::raw_identifier)) {
297 CurPPLexer->ParsingPreprocessorDirective = false;
298 // Restore comment saving mode.
299 if (CurLexer) CurLexer->resetExtendedTokenMode();
303 // If the first letter isn't i or e, it isn't intesting to us. We know that
304 // this is safe in the face of spelling differences, because there is no way
305 // to spell an i/e in a strange way that is another letter. Skipping this
306 // allows us to avoid looking up the identifier info for #define/#undef and
307 // other common directives.
308 const char *RawCharData = Tok.getRawIdentifierData();
310 char FirstChar = RawCharData[0];
311 if (FirstChar >= 'a' && FirstChar <= 'z' &&
312 FirstChar != 'i' && FirstChar != 'e') {
313 CurPPLexer->ParsingPreprocessorDirective = false;
314 // Restore comment saving mode.
315 if (CurLexer) CurLexer->resetExtendedTokenMode();
319 // Get the identifier name without trigraphs or embedded newlines. Note
320 // that we can't use Tok.getIdentifierInfo() because its lookup is disabled
322 char DirectiveBuf[20];
324 if (!Tok.needsCleaning() && Tok.getLength() < 20) {
325 Directive = StringRef(RawCharData, Tok.getLength());
327 std::string DirectiveStr = getSpelling(Tok);
328 unsigned IdLen = DirectiveStr.size();
330 CurPPLexer->ParsingPreprocessorDirective = false;
331 // Restore comment saving mode.
332 if (CurLexer) CurLexer->resetExtendedTokenMode();
335 memcpy(DirectiveBuf, &DirectiveStr[0], IdLen);
336 Directive = StringRef(DirectiveBuf, IdLen);
339 if (Directive.startswith("if")) {
340 StringRef Sub = Directive.substr(2);
341 if (Sub.empty() || // "if"
342 Sub == "def" || // "ifdef"
343 Sub == "ndef") { // "ifndef"
344 // We know the entire #if/#ifdef/#ifndef block will be skipped, don't
345 // bother parsing the condition.
346 DiscardUntilEndOfDirective();
347 CurPPLexer->pushConditionalLevel(Tok.getLocation(), /*wasskipping*/true,
348 /*foundnonskip*/false,
351 } else if (Directive[0] == 'e') {
352 StringRef Sub = Directive.substr(1);
353 if (Sub == "ndif") { // "endif"
354 PPConditionalInfo CondInfo;
355 CondInfo.WasSkipping = true; // Silence bogus warning.
356 bool InCond = CurPPLexer->popConditionalLevel(CondInfo);
357 (void)InCond; // Silence warning in no-asserts mode.
358 assert(!InCond && "Can't be skipping if not in a conditional!");
360 // If we popped the outermost skipping block, we're done skipping!
361 if (!CondInfo.WasSkipping) {
362 // Restore the value of LexingRawMode so that trailing comments
363 // are handled correctly, if we've reached the outermost block.
364 CurPPLexer->LexingRawMode = false;
365 CheckEndOfDirective("endif");
366 CurPPLexer->LexingRawMode = true;
368 Callbacks->Endif(Tok.getLocation(), CondInfo.IfLoc);
371 DiscardUntilEndOfDirective();
373 } else if (Sub == "lse") { // "else".
374 // #else directive in a skipping conditional. If not in some other
375 // skipping conditional, and if #else hasn't already been seen, enter it
376 // as a non-skipping conditional.
377 PPConditionalInfo &CondInfo = CurPPLexer->peekConditionalLevel();
379 // If this is a #else with a #else before it, report the error.
380 if (CondInfo.FoundElse) Diag(Tok, diag::pp_err_else_after_else);
382 // Note that we've seen a #else in this conditional.
383 CondInfo.FoundElse = true;
385 // If the conditional is at the top level, and the #if block wasn't
386 // entered, enter the #else block now.
387 if (!CondInfo.WasSkipping && !CondInfo.FoundNonSkip) {
388 CondInfo.FoundNonSkip = true;
389 // Restore the value of LexingRawMode so that trailing comments
390 // are handled correctly.
391 CurPPLexer->LexingRawMode = false;
392 CheckEndOfDirective("else");
393 CurPPLexer->LexingRawMode = true;
395 Callbacks->Else(Tok.getLocation(), CondInfo.IfLoc);
398 DiscardUntilEndOfDirective(); // C99 6.10p4.
400 } else if (Sub == "lif") { // "elif".
401 PPConditionalInfo &CondInfo = CurPPLexer->peekConditionalLevel();
404 const SourceLocation ConditionalBegin = CurPPLexer->getSourceLocation();
405 // If this is in a skipping block or if we're already handled this #if
406 // block, don't bother parsing the condition.
407 if (CondInfo.WasSkipping || CondInfo.FoundNonSkip) {
408 DiscardUntilEndOfDirective();
411 // Restore the value of LexingRawMode so that identifiers are
412 // looked up, etc, inside the #elif expression.
413 assert(CurPPLexer->LexingRawMode && "We have to be skipping here!");
414 CurPPLexer->LexingRawMode = false;
415 IdentifierInfo *IfNDefMacro = 0;
416 ShouldEnter = EvaluateDirectiveExpression(IfNDefMacro);
417 CurPPLexer->LexingRawMode = true;
419 const SourceLocation ConditionalEnd = CurPPLexer->getSourceLocation();
421 // If this is a #elif with a #else before it, report the error.
422 if (CondInfo.FoundElse) Diag(Tok, diag::pp_err_elif_after_else);
424 // If this condition is true, enter it!
426 CondInfo.FoundNonSkip = true;
428 Callbacks->Elif(Tok.getLocation(),
429 SourceRange(ConditionalBegin, ConditionalEnd),
436 CurPPLexer->ParsingPreprocessorDirective = false;
437 // Restore comment saving mode.
438 if (CurLexer) CurLexer->resetExtendedTokenMode();
441 // Finally, if we are out of the conditional (saw an #endif or ran off the end
442 // of the file, just stop skipping and return to lexing whatever came after
444 CurPPLexer->LexingRawMode = false;
447 SourceLocation BeginLoc = ElseLoc.isValid() ? ElseLoc : IfTokenLoc;
448 Callbacks->SourceRangeSkipped(SourceRange(BeginLoc, Tok.getLocation()));
452 void Preprocessor::PTHSkipExcludedConditionalBlock() {
456 assert(CurPTHLexer->LexingRawMode == false);
458 // Skip to the next '#else', '#elif', or #endif.
459 if (CurPTHLexer->SkipBlock()) {
460 // We have reached an #endif. Both the '#' and 'endif' tokens
461 // have been consumed by the PTHLexer. Just pop off the condition level.
462 PPConditionalInfo CondInfo;
463 bool InCond = CurPTHLexer->popConditionalLevel(CondInfo);
464 (void)InCond; // Silence warning in no-asserts mode.
465 assert(!InCond && "Can't be skipping if not in a conditional!");
469 // We have reached a '#else' or '#elif'. Lex the next token to get
470 // the directive flavor.
472 LexUnexpandedToken(Tok);
474 // We can actually look up the IdentifierInfo here since we aren't in
476 tok::PPKeywordKind K = Tok.getIdentifierInfo()->getPPKeywordID();
478 if (K == tok::pp_else) {
479 // #else: Enter the else condition. We aren't in a nested condition
480 // since we skip those. We're always in the one matching the last
481 // blocked we skipped.
482 PPConditionalInfo &CondInfo = CurPTHLexer->peekConditionalLevel();
483 // Note that we've seen a #else in this conditional.
484 CondInfo.FoundElse = true;
486 // If the #if block wasn't entered then enter the #else block now.
487 if (!CondInfo.FoundNonSkip) {
488 CondInfo.FoundNonSkip = true;
490 // Scan until the eod token.
491 CurPTHLexer->ParsingPreprocessorDirective = true;
492 DiscardUntilEndOfDirective();
493 CurPTHLexer->ParsingPreprocessorDirective = false;
498 // Otherwise skip this block.
502 assert(K == tok::pp_elif);
503 PPConditionalInfo &CondInfo = CurPTHLexer->peekConditionalLevel();
505 // If this is a #elif with a #else before it, report the error.
506 if (CondInfo.FoundElse)
507 Diag(Tok, diag::pp_err_elif_after_else);
509 // If this is in a skipping block or if we're already handled this #if
510 // block, don't bother parsing the condition. We just skip this block.
511 if (CondInfo.FoundNonSkip)
514 // Evaluate the condition of the #elif.
515 IdentifierInfo *IfNDefMacro = 0;
516 CurPTHLexer->ParsingPreprocessorDirective = true;
517 bool ShouldEnter = EvaluateDirectiveExpression(IfNDefMacro);
518 CurPTHLexer->ParsingPreprocessorDirective = false;
520 // If this condition is true, enter it!
522 CondInfo.FoundNonSkip = true;
526 // Otherwise, skip this block and go to the next one.
531 const FileEntry *Preprocessor::LookupFile(
534 const DirectoryLookup *FromDir,
535 const DirectoryLookup *&CurDir,
536 SmallVectorImpl<char> *SearchPath,
537 SmallVectorImpl<char> *RelativePath,
538 Module **SuggestedModule,
540 // If the header lookup mechanism may be relative to the current file, pass in
541 // info about where the current file is.
542 const FileEntry *CurFileEnt = 0;
544 FileID FID = getCurrentFileLexer()->getFileID();
545 CurFileEnt = SourceMgr.getFileEntryForID(FID);
547 // If there is no file entry associated with this file, it must be the
548 // predefines buffer. Any other file is not lexed with a normal lexer, so
549 // it won't be scanned for preprocessor directives. If we have the
550 // predefines buffer, resolve #include references (which come from the
551 // -include command line argument) as if they came from the main file, this
552 // affects file lookup etc.
553 if (CurFileEnt == 0) {
554 FID = SourceMgr.getMainFileID();
555 CurFileEnt = SourceMgr.getFileEntryForID(FID);
559 // Do a standard file entry lookup.
560 CurDir = CurDirLookup;
561 const FileEntry *FE = HeaderInfo.LookupFile(
562 Filename, isAngled, FromDir, CurDir, CurFileEnt,
563 SearchPath, RelativePath, SuggestedModule, SkipCache);
566 // Otherwise, see if this is a subframework header. If so, this is relative
567 // to one of the headers on the #include stack. Walk the list of the current
568 // headers on the #include stack and pass them to HeaderInfo.
570 if ((CurFileEnt = SourceMgr.getFileEntryForID(CurPPLexer->getFileID())))
571 if ((FE = HeaderInfo.LookupSubframeworkHeader(Filename, CurFileEnt,
572 SearchPath, RelativePath,
577 for (unsigned i = 0, e = IncludeMacroStack.size(); i != e; ++i) {
578 IncludeStackInfo &ISEntry = IncludeMacroStack[e-i-1];
579 if (IsFileLexer(ISEntry)) {
581 SourceMgr.getFileEntryForID(ISEntry.ThePPLexer->getFileID())))
582 if ((FE = HeaderInfo.LookupSubframeworkHeader(
583 Filename, CurFileEnt, SearchPath, RelativePath,
589 // Otherwise, we really couldn't find the file.
594 //===----------------------------------------------------------------------===//
595 // Preprocessor Directive Handling.
596 //===----------------------------------------------------------------------===//
598 class Preprocessor::ResetMacroExpansionHelper {
600 ResetMacroExpansionHelper(Preprocessor *pp)
601 : PP(pp), save(pp->DisableMacroExpansion) {
602 if (pp->MacroExpansionInDirectivesOverride)
603 pp->DisableMacroExpansion = false;
605 ~ResetMacroExpansionHelper() {
606 PP->DisableMacroExpansion = save;
613 /// HandleDirective - This callback is invoked when the lexer sees a # token
614 /// at the start of a line. This consumes the directive, modifies the
615 /// lexer/preprocessor state, and advances the lexer(s) so that the next token
616 /// read is the correct one.
617 void Preprocessor::HandleDirective(Token &Result) {
618 // FIXME: Traditional: # with whitespace before it not recognized by K&R?
620 // We just parsed a # character at the start of a line, so we're in directive
621 // mode. Tell the lexer this so any newlines we see will be converted into an
622 // EOD token (which terminates the directive).
623 CurPPLexer->ParsingPreprocessorDirective = true;
624 if (CurLexer) CurLexer->SetKeepWhitespaceMode(false);
628 // We are about to read a token. For the multiple-include optimization FA to
629 // work, we have to remember if we had read any tokens *before* this
631 bool ReadAnyTokensBeforeDirective =CurPPLexer->MIOpt.getHasReadAnyTokensVal();
633 // Save the '#' token in case we need to return it later.
634 Token SavedHash = Result;
636 // Read the next token, the directive flavor. This isn't expanded due to
638 LexUnexpandedToken(Result);
640 // C99 6.10.3p11: Is this preprocessor directive in macro invocation? e.g.:
645 // If so, the user is relying on undefined behavior, emit a diagnostic. Do
646 // not support this for #include-like directives, since that can result in
647 // terrible diagnostics, and does not work in GCC.
649 if (IdentifierInfo *II = Result.getIdentifierInfo()) {
650 switch (II->getPPKeywordID()) {
651 case tok::pp_include:
653 case tok::pp_include_next:
654 case tok::pp___include_macros:
655 Diag(Result, diag::err_embedded_include) << II->getName();
656 DiscardUntilEndOfDirective();
662 Diag(Result, diag::ext_embedded_directive);
665 // Temporarily enable macro expansion if set so
666 // and reset to previous state when returning from this function.
667 ResetMacroExpansionHelper helper(this);
669 switch (Result.getKind()) {
671 return; // null directive.
672 case tok::code_completion:
674 CodeComplete->CodeCompleteDirective(
675 CurPPLexer->getConditionalStackDepth() > 0);
676 setCodeCompletionReached();
678 case tok::numeric_constant: // # 7 GNU line marker directive.
679 if (getLangOpts().AsmPreprocessor)
680 break; // # 4 is not a preprocessor directive in .S files.
681 return HandleDigitDirective(Result);
683 IdentifierInfo *II = Result.getIdentifierInfo();
684 if (II == 0) break; // Not an identifier.
686 // Ask what the preprocessor keyword ID is.
687 switch (II->getPPKeywordID()) {
689 // C99 6.10.1 - Conditional Inclusion.
691 return HandleIfDirective(Result, ReadAnyTokensBeforeDirective);
693 return HandleIfdefDirective(Result, false, true/*not valid for miopt*/);
695 return HandleIfdefDirective(Result, true, ReadAnyTokensBeforeDirective);
697 return HandleElifDirective(Result);
699 return HandleElseDirective(Result);
701 return HandleEndifDirective(Result);
703 // C99 6.10.2 - Source File Inclusion.
704 case tok::pp_include:
706 return HandleIncludeDirective(SavedHash.getLocation(), Result);
707 case tok::pp___include_macros:
709 return HandleIncludeMacrosDirective(SavedHash.getLocation(), Result);
711 // C99 6.10.3 - Macro Replacement.
713 return HandleDefineDirective(Result);
715 return HandleUndefDirective(Result);
717 // C99 6.10.4 - Line Control.
719 return HandleLineDirective(Result);
721 // C99 6.10.5 - Error Directive.
723 return HandleUserDiagnosticDirective(Result, false);
725 // C99 6.10.6 - Pragma Directive.
727 return HandlePragmaDirective(PIK_HashPragma);
731 return HandleImportDirective(SavedHash.getLocation(), Result);
732 case tok::pp_include_next:
733 return HandleIncludeNextDirective(SavedHash.getLocation(), Result);
735 case tok::pp_warning:
736 Diag(Result, diag::ext_pp_warning_directive);
737 return HandleUserDiagnosticDirective(Result, true);
739 return HandleIdentSCCSDirective(Result);
741 return HandleIdentSCCSDirective(Result);
743 //isExtension = true; // FIXME: implement #assert
745 case tok::pp_unassert:
746 //isExtension = true; // FIXME: implement #unassert
749 case tok::pp___public_macro:
750 if (getLangOpts().Modules)
751 return HandleMacroPublicDirective(Result);
754 case tok::pp___private_macro:
755 if (getLangOpts().Modules)
756 return HandleMacroPrivateDirective(Result);
762 // If this is a .S file, treat unknown # directives as non-preprocessor
763 // directives. This is important because # may be a comment or introduce
764 // various pseudo-ops. Just return the # token and push back the following
765 // token to be lexed next time.
766 if (getLangOpts().AsmPreprocessor) {
767 Token *Toks = new Token[2];
768 // Return the # and the token after it.
772 // If the second token is a hashhash token, then we need to translate it to
773 // unknown so the token lexer doesn't try to perform token pasting.
774 if (Result.is(tok::hashhash))
775 Toks[1].setKind(tok::unknown);
777 // Enter this token stream so that we re-lex the tokens. Make sure to
778 // enable macro expansion, in case the token after the # is an identifier
780 EnterTokenStream(Toks, 2, false, true);
784 // If we reached here, the preprocessing token is not valid!
785 Diag(Result, diag::err_pp_invalid_directive);
787 // Read the rest of the PP line.
788 DiscardUntilEndOfDirective();
790 // Okay, we're done parsing the directive.
793 /// GetLineValue - Convert a numeric token into an unsigned value, emitting
794 /// Diagnostic DiagID if it is invalid, and returning the value in Val.
795 static bool GetLineValue(Token &DigitTok, unsigned &Val,
796 unsigned DiagID, Preprocessor &PP) {
797 if (DigitTok.isNot(tok::numeric_constant)) {
798 PP.Diag(DigitTok, DiagID);
800 if (DigitTok.isNot(tok::eod))
801 PP.DiscardUntilEndOfDirective();
805 SmallString<64> IntegerBuffer;
806 IntegerBuffer.resize(DigitTok.getLength());
807 const char *DigitTokBegin = &IntegerBuffer[0];
808 bool Invalid = false;
809 unsigned ActualLength = PP.getSpelling(DigitTok, DigitTokBegin, &Invalid);
813 // Verify that we have a simple digit-sequence, and compute the value. This
814 // is always a simple digit string computed in decimal, so we do this manually
817 for (unsigned i = 0; i != ActualLength; ++i) {
818 if (!isDigit(DigitTokBegin[i])) {
819 PP.Diag(PP.AdvanceToTokenCharacter(DigitTok.getLocation(), i),
820 diag::err_pp_line_digit_sequence);
821 PP.DiscardUntilEndOfDirective();
825 unsigned NextVal = Val*10+(DigitTokBegin[i]-'0');
826 if (NextVal < Val) { // overflow.
827 PP.Diag(DigitTok, DiagID);
828 PP.DiscardUntilEndOfDirective();
834 if (DigitTokBegin[0] == '0' && Val)
835 PP.Diag(DigitTok.getLocation(), diag::warn_pp_line_decimal);
840 /// \brief Handle a \#line directive: C99 6.10.4.
842 /// The two acceptable forms are:
844 /// # line digit-sequence
845 /// # line digit-sequence "s-char-sequence"
847 void Preprocessor::HandleLineDirective(Token &Tok) {
848 // Read the line # and string argument. Per C99 6.10.4p5, these tokens are
853 // Validate the number and convert it to an unsigned.
855 if (GetLineValue(DigitTok, LineNo, diag::err_pp_line_requires_integer,*this))
859 Diag(DigitTok, diag::ext_pp_line_zero);
861 // Enforce C99 6.10.4p3: "The digit sequence shall not specify ... a
862 // number greater than 2147483647". C90 requires that the line # be <= 32767.
863 unsigned LineLimit = 32768U;
864 if (LangOpts.C99 || LangOpts.CPlusPlus11)
865 LineLimit = 2147483648U;
866 if (LineNo >= LineLimit)
867 Diag(DigitTok, diag::ext_pp_line_too_big) << LineLimit;
868 else if (LangOpts.CPlusPlus11 && LineNo >= 32768U)
869 Diag(DigitTok, diag::warn_cxx98_compat_pp_line_too_big);
875 // If the StrTok is "eod", then it wasn't present. Otherwise, it must be a
876 // string followed by eod.
877 if (StrTok.is(tok::eod))
879 else if (StrTok.isNot(tok::string_literal)) {
880 Diag(StrTok, diag::err_pp_line_invalid_filename);
881 return DiscardUntilEndOfDirective();
882 } else if (StrTok.hasUDSuffix()) {
883 Diag(StrTok, diag::err_invalid_string_udl);
884 return DiscardUntilEndOfDirective();
886 // Parse and validate the string, converting it into a unique ID.
887 StringLiteralParser Literal(&StrTok, 1, *this);
888 assert(Literal.isAscii() && "Didn't allow wide strings in");
889 if (Literal.hadError)
890 return DiscardUntilEndOfDirective();
891 if (Literal.Pascal) {
892 Diag(StrTok, diag::err_pp_linemarker_invalid_filename);
893 return DiscardUntilEndOfDirective();
895 FilenameID = SourceMgr.getLineTableFilenameID(Literal.GetString());
897 // Verify that there is nothing after the string, other than EOD. Because
898 // of C99 6.10.4p5, macros that expand to empty tokens are ok.
899 CheckEndOfDirective("line", true);
902 SourceMgr.AddLineNote(DigitTok.getLocation(), LineNo, FilenameID);
905 Callbacks->FileChanged(CurPPLexer->getSourceLocation(),
906 PPCallbacks::RenameFile,
910 /// ReadLineMarkerFlags - Parse and validate any flags at the end of a GNU line
911 /// marker directive.
912 static bool ReadLineMarkerFlags(bool &IsFileEntry, bool &IsFileExit,
913 bool &IsSystemHeader, bool &IsExternCHeader,
918 if (FlagTok.is(tok::eod)) return false;
919 if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag, PP))
926 if (FlagTok.is(tok::eod)) return false;
927 if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag,PP))
929 } else if (FlagVal == 2) {
932 SourceManager &SM = PP.getSourceManager();
933 // If we are leaving the current presumed file, check to make sure the
934 // presumed include stack isn't empty!
936 SM.getDecomposedExpansionLoc(FlagTok.getLocation()).first;
937 PresumedLoc PLoc = SM.getPresumedLoc(FlagTok.getLocation());
938 if (PLoc.isInvalid())
941 // If there is no include loc (main file) or if the include loc is in a
942 // different physical file, then we aren't in a "1" line marker flag region.
943 SourceLocation IncLoc = PLoc.getIncludeLoc();
944 if (IncLoc.isInvalid() ||
945 SM.getDecomposedExpansionLoc(IncLoc).first != CurFileID) {
946 PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_pop);
947 PP.DiscardUntilEndOfDirective();
952 if (FlagTok.is(tok::eod)) return false;
953 if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag,PP))
957 // We must have 3 if there are still flags.
959 PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_flag);
960 PP.DiscardUntilEndOfDirective();
964 IsSystemHeader = true;
967 if (FlagTok.is(tok::eod)) return false;
968 if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag, PP))
971 // We must have 4 if there is yet another flag.
973 PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_flag);
974 PP.DiscardUntilEndOfDirective();
978 IsExternCHeader = true;
981 if (FlagTok.is(tok::eod)) return false;
983 // There are no more valid flags here.
984 PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_flag);
985 PP.DiscardUntilEndOfDirective();
989 /// HandleDigitDirective - Handle a GNU line marker directive, whose syntax is
990 /// one of the following forms:
993 /// # 42 "file" ('1' | '2')?
994 /// # 42 "file" ('1' | '2')? '3' '4'?
996 void Preprocessor::HandleDigitDirective(Token &DigitTok) {
997 // Validate the number and convert it to an unsigned. GNU does not have a
998 // line # limit other than it fit in 32-bits.
1000 if (GetLineValue(DigitTok, LineNo, diag::err_pp_linemarker_requires_integer,
1007 bool IsFileEntry = false, IsFileExit = false;
1008 bool IsSystemHeader = false, IsExternCHeader = false;
1009 int FilenameID = -1;
1011 // If the StrTok is "eod", then it wasn't present. Otherwise, it must be a
1012 // string followed by eod.
1013 if (StrTok.is(tok::eod))
1015 else if (StrTok.isNot(tok::string_literal)) {
1016 Diag(StrTok, diag::err_pp_linemarker_invalid_filename);
1017 return DiscardUntilEndOfDirective();
1018 } else if (StrTok.hasUDSuffix()) {
1019 Diag(StrTok, diag::err_invalid_string_udl);
1020 return DiscardUntilEndOfDirective();
1022 // Parse and validate the string, converting it into a unique ID.
1023 StringLiteralParser Literal(&StrTok, 1, *this);
1024 assert(Literal.isAscii() && "Didn't allow wide strings in");
1025 if (Literal.hadError)
1026 return DiscardUntilEndOfDirective();
1027 if (Literal.Pascal) {
1028 Diag(StrTok, diag::err_pp_linemarker_invalid_filename);
1029 return DiscardUntilEndOfDirective();
1031 FilenameID = SourceMgr.getLineTableFilenameID(Literal.GetString());
1033 // If a filename was present, read any flags that are present.
1034 if (ReadLineMarkerFlags(IsFileEntry, IsFileExit,
1035 IsSystemHeader, IsExternCHeader, *this))
1039 // Create a line note with this information.
1040 SourceMgr.AddLineNote(DigitTok.getLocation(), LineNo, FilenameID,
1041 IsFileEntry, IsFileExit,
1042 IsSystemHeader, IsExternCHeader);
1044 // If the preprocessor has callbacks installed, notify them of the #line
1045 // change. This is used so that the line marker comes out in -E mode for
1048 PPCallbacks::FileChangeReason Reason = PPCallbacks::RenameFile;
1050 Reason = PPCallbacks::EnterFile;
1051 else if (IsFileExit)
1052 Reason = PPCallbacks::ExitFile;
1053 SrcMgr::CharacteristicKind FileKind = SrcMgr::C_User;
1054 if (IsExternCHeader)
1055 FileKind = SrcMgr::C_ExternCSystem;
1056 else if (IsSystemHeader)
1057 FileKind = SrcMgr::C_System;
1059 Callbacks->FileChanged(CurPPLexer->getSourceLocation(), Reason, FileKind);
1064 /// HandleUserDiagnosticDirective - Handle a #warning or #error directive.
1066 void Preprocessor::HandleUserDiagnosticDirective(Token &Tok,
1068 // PTH doesn't emit #warning or #error directives.
1070 return CurPTHLexer->DiscardToEndOfLine();
1072 // Read the rest of the line raw. We do this because we don't want macros
1073 // to be expanded and we don't require that the tokens be valid preprocessing
1074 // tokens. For example, this is allowed: "#warning ` 'foo". GCC does
1075 // collapse multiple consequtive white space between tokens, but this isn't
1076 // specified by the standard.
1077 SmallString<128> Message;
1078 CurLexer->ReadToEndOfLine(&Message);
1080 // Find the first non-whitespace character, so that we can make the
1081 // diagnostic more succinct.
1082 StringRef Msg = Message.str().ltrim(" ");
1085 Diag(Tok, diag::pp_hash_warning) << Msg;
1087 Diag(Tok, diag::err_pp_hash_error) << Msg;
1090 /// HandleIdentSCCSDirective - Handle a #ident/#sccs directive.
1092 void Preprocessor::HandleIdentSCCSDirective(Token &Tok) {
1093 // Yes, this directive is an extension.
1094 Diag(Tok, diag::ext_pp_ident_directive);
1096 // Read the string argument.
1100 // If the token kind isn't a string, it's a malformed directive.
1101 if (StrTok.isNot(tok::string_literal) &&
1102 StrTok.isNot(tok::wide_string_literal)) {
1103 Diag(StrTok, diag::err_pp_malformed_ident);
1104 if (StrTok.isNot(tok::eod))
1105 DiscardUntilEndOfDirective();
1109 if (StrTok.hasUDSuffix()) {
1110 Diag(StrTok, diag::err_invalid_string_udl);
1111 return DiscardUntilEndOfDirective();
1114 // Verify that there is nothing after the string, other than EOD.
1115 CheckEndOfDirective("ident");
1118 bool Invalid = false;
1119 std::string Str = getSpelling(StrTok, &Invalid);
1121 Callbacks->Ident(Tok.getLocation(), Str);
1125 /// \brief Handle a #public directive.
1126 void Preprocessor::HandleMacroPublicDirective(Token &Tok) {
1128 ReadMacroName(MacroNameTok, 2);
1130 // Error reading macro name? If so, diagnostic already issued.
1131 if (MacroNameTok.is(tok::eod))
1134 // Check to see if this is the last token on the #__public_macro line.
1135 CheckEndOfDirective("__public_macro");
1137 IdentifierInfo *II = MacroNameTok.getIdentifierInfo();
1138 // Okay, we finally have a valid identifier to undef.
1139 MacroDirective *MD = getMacroDirective(II);
1141 // If the macro is not defined, this is an error.
1143 Diag(MacroNameTok, diag::err_pp_visibility_non_macro) << II;
1147 // Note that this macro has now been exported.
1148 appendMacroDirective(II, AllocateVisibilityMacroDirective(
1149 MacroNameTok.getLocation(), /*IsPublic=*/true));
1152 /// \brief Handle a #private directive.
1153 void Preprocessor::HandleMacroPrivateDirective(Token &Tok) {
1155 ReadMacroName(MacroNameTok, 2);
1157 // Error reading macro name? If so, diagnostic already issued.
1158 if (MacroNameTok.is(tok::eod))
1161 // Check to see if this is the last token on the #__private_macro line.
1162 CheckEndOfDirective("__private_macro");
1164 IdentifierInfo *II = MacroNameTok.getIdentifierInfo();
1165 // Okay, we finally have a valid identifier to undef.
1166 MacroDirective *MD = getMacroDirective(II);
1168 // If the macro is not defined, this is an error.
1170 Diag(MacroNameTok, diag::err_pp_visibility_non_macro) << II;
1174 // Note that this macro has now been marked private.
1175 appendMacroDirective(II, AllocateVisibilityMacroDirective(
1176 MacroNameTok.getLocation(), /*IsPublic=*/false));
1179 //===----------------------------------------------------------------------===//
1180 // Preprocessor Include Directive Handling.
1181 //===----------------------------------------------------------------------===//
1183 /// GetIncludeFilenameSpelling - Turn the specified lexer token into a fully
1184 /// checked and spelled filename, e.g. as an operand of \#include. This returns
1185 /// true if the input filename was in <>'s or false if it were in ""'s. The
1186 /// caller is expected to provide a buffer that is large enough to hold the
1187 /// spelling of the filename, but is also expected to handle the case when
1188 /// this method decides to use a different buffer.
1189 bool Preprocessor::GetIncludeFilenameSpelling(SourceLocation Loc,
1190 StringRef &Buffer) {
1191 // Get the text form of the filename.
1192 assert(!Buffer.empty() && "Can't have tokens with empty spellings!");
1194 // Make sure the filename is <x> or "x".
1196 if (Buffer[0] == '<') {
1197 if (Buffer.back() != '>') {
1198 Diag(Loc, diag::err_pp_expects_filename);
1199 Buffer = StringRef();
1203 } else if (Buffer[0] == '"') {
1204 if (Buffer.back() != '"') {
1205 Diag(Loc, diag::err_pp_expects_filename);
1206 Buffer = StringRef();
1211 Diag(Loc, diag::err_pp_expects_filename);
1212 Buffer = StringRef();
1216 // Diagnose #include "" as invalid.
1217 if (Buffer.size() <= 2) {
1218 Diag(Loc, diag::err_pp_empty_filename);
1219 Buffer = StringRef();
1223 // Skip the brackets.
1224 Buffer = Buffer.substr(1, Buffer.size()-2);
1228 /// \brief Handle cases where the \#include name is expanded from a macro
1229 /// as multiple tokens, which need to be glued together.
1231 /// This occurs for code like:
1233 /// \#define FOO <a/b.h>
1236 /// because in this case, "<a/b.h>" is returned as 7 tokens, not one.
1238 /// This code concatenates and consumes tokens up to the '>' token. It returns
1239 /// false if the > was found, otherwise it returns true if it finds and consumes
1241 bool Preprocessor::ConcatenateIncludeName(
1242 SmallString<128> &FilenameBuffer,
1243 SourceLocation &End) {
1247 while (CurTok.isNot(tok::eod)) {
1248 End = CurTok.getLocation();
1250 // FIXME: Provide code completion for #includes.
1251 if (CurTok.is(tok::code_completion)) {
1252 setCodeCompletionReached();
1257 // Append the spelling of this token to the buffer. If there was a space
1258 // before it, add it now.
1259 if (CurTok.hasLeadingSpace())
1260 FilenameBuffer.push_back(' ');
1262 // Get the spelling of the token, directly into FilenameBuffer if possible.
1263 unsigned PreAppendSize = FilenameBuffer.size();
1264 FilenameBuffer.resize(PreAppendSize+CurTok.getLength());
1266 const char *BufPtr = &FilenameBuffer[PreAppendSize];
1267 unsigned ActualLen = getSpelling(CurTok, BufPtr);
1269 // If the token was spelled somewhere else, copy it into FilenameBuffer.
1270 if (BufPtr != &FilenameBuffer[PreAppendSize])
1271 memcpy(&FilenameBuffer[PreAppendSize], BufPtr, ActualLen);
1273 // Resize FilenameBuffer to the correct size.
1274 if (CurTok.getLength() != ActualLen)
1275 FilenameBuffer.resize(PreAppendSize+ActualLen);
1277 // If we found the '>' marker, return success.
1278 if (CurTok.is(tok::greater))
1284 // If we hit the eod marker, emit an error and return true so that the caller
1285 // knows the EOD has been read.
1286 Diag(CurTok.getLocation(), diag::err_pp_expects_filename);
1290 /// HandleIncludeDirective - The "\#include" tokens have just been read, read
1291 /// the file to be included from the lexer, then include it! This is a common
1292 /// routine with functionality shared between \#include, \#include_next and
1293 /// \#import. LookupFrom is set when this is a \#include_next directive, it
1294 /// specifies the file to start searching from.
1295 void Preprocessor::HandleIncludeDirective(SourceLocation HashLoc,
1297 const DirectoryLookup *LookupFrom,
1301 CurPPLexer->LexIncludeFilename(FilenameTok);
1303 // Reserve a buffer to get the spelling.
1304 SmallString<128> FilenameBuffer;
1307 SourceLocation CharEnd; // the end of this directive, in characters
1309 switch (FilenameTok.getKind()) {
1311 // If the token kind is EOD, the error has already been diagnosed.
1314 case tok::angle_string_literal:
1315 case tok::string_literal:
1316 Filename = getSpelling(FilenameTok, FilenameBuffer);
1317 End = FilenameTok.getLocation();
1318 CharEnd = End.getLocWithOffset(FilenameTok.getLength());
1322 // This could be a <foo/bar.h> file coming from a macro expansion. In this
1323 // case, glue the tokens together into FilenameBuffer and interpret those.
1324 FilenameBuffer.push_back('<');
1325 if (ConcatenateIncludeName(FilenameBuffer, End))
1326 return; // Found <eod> but no ">"? Diagnostic already emitted.
1327 Filename = FilenameBuffer.str();
1328 CharEnd = End.getLocWithOffset(1);
1331 Diag(FilenameTok.getLocation(), diag::err_pp_expects_filename);
1332 DiscardUntilEndOfDirective();
1336 CharSourceRange FilenameRange
1337 = CharSourceRange::getCharRange(FilenameTok.getLocation(), CharEnd);
1338 StringRef OriginalFilename = Filename;
1340 GetIncludeFilenameSpelling(FilenameTok.getLocation(), Filename);
1341 // If GetIncludeFilenameSpelling set the start ptr to null, there was an
1343 if (Filename.empty()) {
1344 DiscardUntilEndOfDirective();
1348 // Verify that there is nothing after the filename, other than EOD. Note that
1349 // we allow macros that expand to nothing after the filename, because this
1350 // falls into the category of "#include pp-tokens new-line" specified in
1352 CheckEndOfDirective(IncludeTok.getIdentifierInfo()->getNameStart(), true);
1354 // Check that we don't have infinite #include recursion.
1355 if (IncludeMacroStack.size() == MaxAllowedIncludeStackDepth-1) {
1356 Diag(FilenameTok, diag::err_pp_include_too_deep);
1360 // Complain about attempts to #include files in an audit pragma.
1361 if (PragmaARCCFCodeAuditedLoc.isValid()) {
1362 Diag(HashLoc, diag::err_pp_include_in_arc_cf_code_audited);
1363 Diag(PragmaARCCFCodeAuditedLoc, diag::note_pragma_entered_here);
1365 // Immediately leave the pragma.
1366 PragmaARCCFCodeAuditedLoc = SourceLocation();
1369 if (HeaderInfo.HasIncludeAliasMap()) {
1370 // Map the filename with the brackets still attached. If the name doesn't
1371 // map to anything, fall back on the filename we've already gotten the
1373 StringRef NewName = HeaderInfo.MapHeaderToIncludeAlias(OriginalFilename);
1374 if (!NewName.empty())
1378 // Search include directories.
1379 const DirectoryLookup *CurDir;
1380 SmallString<1024> SearchPath;
1381 SmallString<1024> RelativePath;
1382 // We get the raw path only if we have 'Callbacks' to which we later pass
1384 Module *SuggestedModule = 0;
1385 const FileEntry *File = LookupFile(
1386 Filename, isAngled, LookupFrom, CurDir,
1387 Callbacks ? &SearchPath : NULL, Callbacks ? &RelativePath : NULL,
1388 getLangOpts().Modules? &SuggestedModule : 0);
1392 // Give the clients a chance to recover.
1393 SmallString<128> RecoveryPath;
1394 if (Callbacks->FileNotFound(Filename, RecoveryPath)) {
1395 if (const DirectoryEntry *DE = FileMgr.getDirectory(RecoveryPath)) {
1396 // Add the recovery path to the list of search paths.
1397 DirectoryLookup DL(DE, SrcMgr::C_User, false);
1398 HeaderInfo.AddSearchPath(DL, isAngled);
1400 // Try the lookup again, skipping the cache.
1401 File = LookupFile(Filename, isAngled, LookupFrom, CurDir, 0, 0,
1402 getLangOpts().Modules? &SuggestedModule : 0,
1408 if (!SuggestedModule) {
1409 // Notify the callback object that we've seen an inclusion directive.
1410 Callbacks->InclusionDirective(HashLoc, IncludeTok, Filename, isAngled,
1411 FilenameRange, File,
1412 SearchPath, RelativePath,
1413 /*ImportedModule=*/0);
1418 if (!SuppressIncludeNotFoundError) {
1419 // If the file could not be located and it was included via angle
1420 // brackets, we can attempt a lookup as though it were a quoted path to
1421 // provide the user with a possible fixit.
1423 File = LookupFile(Filename, false, LookupFrom, CurDir,
1424 Callbacks ? &SearchPath : 0,
1425 Callbacks ? &RelativePath : 0,
1426 getLangOpts().Modules ? &SuggestedModule : 0);
1428 SourceRange Range(FilenameTok.getLocation(), CharEnd);
1429 Diag(FilenameTok, diag::err_pp_file_not_found_not_fatal) <<
1431 FixItHint::CreateReplacement(Range, "\"" + Filename.str() + "\"");
1434 // If the file is still not found, just go with the vanilla diagnostic
1436 Diag(FilenameTok, diag::err_pp_file_not_found) << Filename;
1442 // If we are supposed to import a module rather than including the header,
1444 if (SuggestedModule) {
1445 // Compute the module access path corresponding to this module.
1446 // FIXME: Should we have a second loadModule() overload to avoid this
1447 // extra lookup step?
1448 SmallVector<std::pair<IdentifierInfo *, SourceLocation>, 2> Path;
1449 for (Module *Mod = SuggestedModule; Mod; Mod = Mod->Parent)
1450 Path.push_back(std::make_pair(getIdentifierInfo(Mod->Name),
1451 FilenameTok.getLocation()));
1452 std::reverse(Path.begin(), Path.end());
1454 // Warn that we're replacing the include/import with a module import.
1455 SmallString<128> PathString;
1456 for (unsigned I = 0, N = Path.size(); I != N; ++I) {
1459 PathString += Path[I].first->getName();
1461 int IncludeKind = 0;
1463 switch (IncludeTok.getIdentifierInfo()->getPPKeywordID()) {
1464 case tok::pp_include:
1468 case tok::pp_import:
1472 case tok::pp_include_next:
1476 case tok::pp___include_macros:
1481 llvm_unreachable("unknown include directive kind");
1484 // Determine whether we are actually building the module that this
1485 // include directive maps to.
1486 bool BuildingImportedModule
1487 = Path[0].first->getName() == getLangOpts().CurrentModule;
1489 if (!BuildingImportedModule && getLangOpts().ObjC2) {
1490 // If we're not building the imported module, warn that we're going
1491 // to automatically turn this inclusion directive into a module import.
1492 // We only do this in Objective-C, where we have a module-import syntax.
1493 CharSourceRange ReplaceRange(SourceRange(HashLoc, CharEnd),
1494 /*IsTokenRange=*/false);
1495 Diag(HashLoc, diag::warn_auto_module_import)
1496 << IncludeKind << PathString
1497 << FixItHint::CreateReplacement(ReplaceRange,
1498 "@import " + PathString.str().str() + ";");
1502 // If this was an #__include_macros directive, only make macros visible.
1503 Module::NameVisibilityKind Visibility
1504 = (IncludeKind == 3)? Module::MacrosVisible : Module::AllVisible;
1505 ModuleLoadResult Imported
1506 = TheModuleLoader.loadModule(IncludeTok.getLocation(), Path, Visibility,
1507 /*IsIncludeDirective=*/true);
1508 assert((Imported == 0 || Imported == SuggestedModule) &&
1509 "the imported module is different than the suggested one");
1511 // If this header isn't part of the module we're building, we're done.
1512 if (!BuildingImportedModule && Imported) {
1514 Callbacks->InclusionDirective(HashLoc, IncludeTok, Filename, isAngled,
1515 FilenameRange, File,
1516 SearchPath, RelativePath, Imported);
1521 // If we failed to find a submodule that we expected to find, we can
1522 // continue. Otherwise, there's an error in the included file, so we
1523 // don't want to include it.
1524 if (!BuildingImportedModule && !Imported.isMissingExpected()) {
1529 if (Callbacks && SuggestedModule) {
1530 // We didn't notify the callback object that we've seen an inclusion
1531 // directive before. Now that we are parsing the include normally and not
1532 // turning it to a module import, notify the callback object.
1533 Callbacks->InclusionDirective(HashLoc, IncludeTok, Filename, isAngled,
1534 FilenameRange, File,
1535 SearchPath, RelativePath,
1536 /*ImportedModule=*/0);
1539 // The #included file will be considered to be a system header if either it is
1540 // in a system include directory, or if the #includer is a system include
1542 SrcMgr::CharacteristicKind FileCharacter =
1543 std::max(HeaderInfo.getFileDirFlavor(File),
1544 SourceMgr.getFileCharacteristic(FilenameTok.getLocation()));
1546 // Ask HeaderInfo if we should enter this #include file. If not, #including
1547 // this file will have no effect.
1548 if (!HeaderInfo.ShouldEnterIncludeFile(File, isImport)) {
1550 Callbacks->FileSkipped(*File, FilenameTok, FileCharacter);
1554 // Look up the file, create a File ID for it.
1555 SourceLocation IncludePos = End;
1556 // If the filename string was the result of macro expansions, set the include
1557 // position on the file where it will be included and after the expansions.
1558 if (IncludePos.isMacroID())
1559 IncludePos = SourceMgr.getExpansionRange(IncludePos).second;
1560 FileID FID = SourceMgr.createFileID(File, IncludePos, FileCharacter);
1561 assert(!FID.isInvalid() && "Expected valid file ID");
1563 // Finally, if all is good, enter the new file!
1564 EnterSourceFile(FID, CurDir, FilenameTok.getLocation());
1567 /// HandleIncludeNextDirective - Implements \#include_next.
1569 void Preprocessor::HandleIncludeNextDirective(SourceLocation HashLoc,
1570 Token &IncludeNextTok) {
1571 Diag(IncludeNextTok, diag::ext_pp_include_next_directive);
1573 // #include_next is like #include, except that we start searching after
1574 // the current found directory. If we can't do this, issue a
1576 const DirectoryLookup *Lookup = CurDirLookup;
1577 if (isInPrimaryFile()) {
1579 Diag(IncludeNextTok, diag::pp_include_next_in_primary);
1580 } else if (Lookup == 0) {
1581 Diag(IncludeNextTok, diag::pp_include_next_absolute_path);
1583 // Start looking up in the next directory.
1587 return HandleIncludeDirective(HashLoc, IncludeNextTok, Lookup);
1590 /// HandleMicrosoftImportDirective - Implements \#import for Microsoft Mode
1591 void Preprocessor::HandleMicrosoftImportDirective(Token &Tok) {
1592 // The Microsoft #import directive takes a type library and generates header
1593 // files from it, and includes those. This is beyond the scope of what clang
1594 // does, so we ignore it and error out. However, #import can optionally have
1595 // trailing attributes that span multiple lines. We're going to eat those
1596 // so we can continue processing from there.
1597 Diag(Tok, diag::err_pp_import_directive_ms );
1599 // Read tokens until we get to the end of the directive. Note that the
1600 // directive can be split over multiple lines using the backslash character.
1601 DiscardUntilEndOfDirective();
1604 /// HandleImportDirective - Implements \#import.
1606 void Preprocessor::HandleImportDirective(SourceLocation HashLoc,
1608 if (!LangOpts.ObjC1) { // #import is standard for ObjC.
1609 if (LangOpts.MicrosoftMode)
1610 return HandleMicrosoftImportDirective(ImportTok);
1611 Diag(ImportTok, diag::ext_pp_import_directive);
1613 return HandleIncludeDirective(HashLoc, ImportTok, 0, true);
1616 /// HandleIncludeMacrosDirective - The -imacros command line option turns into a
1617 /// pseudo directive in the predefines buffer. This handles it by sucking all
1618 /// tokens through the preprocessor and discarding them (only keeping the side
1619 /// effects on the preprocessor).
1620 void Preprocessor::HandleIncludeMacrosDirective(SourceLocation HashLoc,
1621 Token &IncludeMacrosTok) {
1622 // This directive should only occur in the predefines buffer. If not, emit an
1623 // error and reject it.
1624 SourceLocation Loc = IncludeMacrosTok.getLocation();
1625 if (strcmp(SourceMgr.getBufferName(Loc), "<built-in>") != 0) {
1626 Diag(IncludeMacrosTok.getLocation(),
1627 diag::pp_include_macros_out_of_predefines);
1628 DiscardUntilEndOfDirective();
1632 // Treat this as a normal #include for checking purposes. If this is
1633 // successful, it will push a new lexer onto the include stack.
1634 HandleIncludeDirective(HashLoc, IncludeMacrosTok, 0, false);
1639 assert(TmpTok.isNot(tok::eof) && "Didn't find end of -imacros!");
1640 } while (TmpTok.isNot(tok::hashhash));
1643 //===----------------------------------------------------------------------===//
1644 // Preprocessor Macro Directive Handling.
1645 //===----------------------------------------------------------------------===//
1647 /// ReadMacroDefinitionArgList - The ( starting an argument list of a macro
1648 /// definition has just been read. Lex the rest of the arguments and the
1649 /// closing ), updating MI with what we learn. Return true if an error occurs
1650 /// parsing the arg list.
1651 bool Preprocessor::ReadMacroDefinitionArgList(MacroInfo *MI, Token &Tok) {
1652 SmallVector<IdentifierInfo*, 32> Arguments;
1655 LexUnexpandedToken(Tok);
1656 switch (Tok.getKind()) {
1658 // Found the end of the argument list.
1659 if (Arguments.empty()) // #define FOO()
1661 // Otherwise we have #define FOO(A,)
1662 Diag(Tok, diag::err_pp_expected_ident_in_arg_list);
1664 case tok::ellipsis: // #define X(... -> C99 varargs
1666 Diag(Tok, LangOpts.CPlusPlus11 ?
1667 diag::warn_cxx98_compat_variadic_macro :
1668 diag::ext_variadic_macro);
1670 // OpenCL v1.2 s6.9.e: variadic macros are not supported.
1671 if (LangOpts.OpenCL) {
1672 Diag(Tok, diag::err_pp_opencl_variadic_macros);
1676 // Lex the token after the identifier.
1677 LexUnexpandedToken(Tok);
1678 if (Tok.isNot(tok::r_paren)) {
1679 Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
1682 // Add the __VA_ARGS__ identifier as an argument.
1683 Arguments.push_back(Ident__VA_ARGS__);
1684 MI->setIsC99Varargs();
1685 MI->setArgumentList(&Arguments[0], Arguments.size(), BP);
1687 case tok::eod: // #define X(
1688 Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
1691 // Handle keywords and identifiers here to accept things like
1692 // #define Foo(for) for.
1693 IdentifierInfo *II = Tok.getIdentifierInfo();
1696 Diag(Tok, diag::err_pp_invalid_tok_in_arg_list);
1700 // If this is already used as an argument, it is used multiple times (e.g.
1702 if (std::find(Arguments.begin(), Arguments.end(), II) !=
1703 Arguments.end()) { // C99 6.10.3p6
1704 Diag(Tok, diag::err_pp_duplicate_name_in_arg_list) << II;
1708 // Add the argument to the macro info.
1709 Arguments.push_back(II);
1711 // Lex the token after the identifier.
1712 LexUnexpandedToken(Tok);
1714 switch (Tok.getKind()) {
1715 default: // #define X(A B
1716 Diag(Tok, diag::err_pp_expected_comma_in_arg_list);
1718 case tok::r_paren: // #define X(A)
1719 MI->setArgumentList(&Arguments[0], Arguments.size(), BP);
1721 case tok::comma: // #define X(A,
1723 case tok::ellipsis: // #define X(A... -> GCC extension
1724 // Diagnose extension.
1725 Diag(Tok, diag::ext_named_variadic_macro);
1727 // Lex the token after the identifier.
1728 LexUnexpandedToken(Tok);
1729 if (Tok.isNot(tok::r_paren)) {
1730 Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
1734 MI->setIsGNUVarargs();
1735 MI->setArgumentList(&Arguments[0], Arguments.size(), BP);
1742 /// HandleDefineDirective - Implements \#define. This consumes the entire macro
1743 /// line then lets the caller lex the next real token.
1744 void Preprocessor::HandleDefineDirective(Token &DefineTok) {
1748 ReadMacroName(MacroNameTok, 1);
1750 // Error reading macro name? If so, diagnostic already issued.
1751 if (MacroNameTok.is(tok::eod))
1754 Token LastTok = MacroNameTok;
1756 // If we are supposed to keep comments in #defines, reenable comment saving
1758 if (CurLexer) CurLexer->SetCommentRetentionState(KeepMacroComments);
1760 // Create the new macro.
1761 MacroInfo *MI = AllocateMacroInfo(MacroNameTok.getLocation());
1764 LexUnexpandedToken(Tok);
1766 // If this is a function-like macro definition, parse the argument list,
1767 // marking each of the identifiers as being used as macro arguments. Also,
1768 // check other constraints on the first token of the macro body.
1769 if (Tok.is(tok::eod)) {
1770 // If there is no body to this macro, we have no special handling here.
1771 } else if (Tok.hasLeadingSpace()) {
1772 // This is a normal token with leading space. Clear the leading space
1773 // marker on the first token to get proper expansion.
1774 Tok.clearFlag(Token::LeadingSpace);
1775 } else if (Tok.is(tok::l_paren)) {
1776 // This is a function-like macro definition. Read the argument list.
1777 MI->setIsFunctionLike();
1778 if (ReadMacroDefinitionArgList(MI, LastTok)) {
1780 ReleaseMacroInfo(MI);
1781 // Throw away the rest of the line.
1782 if (CurPPLexer->ParsingPreprocessorDirective)
1783 DiscardUntilEndOfDirective();
1787 // If this is a definition of a variadic C99 function-like macro, not using
1788 // the GNU named varargs extension, enabled __VA_ARGS__.
1790 // "Poison" __VA_ARGS__, which can only appear in the expansion of a macro.
1791 // This gets unpoisoned where it is allowed.
1792 assert(Ident__VA_ARGS__->isPoisoned() && "__VA_ARGS__ should be poisoned!");
1793 if (MI->isC99Varargs())
1794 Ident__VA_ARGS__->setIsPoisoned(false);
1796 // Read the first token after the arg list for down below.
1797 LexUnexpandedToken(Tok);
1798 } else if (LangOpts.C99 || LangOpts.CPlusPlus11) {
1799 // C99 requires whitespace between the macro definition and the body. Emit
1800 // a diagnostic for something like "#define X+".
1801 Diag(Tok, diag::ext_c99_whitespace_required_after_macro_name);
1803 // C90 6.8 TC1 says: "In the definition of an object-like macro, if the
1804 // first character of a replacement list is not a character required by
1805 // subclause 5.2.1, then there shall be white-space separation between the
1806 // identifier and the replacement list.". 5.2.1 lists this set:
1807 // "A-Za-z0-9!"#%&'()*+,_./:;<=>?[\]^_{|}~" as well as whitespace, which
1808 // is irrelevant here.
1809 bool isInvalid = false;
1810 if (Tok.is(tok::at)) // @ is not in the list above.
1812 else if (Tok.is(tok::unknown)) {
1813 // If we have an unknown token, it is something strange like "`". Since
1814 // all of valid characters would have lexed into a single character
1815 // token of some sort, we know this is not a valid case.
1819 Diag(Tok, diag::ext_missing_whitespace_after_macro_name);
1821 Diag(Tok, diag::warn_missing_whitespace_after_macro_name);
1824 if (!Tok.is(tok::eod))
1827 // Read the rest of the macro body.
1828 if (MI->isObjectLike()) {
1829 // Object-like macros are very simple, just read their body.
1830 while (Tok.isNot(tok::eod)) {
1832 MI->AddTokenToBody(Tok);
1833 // Get the next token of the macro.
1834 LexUnexpandedToken(Tok);
1838 // Otherwise, read the body of a function-like macro. While we are at it,
1839 // check C99 6.10.3.2p1: ensure that # operators are followed by macro
1840 // parameters in function-like macro expansions.
1841 while (Tok.isNot(tok::eod)) {
1844 if (Tok.isNot(tok::hash) && Tok.isNot(tok::hashhash)) {
1845 MI->AddTokenToBody(Tok);
1847 // Get the next token of the macro.
1848 LexUnexpandedToken(Tok);
1852 if (Tok.is(tok::hashhash)) {
1854 // If we see token pasting, check if it looks like the gcc comma
1855 // pasting extension. We'll use this information to suppress
1856 // diagnostics later on.
1858 // Get the next token of the macro.
1859 LexUnexpandedToken(Tok);
1861 if (Tok.is(tok::eod)) {
1862 MI->AddTokenToBody(LastTok);
1866 unsigned NumTokens = MI->getNumTokens();
1867 if (NumTokens && Tok.getIdentifierInfo() == Ident__VA_ARGS__ &&
1868 MI->getReplacementToken(NumTokens-1).is(tok::comma))
1869 MI->setHasCommaPasting();
1871 // Things look ok, add the '##' and param name tokens to the macro.
1872 MI->AddTokenToBody(LastTok);
1873 MI->AddTokenToBody(Tok);
1876 // Get the next token of the macro.
1877 LexUnexpandedToken(Tok);
1881 // Get the next token of the macro.
1882 LexUnexpandedToken(Tok);
1884 // Check for a valid macro arg identifier.
1885 if (Tok.getIdentifierInfo() == 0 ||
1886 MI->getArgumentNum(Tok.getIdentifierInfo()) == -1) {
1888 // If this is assembler-with-cpp mode, we accept random gibberish after
1889 // the '#' because '#' is often a comment character. However, change
1890 // the kind of the token to tok::unknown so that the preprocessor isn't
1892 if (getLangOpts().AsmPreprocessor && Tok.isNot(tok::eod)) {
1893 LastTok.setKind(tok::unknown);
1895 Diag(Tok, diag::err_pp_stringize_not_parameter);
1896 ReleaseMacroInfo(MI);
1898 // Disable __VA_ARGS__ again.
1899 Ident__VA_ARGS__->setIsPoisoned(true);
1904 // Things look ok, add the '#' and param name tokens to the macro.
1905 MI->AddTokenToBody(LastTok);
1906 MI->AddTokenToBody(Tok);
1909 // Get the next token of the macro.
1910 LexUnexpandedToken(Tok);
1915 // Disable __VA_ARGS__ again.
1916 Ident__VA_ARGS__->setIsPoisoned(true);
1918 // Check that there is no paste (##) operator at the beginning or end of the
1919 // replacement list.
1920 unsigned NumTokens = MI->getNumTokens();
1921 if (NumTokens != 0) {
1922 if (MI->getReplacementToken(0).is(tok::hashhash)) {
1923 Diag(MI->getReplacementToken(0), diag::err_paste_at_start);
1924 ReleaseMacroInfo(MI);
1927 if (MI->getReplacementToken(NumTokens-1).is(tok::hashhash)) {
1928 Diag(MI->getReplacementToken(NumTokens-1), diag::err_paste_at_end);
1929 ReleaseMacroInfo(MI);
1934 MI->setDefinitionEndLoc(LastTok.getLocation());
1936 // Finally, if this identifier already had a macro defined for it, verify that
1937 // the macro bodies are identical, and issue diagnostics if they are not.
1938 if (const MacroInfo *OtherMI=getMacroInfo(MacroNameTok.getIdentifierInfo())) {
1939 // It is very common for system headers to have tons of macro redefinitions
1940 // and for warnings to be disabled in system headers. If this is the case,
1941 // then don't bother calling MacroInfo::isIdenticalTo.
1942 if (!getDiagnostics().getSuppressSystemWarnings() ||
1943 !SourceMgr.isInSystemHeader(DefineTok.getLocation())) {
1944 if (!OtherMI->isUsed() && OtherMI->isWarnIfUnused())
1945 Diag(OtherMI->getDefinitionLoc(), diag::pp_macro_not_used);
1947 // Warn if defining "__LINE__" and other builtins, per C99 6.10.8/4 and
1948 // C++ [cpp.predefined]p4, but allow it as an extension.
1949 if (OtherMI->isBuiltinMacro())
1950 Diag(MacroNameTok, diag::ext_pp_redef_builtin_macro);
1951 // Macros must be identical. This means all tokens and whitespace
1952 // separation must be the same. C99 6.10.3p2.
1953 else if (!OtherMI->isAllowRedefinitionsWithoutWarning() &&
1954 !MI->isIdenticalTo(*OtherMI, *this, /*Syntactic=*/LangOpts.MicrosoftExt)) {
1955 Diag(MI->getDefinitionLoc(), diag::ext_pp_macro_redef)
1956 << MacroNameTok.getIdentifierInfo();
1957 Diag(OtherMI->getDefinitionLoc(), diag::note_previous_definition);
1960 if (OtherMI->isWarnIfUnused())
1961 WarnUnusedMacroLocs.erase(OtherMI->getDefinitionLoc());
1964 DefMacroDirective *MD =
1965 appendDefMacroDirective(MacroNameTok.getIdentifierInfo(), MI);
1967 assert(!MI->isUsed());
1968 // If we need warning for not using the macro, add its location in the
1969 // warn-because-unused-macro set. If it gets used it will be removed from set.
1970 if (isInPrimaryFile() && // don't warn for include'd macros.
1971 Diags->getDiagnosticLevel(diag::pp_macro_not_used,
1972 MI->getDefinitionLoc()) != DiagnosticsEngine::Ignored) {
1973 MI->setIsWarnIfUnused(true);
1974 WarnUnusedMacroLocs.insert(MI->getDefinitionLoc());
1977 // If the callbacks want to know, tell them about the macro definition.
1979 Callbacks->MacroDefined(MacroNameTok, MD);
1982 /// HandleUndefDirective - Implements \#undef.
1984 void Preprocessor::HandleUndefDirective(Token &UndefTok) {
1988 ReadMacroName(MacroNameTok, 2);
1990 // Error reading macro name? If so, diagnostic already issued.
1991 if (MacroNameTok.is(tok::eod))
1994 // Check to see if this is the last token on the #undef line.
1995 CheckEndOfDirective("undef");
1997 // Okay, we finally have a valid identifier to undef.
1998 MacroDirective *MD = getMacroDirective(MacroNameTok.getIdentifierInfo());
1999 const MacroInfo *MI = MD ? MD->getMacroInfo() : 0;
2001 // If the callbacks want to know, tell them about the macro #undef.
2002 // Note: no matter if the macro was defined or not.
2004 Callbacks->MacroUndefined(MacroNameTok, MD);
2006 // If the macro is not defined, this is a noop undef, just return.
2007 if (MI == 0) return;
2009 if (!MI->isUsed() && MI->isWarnIfUnused())
2010 Diag(MI->getDefinitionLoc(), diag::pp_macro_not_used);
2012 if (MI->isWarnIfUnused())
2013 WarnUnusedMacroLocs.erase(MI->getDefinitionLoc());
2015 appendMacroDirective(MacroNameTok.getIdentifierInfo(),
2016 AllocateUndefMacroDirective(MacroNameTok.getLocation()));
2020 //===----------------------------------------------------------------------===//
2021 // Preprocessor Conditional Directive Handling.
2022 //===----------------------------------------------------------------------===//
2024 /// HandleIfdefDirective - Implements the \#ifdef/\#ifndef directive. isIfndef
2025 /// is true when this is a \#ifndef directive. ReadAnyTokensBeforeDirective is
2026 /// true if any tokens have been returned or pp-directives activated before this
2027 /// \#ifndef has been lexed.
2029 void Preprocessor::HandleIfdefDirective(Token &Result, bool isIfndef,
2030 bool ReadAnyTokensBeforeDirective) {
2032 Token DirectiveTok = Result;
2035 ReadMacroName(MacroNameTok);
2037 // Error reading macro name? If so, diagnostic already issued.
2038 if (MacroNameTok.is(tok::eod)) {
2039 // Skip code until we get to #endif. This helps with recovery by not
2040 // emitting an error when the #endif is reached.
2041 SkipExcludedConditionalBlock(DirectiveTok.getLocation(),
2042 /*Foundnonskip*/false, /*FoundElse*/false);
2046 // Check to see if this is the last token on the #if[n]def line.
2047 CheckEndOfDirective(isIfndef ? "ifndef" : "ifdef");
2049 IdentifierInfo *MII = MacroNameTok.getIdentifierInfo();
2050 MacroDirective *MD = getMacroDirective(MII);
2051 MacroInfo *MI = MD ? MD->getMacroInfo() : 0;
2053 if (CurPPLexer->getConditionalStackDepth() == 0) {
2054 // If the start of a top-level #ifdef and if the macro is not defined,
2055 // inform MIOpt that this might be the start of a proper include guard.
2056 // Otherwise it is some other form of unknown conditional which we can't
2058 if (!ReadAnyTokensBeforeDirective && MI == 0) {
2059 assert(isIfndef && "#ifdef shouldn't reach here");
2060 CurPPLexer->MIOpt.EnterTopLevelIFNDEF(MII);
2062 CurPPLexer->MIOpt.EnterTopLevelConditional();
2065 // If there is a macro, process it.
2066 if (MI) // Mark it used.
2067 markMacroAsUsed(MI);
2071 Callbacks->Ifndef(DirectiveTok.getLocation(), MacroNameTok, MD);
2073 Callbacks->Ifdef(DirectiveTok.getLocation(), MacroNameTok, MD);
2076 // Should we include the stuff contained by this directive?
2077 if (!MI == isIfndef) {
2078 // Yes, remember that we are inside a conditional, then lex the next token.
2079 CurPPLexer->pushConditionalLevel(DirectiveTok.getLocation(),
2080 /*wasskip*/false, /*foundnonskip*/true,
2081 /*foundelse*/false);
2083 // No, skip the contents of this block.
2084 SkipExcludedConditionalBlock(DirectiveTok.getLocation(),
2085 /*Foundnonskip*/false,
2086 /*FoundElse*/false);
2090 /// HandleIfDirective - Implements the \#if directive.
2092 void Preprocessor::HandleIfDirective(Token &IfToken,
2093 bool ReadAnyTokensBeforeDirective) {
2096 // Parse and evaluate the conditional expression.
2097 IdentifierInfo *IfNDefMacro = 0;
2098 const SourceLocation ConditionalBegin = CurPPLexer->getSourceLocation();
2099 const bool ConditionalTrue = EvaluateDirectiveExpression(IfNDefMacro);
2100 const SourceLocation ConditionalEnd = CurPPLexer->getSourceLocation();
2102 // If this condition is equivalent to #ifndef X, and if this is the first
2103 // directive seen, handle it for the multiple-include optimization.
2104 if (CurPPLexer->getConditionalStackDepth() == 0) {
2105 if (!ReadAnyTokensBeforeDirective && IfNDefMacro && ConditionalTrue)
2106 CurPPLexer->MIOpt.EnterTopLevelIFNDEF(IfNDefMacro);
2108 CurPPLexer->MIOpt.EnterTopLevelConditional();
2112 Callbacks->If(IfToken.getLocation(),
2113 SourceRange(ConditionalBegin, ConditionalEnd));
2115 // Should we include the stuff contained by this directive?
2116 if (ConditionalTrue) {
2117 // Yes, remember that we are inside a conditional, then lex the next token.
2118 CurPPLexer->pushConditionalLevel(IfToken.getLocation(), /*wasskip*/false,
2119 /*foundnonskip*/true, /*foundelse*/false);
2121 // No, skip the contents of this block.
2122 SkipExcludedConditionalBlock(IfToken.getLocation(), /*Foundnonskip*/false,
2123 /*FoundElse*/false);
2127 /// HandleEndifDirective - Implements the \#endif directive.
2129 void Preprocessor::HandleEndifDirective(Token &EndifToken) {
2132 // Check that this is the whole directive.
2133 CheckEndOfDirective("endif");
2135 PPConditionalInfo CondInfo;
2136 if (CurPPLexer->popConditionalLevel(CondInfo)) {
2137 // No conditionals on the stack: this is an #endif without an #if.
2138 Diag(EndifToken, diag::err_pp_endif_without_if);
2142 // If this the end of a top-level #endif, inform MIOpt.
2143 if (CurPPLexer->getConditionalStackDepth() == 0)
2144 CurPPLexer->MIOpt.ExitTopLevelConditional();
2146 assert(!CondInfo.WasSkipping && !CurPPLexer->LexingRawMode &&
2147 "This code should only be reachable in the non-skipping case!");
2150 Callbacks->Endif(EndifToken.getLocation(), CondInfo.IfLoc);
2153 /// HandleElseDirective - Implements the \#else directive.
2155 void Preprocessor::HandleElseDirective(Token &Result) {
2158 // #else directive in a non-skipping conditional... start skipping.
2159 CheckEndOfDirective("else");
2161 PPConditionalInfo CI;
2162 if (CurPPLexer->popConditionalLevel(CI)) {
2163 Diag(Result, diag::pp_err_else_without_if);
2167 // If this is a top-level #else, inform the MIOpt.
2168 if (CurPPLexer->getConditionalStackDepth() == 0)
2169 CurPPLexer->MIOpt.EnterTopLevelConditional();
2171 // If this is a #else with a #else before it, report the error.
2172 if (CI.FoundElse) Diag(Result, diag::pp_err_else_after_else);
2175 Callbacks->Else(Result.getLocation(), CI.IfLoc);
2177 // Finally, skip the rest of the contents of this block.
2178 SkipExcludedConditionalBlock(CI.IfLoc, /*Foundnonskip*/true,
2179 /*FoundElse*/true, Result.getLocation());
2182 /// HandleElifDirective - Implements the \#elif directive.
2184 void Preprocessor::HandleElifDirective(Token &ElifToken) {
2187 // #elif directive in a non-skipping conditional... start skipping.
2188 // We don't care what the condition is, because we will always skip it (since
2189 // the block immediately before it was included).
2190 const SourceLocation ConditionalBegin = CurPPLexer->getSourceLocation();
2191 DiscardUntilEndOfDirective();
2192 const SourceLocation ConditionalEnd = CurPPLexer->getSourceLocation();
2194 PPConditionalInfo CI;
2195 if (CurPPLexer->popConditionalLevel(CI)) {
2196 Diag(ElifToken, diag::pp_err_elif_without_if);
2200 // If this is a top-level #elif, inform the MIOpt.
2201 if (CurPPLexer->getConditionalStackDepth() == 0)
2202 CurPPLexer->MIOpt.EnterTopLevelConditional();
2204 // If this is a #elif with a #else before it, report the error.
2205 if (CI.FoundElse) Diag(ElifToken, diag::pp_err_elif_after_else);
2208 Callbacks->Elif(ElifToken.getLocation(),
2209 SourceRange(ConditionalBegin, ConditionalEnd), CI.IfLoc);
2211 // Finally, skip the rest of the contents of this block.
2212 SkipExcludedConditionalBlock(CI.IfLoc, /*Foundnonskip*/true,
2213 /*FoundElse*/CI.FoundElse,
2214 ElifToken.getLocation());