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");
64 DeserializedMacroInfoChain *MIChain =
65 BP.Allocate<DeserializedMacroInfoChain>();
66 MIChain->Next = DeserialMIChainHead;
67 DeserialMIChainHead = MIChain;
69 MacroInfo *MI = &MIChain->MI;
70 new (MI) MacroInfo(L);
71 MI->FromASTFile = true;
72 MI->setOwningModuleID(SubModuleID);
77 Preprocessor::AllocateDefMacroDirective(MacroInfo *MI, SourceLocation Loc,
79 DefMacroDirective *MD = BP.Allocate<DefMacroDirective>();
80 new (MD) DefMacroDirective(MI, Loc, isImported);
85 Preprocessor::AllocateUndefMacroDirective(SourceLocation UndefLoc) {
86 UndefMacroDirective *MD = BP.Allocate<UndefMacroDirective>();
87 new (MD) UndefMacroDirective(UndefLoc);
91 VisibilityMacroDirective *
92 Preprocessor::AllocateVisibilityMacroDirective(SourceLocation Loc,
94 VisibilityMacroDirective *MD = BP.Allocate<VisibilityMacroDirective>();
95 new (MD) VisibilityMacroDirective(Loc, isPublic);
99 /// \brief Release the specified MacroInfo to be reused for allocating
100 /// new MacroInfo objects.
101 void Preprocessor::ReleaseMacroInfo(MacroInfo *MI) {
102 MacroInfoChain *MIChain = (MacroInfoChain*) MI;
103 if (MacroInfoChain *Prev = MIChain->Prev) {
104 MacroInfoChain *Next = MIChain->Next;
110 assert(MIChainHead == MIChain);
111 MIChainHead = MIChain->Next;
112 MIChainHead->Prev = 0;
114 MIChain->Next = MICache;
120 /// \brief Read and discard all tokens remaining on the current line until
121 /// the tok::eod token is found.
122 void Preprocessor::DiscardUntilEndOfDirective() {
125 LexUnexpandedToken(Tmp);
126 assert(Tmp.isNot(tok::eof) && "EOF seen while discarding directive tokens");
127 } while (Tmp.isNot(tok::eod));
130 /// \brief Lex and validate a macro name, which occurs after a
131 /// \#define or \#undef.
133 /// This sets the token kind to eod and discards the rest
134 /// of the macro line if the macro name is invalid. \p isDefineUndef is 1 if
135 /// this is due to a a \#define, 2 if \#undef directive, 0 if it is something
136 /// else (e.g. \#ifdef).
137 void Preprocessor::ReadMacroName(Token &MacroNameTok, char isDefineUndef) {
138 // Read the token, don't allow macro expansion on it.
139 LexUnexpandedToken(MacroNameTok);
141 if (MacroNameTok.is(tok::code_completion)) {
143 CodeComplete->CodeCompleteMacroName(isDefineUndef == 1);
144 setCodeCompletionReached();
145 LexUnexpandedToken(MacroNameTok);
148 // Missing macro name?
149 if (MacroNameTok.is(tok::eod)) {
150 Diag(MacroNameTok, diag::err_pp_missing_macro_name);
154 IdentifierInfo *II = MacroNameTok.getIdentifierInfo();
156 bool Invalid = false;
157 std::string Spelling = getSpelling(MacroNameTok, &Invalid);
161 const IdentifierInfo &Info = Identifiers.get(Spelling);
163 // Allow #defining |and| and friends in microsoft mode.
164 if (Info.isCPlusPlusOperatorKeyword() && getLangOpts().MicrosoftMode) {
165 MacroNameTok.setIdentifierInfo(getIdentifierInfo(Spelling));
169 if (Info.isCPlusPlusOperatorKeyword())
170 // C++ 2.5p2: Alternative tokens behave the same as its primary token
171 // except for their spellings.
172 Diag(MacroNameTok, diag::err_pp_operator_used_as_macro_name) << Spelling;
174 Diag(MacroNameTok, diag::err_pp_macro_not_identifier);
175 // Fall through on error.
176 } else if (isDefineUndef && II->getPPKeywordID() == tok::pp_defined) {
177 // Error if defining "defined": C99 6.10.8/4, C++ [cpp.predefined]p4.
178 Diag(MacroNameTok, diag::err_defined_macro_name);
179 } else if (isDefineUndef == 2 && II->hasMacroDefinition() &&
180 getMacroInfo(II)->isBuiltinMacro()) {
181 // Warn if undefining "__LINE__" and other builtins, per C99 6.10.8/4
182 // and C++ [cpp.predefined]p4], but allow it as an extension.
183 Diag(MacroNameTok, diag::ext_pp_undef_builtin_macro);
186 // Okay, we got a good identifier node. Return it.
190 // Invalid macro name, read and discard the rest of the line. Then set the
191 // token kind to tok::eod.
192 MacroNameTok.setKind(tok::eod);
193 return DiscardUntilEndOfDirective();
196 /// \brief Ensure that the next token is a tok::eod token.
198 /// If not, emit a diagnostic and consume up until the eod. If EnableMacros is
199 /// true, then we consider macros that expand to zero tokens as being ok.
200 void Preprocessor::CheckEndOfDirective(const char *DirType, bool EnableMacros) {
202 // Lex unexpanded tokens for most directives: macros might expand to zero
203 // tokens, causing us to miss diagnosing invalid lines. Some directives (like
204 // #line) allow empty macros.
208 LexUnexpandedToken(Tmp);
210 // There should be no tokens after the directive, but we allow them as an
212 while (Tmp.is(tok::comment)) // Skip comments in -C mode.
213 LexUnexpandedToken(Tmp);
215 if (Tmp.isNot(tok::eod)) {
216 // Add a fixit in GNU/C99/C++ mode. Don't offer a fixit for strict-C89,
217 // or if this is a macro-style preprocessing directive, because it is more
218 // trouble than it is worth to insert /**/ and check that there is no /**/
219 // in the range also.
221 if ((LangOpts.GNUMode || LangOpts.C99 || LangOpts.CPlusPlus) &&
223 Hint = FixItHint::CreateInsertion(Tmp.getLocation(),"//");
224 Diag(Tmp, diag::ext_pp_extra_tokens_at_eol) << DirType << Hint;
225 DiscardUntilEndOfDirective();
231 /// SkipExcludedConditionalBlock - We just read a \#if or related directive and
232 /// decided that the subsequent tokens are in the \#if'd out portion of the
233 /// file. Lex the rest of the file, until we see an \#endif. If
234 /// FoundNonSkipPortion is true, then we have already emitted code for part of
235 /// this \#if directive, so \#else/\#elif blocks should never be entered.
236 /// If ElseOk is true, then \#else directives are ok, if not, then we have
237 /// already seen one so a \#else directive is a duplicate. When this returns,
238 /// the caller can lex the first valid token.
239 void Preprocessor::SkipExcludedConditionalBlock(SourceLocation IfTokenLoc,
240 bool FoundNonSkipPortion,
242 SourceLocation ElseLoc) {
244 assert(CurTokenLexer == 0 && CurPPLexer && "Lexing a macro, not a file?");
246 CurPPLexer->pushConditionalLevel(IfTokenLoc, /*isSkipping*/false,
247 FoundNonSkipPortion, FoundElse);
250 PTHSkipExcludedConditionalBlock();
254 // Enter raw mode to disable identifier lookup (and thus macro expansion),
255 // disabling warnings, etc.
256 CurPPLexer->LexingRawMode = true;
261 if (Tok.is(tok::code_completion)) {
263 CodeComplete->CodeCompleteInConditionalExclusion();
264 setCodeCompletionReached();
268 // If this is the end of the buffer, we have an error.
269 if (Tok.is(tok::eof)) {
270 // Emit errors for each unterminated conditional on the stack, including
272 while (!CurPPLexer->ConditionalStack.empty()) {
273 if (CurLexer->getFileLoc() != CodeCompletionFileLoc)
274 Diag(CurPPLexer->ConditionalStack.back().IfLoc,
275 diag::err_pp_unterminated_conditional);
276 CurPPLexer->ConditionalStack.pop_back();
279 // Just return and let the caller lex after this #include.
283 // If this token is not a preprocessor directive, just skip it.
284 if (Tok.isNot(tok::hash) || !Tok.isAtStartOfLine())
287 // We just parsed a # character at the start of a line, so we're in
288 // directive mode. Tell the lexer this so any newlines we see will be
289 // converted into an EOD token (this terminates the macro).
290 CurPPLexer->ParsingPreprocessorDirective = true;
291 if (CurLexer) CurLexer->SetKeepWhitespaceMode(false);
294 // Read the next token, the directive flavor.
295 LexUnexpandedToken(Tok);
297 // If this isn't an identifier directive (e.g. is "# 1\n" or "#\n", or
298 // something bogus), skip it.
299 if (Tok.isNot(tok::raw_identifier)) {
300 CurPPLexer->ParsingPreprocessorDirective = false;
301 // Restore comment saving mode.
302 if (CurLexer) CurLexer->resetExtendedTokenMode();
306 // If the first letter isn't i or e, it isn't intesting to us. We know that
307 // this is safe in the face of spelling differences, because there is no way
308 // to spell an i/e in a strange way that is another letter. Skipping this
309 // allows us to avoid looking up the identifier info for #define/#undef and
310 // other common directives.
311 const char *RawCharData = Tok.getRawIdentifierData();
313 char FirstChar = RawCharData[0];
314 if (FirstChar >= 'a' && FirstChar <= 'z' &&
315 FirstChar != 'i' && FirstChar != 'e') {
316 CurPPLexer->ParsingPreprocessorDirective = false;
317 // Restore comment saving mode.
318 if (CurLexer) CurLexer->resetExtendedTokenMode();
322 // Get the identifier name without trigraphs or embedded newlines. Note
323 // that we can't use Tok.getIdentifierInfo() because its lookup is disabled
325 char DirectiveBuf[20];
327 if (!Tok.needsCleaning() && Tok.getLength() < 20) {
328 Directive = StringRef(RawCharData, Tok.getLength());
330 std::string DirectiveStr = getSpelling(Tok);
331 unsigned IdLen = DirectiveStr.size();
333 CurPPLexer->ParsingPreprocessorDirective = false;
334 // Restore comment saving mode.
335 if (CurLexer) CurLexer->resetExtendedTokenMode();
338 memcpy(DirectiveBuf, &DirectiveStr[0], IdLen);
339 Directive = StringRef(DirectiveBuf, IdLen);
342 if (Directive.startswith("if")) {
343 StringRef Sub = Directive.substr(2);
344 if (Sub.empty() || // "if"
345 Sub == "def" || // "ifdef"
346 Sub == "ndef") { // "ifndef"
347 // We know the entire #if/#ifdef/#ifndef block will be skipped, don't
348 // bother parsing the condition.
349 DiscardUntilEndOfDirective();
350 CurPPLexer->pushConditionalLevel(Tok.getLocation(), /*wasskipping*/true,
351 /*foundnonskip*/false,
354 } else if (Directive[0] == 'e') {
355 StringRef Sub = Directive.substr(1);
356 if (Sub == "ndif") { // "endif"
357 PPConditionalInfo CondInfo;
358 CondInfo.WasSkipping = true; // Silence bogus warning.
359 bool InCond = CurPPLexer->popConditionalLevel(CondInfo);
360 (void)InCond; // Silence warning in no-asserts mode.
361 assert(!InCond && "Can't be skipping if not in a conditional!");
363 // If we popped the outermost skipping block, we're done skipping!
364 if (!CondInfo.WasSkipping) {
365 // Restore the value of LexingRawMode so that trailing comments
366 // are handled correctly, if we've reached the outermost block.
367 CurPPLexer->LexingRawMode = false;
368 CheckEndOfDirective("endif");
369 CurPPLexer->LexingRawMode = true;
371 Callbacks->Endif(Tok.getLocation(), CondInfo.IfLoc);
374 DiscardUntilEndOfDirective();
376 } else if (Sub == "lse") { // "else".
377 // #else directive in a skipping conditional. If not in some other
378 // skipping conditional, and if #else hasn't already been seen, enter it
379 // as a non-skipping conditional.
380 PPConditionalInfo &CondInfo = CurPPLexer->peekConditionalLevel();
382 // If this is a #else with a #else before it, report the error.
383 if (CondInfo.FoundElse) Diag(Tok, diag::pp_err_else_after_else);
385 // Note that we've seen a #else in this conditional.
386 CondInfo.FoundElse = true;
388 // If the conditional is at the top level, and the #if block wasn't
389 // entered, enter the #else block now.
390 if (!CondInfo.WasSkipping && !CondInfo.FoundNonSkip) {
391 CondInfo.FoundNonSkip = true;
392 // Restore the value of LexingRawMode so that trailing comments
393 // are handled correctly.
394 CurPPLexer->LexingRawMode = false;
395 CheckEndOfDirective("else");
396 CurPPLexer->LexingRawMode = true;
398 Callbacks->Else(Tok.getLocation(), CondInfo.IfLoc);
401 DiscardUntilEndOfDirective(); // C99 6.10p4.
403 } else if (Sub == "lif") { // "elif".
404 PPConditionalInfo &CondInfo = CurPPLexer->peekConditionalLevel();
407 const SourceLocation ConditionalBegin = CurPPLexer->getSourceLocation();
408 // If this is in a skipping block or if we're already handled this #if
409 // block, don't bother parsing the condition.
410 if (CondInfo.WasSkipping || CondInfo.FoundNonSkip) {
411 DiscardUntilEndOfDirective();
414 // Restore the value of LexingRawMode so that identifiers are
415 // looked up, etc, inside the #elif expression.
416 assert(CurPPLexer->LexingRawMode && "We have to be skipping here!");
417 CurPPLexer->LexingRawMode = false;
418 IdentifierInfo *IfNDefMacro = 0;
419 ShouldEnter = EvaluateDirectiveExpression(IfNDefMacro);
420 CurPPLexer->LexingRawMode = true;
422 const SourceLocation ConditionalEnd = CurPPLexer->getSourceLocation();
424 // If this is a #elif with a #else before it, report the error.
425 if (CondInfo.FoundElse) Diag(Tok, diag::pp_err_elif_after_else);
427 // If this condition is true, enter it!
429 CondInfo.FoundNonSkip = true;
431 Callbacks->Elif(Tok.getLocation(),
432 SourceRange(ConditionalBegin, ConditionalEnd),
439 CurPPLexer->ParsingPreprocessorDirective = false;
440 // Restore comment saving mode.
441 if (CurLexer) CurLexer->resetExtendedTokenMode();
444 // Finally, if we are out of the conditional (saw an #endif or ran off the end
445 // of the file, just stop skipping and return to lexing whatever came after
447 CurPPLexer->LexingRawMode = false;
450 SourceLocation BeginLoc = ElseLoc.isValid() ? ElseLoc : IfTokenLoc;
451 Callbacks->SourceRangeSkipped(SourceRange(BeginLoc, Tok.getLocation()));
455 void Preprocessor::PTHSkipExcludedConditionalBlock() {
459 assert(CurPTHLexer->LexingRawMode == false);
461 // Skip to the next '#else', '#elif', or #endif.
462 if (CurPTHLexer->SkipBlock()) {
463 // We have reached an #endif. Both the '#' and 'endif' tokens
464 // have been consumed by the PTHLexer. Just pop off the condition level.
465 PPConditionalInfo CondInfo;
466 bool InCond = CurPTHLexer->popConditionalLevel(CondInfo);
467 (void)InCond; // Silence warning in no-asserts mode.
468 assert(!InCond && "Can't be skipping if not in a conditional!");
472 // We have reached a '#else' or '#elif'. Lex the next token to get
473 // the directive flavor.
475 LexUnexpandedToken(Tok);
477 // We can actually look up the IdentifierInfo here since we aren't in
479 tok::PPKeywordKind K = Tok.getIdentifierInfo()->getPPKeywordID();
481 if (K == tok::pp_else) {
482 // #else: Enter the else condition. We aren't in a nested condition
483 // since we skip those. We're always in the one matching the last
484 // blocked we skipped.
485 PPConditionalInfo &CondInfo = CurPTHLexer->peekConditionalLevel();
486 // Note that we've seen a #else in this conditional.
487 CondInfo.FoundElse = true;
489 // If the #if block wasn't entered then enter the #else block now.
490 if (!CondInfo.FoundNonSkip) {
491 CondInfo.FoundNonSkip = true;
493 // Scan until the eod token.
494 CurPTHLexer->ParsingPreprocessorDirective = true;
495 DiscardUntilEndOfDirective();
496 CurPTHLexer->ParsingPreprocessorDirective = false;
501 // Otherwise skip this block.
505 assert(K == tok::pp_elif);
506 PPConditionalInfo &CondInfo = CurPTHLexer->peekConditionalLevel();
508 // If this is a #elif with a #else before it, report the error.
509 if (CondInfo.FoundElse)
510 Diag(Tok, diag::pp_err_elif_after_else);
512 // If this is in a skipping block or if we're already handled this #if
513 // block, don't bother parsing the condition. We just skip this block.
514 if (CondInfo.FoundNonSkip)
517 // Evaluate the condition of the #elif.
518 IdentifierInfo *IfNDefMacro = 0;
519 CurPTHLexer->ParsingPreprocessorDirective = true;
520 bool ShouldEnter = EvaluateDirectiveExpression(IfNDefMacro);
521 CurPTHLexer->ParsingPreprocessorDirective = false;
523 // If this condition is true, enter it!
525 CondInfo.FoundNonSkip = true;
529 // Otherwise, skip this block and go to the next one.
534 const FileEntry *Preprocessor::LookupFile(
537 const DirectoryLookup *FromDir,
538 const DirectoryLookup *&CurDir,
539 SmallVectorImpl<char> *SearchPath,
540 SmallVectorImpl<char> *RelativePath,
541 Module **SuggestedModule,
543 // If the header lookup mechanism may be relative to the current file, pass in
544 // info about where the current file is.
545 const FileEntry *CurFileEnt = 0;
547 FileID FID = getCurrentFileLexer()->getFileID();
548 CurFileEnt = SourceMgr.getFileEntryForID(FID);
550 // If there is no file entry associated with this file, it must be the
551 // predefines buffer. Any other file is not lexed with a normal lexer, so
552 // it won't be scanned for preprocessor directives. If we have the
553 // predefines buffer, resolve #include references (which come from the
554 // -include command line argument) as if they came from the main file, this
555 // affects file lookup etc.
556 if (CurFileEnt == 0) {
557 FID = SourceMgr.getMainFileID();
558 CurFileEnt = SourceMgr.getFileEntryForID(FID);
562 // Do a standard file entry lookup.
563 CurDir = CurDirLookup;
564 const FileEntry *FE = HeaderInfo.LookupFile(
565 Filename, isAngled, FromDir, CurDir, CurFileEnt,
566 SearchPath, RelativePath, SuggestedModule, SkipCache);
569 // Otherwise, see if this is a subframework header. If so, this is relative
570 // to one of the headers on the #include stack. Walk the list of the current
571 // headers on the #include stack and pass them to HeaderInfo.
573 if ((CurFileEnt = SourceMgr.getFileEntryForID(CurPPLexer->getFileID())))
574 if ((FE = HeaderInfo.LookupSubframeworkHeader(Filename, CurFileEnt,
575 SearchPath, RelativePath,
580 for (unsigned i = 0, e = IncludeMacroStack.size(); i != e; ++i) {
581 IncludeStackInfo &ISEntry = IncludeMacroStack[e-i-1];
582 if (IsFileLexer(ISEntry)) {
584 SourceMgr.getFileEntryForID(ISEntry.ThePPLexer->getFileID())))
585 if ((FE = HeaderInfo.LookupSubframeworkHeader(
586 Filename, CurFileEnt, SearchPath, RelativePath,
592 // Otherwise, we really couldn't find the file.
597 //===----------------------------------------------------------------------===//
598 // Preprocessor Directive Handling.
599 //===----------------------------------------------------------------------===//
601 class Preprocessor::ResetMacroExpansionHelper {
603 ResetMacroExpansionHelper(Preprocessor *pp)
604 : PP(pp), save(pp->DisableMacroExpansion) {
605 if (pp->MacroExpansionInDirectivesOverride)
606 pp->DisableMacroExpansion = false;
608 ~ResetMacroExpansionHelper() {
609 PP->DisableMacroExpansion = save;
616 /// HandleDirective - This callback is invoked when the lexer sees a # token
617 /// at the start of a line. This consumes the directive, modifies the
618 /// lexer/preprocessor state, and advances the lexer(s) so that the next token
619 /// read is the correct one.
620 void Preprocessor::HandleDirective(Token &Result) {
621 // FIXME: Traditional: # with whitespace before it not recognized by K&R?
623 // We just parsed a # character at the start of a line, so we're in directive
624 // mode. Tell the lexer this so any newlines we see will be converted into an
625 // EOD token (which terminates the directive).
626 CurPPLexer->ParsingPreprocessorDirective = true;
627 if (CurLexer) CurLexer->SetKeepWhitespaceMode(false);
631 // We are about to read a token. For the multiple-include optimization FA to
632 // work, we have to remember if we had read any tokens *before* this
634 bool ReadAnyTokensBeforeDirective =CurPPLexer->MIOpt.getHasReadAnyTokensVal();
636 // Save the '#' token in case we need to return it later.
637 Token SavedHash = Result;
639 // Read the next token, the directive flavor. This isn't expanded due to
641 LexUnexpandedToken(Result);
643 // C99 6.10.3p11: Is this preprocessor directive in macro invocation? e.g.:
648 // If so, the user is relying on undefined behavior, emit a diagnostic. Do
649 // not support this for #include-like directives, since that can result in
650 // terrible diagnostics, and does not work in GCC.
652 if (IdentifierInfo *II = Result.getIdentifierInfo()) {
653 switch (II->getPPKeywordID()) {
654 case tok::pp_include:
656 case tok::pp_include_next:
657 case tok::pp___include_macros:
658 Diag(Result, diag::err_embedded_include) << II->getName();
659 DiscardUntilEndOfDirective();
665 Diag(Result, diag::ext_embedded_directive);
668 // Temporarily enable macro expansion if set so
669 // and reset to previous state when returning from this function.
670 ResetMacroExpansionHelper helper(this);
672 switch (Result.getKind()) {
674 return; // null directive.
675 case tok::code_completion:
677 CodeComplete->CodeCompleteDirective(
678 CurPPLexer->getConditionalStackDepth() > 0);
679 setCodeCompletionReached();
681 case tok::numeric_constant: // # 7 GNU line marker directive.
682 if (getLangOpts().AsmPreprocessor)
683 break; // # 4 is not a preprocessor directive in .S files.
684 return HandleDigitDirective(Result);
686 IdentifierInfo *II = Result.getIdentifierInfo();
687 if (II == 0) break; // Not an identifier.
689 // Ask what the preprocessor keyword ID is.
690 switch (II->getPPKeywordID()) {
692 // C99 6.10.1 - Conditional Inclusion.
694 return HandleIfDirective(Result, ReadAnyTokensBeforeDirective);
696 return HandleIfdefDirective(Result, false, true/*not valid for miopt*/);
698 return HandleIfdefDirective(Result, true, ReadAnyTokensBeforeDirective);
700 return HandleElifDirective(Result);
702 return HandleElseDirective(Result);
704 return HandleEndifDirective(Result);
706 // C99 6.10.2 - Source File Inclusion.
707 case tok::pp_include:
709 return HandleIncludeDirective(SavedHash.getLocation(), Result);
710 case tok::pp___include_macros:
712 return HandleIncludeMacrosDirective(SavedHash.getLocation(), Result);
714 // C99 6.10.3 - Macro Replacement.
716 return HandleDefineDirective(Result);
718 return HandleUndefDirective(Result);
720 // C99 6.10.4 - Line Control.
722 return HandleLineDirective(Result);
724 // C99 6.10.5 - Error Directive.
726 return HandleUserDiagnosticDirective(Result, false);
728 // C99 6.10.6 - Pragma Directive.
730 return HandlePragmaDirective(PIK_HashPragma);
734 return HandleImportDirective(SavedHash.getLocation(), Result);
735 case tok::pp_include_next:
736 return HandleIncludeNextDirective(SavedHash.getLocation(), Result);
738 case tok::pp_warning:
739 Diag(Result, diag::ext_pp_warning_directive);
740 return HandleUserDiagnosticDirective(Result, true);
742 return HandleIdentSCCSDirective(Result);
744 return HandleIdentSCCSDirective(Result);
746 //isExtension = true; // FIXME: implement #assert
748 case tok::pp_unassert:
749 //isExtension = true; // FIXME: implement #unassert
752 case tok::pp___public_macro:
753 if (getLangOpts().Modules)
754 return HandleMacroPublicDirective(Result);
757 case tok::pp___private_macro:
758 if (getLangOpts().Modules)
759 return HandleMacroPrivateDirective(Result);
765 // If this is a .S file, treat unknown # directives as non-preprocessor
766 // directives. This is important because # may be a comment or introduce
767 // various pseudo-ops. Just return the # token and push back the following
768 // token to be lexed next time.
769 if (getLangOpts().AsmPreprocessor) {
770 Token *Toks = new Token[2];
771 // Return the # and the token after it.
775 // If the second token is a hashhash token, then we need to translate it to
776 // unknown so the token lexer doesn't try to perform token pasting.
777 if (Result.is(tok::hashhash))
778 Toks[1].setKind(tok::unknown);
780 // Enter this token stream so that we re-lex the tokens. Make sure to
781 // enable macro expansion, in case the token after the # is an identifier
783 EnterTokenStream(Toks, 2, false, true);
787 // If we reached here, the preprocessing token is not valid!
788 Diag(Result, diag::err_pp_invalid_directive);
790 // Read the rest of the PP line.
791 DiscardUntilEndOfDirective();
793 // Okay, we're done parsing the directive.
796 /// GetLineValue - Convert a numeric token into an unsigned value, emitting
797 /// Diagnostic DiagID if it is invalid, and returning the value in Val.
798 static bool GetLineValue(Token &DigitTok, unsigned &Val,
799 unsigned DiagID, Preprocessor &PP,
800 bool IsGNULineDirective=false) {
801 if (DigitTok.isNot(tok::numeric_constant)) {
802 PP.Diag(DigitTok, DiagID);
804 if (DigitTok.isNot(tok::eod))
805 PP.DiscardUntilEndOfDirective();
809 SmallString<64> IntegerBuffer;
810 IntegerBuffer.resize(DigitTok.getLength());
811 const char *DigitTokBegin = &IntegerBuffer[0];
812 bool Invalid = false;
813 unsigned ActualLength = PP.getSpelling(DigitTok, DigitTokBegin, &Invalid);
817 // Verify that we have a simple digit-sequence, and compute the value. This
818 // is always a simple digit string computed in decimal, so we do this manually
821 for (unsigned i = 0; i != ActualLength; ++i) {
822 if (!isDigit(DigitTokBegin[i])) {
823 PP.Diag(PP.AdvanceToTokenCharacter(DigitTok.getLocation(), i),
824 diag::err_pp_line_digit_sequence) << IsGNULineDirective;
825 PP.DiscardUntilEndOfDirective();
829 unsigned NextVal = Val*10+(DigitTokBegin[i]-'0');
830 if (NextVal < Val) { // overflow.
831 PP.Diag(DigitTok, DiagID);
832 PP.DiscardUntilEndOfDirective();
838 if (DigitTokBegin[0] == '0' && Val)
839 PP.Diag(DigitTok.getLocation(), diag::warn_pp_line_decimal)
840 << IsGNULineDirective;
845 /// \brief Handle a \#line directive: C99 6.10.4.
847 /// The two acceptable forms are:
849 /// # line digit-sequence
850 /// # line digit-sequence "s-char-sequence"
852 void Preprocessor::HandleLineDirective(Token &Tok) {
853 // Read the line # and string argument. Per C99 6.10.4p5, these tokens are
858 // Validate the number and convert it to an unsigned.
860 if (GetLineValue(DigitTok, LineNo, diag::err_pp_line_requires_integer,*this))
864 Diag(DigitTok, diag::ext_pp_line_zero);
866 // Enforce C99 6.10.4p3: "The digit sequence shall not specify ... a
867 // number greater than 2147483647". C90 requires that the line # be <= 32767.
868 unsigned LineLimit = 32768U;
869 if (LangOpts.C99 || LangOpts.CPlusPlus11)
870 LineLimit = 2147483648U;
871 if (LineNo >= LineLimit)
872 Diag(DigitTok, diag::ext_pp_line_too_big) << LineLimit;
873 else if (LangOpts.CPlusPlus11 && LineNo >= 32768U)
874 Diag(DigitTok, diag::warn_cxx98_compat_pp_line_too_big);
880 // If the StrTok is "eod", then it wasn't present. Otherwise, it must be a
881 // string followed by eod.
882 if (StrTok.is(tok::eod))
884 else if (StrTok.isNot(tok::string_literal)) {
885 Diag(StrTok, diag::err_pp_line_invalid_filename);
886 return DiscardUntilEndOfDirective();
887 } else if (StrTok.hasUDSuffix()) {
888 Diag(StrTok, diag::err_invalid_string_udl);
889 return DiscardUntilEndOfDirective();
891 // Parse and validate the string, converting it into a unique ID.
892 StringLiteralParser Literal(&StrTok, 1, *this);
893 assert(Literal.isAscii() && "Didn't allow wide strings in");
894 if (Literal.hadError)
895 return DiscardUntilEndOfDirective();
896 if (Literal.Pascal) {
897 Diag(StrTok, diag::err_pp_linemarker_invalid_filename);
898 return DiscardUntilEndOfDirective();
900 FilenameID = SourceMgr.getLineTableFilenameID(Literal.GetString());
902 // Verify that there is nothing after the string, other than EOD. Because
903 // of C99 6.10.4p5, macros that expand to empty tokens are ok.
904 CheckEndOfDirective("line", true);
907 SourceMgr.AddLineNote(DigitTok.getLocation(), LineNo, FilenameID);
910 Callbacks->FileChanged(CurPPLexer->getSourceLocation(),
911 PPCallbacks::RenameFile,
915 /// ReadLineMarkerFlags - Parse and validate any flags at the end of a GNU line
916 /// marker directive.
917 static bool ReadLineMarkerFlags(bool &IsFileEntry, bool &IsFileExit,
918 bool &IsSystemHeader, bool &IsExternCHeader,
923 if (FlagTok.is(tok::eod)) return false;
924 if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag, PP))
931 if (FlagTok.is(tok::eod)) return false;
932 if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag,PP))
934 } else if (FlagVal == 2) {
937 SourceManager &SM = PP.getSourceManager();
938 // If we are leaving the current presumed file, check to make sure the
939 // presumed include stack isn't empty!
941 SM.getDecomposedExpansionLoc(FlagTok.getLocation()).first;
942 PresumedLoc PLoc = SM.getPresumedLoc(FlagTok.getLocation());
943 if (PLoc.isInvalid())
946 // If there is no include loc (main file) or if the include loc is in a
947 // different physical file, then we aren't in a "1" line marker flag region.
948 SourceLocation IncLoc = PLoc.getIncludeLoc();
949 if (IncLoc.isInvalid() ||
950 SM.getDecomposedExpansionLoc(IncLoc).first != CurFileID) {
951 PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_pop);
952 PP.DiscardUntilEndOfDirective();
957 if (FlagTok.is(tok::eod)) return false;
958 if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag,PP))
962 // We must have 3 if there are still flags.
964 PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_flag);
965 PP.DiscardUntilEndOfDirective();
969 IsSystemHeader = true;
972 if (FlagTok.is(tok::eod)) return false;
973 if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag, PP))
976 // We must have 4 if there is yet another flag.
978 PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_flag);
979 PP.DiscardUntilEndOfDirective();
983 IsExternCHeader = true;
986 if (FlagTok.is(tok::eod)) return false;
988 // There are no more valid flags here.
989 PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_flag);
990 PP.DiscardUntilEndOfDirective();
994 /// HandleDigitDirective - Handle a GNU line marker directive, whose syntax is
995 /// one of the following forms:
998 /// # 42 "file" ('1' | '2')?
999 /// # 42 "file" ('1' | '2')? '3' '4'?
1001 void Preprocessor::HandleDigitDirective(Token &DigitTok) {
1002 // Validate the number and convert it to an unsigned. GNU does not have a
1003 // line # limit other than it fit in 32-bits.
1005 if (GetLineValue(DigitTok, LineNo, diag::err_pp_linemarker_requires_integer,
1012 bool IsFileEntry = false, IsFileExit = false;
1013 bool IsSystemHeader = false, IsExternCHeader = false;
1014 int FilenameID = -1;
1016 // If the StrTok is "eod", then it wasn't present. Otherwise, it must be a
1017 // string followed by eod.
1018 if (StrTok.is(tok::eod))
1020 else if (StrTok.isNot(tok::string_literal)) {
1021 Diag(StrTok, diag::err_pp_linemarker_invalid_filename);
1022 return DiscardUntilEndOfDirective();
1023 } else if (StrTok.hasUDSuffix()) {
1024 Diag(StrTok, diag::err_invalid_string_udl);
1025 return DiscardUntilEndOfDirective();
1027 // Parse and validate the string, converting it into a unique ID.
1028 StringLiteralParser Literal(&StrTok, 1, *this);
1029 assert(Literal.isAscii() && "Didn't allow wide strings in");
1030 if (Literal.hadError)
1031 return DiscardUntilEndOfDirective();
1032 if (Literal.Pascal) {
1033 Diag(StrTok, diag::err_pp_linemarker_invalid_filename);
1034 return DiscardUntilEndOfDirective();
1036 FilenameID = SourceMgr.getLineTableFilenameID(Literal.GetString());
1038 // If a filename was present, read any flags that are present.
1039 if (ReadLineMarkerFlags(IsFileEntry, IsFileExit,
1040 IsSystemHeader, IsExternCHeader, *this))
1044 // Create a line note with this information.
1045 SourceMgr.AddLineNote(DigitTok.getLocation(), LineNo, FilenameID,
1046 IsFileEntry, IsFileExit,
1047 IsSystemHeader, IsExternCHeader);
1049 // If the preprocessor has callbacks installed, notify them of the #line
1050 // change. This is used so that the line marker comes out in -E mode for
1053 PPCallbacks::FileChangeReason Reason = PPCallbacks::RenameFile;
1055 Reason = PPCallbacks::EnterFile;
1056 else if (IsFileExit)
1057 Reason = PPCallbacks::ExitFile;
1058 SrcMgr::CharacteristicKind FileKind = SrcMgr::C_User;
1059 if (IsExternCHeader)
1060 FileKind = SrcMgr::C_ExternCSystem;
1061 else if (IsSystemHeader)
1062 FileKind = SrcMgr::C_System;
1064 Callbacks->FileChanged(CurPPLexer->getSourceLocation(), Reason, FileKind);
1069 /// HandleUserDiagnosticDirective - Handle a #warning or #error directive.
1071 void Preprocessor::HandleUserDiagnosticDirective(Token &Tok,
1073 // PTH doesn't emit #warning or #error directives.
1075 return CurPTHLexer->DiscardToEndOfLine();
1077 // Read the rest of the line raw. We do this because we don't want macros
1078 // to be expanded and we don't require that the tokens be valid preprocessing
1079 // tokens. For example, this is allowed: "#warning ` 'foo". GCC does
1080 // collapse multiple consequtive white space between tokens, but this isn't
1081 // specified by the standard.
1082 SmallString<128> Message;
1083 CurLexer->ReadToEndOfLine(&Message);
1085 // Find the first non-whitespace character, so that we can make the
1086 // diagnostic more succinct.
1087 StringRef Msg = Message.str().ltrim(" ");
1090 Diag(Tok, diag::pp_hash_warning) << Msg;
1092 Diag(Tok, diag::err_pp_hash_error) << Msg;
1095 /// HandleIdentSCCSDirective - Handle a #ident/#sccs directive.
1097 void Preprocessor::HandleIdentSCCSDirective(Token &Tok) {
1098 // Yes, this directive is an extension.
1099 Diag(Tok, diag::ext_pp_ident_directive);
1101 // Read the string argument.
1105 // If the token kind isn't a string, it's a malformed directive.
1106 if (StrTok.isNot(tok::string_literal) &&
1107 StrTok.isNot(tok::wide_string_literal)) {
1108 Diag(StrTok, diag::err_pp_malformed_ident);
1109 if (StrTok.isNot(tok::eod))
1110 DiscardUntilEndOfDirective();
1114 if (StrTok.hasUDSuffix()) {
1115 Diag(StrTok, diag::err_invalid_string_udl);
1116 return DiscardUntilEndOfDirective();
1119 // Verify that there is nothing after the string, other than EOD.
1120 CheckEndOfDirective("ident");
1123 bool Invalid = false;
1124 std::string Str = getSpelling(StrTok, &Invalid);
1126 Callbacks->Ident(Tok.getLocation(), Str);
1130 /// \brief Handle a #public directive.
1131 void Preprocessor::HandleMacroPublicDirective(Token &Tok) {
1133 ReadMacroName(MacroNameTok, 2);
1135 // Error reading macro name? If so, diagnostic already issued.
1136 if (MacroNameTok.is(tok::eod))
1139 // Check to see if this is the last token on the #__public_macro line.
1140 CheckEndOfDirective("__public_macro");
1142 IdentifierInfo *II = MacroNameTok.getIdentifierInfo();
1143 // Okay, we finally have a valid identifier to undef.
1144 MacroDirective *MD = getMacroDirective(II);
1146 // If the macro is not defined, this is an error.
1148 Diag(MacroNameTok, diag::err_pp_visibility_non_macro) << II;
1152 // Note that this macro has now been exported.
1153 appendMacroDirective(II, AllocateVisibilityMacroDirective(
1154 MacroNameTok.getLocation(), /*IsPublic=*/true));
1157 /// \brief Handle a #private directive.
1158 void Preprocessor::HandleMacroPrivateDirective(Token &Tok) {
1160 ReadMacroName(MacroNameTok, 2);
1162 // Error reading macro name? If so, diagnostic already issued.
1163 if (MacroNameTok.is(tok::eod))
1166 // Check to see if this is the last token on the #__private_macro line.
1167 CheckEndOfDirective("__private_macro");
1169 IdentifierInfo *II = MacroNameTok.getIdentifierInfo();
1170 // Okay, we finally have a valid identifier to undef.
1171 MacroDirective *MD = getMacroDirective(II);
1173 // If the macro is not defined, this is an error.
1175 Diag(MacroNameTok, diag::err_pp_visibility_non_macro) << II;
1179 // Note that this macro has now been marked private.
1180 appendMacroDirective(II, AllocateVisibilityMacroDirective(
1181 MacroNameTok.getLocation(), /*IsPublic=*/false));
1184 //===----------------------------------------------------------------------===//
1185 // Preprocessor Include Directive Handling.
1186 //===----------------------------------------------------------------------===//
1188 /// GetIncludeFilenameSpelling - Turn the specified lexer token into a fully
1189 /// checked and spelled filename, e.g. as an operand of \#include. This returns
1190 /// true if the input filename was in <>'s or false if it were in ""'s. The
1191 /// caller is expected to provide a buffer that is large enough to hold the
1192 /// spelling of the filename, but is also expected to handle the case when
1193 /// this method decides to use a different buffer.
1194 bool Preprocessor::GetIncludeFilenameSpelling(SourceLocation Loc,
1195 StringRef &Buffer) {
1196 // Get the text form of the filename.
1197 assert(!Buffer.empty() && "Can't have tokens with empty spellings!");
1199 // Make sure the filename is <x> or "x".
1201 if (Buffer[0] == '<') {
1202 if (Buffer.back() != '>') {
1203 Diag(Loc, diag::err_pp_expects_filename);
1204 Buffer = StringRef();
1208 } else if (Buffer[0] == '"') {
1209 if (Buffer.back() != '"') {
1210 Diag(Loc, diag::err_pp_expects_filename);
1211 Buffer = StringRef();
1216 Diag(Loc, diag::err_pp_expects_filename);
1217 Buffer = StringRef();
1221 // Diagnose #include "" as invalid.
1222 if (Buffer.size() <= 2) {
1223 Diag(Loc, diag::err_pp_empty_filename);
1224 Buffer = StringRef();
1228 // Skip the brackets.
1229 Buffer = Buffer.substr(1, Buffer.size()-2);
1233 /// \brief Handle cases where the \#include name is expanded from a macro
1234 /// as multiple tokens, which need to be glued together.
1236 /// This occurs for code like:
1238 /// \#define FOO <a/b.h>
1241 /// because in this case, "<a/b.h>" is returned as 7 tokens, not one.
1243 /// This code concatenates and consumes tokens up to the '>' token. It returns
1244 /// false if the > was found, otherwise it returns true if it finds and consumes
1246 bool Preprocessor::ConcatenateIncludeName(
1247 SmallString<128> &FilenameBuffer,
1248 SourceLocation &End) {
1252 while (CurTok.isNot(tok::eod)) {
1253 End = CurTok.getLocation();
1255 // FIXME: Provide code completion for #includes.
1256 if (CurTok.is(tok::code_completion)) {
1257 setCodeCompletionReached();
1262 // Append the spelling of this token to the buffer. If there was a space
1263 // before it, add it now.
1264 if (CurTok.hasLeadingSpace())
1265 FilenameBuffer.push_back(' ');
1267 // Get the spelling of the token, directly into FilenameBuffer if possible.
1268 unsigned PreAppendSize = FilenameBuffer.size();
1269 FilenameBuffer.resize(PreAppendSize+CurTok.getLength());
1271 const char *BufPtr = &FilenameBuffer[PreAppendSize];
1272 unsigned ActualLen = getSpelling(CurTok, BufPtr);
1274 // If the token was spelled somewhere else, copy it into FilenameBuffer.
1275 if (BufPtr != &FilenameBuffer[PreAppendSize])
1276 memcpy(&FilenameBuffer[PreAppendSize], BufPtr, ActualLen);
1278 // Resize FilenameBuffer to the correct size.
1279 if (CurTok.getLength() != ActualLen)
1280 FilenameBuffer.resize(PreAppendSize+ActualLen);
1282 // If we found the '>' marker, return success.
1283 if (CurTok.is(tok::greater))
1289 // If we hit the eod marker, emit an error and return true so that the caller
1290 // knows the EOD has been read.
1291 Diag(CurTok.getLocation(), diag::err_pp_expects_filename);
1295 /// HandleIncludeDirective - The "\#include" tokens have just been read, read
1296 /// the file to be included from the lexer, then include it! This is a common
1297 /// routine with functionality shared between \#include, \#include_next and
1298 /// \#import. LookupFrom is set when this is a \#include_next directive, it
1299 /// specifies the file to start searching from.
1300 void Preprocessor::HandleIncludeDirective(SourceLocation HashLoc,
1302 const DirectoryLookup *LookupFrom,
1306 CurPPLexer->LexIncludeFilename(FilenameTok);
1308 // Reserve a buffer to get the spelling.
1309 SmallString<128> FilenameBuffer;
1312 SourceLocation CharEnd; // the end of this directive, in characters
1314 switch (FilenameTok.getKind()) {
1316 // If the token kind is EOD, the error has already been diagnosed.
1319 case tok::angle_string_literal:
1320 case tok::string_literal:
1321 Filename = getSpelling(FilenameTok, FilenameBuffer);
1322 End = FilenameTok.getLocation();
1323 CharEnd = End.getLocWithOffset(FilenameTok.getLength());
1327 // This could be a <foo/bar.h> file coming from a macro expansion. In this
1328 // case, glue the tokens together into FilenameBuffer and interpret those.
1329 FilenameBuffer.push_back('<');
1330 if (ConcatenateIncludeName(FilenameBuffer, End))
1331 return; // Found <eod> but no ">"? Diagnostic already emitted.
1332 Filename = FilenameBuffer.str();
1333 CharEnd = End.getLocWithOffset(1);
1336 Diag(FilenameTok.getLocation(), diag::err_pp_expects_filename);
1337 DiscardUntilEndOfDirective();
1341 CharSourceRange FilenameRange
1342 = CharSourceRange::getCharRange(FilenameTok.getLocation(), CharEnd);
1343 StringRef OriginalFilename = Filename;
1345 GetIncludeFilenameSpelling(FilenameTok.getLocation(), Filename);
1346 // If GetIncludeFilenameSpelling set the start ptr to null, there was an
1348 if (Filename.empty()) {
1349 DiscardUntilEndOfDirective();
1353 // Verify that there is nothing after the filename, other than EOD. Note that
1354 // we allow macros that expand to nothing after the filename, because this
1355 // falls into the category of "#include pp-tokens new-line" specified in
1357 CheckEndOfDirective(IncludeTok.getIdentifierInfo()->getNameStart(), true);
1359 // Check that we don't have infinite #include recursion.
1360 if (IncludeMacroStack.size() == MaxAllowedIncludeStackDepth-1) {
1361 Diag(FilenameTok, diag::err_pp_include_too_deep);
1365 // Complain about attempts to #include files in an audit pragma.
1366 if (PragmaARCCFCodeAuditedLoc.isValid()) {
1367 Diag(HashLoc, diag::err_pp_include_in_arc_cf_code_audited);
1368 Diag(PragmaARCCFCodeAuditedLoc, diag::note_pragma_entered_here);
1370 // Immediately leave the pragma.
1371 PragmaARCCFCodeAuditedLoc = SourceLocation();
1374 if (HeaderInfo.HasIncludeAliasMap()) {
1375 // Map the filename with the brackets still attached. If the name doesn't
1376 // map to anything, fall back on the filename we've already gotten the
1378 StringRef NewName = HeaderInfo.MapHeaderToIncludeAlias(OriginalFilename);
1379 if (!NewName.empty())
1383 // Search include directories.
1384 const DirectoryLookup *CurDir;
1385 SmallString<1024> SearchPath;
1386 SmallString<1024> RelativePath;
1387 // We get the raw path only if we have 'Callbacks' to which we later pass
1389 Module *SuggestedModule = 0;
1390 const FileEntry *File = LookupFile(
1391 Filename, isAngled, LookupFrom, CurDir,
1392 Callbacks ? &SearchPath : NULL, Callbacks ? &RelativePath : NULL,
1393 getLangOpts().Modules? &SuggestedModule : 0);
1397 // Give the clients a chance to recover.
1398 SmallString<128> RecoveryPath;
1399 if (Callbacks->FileNotFound(Filename, RecoveryPath)) {
1400 if (const DirectoryEntry *DE = FileMgr.getDirectory(RecoveryPath)) {
1401 // Add the recovery path to the list of search paths.
1402 DirectoryLookup DL(DE, SrcMgr::C_User, false);
1403 HeaderInfo.AddSearchPath(DL, isAngled);
1405 // Try the lookup again, skipping the cache.
1406 File = LookupFile(Filename, isAngled, LookupFrom, CurDir, 0, 0,
1407 getLangOpts().Modules? &SuggestedModule : 0,
1413 if (!SuggestedModule) {
1414 // Notify the callback object that we've seen an inclusion directive.
1415 Callbacks->InclusionDirective(HashLoc, IncludeTok, Filename, isAngled,
1416 FilenameRange, File,
1417 SearchPath, RelativePath,
1418 /*ImportedModule=*/0);
1423 if (!SuppressIncludeNotFoundError) {
1424 // If the file could not be located and it was included via angle
1425 // brackets, we can attempt a lookup as though it were a quoted path to
1426 // provide the user with a possible fixit.
1428 File = LookupFile(Filename, false, LookupFrom, CurDir,
1429 Callbacks ? &SearchPath : 0,
1430 Callbacks ? &RelativePath : 0,
1431 getLangOpts().Modules ? &SuggestedModule : 0);
1433 SourceRange Range(FilenameTok.getLocation(), CharEnd);
1434 Diag(FilenameTok, diag::err_pp_file_not_found_not_fatal) <<
1436 FixItHint::CreateReplacement(Range, "\"" + Filename.str() + "\"");
1439 // If the file is still not found, just go with the vanilla diagnostic
1441 Diag(FilenameTok, diag::err_pp_file_not_found) << Filename;
1447 // If we are supposed to import a module rather than including the header,
1449 if (SuggestedModule) {
1450 // Compute the module access path corresponding to this module.
1451 // FIXME: Should we have a second loadModule() overload to avoid this
1452 // extra lookup step?
1453 SmallVector<std::pair<IdentifierInfo *, SourceLocation>, 2> Path;
1454 for (Module *Mod = SuggestedModule; Mod; Mod = Mod->Parent)
1455 Path.push_back(std::make_pair(getIdentifierInfo(Mod->Name),
1456 FilenameTok.getLocation()));
1457 std::reverse(Path.begin(), Path.end());
1459 // Warn that we're replacing the include/import with a module import.
1460 SmallString<128> PathString;
1461 for (unsigned I = 0, N = Path.size(); I != N; ++I) {
1464 PathString += Path[I].first->getName();
1466 int IncludeKind = 0;
1468 switch (IncludeTok.getIdentifierInfo()->getPPKeywordID()) {
1469 case tok::pp_include:
1473 case tok::pp_import:
1477 case tok::pp_include_next:
1481 case tok::pp___include_macros:
1486 llvm_unreachable("unknown include directive kind");
1489 // Determine whether we are actually building the module that this
1490 // include directive maps to.
1491 bool BuildingImportedModule
1492 = Path[0].first->getName() == getLangOpts().CurrentModule;
1494 if (!BuildingImportedModule && getLangOpts().ObjC2) {
1495 // If we're not building the imported module, warn that we're going
1496 // to automatically turn this inclusion directive into a module import.
1497 // We only do this in Objective-C, where we have a module-import syntax.
1498 CharSourceRange ReplaceRange(SourceRange(HashLoc, CharEnd),
1499 /*IsTokenRange=*/false);
1500 Diag(HashLoc, diag::warn_auto_module_import)
1501 << IncludeKind << PathString
1502 << FixItHint::CreateReplacement(ReplaceRange,
1503 "@import " + PathString.str().str() + ";");
1507 // If this was an #__include_macros directive, only make macros visible.
1508 Module::NameVisibilityKind Visibility
1509 = (IncludeKind == 3)? Module::MacrosVisible : Module::AllVisible;
1510 ModuleLoadResult Imported
1511 = TheModuleLoader.loadModule(IncludeTok.getLocation(), Path, Visibility,
1512 /*IsIncludeDirective=*/true);
1513 assert((Imported == 0 || Imported == SuggestedModule) &&
1514 "the imported module is different than the suggested one");
1516 // If this header isn't part of the module we're building, we're done.
1517 if (!BuildingImportedModule && Imported) {
1519 Callbacks->InclusionDirective(HashLoc, IncludeTok, Filename, isAngled,
1520 FilenameRange, File,
1521 SearchPath, RelativePath, Imported);
1526 // If we failed to find a submodule that we expected to find, we can
1527 // continue. Otherwise, there's an error in the included file, so we
1528 // don't want to include it.
1529 if (!BuildingImportedModule && !Imported.isMissingExpected()) {
1534 if (Callbacks && SuggestedModule) {
1535 // We didn't notify the callback object that we've seen an inclusion
1536 // directive before. Now that we are parsing the include normally and not
1537 // turning it to a module import, notify the callback object.
1538 Callbacks->InclusionDirective(HashLoc, IncludeTok, Filename, isAngled,
1539 FilenameRange, File,
1540 SearchPath, RelativePath,
1541 /*ImportedModule=*/0);
1544 // The #included file will be considered to be a system header if either it is
1545 // in a system include directory, or if the #includer is a system include
1547 SrcMgr::CharacteristicKind FileCharacter =
1548 std::max(HeaderInfo.getFileDirFlavor(File),
1549 SourceMgr.getFileCharacteristic(FilenameTok.getLocation()));
1551 // Ask HeaderInfo if we should enter this #include file. If not, #including
1552 // this file will have no effect.
1553 if (!HeaderInfo.ShouldEnterIncludeFile(File, isImport)) {
1555 Callbacks->FileSkipped(*File, FilenameTok, FileCharacter);
1559 // Look up the file, create a File ID for it.
1560 SourceLocation IncludePos = End;
1561 // If the filename string was the result of macro expansions, set the include
1562 // position on the file where it will be included and after the expansions.
1563 if (IncludePos.isMacroID())
1564 IncludePos = SourceMgr.getExpansionRange(IncludePos).second;
1565 FileID FID = SourceMgr.createFileID(File, IncludePos, FileCharacter);
1566 assert(!FID.isInvalid() && "Expected valid file ID");
1568 // Finally, if all is good, enter the new file!
1569 EnterSourceFile(FID, CurDir, FilenameTok.getLocation());
1572 /// HandleIncludeNextDirective - Implements \#include_next.
1574 void Preprocessor::HandleIncludeNextDirective(SourceLocation HashLoc,
1575 Token &IncludeNextTok) {
1576 Diag(IncludeNextTok, diag::ext_pp_include_next_directive);
1578 // #include_next is like #include, except that we start searching after
1579 // the current found directory. If we can't do this, issue a
1581 const DirectoryLookup *Lookup = CurDirLookup;
1582 if (isInPrimaryFile()) {
1584 Diag(IncludeNextTok, diag::pp_include_next_in_primary);
1585 } else if (Lookup == 0) {
1586 Diag(IncludeNextTok, diag::pp_include_next_absolute_path);
1588 // Start looking up in the next directory.
1592 return HandleIncludeDirective(HashLoc, IncludeNextTok, Lookup);
1595 /// HandleMicrosoftImportDirective - Implements \#import for Microsoft Mode
1596 void Preprocessor::HandleMicrosoftImportDirective(Token &Tok) {
1597 // The Microsoft #import directive takes a type library and generates header
1598 // files from it, and includes those. This is beyond the scope of what clang
1599 // does, so we ignore it and error out. However, #import can optionally have
1600 // trailing attributes that span multiple lines. We're going to eat those
1601 // so we can continue processing from there.
1602 Diag(Tok, diag::err_pp_import_directive_ms );
1604 // Read tokens until we get to the end of the directive. Note that the
1605 // directive can be split over multiple lines using the backslash character.
1606 DiscardUntilEndOfDirective();
1609 /// HandleImportDirective - Implements \#import.
1611 void Preprocessor::HandleImportDirective(SourceLocation HashLoc,
1613 if (!LangOpts.ObjC1) { // #import is standard for ObjC.
1614 if (LangOpts.MicrosoftMode)
1615 return HandleMicrosoftImportDirective(ImportTok);
1616 Diag(ImportTok, diag::ext_pp_import_directive);
1618 return HandleIncludeDirective(HashLoc, ImportTok, 0, true);
1621 /// HandleIncludeMacrosDirective - The -imacros command line option turns into a
1622 /// pseudo directive in the predefines buffer. This handles it by sucking all
1623 /// tokens through the preprocessor and discarding them (only keeping the side
1624 /// effects on the preprocessor).
1625 void Preprocessor::HandleIncludeMacrosDirective(SourceLocation HashLoc,
1626 Token &IncludeMacrosTok) {
1627 // This directive should only occur in the predefines buffer. If not, emit an
1628 // error and reject it.
1629 SourceLocation Loc = IncludeMacrosTok.getLocation();
1630 if (strcmp(SourceMgr.getBufferName(Loc), "<built-in>") != 0) {
1631 Diag(IncludeMacrosTok.getLocation(),
1632 diag::pp_include_macros_out_of_predefines);
1633 DiscardUntilEndOfDirective();
1637 // Treat this as a normal #include for checking purposes. If this is
1638 // successful, it will push a new lexer onto the include stack.
1639 HandleIncludeDirective(HashLoc, IncludeMacrosTok, 0, false);
1644 assert(TmpTok.isNot(tok::eof) && "Didn't find end of -imacros!");
1645 } while (TmpTok.isNot(tok::hashhash));
1648 //===----------------------------------------------------------------------===//
1649 // Preprocessor Macro Directive Handling.
1650 //===----------------------------------------------------------------------===//
1652 /// ReadMacroDefinitionArgList - The ( starting an argument list of a macro
1653 /// definition has just been read. Lex the rest of the arguments and the
1654 /// closing ), updating MI with what we learn. Return true if an error occurs
1655 /// parsing the arg list.
1656 bool Preprocessor::ReadMacroDefinitionArgList(MacroInfo *MI, Token &Tok) {
1657 SmallVector<IdentifierInfo*, 32> Arguments;
1660 LexUnexpandedToken(Tok);
1661 switch (Tok.getKind()) {
1663 // Found the end of the argument list.
1664 if (Arguments.empty()) // #define FOO()
1666 // Otherwise we have #define FOO(A,)
1667 Diag(Tok, diag::err_pp_expected_ident_in_arg_list);
1669 case tok::ellipsis: // #define X(... -> C99 varargs
1671 Diag(Tok, LangOpts.CPlusPlus11 ?
1672 diag::warn_cxx98_compat_variadic_macro :
1673 diag::ext_variadic_macro);
1675 // OpenCL v1.2 s6.9.e: variadic macros are not supported.
1676 if (LangOpts.OpenCL) {
1677 Diag(Tok, diag::err_pp_opencl_variadic_macros);
1681 // Lex the token after the identifier.
1682 LexUnexpandedToken(Tok);
1683 if (Tok.isNot(tok::r_paren)) {
1684 Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
1687 // Add the __VA_ARGS__ identifier as an argument.
1688 Arguments.push_back(Ident__VA_ARGS__);
1689 MI->setIsC99Varargs();
1690 MI->setArgumentList(&Arguments[0], Arguments.size(), BP);
1692 case tok::eod: // #define X(
1693 Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
1696 // Handle keywords and identifiers here to accept things like
1697 // #define Foo(for) for.
1698 IdentifierInfo *II = Tok.getIdentifierInfo();
1701 Diag(Tok, diag::err_pp_invalid_tok_in_arg_list);
1705 // If this is already used as an argument, it is used multiple times (e.g.
1707 if (std::find(Arguments.begin(), Arguments.end(), II) !=
1708 Arguments.end()) { // C99 6.10.3p6
1709 Diag(Tok, diag::err_pp_duplicate_name_in_arg_list) << II;
1713 // Add the argument to the macro info.
1714 Arguments.push_back(II);
1716 // Lex the token after the identifier.
1717 LexUnexpandedToken(Tok);
1719 switch (Tok.getKind()) {
1720 default: // #define X(A B
1721 Diag(Tok, diag::err_pp_expected_comma_in_arg_list);
1723 case tok::r_paren: // #define X(A)
1724 MI->setArgumentList(&Arguments[0], Arguments.size(), BP);
1726 case tok::comma: // #define X(A,
1728 case tok::ellipsis: // #define X(A... -> GCC extension
1729 // Diagnose extension.
1730 Diag(Tok, diag::ext_named_variadic_macro);
1732 // Lex the token after the identifier.
1733 LexUnexpandedToken(Tok);
1734 if (Tok.isNot(tok::r_paren)) {
1735 Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
1739 MI->setIsGNUVarargs();
1740 MI->setArgumentList(&Arguments[0], Arguments.size(), BP);
1747 /// HandleDefineDirective - Implements \#define. This consumes the entire macro
1748 /// line then lets the caller lex the next real token.
1749 void Preprocessor::HandleDefineDirective(Token &DefineTok) {
1753 ReadMacroName(MacroNameTok, 1);
1755 // Error reading macro name? If so, diagnostic already issued.
1756 if (MacroNameTok.is(tok::eod))
1759 Token LastTok = MacroNameTok;
1761 // If we are supposed to keep comments in #defines, reenable comment saving
1763 if (CurLexer) CurLexer->SetCommentRetentionState(KeepMacroComments);
1765 // Create the new macro.
1766 MacroInfo *MI = AllocateMacroInfo(MacroNameTok.getLocation());
1769 LexUnexpandedToken(Tok);
1771 // If this is a function-like macro definition, parse the argument list,
1772 // marking each of the identifiers as being used as macro arguments. Also,
1773 // check other constraints on the first token of the macro body.
1774 if (Tok.is(tok::eod)) {
1775 // If there is no body to this macro, we have no special handling here.
1776 } else if (Tok.hasLeadingSpace()) {
1777 // This is a normal token with leading space. Clear the leading space
1778 // marker on the first token to get proper expansion.
1779 Tok.clearFlag(Token::LeadingSpace);
1780 } else if (Tok.is(tok::l_paren)) {
1781 // This is a function-like macro definition. Read the argument list.
1782 MI->setIsFunctionLike();
1783 if (ReadMacroDefinitionArgList(MI, LastTok)) {
1785 ReleaseMacroInfo(MI);
1786 // Throw away the rest of the line.
1787 if (CurPPLexer->ParsingPreprocessorDirective)
1788 DiscardUntilEndOfDirective();
1792 // If this is a definition of a variadic C99 function-like macro, not using
1793 // the GNU named varargs extension, enabled __VA_ARGS__.
1795 // "Poison" __VA_ARGS__, which can only appear in the expansion of a macro.
1796 // This gets unpoisoned where it is allowed.
1797 assert(Ident__VA_ARGS__->isPoisoned() && "__VA_ARGS__ should be poisoned!");
1798 if (MI->isC99Varargs())
1799 Ident__VA_ARGS__->setIsPoisoned(false);
1801 // Read the first token after the arg list for down below.
1802 LexUnexpandedToken(Tok);
1803 } else if (LangOpts.C99 || LangOpts.CPlusPlus11) {
1804 // C99 requires whitespace between the macro definition and the body. Emit
1805 // a diagnostic for something like "#define X+".
1806 Diag(Tok, diag::ext_c99_whitespace_required_after_macro_name);
1808 // C90 6.8 TC1 says: "In the definition of an object-like macro, if the
1809 // first character of a replacement list is not a character required by
1810 // subclause 5.2.1, then there shall be white-space separation between the
1811 // identifier and the replacement list.". 5.2.1 lists this set:
1812 // "A-Za-z0-9!"#%&'()*+,_./:;<=>?[\]^_{|}~" as well as whitespace, which
1813 // is irrelevant here.
1814 bool isInvalid = false;
1815 if (Tok.is(tok::at)) // @ is not in the list above.
1817 else if (Tok.is(tok::unknown)) {
1818 // If we have an unknown token, it is something strange like "`". Since
1819 // all of valid characters would have lexed into a single character
1820 // token of some sort, we know this is not a valid case.
1824 Diag(Tok, diag::ext_missing_whitespace_after_macro_name);
1826 Diag(Tok, diag::warn_missing_whitespace_after_macro_name);
1829 if (!Tok.is(tok::eod))
1832 // Read the rest of the macro body.
1833 if (MI->isObjectLike()) {
1834 // Object-like macros are very simple, just read their body.
1835 while (Tok.isNot(tok::eod)) {
1837 MI->AddTokenToBody(Tok);
1838 // Get the next token of the macro.
1839 LexUnexpandedToken(Tok);
1843 // Otherwise, read the body of a function-like macro. While we are at it,
1844 // check C99 6.10.3.2p1: ensure that # operators are followed by macro
1845 // parameters in function-like macro expansions.
1846 while (Tok.isNot(tok::eod)) {
1849 if (Tok.isNot(tok::hash) && Tok.isNot(tok::hashhash)) {
1850 MI->AddTokenToBody(Tok);
1852 // Get the next token of the macro.
1853 LexUnexpandedToken(Tok);
1857 if (Tok.is(tok::hashhash)) {
1859 // If we see token pasting, check if it looks like the gcc comma
1860 // pasting extension. We'll use this information to suppress
1861 // diagnostics later on.
1863 // Get the next token of the macro.
1864 LexUnexpandedToken(Tok);
1866 if (Tok.is(tok::eod)) {
1867 MI->AddTokenToBody(LastTok);
1871 unsigned NumTokens = MI->getNumTokens();
1872 if (NumTokens && Tok.getIdentifierInfo() == Ident__VA_ARGS__ &&
1873 MI->getReplacementToken(NumTokens-1).is(tok::comma))
1874 MI->setHasCommaPasting();
1876 // Things look ok, add the '##' and param name tokens to the macro.
1877 MI->AddTokenToBody(LastTok);
1878 MI->AddTokenToBody(Tok);
1881 // Get the next token of the macro.
1882 LexUnexpandedToken(Tok);
1886 // Get the next token of the macro.
1887 LexUnexpandedToken(Tok);
1889 // Check for a valid macro arg identifier.
1890 if (Tok.getIdentifierInfo() == 0 ||
1891 MI->getArgumentNum(Tok.getIdentifierInfo()) == -1) {
1893 // If this is assembler-with-cpp mode, we accept random gibberish after
1894 // the '#' because '#' is often a comment character. However, change
1895 // the kind of the token to tok::unknown so that the preprocessor isn't
1897 if (getLangOpts().AsmPreprocessor && Tok.isNot(tok::eod)) {
1898 LastTok.setKind(tok::unknown);
1900 Diag(Tok, diag::err_pp_stringize_not_parameter);
1901 ReleaseMacroInfo(MI);
1903 // Disable __VA_ARGS__ again.
1904 Ident__VA_ARGS__->setIsPoisoned(true);
1909 // Things look ok, add the '#' and param name tokens to the macro.
1910 MI->AddTokenToBody(LastTok);
1911 MI->AddTokenToBody(Tok);
1914 // Get the next token of the macro.
1915 LexUnexpandedToken(Tok);
1920 // Disable __VA_ARGS__ again.
1921 Ident__VA_ARGS__->setIsPoisoned(true);
1923 // Check that there is no paste (##) operator at the beginning or end of the
1924 // replacement list.
1925 unsigned NumTokens = MI->getNumTokens();
1926 if (NumTokens != 0) {
1927 if (MI->getReplacementToken(0).is(tok::hashhash)) {
1928 Diag(MI->getReplacementToken(0), diag::err_paste_at_start);
1929 ReleaseMacroInfo(MI);
1932 if (MI->getReplacementToken(NumTokens-1).is(tok::hashhash)) {
1933 Diag(MI->getReplacementToken(NumTokens-1), diag::err_paste_at_end);
1934 ReleaseMacroInfo(MI);
1939 MI->setDefinitionEndLoc(LastTok.getLocation());
1941 // Finally, if this identifier already had a macro defined for it, verify that
1942 // the macro bodies are identical, and issue diagnostics if they are not.
1943 if (const MacroInfo *OtherMI=getMacroInfo(MacroNameTok.getIdentifierInfo())) {
1944 // It is very common for system headers to have tons of macro redefinitions
1945 // and for warnings to be disabled in system headers. If this is the case,
1946 // then don't bother calling MacroInfo::isIdenticalTo.
1947 if (!getDiagnostics().getSuppressSystemWarnings() ||
1948 !SourceMgr.isInSystemHeader(DefineTok.getLocation())) {
1949 if (!OtherMI->isUsed() && OtherMI->isWarnIfUnused())
1950 Diag(OtherMI->getDefinitionLoc(), diag::pp_macro_not_used);
1952 // Warn if defining "__LINE__" and other builtins, per C99 6.10.8/4 and
1953 // C++ [cpp.predefined]p4, but allow it as an extension.
1954 if (OtherMI->isBuiltinMacro())
1955 Diag(MacroNameTok, diag::ext_pp_redef_builtin_macro);
1956 // Macros must be identical. This means all tokens and whitespace
1957 // separation must be the same. C99 6.10.3p2.
1958 else if (!OtherMI->isAllowRedefinitionsWithoutWarning() &&
1959 !MI->isIdenticalTo(*OtherMI, *this, /*Syntactic=*/LangOpts.MicrosoftExt)) {
1960 Diag(MI->getDefinitionLoc(), diag::ext_pp_macro_redef)
1961 << MacroNameTok.getIdentifierInfo();
1962 Diag(OtherMI->getDefinitionLoc(), diag::note_previous_definition);
1965 if (OtherMI->isWarnIfUnused())
1966 WarnUnusedMacroLocs.erase(OtherMI->getDefinitionLoc());
1969 DefMacroDirective *MD =
1970 appendDefMacroDirective(MacroNameTok.getIdentifierInfo(), MI);
1972 assert(!MI->isUsed());
1973 // If we need warning for not using the macro, add its location in the
1974 // warn-because-unused-macro set. If it gets used it will be removed from set.
1975 if (isInPrimaryFile() && // don't warn for include'd macros.
1976 Diags->getDiagnosticLevel(diag::pp_macro_not_used,
1977 MI->getDefinitionLoc()) != DiagnosticsEngine::Ignored) {
1978 MI->setIsWarnIfUnused(true);
1979 WarnUnusedMacroLocs.insert(MI->getDefinitionLoc());
1982 // If the callbacks want to know, tell them about the macro definition.
1984 Callbacks->MacroDefined(MacroNameTok, MD);
1987 /// HandleUndefDirective - Implements \#undef.
1989 void Preprocessor::HandleUndefDirective(Token &UndefTok) {
1993 ReadMacroName(MacroNameTok, 2);
1995 // Error reading macro name? If so, diagnostic already issued.
1996 if (MacroNameTok.is(tok::eod))
1999 // Check to see if this is the last token on the #undef line.
2000 CheckEndOfDirective("undef");
2002 // Okay, we finally have a valid identifier to undef.
2003 MacroDirective *MD = getMacroDirective(MacroNameTok.getIdentifierInfo());
2004 const MacroInfo *MI = MD ? MD->getMacroInfo() : 0;
2006 // If the callbacks want to know, tell them about the macro #undef.
2007 // Note: no matter if the macro was defined or not.
2009 Callbacks->MacroUndefined(MacroNameTok, MD);
2011 // If the macro is not defined, this is a noop undef, just return.
2012 if (MI == 0) return;
2014 if (!MI->isUsed() && MI->isWarnIfUnused())
2015 Diag(MI->getDefinitionLoc(), diag::pp_macro_not_used);
2017 if (MI->isWarnIfUnused())
2018 WarnUnusedMacroLocs.erase(MI->getDefinitionLoc());
2020 appendMacroDirective(MacroNameTok.getIdentifierInfo(),
2021 AllocateUndefMacroDirective(MacroNameTok.getLocation()));
2025 //===----------------------------------------------------------------------===//
2026 // Preprocessor Conditional Directive Handling.
2027 //===----------------------------------------------------------------------===//
2029 /// HandleIfdefDirective - Implements the \#ifdef/\#ifndef directive. isIfndef
2030 /// is true when this is a \#ifndef directive. ReadAnyTokensBeforeDirective is
2031 /// true if any tokens have been returned or pp-directives activated before this
2032 /// \#ifndef has been lexed.
2034 void Preprocessor::HandleIfdefDirective(Token &Result, bool isIfndef,
2035 bool ReadAnyTokensBeforeDirective) {
2037 Token DirectiveTok = Result;
2040 ReadMacroName(MacroNameTok);
2042 // Error reading macro name? If so, diagnostic already issued.
2043 if (MacroNameTok.is(tok::eod)) {
2044 // Skip code until we get to #endif. This helps with recovery by not
2045 // emitting an error when the #endif is reached.
2046 SkipExcludedConditionalBlock(DirectiveTok.getLocation(),
2047 /*Foundnonskip*/false, /*FoundElse*/false);
2051 // Check to see if this is the last token on the #if[n]def line.
2052 CheckEndOfDirective(isIfndef ? "ifndef" : "ifdef");
2054 IdentifierInfo *MII = MacroNameTok.getIdentifierInfo();
2055 MacroDirective *MD = getMacroDirective(MII);
2056 MacroInfo *MI = MD ? MD->getMacroInfo() : 0;
2058 if (CurPPLexer->getConditionalStackDepth() == 0) {
2059 // If the start of a top-level #ifdef and if the macro is not defined,
2060 // inform MIOpt that this might be the start of a proper include guard.
2061 // Otherwise it is some other form of unknown conditional which we can't
2063 if (!ReadAnyTokensBeforeDirective && MI == 0) {
2064 assert(isIfndef && "#ifdef shouldn't reach here");
2065 CurPPLexer->MIOpt.EnterTopLevelIFNDEF(MII);
2067 CurPPLexer->MIOpt.EnterTopLevelConditional();
2070 // If there is a macro, process it.
2071 if (MI) // Mark it used.
2072 markMacroAsUsed(MI);
2076 Callbacks->Ifndef(DirectiveTok.getLocation(), MacroNameTok, MD);
2078 Callbacks->Ifdef(DirectiveTok.getLocation(), MacroNameTok, MD);
2081 // Should we include the stuff contained by this directive?
2082 if (!MI == isIfndef) {
2083 // Yes, remember that we are inside a conditional, then lex the next token.
2084 CurPPLexer->pushConditionalLevel(DirectiveTok.getLocation(),
2085 /*wasskip*/false, /*foundnonskip*/true,
2086 /*foundelse*/false);
2088 // No, skip the contents of this block.
2089 SkipExcludedConditionalBlock(DirectiveTok.getLocation(),
2090 /*Foundnonskip*/false,
2091 /*FoundElse*/false);
2095 /// HandleIfDirective - Implements the \#if directive.
2097 void Preprocessor::HandleIfDirective(Token &IfToken,
2098 bool ReadAnyTokensBeforeDirective) {
2101 // Parse and evaluate the conditional expression.
2102 IdentifierInfo *IfNDefMacro = 0;
2103 const SourceLocation ConditionalBegin = CurPPLexer->getSourceLocation();
2104 const bool ConditionalTrue = EvaluateDirectiveExpression(IfNDefMacro);
2105 const SourceLocation ConditionalEnd = CurPPLexer->getSourceLocation();
2107 // If this condition is equivalent to #ifndef X, and if this is the first
2108 // directive seen, handle it for the multiple-include optimization.
2109 if (CurPPLexer->getConditionalStackDepth() == 0) {
2110 if (!ReadAnyTokensBeforeDirective && IfNDefMacro && ConditionalTrue)
2111 CurPPLexer->MIOpt.EnterTopLevelIFNDEF(IfNDefMacro);
2113 CurPPLexer->MIOpt.EnterTopLevelConditional();
2117 Callbacks->If(IfToken.getLocation(),
2118 SourceRange(ConditionalBegin, ConditionalEnd));
2120 // Should we include the stuff contained by this directive?
2121 if (ConditionalTrue) {
2122 // Yes, remember that we are inside a conditional, then lex the next token.
2123 CurPPLexer->pushConditionalLevel(IfToken.getLocation(), /*wasskip*/false,
2124 /*foundnonskip*/true, /*foundelse*/false);
2126 // No, skip the contents of this block.
2127 SkipExcludedConditionalBlock(IfToken.getLocation(), /*Foundnonskip*/false,
2128 /*FoundElse*/false);
2132 /// HandleEndifDirective - Implements the \#endif directive.
2134 void Preprocessor::HandleEndifDirective(Token &EndifToken) {
2137 // Check that this is the whole directive.
2138 CheckEndOfDirective("endif");
2140 PPConditionalInfo CondInfo;
2141 if (CurPPLexer->popConditionalLevel(CondInfo)) {
2142 // No conditionals on the stack: this is an #endif without an #if.
2143 Diag(EndifToken, diag::err_pp_endif_without_if);
2147 // If this the end of a top-level #endif, inform MIOpt.
2148 if (CurPPLexer->getConditionalStackDepth() == 0)
2149 CurPPLexer->MIOpt.ExitTopLevelConditional();
2151 assert(!CondInfo.WasSkipping && !CurPPLexer->LexingRawMode &&
2152 "This code should only be reachable in the non-skipping case!");
2155 Callbacks->Endif(EndifToken.getLocation(), CondInfo.IfLoc);
2158 /// HandleElseDirective - Implements the \#else directive.
2160 void Preprocessor::HandleElseDirective(Token &Result) {
2163 // #else directive in a non-skipping conditional... start skipping.
2164 CheckEndOfDirective("else");
2166 PPConditionalInfo CI;
2167 if (CurPPLexer->popConditionalLevel(CI)) {
2168 Diag(Result, diag::pp_err_else_without_if);
2172 // If this is a top-level #else, inform the MIOpt.
2173 if (CurPPLexer->getConditionalStackDepth() == 0)
2174 CurPPLexer->MIOpt.EnterTopLevelConditional();
2176 // If this is a #else with a #else before it, report the error.
2177 if (CI.FoundElse) Diag(Result, diag::pp_err_else_after_else);
2180 Callbacks->Else(Result.getLocation(), CI.IfLoc);
2182 // Finally, skip the rest of the contents of this block.
2183 SkipExcludedConditionalBlock(CI.IfLoc, /*Foundnonskip*/true,
2184 /*FoundElse*/true, Result.getLocation());
2187 /// HandleElifDirective - Implements the \#elif directive.
2189 void Preprocessor::HandleElifDirective(Token &ElifToken) {
2192 // #elif directive in a non-skipping conditional... start skipping.
2193 // We don't care what the condition is, because we will always skip it (since
2194 // the block immediately before it was included).
2195 const SourceLocation ConditionalBegin = CurPPLexer->getSourceLocation();
2196 DiscardUntilEndOfDirective();
2197 const SourceLocation ConditionalEnd = CurPPLexer->getSourceLocation();
2199 PPConditionalInfo CI;
2200 if (CurPPLexer->popConditionalLevel(CI)) {
2201 Diag(ElifToken, diag::pp_err_elif_without_if);
2205 // If this is a top-level #elif, inform the MIOpt.
2206 if (CurPPLexer->getConditionalStackDepth() == 0)
2207 CurPPLexer->MIOpt.EnterTopLevelConditional();
2209 // If this is a #elif with a #else before it, report the error.
2210 if (CI.FoundElse) Diag(ElifToken, diag::pp_err_elif_after_else);
2213 Callbacks->Elif(ElifToken.getLocation(),
2214 SourceRange(ConditionalBegin, ConditionalEnd), CI.IfLoc);
2216 // Finally, skip the rest of the contents of this block.
2217 SkipExcludedConditionalBlock(CI.IfLoc, /*Foundnonskip*/true,
2218 /*FoundElse*/CI.FoundElse,
2219 ElifToken.getLocation());