1*f4a2713aSLionel Sambuc //===--- PPDirectives.cpp - Directive Handling for Preprocessor -----------===// 2*f4a2713aSLionel Sambuc // 3*f4a2713aSLionel Sambuc // The LLVM Compiler Infrastructure 4*f4a2713aSLionel Sambuc // 5*f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source 6*f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details. 7*f4a2713aSLionel Sambuc // 8*f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===// 9*f4a2713aSLionel Sambuc /// 10*f4a2713aSLionel Sambuc /// \file 11*f4a2713aSLionel Sambuc /// \brief Implements # directive processing for the Preprocessor. 12*f4a2713aSLionel Sambuc /// 13*f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===// 14*f4a2713aSLionel Sambuc 15*f4a2713aSLionel Sambuc #include "clang/Lex/Preprocessor.h" 16*f4a2713aSLionel Sambuc #include "clang/Basic/FileManager.h" 17*f4a2713aSLionel Sambuc #include "clang/Basic/SourceManager.h" 18*f4a2713aSLionel Sambuc #include "clang/Lex/CodeCompletionHandler.h" 19*f4a2713aSLionel Sambuc #include "clang/Lex/HeaderSearch.h" 20*f4a2713aSLionel Sambuc #include "clang/Lex/HeaderSearchOptions.h" 21*f4a2713aSLionel Sambuc #include "clang/Lex/LexDiagnostic.h" 22*f4a2713aSLionel Sambuc #include "clang/Lex/LiteralSupport.h" 23*f4a2713aSLionel Sambuc #include "clang/Lex/MacroInfo.h" 24*f4a2713aSLionel Sambuc #include "clang/Lex/ModuleLoader.h" 25*f4a2713aSLionel Sambuc #include "clang/Lex/Pragma.h" 26*f4a2713aSLionel Sambuc #include "llvm/ADT/APInt.h" 27*f4a2713aSLionel Sambuc #include "llvm/Support/ErrorHandling.h" 28*f4a2713aSLionel Sambuc #include "llvm/Support/SaveAndRestore.h" 29*f4a2713aSLionel Sambuc using namespace clang; 30*f4a2713aSLionel Sambuc 31*f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===// 32*f4a2713aSLionel Sambuc // Utility Methods for Preprocessor Directive Handling. 33*f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===// 34*f4a2713aSLionel Sambuc 35*f4a2713aSLionel Sambuc MacroInfo *Preprocessor::AllocateMacroInfo() { 36*f4a2713aSLionel Sambuc MacroInfoChain *MIChain; 37*f4a2713aSLionel Sambuc 38*f4a2713aSLionel Sambuc if (MICache) { 39*f4a2713aSLionel Sambuc MIChain = MICache; 40*f4a2713aSLionel Sambuc MICache = MICache->Next; 41*f4a2713aSLionel Sambuc } 42*f4a2713aSLionel Sambuc else { 43*f4a2713aSLionel Sambuc MIChain = BP.Allocate<MacroInfoChain>(); 44*f4a2713aSLionel Sambuc } 45*f4a2713aSLionel Sambuc 46*f4a2713aSLionel Sambuc MIChain->Next = MIChainHead; 47*f4a2713aSLionel Sambuc MIChain->Prev = 0; 48*f4a2713aSLionel Sambuc if (MIChainHead) 49*f4a2713aSLionel Sambuc MIChainHead->Prev = MIChain; 50*f4a2713aSLionel Sambuc MIChainHead = MIChain; 51*f4a2713aSLionel Sambuc 52*f4a2713aSLionel Sambuc return &(MIChain->MI); 53*f4a2713aSLionel Sambuc } 54*f4a2713aSLionel Sambuc 55*f4a2713aSLionel Sambuc MacroInfo *Preprocessor::AllocateMacroInfo(SourceLocation L) { 56*f4a2713aSLionel Sambuc MacroInfo *MI = AllocateMacroInfo(); 57*f4a2713aSLionel Sambuc new (MI) MacroInfo(L); 58*f4a2713aSLionel Sambuc return MI; 59*f4a2713aSLionel Sambuc } 60*f4a2713aSLionel Sambuc 61*f4a2713aSLionel Sambuc MacroInfo *Preprocessor::AllocateDeserializedMacroInfo(SourceLocation L, 62*f4a2713aSLionel Sambuc unsigned SubModuleID) { 63*f4a2713aSLionel Sambuc LLVM_STATIC_ASSERT(llvm::AlignOf<MacroInfo>::Alignment >= sizeof(SubModuleID), 64*f4a2713aSLionel Sambuc "alignment for MacroInfo is less than the ID"); 65*f4a2713aSLionel Sambuc DeserializedMacroInfoChain *MIChain = 66*f4a2713aSLionel Sambuc BP.Allocate<DeserializedMacroInfoChain>(); 67*f4a2713aSLionel Sambuc MIChain->Next = DeserialMIChainHead; 68*f4a2713aSLionel Sambuc DeserialMIChainHead = MIChain; 69*f4a2713aSLionel Sambuc 70*f4a2713aSLionel Sambuc MacroInfo *MI = &MIChain->MI; 71*f4a2713aSLionel Sambuc new (MI) MacroInfo(L); 72*f4a2713aSLionel Sambuc MI->FromASTFile = true; 73*f4a2713aSLionel Sambuc MI->setOwningModuleID(SubModuleID); 74*f4a2713aSLionel Sambuc return MI; 75*f4a2713aSLionel Sambuc } 76*f4a2713aSLionel Sambuc 77*f4a2713aSLionel Sambuc DefMacroDirective * 78*f4a2713aSLionel Sambuc Preprocessor::AllocateDefMacroDirective(MacroInfo *MI, SourceLocation Loc, 79*f4a2713aSLionel Sambuc bool isImported) { 80*f4a2713aSLionel Sambuc DefMacroDirective *MD = BP.Allocate<DefMacroDirective>(); 81*f4a2713aSLionel Sambuc new (MD) DefMacroDirective(MI, Loc, isImported); 82*f4a2713aSLionel Sambuc return MD; 83*f4a2713aSLionel Sambuc } 84*f4a2713aSLionel Sambuc 85*f4a2713aSLionel Sambuc UndefMacroDirective * 86*f4a2713aSLionel Sambuc Preprocessor::AllocateUndefMacroDirective(SourceLocation UndefLoc) { 87*f4a2713aSLionel Sambuc UndefMacroDirective *MD = BP.Allocate<UndefMacroDirective>(); 88*f4a2713aSLionel Sambuc new (MD) UndefMacroDirective(UndefLoc); 89*f4a2713aSLionel Sambuc return MD; 90*f4a2713aSLionel Sambuc } 91*f4a2713aSLionel Sambuc 92*f4a2713aSLionel Sambuc VisibilityMacroDirective * 93*f4a2713aSLionel Sambuc Preprocessor::AllocateVisibilityMacroDirective(SourceLocation Loc, 94*f4a2713aSLionel Sambuc bool isPublic) { 95*f4a2713aSLionel Sambuc VisibilityMacroDirective *MD = BP.Allocate<VisibilityMacroDirective>(); 96*f4a2713aSLionel Sambuc new (MD) VisibilityMacroDirective(Loc, isPublic); 97*f4a2713aSLionel Sambuc return MD; 98*f4a2713aSLionel Sambuc } 99*f4a2713aSLionel Sambuc 100*f4a2713aSLionel Sambuc /// \brief Release the specified MacroInfo to be reused for allocating 101*f4a2713aSLionel Sambuc /// new MacroInfo objects. 102*f4a2713aSLionel Sambuc void Preprocessor::ReleaseMacroInfo(MacroInfo *MI) { 103*f4a2713aSLionel Sambuc MacroInfoChain *MIChain = (MacroInfoChain*) MI; 104*f4a2713aSLionel Sambuc if (MacroInfoChain *Prev = MIChain->Prev) { 105*f4a2713aSLionel Sambuc MacroInfoChain *Next = MIChain->Next; 106*f4a2713aSLionel Sambuc Prev->Next = Next; 107*f4a2713aSLionel Sambuc if (Next) 108*f4a2713aSLionel Sambuc Next->Prev = Prev; 109*f4a2713aSLionel Sambuc } 110*f4a2713aSLionel Sambuc else { 111*f4a2713aSLionel Sambuc assert(MIChainHead == MIChain); 112*f4a2713aSLionel Sambuc MIChainHead = MIChain->Next; 113*f4a2713aSLionel Sambuc MIChainHead->Prev = 0; 114*f4a2713aSLionel Sambuc } 115*f4a2713aSLionel Sambuc MIChain->Next = MICache; 116*f4a2713aSLionel Sambuc MICache = MIChain; 117*f4a2713aSLionel Sambuc 118*f4a2713aSLionel Sambuc MI->Destroy(); 119*f4a2713aSLionel Sambuc } 120*f4a2713aSLionel Sambuc 121*f4a2713aSLionel Sambuc /// \brief Read and discard all tokens remaining on the current line until 122*f4a2713aSLionel Sambuc /// the tok::eod token is found. 123*f4a2713aSLionel Sambuc void Preprocessor::DiscardUntilEndOfDirective() { 124*f4a2713aSLionel Sambuc Token Tmp; 125*f4a2713aSLionel Sambuc do { 126*f4a2713aSLionel Sambuc LexUnexpandedToken(Tmp); 127*f4a2713aSLionel Sambuc assert(Tmp.isNot(tok::eof) && "EOF seen while discarding directive tokens"); 128*f4a2713aSLionel Sambuc } while (Tmp.isNot(tok::eod)); 129*f4a2713aSLionel Sambuc } 130*f4a2713aSLionel Sambuc 131*f4a2713aSLionel Sambuc /// \brief Lex and validate a macro name, which occurs after a 132*f4a2713aSLionel Sambuc /// \#define or \#undef. 133*f4a2713aSLionel Sambuc /// 134*f4a2713aSLionel Sambuc /// This sets the token kind to eod and discards the rest 135*f4a2713aSLionel Sambuc /// of the macro line if the macro name is invalid. \p isDefineUndef is 1 if 136*f4a2713aSLionel Sambuc /// this is due to a a \#define, 2 if \#undef directive, 0 if it is something 137*f4a2713aSLionel Sambuc /// else (e.g. \#ifdef). 138*f4a2713aSLionel Sambuc void Preprocessor::ReadMacroName(Token &MacroNameTok, char isDefineUndef) { 139*f4a2713aSLionel Sambuc // Read the token, don't allow macro expansion on it. 140*f4a2713aSLionel Sambuc LexUnexpandedToken(MacroNameTok); 141*f4a2713aSLionel Sambuc 142*f4a2713aSLionel Sambuc if (MacroNameTok.is(tok::code_completion)) { 143*f4a2713aSLionel Sambuc if (CodeComplete) 144*f4a2713aSLionel Sambuc CodeComplete->CodeCompleteMacroName(isDefineUndef == 1); 145*f4a2713aSLionel Sambuc setCodeCompletionReached(); 146*f4a2713aSLionel Sambuc LexUnexpandedToken(MacroNameTok); 147*f4a2713aSLionel Sambuc } 148*f4a2713aSLionel Sambuc 149*f4a2713aSLionel Sambuc // Missing macro name? 150*f4a2713aSLionel Sambuc if (MacroNameTok.is(tok::eod)) { 151*f4a2713aSLionel Sambuc Diag(MacroNameTok, diag::err_pp_missing_macro_name); 152*f4a2713aSLionel Sambuc return; 153*f4a2713aSLionel Sambuc } 154*f4a2713aSLionel Sambuc 155*f4a2713aSLionel Sambuc IdentifierInfo *II = MacroNameTok.getIdentifierInfo(); 156*f4a2713aSLionel Sambuc if (II == 0) { 157*f4a2713aSLionel Sambuc bool Invalid = false; 158*f4a2713aSLionel Sambuc std::string Spelling = getSpelling(MacroNameTok, &Invalid); 159*f4a2713aSLionel Sambuc if (Invalid) 160*f4a2713aSLionel Sambuc return; 161*f4a2713aSLionel Sambuc 162*f4a2713aSLionel Sambuc const IdentifierInfo &Info = Identifiers.get(Spelling); 163*f4a2713aSLionel Sambuc 164*f4a2713aSLionel Sambuc // Allow #defining |and| and friends in microsoft mode. 165*f4a2713aSLionel Sambuc if (Info.isCPlusPlusOperatorKeyword() && getLangOpts().MicrosoftMode) { 166*f4a2713aSLionel Sambuc MacroNameTok.setIdentifierInfo(getIdentifierInfo(Spelling)); 167*f4a2713aSLionel Sambuc return; 168*f4a2713aSLionel Sambuc } 169*f4a2713aSLionel Sambuc 170*f4a2713aSLionel Sambuc if (Info.isCPlusPlusOperatorKeyword()) 171*f4a2713aSLionel Sambuc // C++ 2.5p2: Alternative tokens behave the same as its primary token 172*f4a2713aSLionel Sambuc // except for their spellings. 173*f4a2713aSLionel Sambuc Diag(MacroNameTok, diag::err_pp_operator_used_as_macro_name) << Spelling; 174*f4a2713aSLionel Sambuc else 175*f4a2713aSLionel Sambuc Diag(MacroNameTok, diag::err_pp_macro_not_identifier); 176*f4a2713aSLionel Sambuc // Fall through on error. 177*f4a2713aSLionel Sambuc } else if (isDefineUndef && II->getPPKeywordID() == tok::pp_defined) { 178*f4a2713aSLionel Sambuc // Error if defining "defined": C99 6.10.8/4, C++ [cpp.predefined]p4. 179*f4a2713aSLionel Sambuc Diag(MacroNameTok, diag::err_defined_macro_name); 180*f4a2713aSLionel Sambuc } else if (isDefineUndef == 2 && II->hasMacroDefinition() && 181*f4a2713aSLionel Sambuc getMacroInfo(II)->isBuiltinMacro()) { 182*f4a2713aSLionel Sambuc // Warn if undefining "__LINE__" and other builtins, per C99 6.10.8/4 183*f4a2713aSLionel Sambuc // and C++ [cpp.predefined]p4], but allow it as an extension. 184*f4a2713aSLionel Sambuc Diag(MacroNameTok, diag::ext_pp_undef_builtin_macro); 185*f4a2713aSLionel Sambuc return; 186*f4a2713aSLionel Sambuc } else { 187*f4a2713aSLionel Sambuc // Okay, we got a good identifier node. Return it. 188*f4a2713aSLionel Sambuc return; 189*f4a2713aSLionel Sambuc } 190*f4a2713aSLionel Sambuc 191*f4a2713aSLionel Sambuc // Invalid macro name, read and discard the rest of the line. Then set the 192*f4a2713aSLionel Sambuc // token kind to tok::eod. 193*f4a2713aSLionel Sambuc MacroNameTok.setKind(tok::eod); 194*f4a2713aSLionel Sambuc return DiscardUntilEndOfDirective(); 195*f4a2713aSLionel Sambuc } 196*f4a2713aSLionel Sambuc 197*f4a2713aSLionel Sambuc /// \brief Ensure that the next token is a tok::eod token. 198*f4a2713aSLionel Sambuc /// 199*f4a2713aSLionel Sambuc /// If not, emit a diagnostic and consume up until the eod. If EnableMacros is 200*f4a2713aSLionel Sambuc /// true, then we consider macros that expand to zero tokens as being ok. 201*f4a2713aSLionel Sambuc void Preprocessor::CheckEndOfDirective(const char *DirType, bool EnableMacros) { 202*f4a2713aSLionel Sambuc Token Tmp; 203*f4a2713aSLionel Sambuc // Lex unexpanded tokens for most directives: macros might expand to zero 204*f4a2713aSLionel Sambuc // tokens, causing us to miss diagnosing invalid lines. Some directives (like 205*f4a2713aSLionel Sambuc // #line) allow empty macros. 206*f4a2713aSLionel Sambuc if (EnableMacros) 207*f4a2713aSLionel Sambuc Lex(Tmp); 208*f4a2713aSLionel Sambuc else 209*f4a2713aSLionel Sambuc LexUnexpandedToken(Tmp); 210*f4a2713aSLionel Sambuc 211*f4a2713aSLionel Sambuc // There should be no tokens after the directive, but we allow them as an 212*f4a2713aSLionel Sambuc // extension. 213*f4a2713aSLionel Sambuc while (Tmp.is(tok::comment)) // Skip comments in -C mode. 214*f4a2713aSLionel Sambuc LexUnexpandedToken(Tmp); 215*f4a2713aSLionel Sambuc 216*f4a2713aSLionel Sambuc if (Tmp.isNot(tok::eod)) { 217*f4a2713aSLionel Sambuc // Add a fixit in GNU/C99/C++ mode. Don't offer a fixit for strict-C89, 218*f4a2713aSLionel Sambuc // or if this is a macro-style preprocessing directive, because it is more 219*f4a2713aSLionel Sambuc // trouble than it is worth to insert /**/ and check that there is no /**/ 220*f4a2713aSLionel Sambuc // in the range also. 221*f4a2713aSLionel Sambuc FixItHint Hint; 222*f4a2713aSLionel Sambuc if ((LangOpts.GNUMode || LangOpts.C99 || LangOpts.CPlusPlus) && 223*f4a2713aSLionel Sambuc !CurTokenLexer) 224*f4a2713aSLionel Sambuc Hint = FixItHint::CreateInsertion(Tmp.getLocation(),"//"); 225*f4a2713aSLionel Sambuc Diag(Tmp, diag::ext_pp_extra_tokens_at_eol) << DirType << Hint; 226*f4a2713aSLionel Sambuc DiscardUntilEndOfDirective(); 227*f4a2713aSLionel Sambuc } 228*f4a2713aSLionel Sambuc } 229*f4a2713aSLionel Sambuc 230*f4a2713aSLionel Sambuc 231*f4a2713aSLionel Sambuc 232*f4a2713aSLionel Sambuc /// SkipExcludedConditionalBlock - We just read a \#if or related directive and 233*f4a2713aSLionel Sambuc /// decided that the subsequent tokens are in the \#if'd out portion of the 234*f4a2713aSLionel Sambuc /// file. Lex the rest of the file, until we see an \#endif. If 235*f4a2713aSLionel Sambuc /// FoundNonSkipPortion is true, then we have already emitted code for part of 236*f4a2713aSLionel Sambuc /// this \#if directive, so \#else/\#elif blocks should never be entered. 237*f4a2713aSLionel Sambuc /// If ElseOk is true, then \#else directives are ok, if not, then we have 238*f4a2713aSLionel Sambuc /// already seen one so a \#else directive is a duplicate. When this returns, 239*f4a2713aSLionel Sambuc /// the caller can lex the first valid token. 240*f4a2713aSLionel Sambuc void Preprocessor::SkipExcludedConditionalBlock(SourceLocation IfTokenLoc, 241*f4a2713aSLionel Sambuc bool FoundNonSkipPortion, 242*f4a2713aSLionel Sambuc bool FoundElse, 243*f4a2713aSLionel Sambuc SourceLocation ElseLoc) { 244*f4a2713aSLionel Sambuc ++NumSkipped; 245*f4a2713aSLionel Sambuc assert(!CurTokenLexer && CurPPLexer && "Lexing a macro, not a file?"); 246*f4a2713aSLionel Sambuc 247*f4a2713aSLionel Sambuc CurPPLexer->pushConditionalLevel(IfTokenLoc, /*isSkipping*/false, 248*f4a2713aSLionel Sambuc FoundNonSkipPortion, FoundElse); 249*f4a2713aSLionel Sambuc 250*f4a2713aSLionel Sambuc if (CurPTHLexer) { 251*f4a2713aSLionel Sambuc PTHSkipExcludedConditionalBlock(); 252*f4a2713aSLionel Sambuc return; 253*f4a2713aSLionel Sambuc } 254*f4a2713aSLionel Sambuc 255*f4a2713aSLionel Sambuc // Enter raw mode to disable identifier lookup (and thus macro expansion), 256*f4a2713aSLionel Sambuc // disabling warnings, etc. 257*f4a2713aSLionel Sambuc CurPPLexer->LexingRawMode = true; 258*f4a2713aSLionel Sambuc Token Tok; 259*f4a2713aSLionel Sambuc while (1) { 260*f4a2713aSLionel Sambuc CurLexer->Lex(Tok); 261*f4a2713aSLionel Sambuc 262*f4a2713aSLionel Sambuc if (Tok.is(tok::code_completion)) { 263*f4a2713aSLionel Sambuc if (CodeComplete) 264*f4a2713aSLionel Sambuc CodeComplete->CodeCompleteInConditionalExclusion(); 265*f4a2713aSLionel Sambuc setCodeCompletionReached(); 266*f4a2713aSLionel Sambuc continue; 267*f4a2713aSLionel Sambuc } 268*f4a2713aSLionel Sambuc 269*f4a2713aSLionel Sambuc // If this is the end of the buffer, we have an error. 270*f4a2713aSLionel Sambuc if (Tok.is(tok::eof)) { 271*f4a2713aSLionel Sambuc // Emit errors for each unterminated conditional on the stack, including 272*f4a2713aSLionel Sambuc // the current one. 273*f4a2713aSLionel Sambuc while (!CurPPLexer->ConditionalStack.empty()) { 274*f4a2713aSLionel Sambuc if (CurLexer->getFileLoc() != CodeCompletionFileLoc) 275*f4a2713aSLionel Sambuc Diag(CurPPLexer->ConditionalStack.back().IfLoc, 276*f4a2713aSLionel Sambuc diag::err_pp_unterminated_conditional); 277*f4a2713aSLionel Sambuc CurPPLexer->ConditionalStack.pop_back(); 278*f4a2713aSLionel Sambuc } 279*f4a2713aSLionel Sambuc 280*f4a2713aSLionel Sambuc // Just return and let the caller lex after this #include. 281*f4a2713aSLionel Sambuc break; 282*f4a2713aSLionel Sambuc } 283*f4a2713aSLionel Sambuc 284*f4a2713aSLionel Sambuc // If this token is not a preprocessor directive, just skip it. 285*f4a2713aSLionel Sambuc if (Tok.isNot(tok::hash) || !Tok.isAtStartOfLine()) 286*f4a2713aSLionel Sambuc continue; 287*f4a2713aSLionel Sambuc 288*f4a2713aSLionel Sambuc // We just parsed a # character at the start of a line, so we're in 289*f4a2713aSLionel Sambuc // directive mode. Tell the lexer this so any newlines we see will be 290*f4a2713aSLionel Sambuc // converted into an EOD token (this terminates the macro). 291*f4a2713aSLionel Sambuc CurPPLexer->ParsingPreprocessorDirective = true; 292*f4a2713aSLionel Sambuc if (CurLexer) CurLexer->SetKeepWhitespaceMode(false); 293*f4a2713aSLionel Sambuc 294*f4a2713aSLionel Sambuc 295*f4a2713aSLionel Sambuc // Read the next token, the directive flavor. 296*f4a2713aSLionel Sambuc LexUnexpandedToken(Tok); 297*f4a2713aSLionel Sambuc 298*f4a2713aSLionel Sambuc // If this isn't an identifier directive (e.g. is "# 1\n" or "#\n", or 299*f4a2713aSLionel Sambuc // something bogus), skip it. 300*f4a2713aSLionel Sambuc if (Tok.isNot(tok::raw_identifier)) { 301*f4a2713aSLionel Sambuc CurPPLexer->ParsingPreprocessorDirective = false; 302*f4a2713aSLionel Sambuc // Restore comment saving mode. 303*f4a2713aSLionel Sambuc if (CurLexer) CurLexer->resetExtendedTokenMode(); 304*f4a2713aSLionel Sambuc continue; 305*f4a2713aSLionel Sambuc } 306*f4a2713aSLionel Sambuc 307*f4a2713aSLionel Sambuc // If the first letter isn't i or e, it isn't intesting to us. We know that 308*f4a2713aSLionel Sambuc // this is safe in the face of spelling differences, because there is no way 309*f4a2713aSLionel Sambuc // to spell an i/e in a strange way that is another letter. Skipping this 310*f4a2713aSLionel Sambuc // allows us to avoid looking up the identifier info for #define/#undef and 311*f4a2713aSLionel Sambuc // other common directives. 312*f4a2713aSLionel Sambuc const char *RawCharData = Tok.getRawIdentifierData(); 313*f4a2713aSLionel Sambuc 314*f4a2713aSLionel Sambuc char FirstChar = RawCharData[0]; 315*f4a2713aSLionel Sambuc if (FirstChar >= 'a' && FirstChar <= 'z' && 316*f4a2713aSLionel Sambuc FirstChar != 'i' && FirstChar != 'e') { 317*f4a2713aSLionel Sambuc CurPPLexer->ParsingPreprocessorDirective = false; 318*f4a2713aSLionel Sambuc // Restore comment saving mode. 319*f4a2713aSLionel Sambuc if (CurLexer) CurLexer->resetExtendedTokenMode(); 320*f4a2713aSLionel Sambuc continue; 321*f4a2713aSLionel Sambuc } 322*f4a2713aSLionel Sambuc 323*f4a2713aSLionel Sambuc // Get the identifier name without trigraphs or embedded newlines. Note 324*f4a2713aSLionel Sambuc // that we can't use Tok.getIdentifierInfo() because its lookup is disabled 325*f4a2713aSLionel Sambuc // when skipping. 326*f4a2713aSLionel Sambuc char DirectiveBuf[20]; 327*f4a2713aSLionel Sambuc StringRef Directive; 328*f4a2713aSLionel Sambuc if (!Tok.needsCleaning() && Tok.getLength() < 20) { 329*f4a2713aSLionel Sambuc Directive = StringRef(RawCharData, Tok.getLength()); 330*f4a2713aSLionel Sambuc } else { 331*f4a2713aSLionel Sambuc std::string DirectiveStr = getSpelling(Tok); 332*f4a2713aSLionel Sambuc unsigned IdLen = DirectiveStr.size(); 333*f4a2713aSLionel Sambuc if (IdLen >= 20) { 334*f4a2713aSLionel Sambuc CurPPLexer->ParsingPreprocessorDirective = false; 335*f4a2713aSLionel Sambuc // Restore comment saving mode. 336*f4a2713aSLionel Sambuc if (CurLexer) CurLexer->resetExtendedTokenMode(); 337*f4a2713aSLionel Sambuc continue; 338*f4a2713aSLionel Sambuc } 339*f4a2713aSLionel Sambuc memcpy(DirectiveBuf, &DirectiveStr[0], IdLen); 340*f4a2713aSLionel Sambuc Directive = StringRef(DirectiveBuf, IdLen); 341*f4a2713aSLionel Sambuc } 342*f4a2713aSLionel Sambuc 343*f4a2713aSLionel Sambuc if (Directive.startswith("if")) { 344*f4a2713aSLionel Sambuc StringRef Sub = Directive.substr(2); 345*f4a2713aSLionel Sambuc if (Sub.empty() || // "if" 346*f4a2713aSLionel Sambuc Sub == "def" || // "ifdef" 347*f4a2713aSLionel Sambuc Sub == "ndef") { // "ifndef" 348*f4a2713aSLionel Sambuc // We know the entire #if/#ifdef/#ifndef block will be skipped, don't 349*f4a2713aSLionel Sambuc // bother parsing the condition. 350*f4a2713aSLionel Sambuc DiscardUntilEndOfDirective(); 351*f4a2713aSLionel Sambuc CurPPLexer->pushConditionalLevel(Tok.getLocation(), /*wasskipping*/true, 352*f4a2713aSLionel Sambuc /*foundnonskip*/false, 353*f4a2713aSLionel Sambuc /*foundelse*/false); 354*f4a2713aSLionel Sambuc } 355*f4a2713aSLionel Sambuc } else if (Directive[0] == 'e') { 356*f4a2713aSLionel Sambuc StringRef Sub = Directive.substr(1); 357*f4a2713aSLionel Sambuc if (Sub == "ndif") { // "endif" 358*f4a2713aSLionel Sambuc PPConditionalInfo CondInfo; 359*f4a2713aSLionel Sambuc CondInfo.WasSkipping = true; // Silence bogus warning. 360*f4a2713aSLionel Sambuc bool InCond = CurPPLexer->popConditionalLevel(CondInfo); 361*f4a2713aSLionel Sambuc (void)InCond; // Silence warning in no-asserts mode. 362*f4a2713aSLionel Sambuc assert(!InCond && "Can't be skipping if not in a conditional!"); 363*f4a2713aSLionel Sambuc 364*f4a2713aSLionel Sambuc // If we popped the outermost skipping block, we're done skipping! 365*f4a2713aSLionel Sambuc if (!CondInfo.WasSkipping) { 366*f4a2713aSLionel Sambuc // Restore the value of LexingRawMode so that trailing comments 367*f4a2713aSLionel Sambuc // are handled correctly, if we've reached the outermost block. 368*f4a2713aSLionel Sambuc CurPPLexer->LexingRawMode = false; 369*f4a2713aSLionel Sambuc CheckEndOfDirective("endif"); 370*f4a2713aSLionel Sambuc CurPPLexer->LexingRawMode = true; 371*f4a2713aSLionel Sambuc if (Callbacks) 372*f4a2713aSLionel Sambuc Callbacks->Endif(Tok.getLocation(), CondInfo.IfLoc); 373*f4a2713aSLionel Sambuc break; 374*f4a2713aSLionel Sambuc } else { 375*f4a2713aSLionel Sambuc DiscardUntilEndOfDirective(); 376*f4a2713aSLionel Sambuc } 377*f4a2713aSLionel Sambuc } else if (Sub == "lse") { // "else". 378*f4a2713aSLionel Sambuc // #else directive in a skipping conditional. If not in some other 379*f4a2713aSLionel Sambuc // skipping conditional, and if #else hasn't already been seen, enter it 380*f4a2713aSLionel Sambuc // as a non-skipping conditional. 381*f4a2713aSLionel Sambuc PPConditionalInfo &CondInfo = CurPPLexer->peekConditionalLevel(); 382*f4a2713aSLionel Sambuc 383*f4a2713aSLionel Sambuc // If this is a #else with a #else before it, report the error. 384*f4a2713aSLionel Sambuc if (CondInfo.FoundElse) Diag(Tok, diag::pp_err_else_after_else); 385*f4a2713aSLionel Sambuc 386*f4a2713aSLionel Sambuc // Note that we've seen a #else in this conditional. 387*f4a2713aSLionel Sambuc CondInfo.FoundElse = true; 388*f4a2713aSLionel Sambuc 389*f4a2713aSLionel Sambuc // If the conditional is at the top level, and the #if block wasn't 390*f4a2713aSLionel Sambuc // entered, enter the #else block now. 391*f4a2713aSLionel Sambuc if (!CondInfo.WasSkipping && !CondInfo.FoundNonSkip) { 392*f4a2713aSLionel Sambuc CondInfo.FoundNonSkip = true; 393*f4a2713aSLionel Sambuc // Restore the value of LexingRawMode so that trailing comments 394*f4a2713aSLionel Sambuc // are handled correctly. 395*f4a2713aSLionel Sambuc CurPPLexer->LexingRawMode = false; 396*f4a2713aSLionel Sambuc CheckEndOfDirective("else"); 397*f4a2713aSLionel Sambuc CurPPLexer->LexingRawMode = true; 398*f4a2713aSLionel Sambuc if (Callbacks) 399*f4a2713aSLionel Sambuc Callbacks->Else(Tok.getLocation(), CondInfo.IfLoc); 400*f4a2713aSLionel Sambuc break; 401*f4a2713aSLionel Sambuc } else { 402*f4a2713aSLionel Sambuc DiscardUntilEndOfDirective(); // C99 6.10p4. 403*f4a2713aSLionel Sambuc } 404*f4a2713aSLionel Sambuc } else if (Sub == "lif") { // "elif". 405*f4a2713aSLionel Sambuc PPConditionalInfo &CondInfo = CurPPLexer->peekConditionalLevel(); 406*f4a2713aSLionel Sambuc 407*f4a2713aSLionel Sambuc bool ShouldEnter; 408*f4a2713aSLionel Sambuc const SourceLocation ConditionalBegin = CurPPLexer->getSourceLocation(); 409*f4a2713aSLionel Sambuc // If this is in a skipping block or if we're already handled this #if 410*f4a2713aSLionel Sambuc // block, don't bother parsing the condition. 411*f4a2713aSLionel Sambuc if (CondInfo.WasSkipping || CondInfo.FoundNonSkip) { 412*f4a2713aSLionel Sambuc DiscardUntilEndOfDirective(); 413*f4a2713aSLionel Sambuc ShouldEnter = false; 414*f4a2713aSLionel Sambuc } else { 415*f4a2713aSLionel Sambuc // Restore the value of LexingRawMode so that identifiers are 416*f4a2713aSLionel Sambuc // looked up, etc, inside the #elif expression. 417*f4a2713aSLionel Sambuc assert(CurPPLexer->LexingRawMode && "We have to be skipping here!"); 418*f4a2713aSLionel Sambuc CurPPLexer->LexingRawMode = false; 419*f4a2713aSLionel Sambuc IdentifierInfo *IfNDefMacro = 0; 420*f4a2713aSLionel Sambuc ShouldEnter = EvaluateDirectiveExpression(IfNDefMacro); 421*f4a2713aSLionel Sambuc CurPPLexer->LexingRawMode = true; 422*f4a2713aSLionel Sambuc } 423*f4a2713aSLionel Sambuc const SourceLocation ConditionalEnd = CurPPLexer->getSourceLocation(); 424*f4a2713aSLionel Sambuc 425*f4a2713aSLionel Sambuc // If this is a #elif with a #else before it, report the error. 426*f4a2713aSLionel Sambuc if (CondInfo.FoundElse) Diag(Tok, diag::pp_err_elif_after_else); 427*f4a2713aSLionel Sambuc 428*f4a2713aSLionel Sambuc // If this condition is true, enter it! 429*f4a2713aSLionel Sambuc if (ShouldEnter) { 430*f4a2713aSLionel Sambuc CondInfo.FoundNonSkip = true; 431*f4a2713aSLionel Sambuc if (Callbacks) 432*f4a2713aSLionel Sambuc Callbacks->Elif(Tok.getLocation(), 433*f4a2713aSLionel Sambuc SourceRange(ConditionalBegin, ConditionalEnd), 434*f4a2713aSLionel Sambuc ShouldEnter, CondInfo.IfLoc); 435*f4a2713aSLionel Sambuc break; 436*f4a2713aSLionel Sambuc } 437*f4a2713aSLionel Sambuc } 438*f4a2713aSLionel Sambuc } 439*f4a2713aSLionel Sambuc 440*f4a2713aSLionel Sambuc CurPPLexer->ParsingPreprocessorDirective = false; 441*f4a2713aSLionel Sambuc // Restore comment saving mode. 442*f4a2713aSLionel Sambuc if (CurLexer) CurLexer->resetExtendedTokenMode(); 443*f4a2713aSLionel Sambuc } 444*f4a2713aSLionel Sambuc 445*f4a2713aSLionel Sambuc // Finally, if we are out of the conditional (saw an #endif or ran off the end 446*f4a2713aSLionel Sambuc // of the file, just stop skipping and return to lexing whatever came after 447*f4a2713aSLionel Sambuc // the #if block. 448*f4a2713aSLionel Sambuc CurPPLexer->LexingRawMode = false; 449*f4a2713aSLionel Sambuc 450*f4a2713aSLionel Sambuc if (Callbacks) { 451*f4a2713aSLionel Sambuc SourceLocation BeginLoc = ElseLoc.isValid() ? ElseLoc : IfTokenLoc; 452*f4a2713aSLionel Sambuc Callbacks->SourceRangeSkipped(SourceRange(BeginLoc, Tok.getLocation())); 453*f4a2713aSLionel Sambuc } 454*f4a2713aSLionel Sambuc } 455*f4a2713aSLionel Sambuc 456*f4a2713aSLionel Sambuc void Preprocessor::PTHSkipExcludedConditionalBlock() { 457*f4a2713aSLionel Sambuc 458*f4a2713aSLionel Sambuc while (1) { 459*f4a2713aSLionel Sambuc assert(CurPTHLexer); 460*f4a2713aSLionel Sambuc assert(CurPTHLexer->LexingRawMode == false); 461*f4a2713aSLionel Sambuc 462*f4a2713aSLionel Sambuc // Skip to the next '#else', '#elif', or #endif. 463*f4a2713aSLionel Sambuc if (CurPTHLexer->SkipBlock()) { 464*f4a2713aSLionel Sambuc // We have reached an #endif. Both the '#' and 'endif' tokens 465*f4a2713aSLionel Sambuc // have been consumed by the PTHLexer. Just pop off the condition level. 466*f4a2713aSLionel Sambuc PPConditionalInfo CondInfo; 467*f4a2713aSLionel Sambuc bool InCond = CurPTHLexer->popConditionalLevel(CondInfo); 468*f4a2713aSLionel Sambuc (void)InCond; // Silence warning in no-asserts mode. 469*f4a2713aSLionel Sambuc assert(!InCond && "Can't be skipping if not in a conditional!"); 470*f4a2713aSLionel Sambuc break; 471*f4a2713aSLionel Sambuc } 472*f4a2713aSLionel Sambuc 473*f4a2713aSLionel Sambuc // We have reached a '#else' or '#elif'. Lex the next token to get 474*f4a2713aSLionel Sambuc // the directive flavor. 475*f4a2713aSLionel Sambuc Token Tok; 476*f4a2713aSLionel Sambuc LexUnexpandedToken(Tok); 477*f4a2713aSLionel Sambuc 478*f4a2713aSLionel Sambuc // We can actually look up the IdentifierInfo here since we aren't in 479*f4a2713aSLionel Sambuc // raw mode. 480*f4a2713aSLionel Sambuc tok::PPKeywordKind K = Tok.getIdentifierInfo()->getPPKeywordID(); 481*f4a2713aSLionel Sambuc 482*f4a2713aSLionel Sambuc if (K == tok::pp_else) { 483*f4a2713aSLionel Sambuc // #else: Enter the else condition. We aren't in a nested condition 484*f4a2713aSLionel Sambuc // since we skip those. We're always in the one matching the last 485*f4a2713aSLionel Sambuc // blocked we skipped. 486*f4a2713aSLionel Sambuc PPConditionalInfo &CondInfo = CurPTHLexer->peekConditionalLevel(); 487*f4a2713aSLionel Sambuc // Note that we've seen a #else in this conditional. 488*f4a2713aSLionel Sambuc CondInfo.FoundElse = true; 489*f4a2713aSLionel Sambuc 490*f4a2713aSLionel Sambuc // If the #if block wasn't entered then enter the #else block now. 491*f4a2713aSLionel Sambuc if (!CondInfo.FoundNonSkip) { 492*f4a2713aSLionel Sambuc CondInfo.FoundNonSkip = true; 493*f4a2713aSLionel Sambuc 494*f4a2713aSLionel Sambuc // Scan until the eod token. 495*f4a2713aSLionel Sambuc CurPTHLexer->ParsingPreprocessorDirective = true; 496*f4a2713aSLionel Sambuc DiscardUntilEndOfDirective(); 497*f4a2713aSLionel Sambuc CurPTHLexer->ParsingPreprocessorDirective = false; 498*f4a2713aSLionel Sambuc 499*f4a2713aSLionel Sambuc break; 500*f4a2713aSLionel Sambuc } 501*f4a2713aSLionel Sambuc 502*f4a2713aSLionel Sambuc // Otherwise skip this block. 503*f4a2713aSLionel Sambuc continue; 504*f4a2713aSLionel Sambuc } 505*f4a2713aSLionel Sambuc 506*f4a2713aSLionel Sambuc assert(K == tok::pp_elif); 507*f4a2713aSLionel Sambuc PPConditionalInfo &CondInfo = CurPTHLexer->peekConditionalLevel(); 508*f4a2713aSLionel Sambuc 509*f4a2713aSLionel Sambuc // If this is a #elif with a #else before it, report the error. 510*f4a2713aSLionel Sambuc if (CondInfo.FoundElse) 511*f4a2713aSLionel Sambuc Diag(Tok, diag::pp_err_elif_after_else); 512*f4a2713aSLionel Sambuc 513*f4a2713aSLionel Sambuc // If this is in a skipping block or if we're already handled this #if 514*f4a2713aSLionel Sambuc // block, don't bother parsing the condition. We just skip this block. 515*f4a2713aSLionel Sambuc if (CondInfo.FoundNonSkip) 516*f4a2713aSLionel Sambuc continue; 517*f4a2713aSLionel Sambuc 518*f4a2713aSLionel Sambuc // Evaluate the condition of the #elif. 519*f4a2713aSLionel Sambuc IdentifierInfo *IfNDefMacro = 0; 520*f4a2713aSLionel Sambuc CurPTHLexer->ParsingPreprocessorDirective = true; 521*f4a2713aSLionel Sambuc bool ShouldEnter = EvaluateDirectiveExpression(IfNDefMacro); 522*f4a2713aSLionel Sambuc CurPTHLexer->ParsingPreprocessorDirective = false; 523*f4a2713aSLionel Sambuc 524*f4a2713aSLionel Sambuc // If this condition is true, enter it! 525*f4a2713aSLionel Sambuc if (ShouldEnter) { 526*f4a2713aSLionel Sambuc CondInfo.FoundNonSkip = true; 527*f4a2713aSLionel Sambuc break; 528*f4a2713aSLionel Sambuc } 529*f4a2713aSLionel Sambuc 530*f4a2713aSLionel Sambuc // Otherwise, skip this block and go to the next one. 531*f4a2713aSLionel Sambuc continue; 532*f4a2713aSLionel Sambuc } 533*f4a2713aSLionel Sambuc } 534*f4a2713aSLionel Sambuc 535*f4a2713aSLionel Sambuc Module *Preprocessor::getModuleForLocation(SourceLocation FilenameLoc) { 536*f4a2713aSLionel Sambuc ModuleMap &ModMap = HeaderInfo.getModuleMap(); 537*f4a2713aSLionel Sambuc if (SourceMgr.isInMainFile(FilenameLoc)) { 538*f4a2713aSLionel Sambuc if (Module *CurMod = getCurrentModule()) 539*f4a2713aSLionel Sambuc return CurMod; // Compiling a module. 540*f4a2713aSLionel Sambuc return HeaderInfo.getModuleMap().SourceModule; // Compiling a source. 541*f4a2713aSLionel Sambuc } 542*f4a2713aSLionel Sambuc // Try to determine the module of the include directive. 543*f4a2713aSLionel Sambuc FileID IDOfIncl = SourceMgr.getFileID(FilenameLoc); 544*f4a2713aSLionel Sambuc if (const FileEntry *EntryOfIncl = SourceMgr.getFileEntryForID(IDOfIncl)) { 545*f4a2713aSLionel Sambuc // The include comes from a file. 546*f4a2713aSLionel Sambuc return ModMap.findModuleForHeader(EntryOfIncl).getModule(); 547*f4a2713aSLionel Sambuc } else { 548*f4a2713aSLionel Sambuc // The include does not come from a file, 549*f4a2713aSLionel Sambuc // so it is probably a module compilation. 550*f4a2713aSLionel Sambuc return getCurrentModule(); 551*f4a2713aSLionel Sambuc } 552*f4a2713aSLionel Sambuc } 553*f4a2713aSLionel Sambuc 554*f4a2713aSLionel Sambuc bool Preprocessor::violatesPrivateInclude( 555*f4a2713aSLionel Sambuc Module *RequestingModule, 556*f4a2713aSLionel Sambuc const FileEntry *IncFileEnt, 557*f4a2713aSLionel Sambuc ModuleMap::ModuleHeaderRole Role, 558*f4a2713aSLionel Sambuc Module *RequestedModule) { 559*f4a2713aSLionel Sambuc #ifndef NDEBUG 560*f4a2713aSLionel Sambuc // Check for consistency between the module header role 561*f4a2713aSLionel Sambuc // as obtained from the lookup and as obtained from the module. 562*f4a2713aSLionel Sambuc // This check is not cheap, so enable it only for debugging. 563*f4a2713aSLionel Sambuc SmallVectorImpl<const FileEntry *> &PvtHdrs 564*f4a2713aSLionel Sambuc = RequestedModule->PrivateHeaders; 565*f4a2713aSLionel Sambuc SmallVectorImpl<const FileEntry *>::iterator Look 566*f4a2713aSLionel Sambuc = std::find(PvtHdrs.begin(), PvtHdrs.end(), IncFileEnt); 567*f4a2713aSLionel Sambuc bool IsPrivate = Look != PvtHdrs.end(); 568*f4a2713aSLionel Sambuc assert((IsPrivate && Role == ModuleMap::PrivateHeader) 569*f4a2713aSLionel Sambuc || (!IsPrivate && Role != ModuleMap::PrivateHeader)); 570*f4a2713aSLionel Sambuc #endif 571*f4a2713aSLionel Sambuc return Role == ModuleMap::PrivateHeader && 572*f4a2713aSLionel Sambuc RequestedModule->getTopLevelModule() != RequestingModule; 573*f4a2713aSLionel Sambuc } 574*f4a2713aSLionel Sambuc 575*f4a2713aSLionel Sambuc bool Preprocessor::violatesUseDeclarations( 576*f4a2713aSLionel Sambuc Module *RequestingModule, 577*f4a2713aSLionel Sambuc Module *RequestedModule) { 578*f4a2713aSLionel Sambuc ModuleMap &ModMap = HeaderInfo.getModuleMap(); 579*f4a2713aSLionel Sambuc ModMap.resolveUses(RequestingModule, /*Complain=*/false); 580*f4a2713aSLionel Sambuc const SmallVectorImpl<Module *> &AllowedUses = RequestingModule->DirectUses; 581*f4a2713aSLionel Sambuc SmallVectorImpl<Module *>::const_iterator Declared = 582*f4a2713aSLionel Sambuc std::find(AllowedUses.begin(), AllowedUses.end(), RequestedModule); 583*f4a2713aSLionel Sambuc return Declared == AllowedUses.end(); 584*f4a2713aSLionel Sambuc } 585*f4a2713aSLionel Sambuc 586*f4a2713aSLionel Sambuc void Preprocessor::verifyModuleInclude(SourceLocation FilenameLoc, 587*f4a2713aSLionel Sambuc StringRef Filename, 588*f4a2713aSLionel Sambuc const FileEntry *IncFileEnt) { 589*f4a2713aSLionel Sambuc Module *RequestingModule = getModuleForLocation(FilenameLoc); 590*f4a2713aSLionel Sambuc if (RequestingModule) 591*f4a2713aSLionel Sambuc HeaderInfo.getModuleMap().resolveUses(RequestingModule, /*Complain=*/false); 592*f4a2713aSLionel Sambuc ModuleMap::KnownHeader RequestedModule = 593*f4a2713aSLionel Sambuc HeaderInfo.getModuleMap().findModuleForHeader(IncFileEnt, 594*f4a2713aSLionel Sambuc RequestingModule); 595*f4a2713aSLionel Sambuc 596*f4a2713aSLionel Sambuc if (RequestingModule == RequestedModule.getModule()) 597*f4a2713aSLionel Sambuc return; // No faults wihin a module, or between files both not in modules. 598*f4a2713aSLionel Sambuc 599*f4a2713aSLionel Sambuc if (RequestingModule != HeaderInfo.getModuleMap().SourceModule) 600*f4a2713aSLionel Sambuc return; // No errors for indirect modules. 601*f4a2713aSLionel Sambuc // This may be a bit of a problem for modules with no source files. 602*f4a2713aSLionel Sambuc 603*f4a2713aSLionel Sambuc if (RequestedModule && violatesPrivateInclude(RequestingModule, IncFileEnt, 604*f4a2713aSLionel Sambuc RequestedModule.getRole(), 605*f4a2713aSLionel Sambuc RequestedModule.getModule())) 606*f4a2713aSLionel Sambuc Diag(FilenameLoc, diag::error_use_of_private_header_outside_module) 607*f4a2713aSLionel Sambuc << Filename; 608*f4a2713aSLionel Sambuc 609*f4a2713aSLionel Sambuc // FIXME: Add support for FixIts in module map files and offer adding the 610*f4a2713aSLionel Sambuc // required use declaration. 611*f4a2713aSLionel Sambuc if (RequestingModule && getLangOpts().ModulesDeclUse && 612*f4a2713aSLionel Sambuc violatesUseDeclarations(RequestingModule, RequestedModule.getModule())) 613*f4a2713aSLionel Sambuc Diag(FilenameLoc, diag::error_undeclared_use_of_module) 614*f4a2713aSLionel Sambuc << Filename; 615*f4a2713aSLionel Sambuc } 616*f4a2713aSLionel Sambuc 617*f4a2713aSLionel Sambuc const FileEntry *Preprocessor::LookupFile( 618*f4a2713aSLionel Sambuc SourceLocation FilenameLoc, 619*f4a2713aSLionel Sambuc StringRef Filename, 620*f4a2713aSLionel Sambuc bool isAngled, 621*f4a2713aSLionel Sambuc const DirectoryLookup *FromDir, 622*f4a2713aSLionel Sambuc const DirectoryLookup *&CurDir, 623*f4a2713aSLionel Sambuc SmallVectorImpl<char> *SearchPath, 624*f4a2713aSLionel Sambuc SmallVectorImpl<char> *RelativePath, 625*f4a2713aSLionel Sambuc ModuleMap::KnownHeader *SuggestedModule, 626*f4a2713aSLionel Sambuc bool SkipCache) { 627*f4a2713aSLionel Sambuc // If the header lookup mechanism may be relative to the current file, pass in 628*f4a2713aSLionel Sambuc // info about where the current file is. 629*f4a2713aSLionel Sambuc const FileEntry *CurFileEnt = 0; 630*f4a2713aSLionel Sambuc if (!FromDir) { 631*f4a2713aSLionel Sambuc FileID FID = getCurrentFileLexer()->getFileID(); 632*f4a2713aSLionel Sambuc CurFileEnt = SourceMgr.getFileEntryForID(FID); 633*f4a2713aSLionel Sambuc 634*f4a2713aSLionel Sambuc // If there is no file entry associated with this file, it must be the 635*f4a2713aSLionel Sambuc // predefines buffer. Any other file is not lexed with a normal lexer, so 636*f4a2713aSLionel Sambuc // it won't be scanned for preprocessor directives. If we have the 637*f4a2713aSLionel Sambuc // predefines buffer, resolve #include references (which come from the 638*f4a2713aSLionel Sambuc // -include command line argument) as if they came from the main file, this 639*f4a2713aSLionel Sambuc // affects file lookup etc. 640*f4a2713aSLionel Sambuc if (CurFileEnt == 0) { 641*f4a2713aSLionel Sambuc FID = SourceMgr.getMainFileID(); 642*f4a2713aSLionel Sambuc CurFileEnt = SourceMgr.getFileEntryForID(FID); 643*f4a2713aSLionel Sambuc } 644*f4a2713aSLionel Sambuc } 645*f4a2713aSLionel Sambuc 646*f4a2713aSLionel Sambuc // Do a standard file entry lookup. 647*f4a2713aSLionel Sambuc CurDir = CurDirLookup; 648*f4a2713aSLionel Sambuc const FileEntry *FE = HeaderInfo.LookupFile( 649*f4a2713aSLionel Sambuc Filename, isAngled, FromDir, CurDir, CurFileEnt, 650*f4a2713aSLionel Sambuc SearchPath, RelativePath, SuggestedModule, SkipCache); 651*f4a2713aSLionel Sambuc if (FE) { 652*f4a2713aSLionel Sambuc if (SuggestedModule) 653*f4a2713aSLionel Sambuc verifyModuleInclude(FilenameLoc, Filename, FE); 654*f4a2713aSLionel Sambuc return FE; 655*f4a2713aSLionel Sambuc } 656*f4a2713aSLionel Sambuc 657*f4a2713aSLionel Sambuc // Otherwise, see if this is a subframework header. If so, this is relative 658*f4a2713aSLionel Sambuc // to one of the headers on the #include stack. Walk the list of the current 659*f4a2713aSLionel Sambuc // headers on the #include stack and pass them to HeaderInfo. 660*f4a2713aSLionel Sambuc if (IsFileLexer()) { 661*f4a2713aSLionel Sambuc if ((CurFileEnt = SourceMgr.getFileEntryForID(CurPPLexer->getFileID()))) 662*f4a2713aSLionel Sambuc if ((FE = HeaderInfo.LookupSubframeworkHeader(Filename, CurFileEnt, 663*f4a2713aSLionel Sambuc SearchPath, RelativePath, 664*f4a2713aSLionel Sambuc SuggestedModule))) 665*f4a2713aSLionel Sambuc return FE; 666*f4a2713aSLionel Sambuc } 667*f4a2713aSLionel Sambuc 668*f4a2713aSLionel Sambuc for (unsigned i = 0, e = IncludeMacroStack.size(); i != e; ++i) { 669*f4a2713aSLionel Sambuc IncludeStackInfo &ISEntry = IncludeMacroStack[e-i-1]; 670*f4a2713aSLionel Sambuc if (IsFileLexer(ISEntry)) { 671*f4a2713aSLionel Sambuc if ((CurFileEnt = 672*f4a2713aSLionel Sambuc SourceMgr.getFileEntryForID(ISEntry.ThePPLexer->getFileID()))) 673*f4a2713aSLionel Sambuc if ((FE = HeaderInfo.LookupSubframeworkHeader( 674*f4a2713aSLionel Sambuc Filename, CurFileEnt, SearchPath, RelativePath, 675*f4a2713aSLionel Sambuc SuggestedModule))) 676*f4a2713aSLionel Sambuc return FE; 677*f4a2713aSLionel Sambuc } 678*f4a2713aSLionel Sambuc } 679*f4a2713aSLionel Sambuc 680*f4a2713aSLionel Sambuc // Otherwise, we really couldn't find the file. 681*f4a2713aSLionel Sambuc return 0; 682*f4a2713aSLionel Sambuc } 683*f4a2713aSLionel Sambuc 684*f4a2713aSLionel Sambuc 685*f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===// 686*f4a2713aSLionel Sambuc // Preprocessor Directive Handling. 687*f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===// 688*f4a2713aSLionel Sambuc 689*f4a2713aSLionel Sambuc class Preprocessor::ResetMacroExpansionHelper { 690*f4a2713aSLionel Sambuc public: 691*f4a2713aSLionel Sambuc ResetMacroExpansionHelper(Preprocessor *pp) 692*f4a2713aSLionel Sambuc : PP(pp), save(pp->DisableMacroExpansion) { 693*f4a2713aSLionel Sambuc if (pp->MacroExpansionInDirectivesOverride) 694*f4a2713aSLionel Sambuc pp->DisableMacroExpansion = false; 695*f4a2713aSLionel Sambuc } 696*f4a2713aSLionel Sambuc ~ResetMacroExpansionHelper() { 697*f4a2713aSLionel Sambuc PP->DisableMacroExpansion = save; 698*f4a2713aSLionel Sambuc } 699*f4a2713aSLionel Sambuc private: 700*f4a2713aSLionel Sambuc Preprocessor *PP; 701*f4a2713aSLionel Sambuc bool save; 702*f4a2713aSLionel Sambuc }; 703*f4a2713aSLionel Sambuc 704*f4a2713aSLionel Sambuc /// HandleDirective - This callback is invoked when the lexer sees a # token 705*f4a2713aSLionel Sambuc /// at the start of a line. This consumes the directive, modifies the 706*f4a2713aSLionel Sambuc /// lexer/preprocessor state, and advances the lexer(s) so that the next token 707*f4a2713aSLionel Sambuc /// read is the correct one. 708*f4a2713aSLionel Sambuc void Preprocessor::HandleDirective(Token &Result) { 709*f4a2713aSLionel Sambuc // FIXME: Traditional: # with whitespace before it not recognized by K&R? 710*f4a2713aSLionel Sambuc 711*f4a2713aSLionel Sambuc // We just parsed a # character at the start of a line, so we're in directive 712*f4a2713aSLionel Sambuc // mode. Tell the lexer this so any newlines we see will be converted into an 713*f4a2713aSLionel Sambuc // EOD token (which terminates the directive). 714*f4a2713aSLionel Sambuc CurPPLexer->ParsingPreprocessorDirective = true; 715*f4a2713aSLionel Sambuc if (CurLexer) CurLexer->SetKeepWhitespaceMode(false); 716*f4a2713aSLionel Sambuc 717*f4a2713aSLionel Sambuc bool ImmediatelyAfterTopLevelIfndef = 718*f4a2713aSLionel Sambuc CurPPLexer->MIOpt.getImmediatelyAfterTopLevelIfndef(); 719*f4a2713aSLionel Sambuc CurPPLexer->MIOpt.resetImmediatelyAfterTopLevelIfndef(); 720*f4a2713aSLionel Sambuc 721*f4a2713aSLionel Sambuc ++NumDirectives; 722*f4a2713aSLionel Sambuc 723*f4a2713aSLionel Sambuc // We are about to read a token. For the multiple-include optimization FA to 724*f4a2713aSLionel Sambuc // work, we have to remember if we had read any tokens *before* this 725*f4a2713aSLionel Sambuc // pp-directive. 726*f4a2713aSLionel Sambuc bool ReadAnyTokensBeforeDirective =CurPPLexer->MIOpt.getHasReadAnyTokensVal(); 727*f4a2713aSLionel Sambuc 728*f4a2713aSLionel Sambuc // Save the '#' token in case we need to return it later. 729*f4a2713aSLionel Sambuc Token SavedHash = Result; 730*f4a2713aSLionel Sambuc 731*f4a2713aSLionel Sambuc // Read the next token, the directive flavor. This isn't expanded due to 732*f4a2713aSLionel Sambuc // C99 6.10.3p8. 733*f4a2713aSLionel Sambuc LexUnexpandedToken(Result); 734*f4a2713aSLionel Sambuc 735*f4a2713aSLionel Sambuc // C99 6.10.3p11: Is this preprocessor directive in macro invocation? e.g.: 736*f4a2713aSLionel Sambuc // #define A(x) #x 737*f4a2713aSLionel Sambuc // A(abc 738*f4a2713aSLionel Sambuc // #warning blah 739*f4a2713aSLionel Sambuc // def) 740*f4a2713aSLionel Sambuc // If so, the user is relying on undefined behavior, emit a diagnostic. Do 741*f4a2713aSLionel Sambuc // not support this for #include-like directives, since that can result in 742*f4a2713aSLionel Sambuc // terrible diagnostics, and does not work in GCC. 743*f4a2713aSLionel Sambuc if (InMacroArgs) { 744*f4a2713aSLionel Sambuc if (IdentifierInfo *II = Result.getIdentifierInfo()) { 745*f4a2713aSLionel Sambuc switch (II->getPPKeywordID()) { 746*f4a2713aSLionel Sambuc case tok::pp_include: 747*f4a2713aSLionel Sambuc case tok::pp_import: 748*f4a2713aSLionel Sambuc case tok::pp_include_next: 749*f4a2713aSLionel Sambuc case tok::pp___include_macros: 750*f4a2713aSLionel Sambuc Diag(Result, diag::err_embedded_include) << II->getName(); 751*f4a2713aSLionel Sambuc DiscardUntilEndOfDirective(); 752*f4a2713aSLionel Sambuc return; 753*f4a2713aSLionel Sambuc default: 754*f4a2713aSLionel Sambuc break; 755*f4a2713aSLionel Sambuc } 756*f4a2713aSLionel Sambuc } 757*f4a2713aSLionel Sambuc Diag(Result, diag::ext_embedded_directive); 758*f4a2713aSLionel Sambuc } 759*f4a2713aSLionel Sambuc 760*f4a2713aSLionel Sambuc // Temporarily enable macro expansion if set so 761*f4a2713aSLionel Sambuc // and reset to previous state when returning from this function. 762*f4a2713aSLionel Sambuc ResetMacroExpansionHelper helper(this); 763*f4a2713aSLionel Sambuc 764*f4a2713aSLionel Sambuc switch (Result.getKind()) { 765*f4a2713aSLionel Sambuc case tok::eod: 766*f4a2713aSLionel Sambuc return; // null directive. 767*f4a2713aSLionel Sambuc case tok::code_completion: 768*f4a2713aSLionel Sambuc if (CodeComplete) 769*f4a2713aSLionel Sambuc CodeComplete->CodeCompleteDirective( 770*f4a2713aSLionel Sambuc CurPPLexer->getConditionalStackDepth() > 0); 771*f4a2713aSLionel Sambuc setCodeCompletionReached(); 772*f4a2713aSLionel Sambuc return; 773*f4a2713aSLionel Sambuc case tok::numeric_constant: // # 7 GNU line marker directive. 774*f4a2713aSLionel Sambuc if (getLangOpts().AsmPreprocessor) 775*f4a2713aSLionel Sambuc break; // # 4 is not a preprocessor directive in .S files. 776*f4a2713aSLionel Sambuc return HandleDigitDirective(Result); 777*f4a2713aSLionel Sambuc default: 778*f4a2713aSLionel Sambuc IdentifierInfo *II = Result.getIdentifierInfo(); 779*f4a2713aSLionel Sambuc if (II == 0) break; // Not an identifier. 780*f4a2713aSLionel Sambuc 781*f4a2713aSLionel Sambuc // Ask what the preprocessor keyword ID is. 782*f4a2713aSLionel Sambuc switch (II->getPPKeywordID()) { 783*f4a2713aSLionel Sambuc default: break; 784*f4a2713aSLionel Sambuc // C99 6.10.1 - Conditional Inclusion. 785*f4a2713aSLionel Sambuc case tok::pp_if: 786*f4a2713aSLionel Sambuc return HandleIfDirective(Result, ReadAnyTokensBeforeDirective); 787*f4a2713aSLionel Sambuc case tok::pp_ifdef: 788*f4a2713aSLionel Sambuc return HandleIfdefDirective(Result, false, true/*not valid for miopt*/); 789*f4a2713aSLionel Sambuc case tok::pp_ifndef: 790*f4a2713aSLionel Sambuc return HandleIfdefDirective(Result, true, ReadAnyTokensBeforeDirective); 791*f4a2713aSLionel Sambuc case tok::pp_elif: 792*f4a2713aSLionel Sambuc return HandleElifDirective(Result); 793*f4a2713aSLionel Sambuc case tok::pp_else: 794*f4a2713aSLionel Sambuc return HandleElseDirective(Result); 795*f4a2713aSLionel Sambuc case tok::pp_endif: 796*f4a2713aSLionel Sambuc return HandleEndifDirective(Result); 797*f4a2713aSLionel Sambuc 798*f4a2713aSLionel Sambuc // C99 6.10.2 - Source File Inclusion. 799*f4a2713aSLionel Sambuc case tok::pp_include: 800*f4a2713aSLionel Sambuc // Handle #include. 801*f4a2713aSLionel Sambuc return HandleIncludeDirective(SavedHash.getLocation(), Result); 802*f4a2713aSLionel Sambuc case tok::pp___include_macros: 803*f4a2713aSLionel Sambuc // Handle -imacros. 804*f4a2713aSLionel Sambuc return HandleIncludeMacrosDirective(SavedHash.getLocation(), Result); 805*f4a2713aSLionel Sambuc 806*f4a2713aSLionel Sambuc // C99 6.10.3 - Macro Replacement. 807*f4a2713aSLionel Sambuc case tok::pp_define: 808*f4a2713aSLionel Sambuc return HandleDefineDirective(Result, ImmediatelyAfterTopLevelIfndef); 809*f4a2713aSLionel Sambuc case tok::pp_undef: 810*f4a2713aSLionel Sambuc return HandleUndefDirective(Result); 811*f4a2713aSLionel Sambuc 812*f4a2713aSLionel Sambuc // C99 6.10.4 - Line Control. 813*f4a2713aSLionel Sambuc case tok::pp_line: 814*f4a2713aSLionel Sambuc return HandleLineDirective(Result); 815*f4a2713aSLionel Sambuc 816*f4a2713aSLionel Sambuc // C99 6.10.5 - Error Directive. 817*f4a2713aSLionel Sambuc case tok::pp_error: 818*f4a2713aSLionel Sambuc return HandleUserDiagnosticDirective(Result, false); 819*f4a2713aSLionel Sambuc 820*f4a2713aSLionel Sambuc // C99 6.10.6 - Pragma Directive. 821*f4a2713aSLionel Sambuc case tok::pp_pragma: 822*f4a2713aSLionel Sambuc return HandlePragmaDirective(SavedHash.getLocation(), PIK_HashPragma); 823*f4a2713aSLionel Sambuc 824*f4a2713aSLionel Sambuc // GNU Extensions. 825*f4a2713aSLionel Sambuc case tok::pp_import: 826*f4a2713aSLionel Sambuc return HandleImportDirective(SavedHash.getLocation(), Result); 827*f4a2713aSLionel Sambuc case tok::pp_include_next: 828*f4a2713aSLionel Sambuc return HandleIncludeNextDirective(SavedHash.getLocation(), Result); 829*f4a2713aSLionel Sambuc 830*f4a2713aSLionel Sambuc case tok::pp_warning: 831*f4a2713aSLionel Sambuc Diag(Result, diag::ext_pp_warning_directive); 832*f4a2713aSLionel Sambuc return HandleUserDiagnosticDirective(Result, true); 833*f4a2713aSLionel Sambuc case tok::pp_ident: 834*f4a2713aSLionel Sambuc return HandleIdentSCCSDirective(Result); 835*f4a2713aSLionel Sambuc case tok::pp_sccs: 836*f4a2713aSLionel Sambuc return HandleIdentSCCSDirective(Result); 837*f4a2713aSLionel Sambuc case tok::pp_assert: 838*f4a2713aSLionel Sambuc //isExtension = true; // FIXME: implement #assert 839*f4a2713aSLionel Sambuc break; 840*f4a2713aSLionel Sambuc case tok::pp_unassert: 841*f4a2713aSLionel Sambuc //isExtension = true; // FIXME: implement #unassert 842*f4a2713aSLionel Sambuc break; 843*f4a2713aSLionel Sambuc 844*f4a2713aSLionel Sambuc case tok::pp___public_macro: 845*f4a2713aSLionel Sambuc if (getLangOpts().Modules) 846*f4a2713aSLionel Sambuc return HandleMacroPublicDirective(Result); 847*f4a2713aSLionel Sambuc break; 848*f4a2713aSLionel Sambuc 849*f4a2713aSLionel Sambuc case tok::pp___private_macro: 850*f4a2713aSLionel Sambuc if (getLangOpts().Modules) 851*f4a2713aSLionel Sambuc return HandleMacroPrivateDirective(Result); 852*f4a2713aSLionel Sambuc break; 853*f4a2713aSLionel Sambuc } 854*f4a2713aSLionel Sambuc break; 855*f4a2713aSLionel Sambuc } 856*f4a2713aSLionel Sambuc 857*f4a2713aSLionel Sambuc // If this is a .S file, treat unknown # directives as non-preprocessor 858*f4a2713aSLionel Sambuc // directives. This is important because # may be a comment or introduce 859*f4a2713aSLionel Sambuc // various pseudo-ops. Just return the # token and push back the following 860*f4a2713aSLionel Sambuc // token to be lexed next time. 861*f4a2713aSLionel Sambuc if (getLangOpts().AsmPreprocessor) { 862*f4a2713aSLionel Sambuc Token *Toks = new Token[2]; 863*f4a2713aSLionel Sambuc // Return the # and the token after it. 864*f4a2713aSLionel Sambuc Toks[0] = SavedHash; 865*f4a2713aSLionel Sambuc Toks[1] = Result; 866*f4a2713aSLionel Sambuc 867*f4a2713aSLionel Sambuc // If the second token is a hashhash token, then we need to translate it to 868*f4a2713aSLionel Sambuc // unknown so the token lexer doesn't try to perform token pasting. 869*f4a2713aSLionel Sambuc if (Result.is(tok::hashhash)) 870*f4a2713aSLionel Sambuc Toks[1].setKind(tok::unknown); 871*f4a2713aSLionel Sambuc 872*f4a2713aSLionel Sambuc // Enter this token stream so that we re-lex the tokens. Make sure to 873*f4a2713aSLionel Sambuc // enable macro expansion, in case the token after the # is an identifier 874*f4a2713aSLionel Sambuc // that is expanded. 875*f4a2713aSLionel Sambuc EnterTokenStream(Toks, 2, false, true); 876*f4a2713aSLionel Sambuc return; 877*f4a2713aSLionel Sambuc } 878*f4a2713aSLionel Sambuc 879*f4a2713aSLionel Sambuc // If we reached here, the preprocessing token is not valid! 880*f4a2713aSLionel Sambuc Diag(Result, diag::err_pp_invalid_directive); 881*f4a2713aSLionel Sambuc 882*f4a2713aSLionel Sambuc // Read the rest of the PP line. 883*f4a2713aSLionel Sambuc DiscardUntilEndOfDirective(); 884*f4a2713aSLionel Sambuc 885*f4a2713aSLionel Sambuc // Okay, we're done parsing the directive. 886*f4a2713aSLionel Sambuc } 887*f4a2713aSLionel Sambuc 888*f4a2713aSLionel Sambuc /// GetLineValue - Convert a numeric token into an unsigned value, emitting 889*f4a2713aSLionel Sambuc /// Diagnostic DiagID if it is invalid, and returning the value in Val. 890*f4a2713aSLionel Sambuc static bool GetLineValue(Token &DigitTok, unsigned &Val, 891*f4a2713aSLionel Sambuc unsigned DiagID, Preprocessor &PP, 892*f4a2713aSLionel Sambuc bool IsGNULineDirective=false) { 893*f4a2713aSLionel Sambuc if (DigitTok.isNot(tok::numeric_constant)) { 894*f4a2713aSLionel Sambuc PP.Diag(DigitTok, DiagID); 895*f4a2713aSLionel Sambuc 896*f4a2713aSLionel Sambuc if (DigitTok.isNot(tok::eod)) 897*f4a2713aSLionel Sambuc PP.DiscardUntilEndOfDirective(); 898*f4a2713aSLionel Sambuc return true; 899*f4a2713aSLionel Sambuc } 900*f4a2713aSLionel Sambuc 901*f4a2713aSLionel Sambuc SmallString<64> IntegerBuffer; 902*f4a2713aSLionel Sambuc IntegerBuffer.resize(DigitTok.getLength()); 903*f4a2713aSLionel Sambuc const char *DigitTokBegin = &IntegerBuffer[0]; 904*f4a2713aSLionel Sambuc bool Invalid = false; 905*f4a2713aSLionel Sambuc unsigned ActualLength = PP.getSpelling(DigitTok, DigitTokBegin, &Invalid); 906*f4a2713aSLionel Sambuc if (Invalid) 907*f4a2713aSLionel Sambuc return true; 908*f4a2713aSLionel Sambuc 909*f4a2713aSLionel Sambuc // Verify that we have a simple digit-sequence, and compute the value. This 910*f4a2713aSLionel Sambuc // is always a simple digit string computed in decimal, so we do this manually 911*f4a2713aSLionel Sambuc // here. 912*f4a2713aSLionel Sambuc Val = 0; 913*f4a2713aSLionel Sambuc for (unsigned i = 0; i != ActualLength; ++i) { 914*f4a2713aSLionel Sambuc // C++1y [lex.fcon]p1: 915*f4a2713aSLionel Sambuc // Optional separating single quotes in a digit-sequence are ignored 916*f4a2713aSLionel Sambuc if (DigitTokBegin[i] == '\'') 917*f4a2713aSLionel Sambuc continue; 918*f4a2713aSLionel Sambuc 919*f4a2713aSLionel Sambuc if (!isDigit(DigitTokBegin[i])) { 920*f4a2713aSLionel Sambuc PP.Diag(PP.AdvanceToTokenCharacter(DigitTok.getLocation(), i), 921*f4a2713aSLionel Sambuc diag::err_pp_line_digit_sequence) << IsGNULineDirective; 922*f4a2713aSLionel Sambuc PP.DiscardUntilEndOfDirective(); 923*f4a2713aSLionel Sambuc return true; 924*f4a2713aSLionel Sambuc } 925*f4a2713aSLionel Sambuc 926*f4a2713aSLionel Sambuc unsigned NextVal = Val*10+(DigitTokBegin[i]-'0'); 927*f4a2713aSLionel Sambuc if (NextVal < Val) { // overflow. 928*f4a2713aSLionel Sambuc PP.Diag(DigitTok, DiagID); 929*f4a2713aSLionel Sambuc PP.DiscardUntilEndOfDirective(); 930*f4a2713aSLionel Sambuc return true; 931*f4a2713aSLionel Sambuc } 932*f4a2713aSLionel Sambuc Val = NextVal; 933*f4a2713aSLionel Sambuc } 934*f4a2713aSLionel Sambuc 935*f4a2713aSLionel Sambuc if (DigitTokBegin[0] == '0' && Val) 936*f4a2713aSLionel Sambuc PP.Diag(DigitTok.getLocation(), diag::warn_pp_line_decimal) 937*f4a2713aSLionel Sambuc << IsGNULineDirective; 938*f4a2713aSLionel Sambuc 939*f4a2713aSLionel Sambuc return false; 940*f4a2713aSLionel Sambuc } 941*f4a2713aSLionel Sambuc 942*f4a2713aSLionel Sambuc /// \brief Handle a \#line directive: C99 6.10.4. 943*f4a2713aSLionel Sambuc /// 944*f4a2713aSLionel Sambuc /// The two acceptable forms are: 945*f4a2713aSLionel Sambuc /// \verbatim 946*f4a2713aSLionel Sambuc /// # line digit-sequence 947*f4a2713aSLionel Sambuc /// # line digit-sequence "s-char-sequence" 948*f4a2713aSLionel Sambuc /// \endverbatim 949*f4a2713aSLionel Sambuc void Preprocessor::HandleLineDirective(Token &Tok) { 950*f4a2713aSLionel Sambuc // Read the line # and string argument. Per C99 6.10.4p5, these tokens are 951*f4a2713aSLionel Sambuc // expanded. 952*f4a2713aSLionel Sambuc Token DigitTok; 953*f4a2713aSLionel Sambuc Lex(DigitTok); 954*f4a2713aSLionel Sambuc 955*f4a2713aSLionel Sambuc // Validate the number and convert it to an unsigned. 956*f4a2713aSLionel Sambuc unsigned LineNo; 957*f4a2713aSLionel Sambuc if (GetLineValue(DigitTok, LineNo, diag::err_pp_line_requires_integer,*this)) 958*f4a2713aSLionel Sambuc return; 959*f4a2713aSLionel Sambuc 960*f4a2713aSLionel Sambuc if (LineNo == 0) 961*f4a2713aSLionel Sambuc Diag(DigitTok, diag::ext_pp_line_zero); 962*f4a2713aSLionel Sambuc 963*f4a2713aSLionel Sambuc // Enforce C99 6.10.4p3: "The digit sequence shall not specify ... a 964*f4a2713aSLionel Sambuc // number greater than 2147483647". C90 requires that the line # be <= 32767. 965*f4a2713aSLionel Sambuc unsigned LineLimit = 32768U; 966*f4a2713aSLionel Sambuc if (LangOpts.C99 || LangOpts.CPlusPlus11) 967*f4a2713aSLionel Sambuc LineLimit = 2147483648U; 968*f4a2713aSLionel Sambuc if (LineNo >= LineLimit) 969*f4a2713aSLionel Sambuc Diag(DigitTok, diag::ext_pp_line_too_big) << LineLimit; 970*f4a2713aSLionel Sambuc else if (LangOpts.CPlusPlus11 && LineNo >= 32768U) 971*f4a2713aSLionel Sambuc Diag(DigitTok, diag::warn_cxx98_compat_pp_line_too_big); 972*f4a2713aSLionel Sambuc 973*f4a2713aSLionel Sambuc int FilenameID = -1; 974*f4a2713aSLionel Sambuc Token StrTok; 975*f4a2713aSLionel Sambuc Lex(StrTok); 976*f4a2713aSLionel Sambuc 977*f4a2713aSLionel Sambuc // If the StrTok is "eod", then it wasn't present. Otherwise, it must be a 978*f4a2713aSLionel Sambuc // string followed by eod. 979*f4a2713aSLionel Sambuc if (StrTok.is(tok::eod)) 980*f4a2713aSLionel Sambuc ; // ok 981*f4a2713aSLionel Sambuc else if (StrTok.isNot(tok::string_literal)) { 982*f4a2713aSLionel Sambuc Diag(StrTok, diag::err_pp_line_invalid_filename); 983*f4a2713aSLionel Sambuc return DiscardUntilEndOfDirective(); 984*f4a2713aSLionel Sambuc } else if (StrTok.hasUDSuffix()) { 985*f4a2713aSLionel Sambuc Diag(StrTok, diag::err_invalid_string_udl); 986*f4a2713aSLionel Sambuc return DiscardUntilEndOfDirective(); 987*f4a2713aSLionel Sambuc } else { 988*f4a2713aSLionel Sambuc // Parse and validate the string, converting it into a unique ID. 989*f4a2713aSLionel Sambuc StringLiteralParser Literal(&StrTok, 1, *this); 990*f4a2713aSLionel Sambuc assert(Literal.isAscii() && "Didn't allow wide strings in"); 991*f4a2713aSLionel Sambuc if (Literal.hadError) 992*f4a2713aSLionel Sambuc return DiscardUntilEndOfDirective(); 993*f4a2713aSLionel Sambuc if (Literal.Pascal) { 994*f4a2713aSLionel Sambuc Diag(StrTok, diag::err_pp_linemarker_invalid_filename); 995*f4a2713aSLionel Sambuc return DiscardUntilEndOfDirective(); 996*f4a2713aSLionel Sambuc } 997*f4a2713aSLionel Sambuc FilenameID = SourceMgr.getLineTableFilenameID(Literal.GetString()); 998*f4a2713aSLionel Sambuc 999*f4a2713aSLionel Sambuc // Verify that there is nothing after the string, other than EOD. Because 1000*f4a2713aSLionel Sambuc // of C99 6.10.4p5, macros that expand to empty tokens are ok. 1001*f4a2713aSLionel Sambuc CheckEndOfDirective("line", true); 1002*f4a2713aSLionel Sambuc } 1003*f4a2713aSLionel Sambuc 1004*f4a2713aSLionel Sambuc SourceMgr.AddLineNote(DigitTok.getLocation(), LineNo, FilenameID); 1005*f4a2713aSLionel Sambuc 1006*f4a2713aSLionel Sambuc if (Callbacks) 1007*f4a2713aSLionel Sambuc Callbacks->FileChanged(CurPPLexer->getSourceLocation(), 1008*f4a2713aSLionel Sambuc PPCallbacks::RenameFile, 1009*f4a2713aSLionel Sambuc SrcMgr::C_User); 1010*f4a2713aSLionel Sambuc } 1011*f4a2713aSLionel Sambuc 1012*f4a2713aSLionel Sambuc /// ReadLineMarkerFlags - Parse and validate any flags at the end of a GNU line 1013*f4a2713aSLionel Sambuc /// marker directive. 1014*f4a2713aSLionel Sambuc static bool ReadLineMarkerFlags(bool &IsFileEntry, bool &IsFileExit, 1015*f4a2713aSLionel Sambuc bool &IsSystemHeader, bool &IsExternCHeader, 1016*f4a2713aSLionel Sambuc Preprocessor &PP) { 1017*f4a2713aSLionel Sambuc unsigned FlagVal; 1018*f4a2713aSLionel Sambuc Token FlagTok; 1019*f4a2713aSLionel Sambuc PP.Lex(FlagTok); 1020*f4a2713aSLionel Sambuc if (FlagTok.is(tok::eod)) return false; 1021*f4a2713aSLionel Sambuc if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag, PP)) 1022*f4a2713aSLionel Sambuc return true; 1023*f4a2713aSLionel Sambuc 1024*f4a2713aSLionel Sambuc if (FlagVal == 1) { 1025*f4a2713aSLionel Sambuc IsFileEntry = true; 1026*f4a2713aSLionel Sambuc 1027*f4a2713aSLionel Sambuc PP.Lex(FlagTok); 1028*f4a2713aSLionel Sambuc if (FlagTok.is(tok::eod)) return false; 1029*f4a2713aSLionel Sambuc if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag,PP)) 1030*f4a2713aSLionel Sambuc return true; 1031*f4a2713aSLionel Sambuc } else if (FlagVal == 2) { 1032*f4a2713aSLionel Sambuc IsFileExit = true; 1033*f4a2713aSLionel Sambuc 1034*f4a2713aSLionel Sambuc SourceManager &SM = PP.getSourceManager(); 1035*f4a2713aSLionel Sambuc // If we are leaving the current presumed file, check to make sure the 1036*f4a2713aSLionel Sambuc // presumed include stack isn't empty! 1037*f4a2713aSLionel Sambuc FileID CurFileID = 1038*f4a2713aSLionel Sambuc SM.getDecomposedExpansionLoc(FlagTok.getLocation()).first; 1039*f4a2713aSLionel Sambuc PresumedLoc PLoc = SM.getPresumedLoc(FlagTok.getLocation()); 1040*f4a2713aSLionel Sambuc if (PLoc.isInvalid()) 1041*f4a2713aSLionel Sambuc return true; 1042*f4a2713aSLionel Sambuc 1043*f4a2713aSLionel Sambuc // If there is no include loc (main file) or if the include loc is in a 1044*f4a2713aSLionel Sambuc // different physical file, then we aren't in a "1" line marker flag region. 1045*f4a2713aSLionel Sambuc SourceLocation IncLoc = PLoc.getIncludeLoc(); 1046*f4a2713aSLionel Sambuc if (IncLoc.isInvalid() || 1047*f4a2713aSLionel Sambuc SM.getDecomposedExpansionLoc(IncLoc).first != CurFileID) { 1048*f4a2713aSLionel Sambuc PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_pop); 1049*f4a2713aSLionel Sambuc PP.DiscardUntilEndOfDirective(); 1050*f4a2713aSLionel Sambuc return true; 1051*f4a2713aSLionel Sambuc } 1052*f4a2713aSLionel Sambuc 1053*f4a2713aSLionel Sambuc PP.Lex(FlagTok); 1054*f4a2713aSLionel Sambuc if (FlagTok.is(tok::eod)) return false; 1055*f4a2713aSLionel Sambuc if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag,PP)) 1056*f4a2713aSLionel Sambuc return true; 1057*f4a2713aSLionel Sambuc } 1058*f4a2713aSLionel Sambuc 1059*f4a2713aSLionel Sambuc // We must have 3 if there are still flags. 1060*f4a2713aSLionel Sambuc if (FlagVal != 3) { 1061*f4a2713aSLionel Sambuc PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_flag); 1062*f4a2713aSLionel Sambuc PP.DiscardUntilEndOfDirective(); 1063*f4a2713aSLionel Sambuc return true; 1064*f4a2713aSLionel Sambuc } 1065*f4a2713aSLionel Sambuc 1066*f4a2713aSLionel Sambuc IsSystemHeader = true; 1067*f4a2713aSLionel Sambuc 1068*f4a2713aSLionel Sambuc PP.Lex(FlagTok); 1069*f4a2713aSLionel Sambuc if (FlagTok.is(tok::eod)) return false; 1070*f4a2713aSLionel Sambuc if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag, PP)) 1071*f4a2713aSLionel Sambuc return true; 1072*f4a2713aSLionel Sambuc 1073*f4a2713aSLionel Sambuc // We must have 4 if there is yet another flag. 1074*f4a2713aSLionel Sambuc if (FlagVal != 4) { 1075*f4a2713aSLionel Sambuc PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_flag); 1076*f4a2713aSLionel Sambuc PP.DiscardUntilEndOfDirective(); 1077*f4a2713aSLionel Sambuc return true; 1078*f4a2713aSLionel Sambuc } 1079*f4a2713aSLionel Sambuc 1080*f4a2713aSLionel Sambuc IsExternCHeader = true; 1081*f4a2713aSLionel Sambuc 1082*f4a2713aSLionel Sambuc PP.Lex(FlagTok); 1083*f4a2713aSLionel Sambuc if (FlagTok.is(tok::eod)) return false; 1084*f4a2713aSLionel Sambuc 1085*f4a2713aSLionel Sambuc // There are no more valid flags here. 1086*f4a2713aSLionel Sambuc PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_flag); 1087*f4a2713aSLionel Sambuc PP.DiscardUntilEndOfDirective(); 1088*f4a2713aSLionel Sambuc return true; 1089*f4a2713aSLionel Sambuc } 1090*f4a2713aSLionel Sambuc 1091*f4a2713aSLionel Sambuc /// HandleDigitDirective - Handle a GNU line marker directive, whose syntax is 1092*f4a2713aSLionel Sambuc /// one of the following forms: 1093*f4a2713aSLionel Sambuc /// 1094*f4a2713aSLionel Sambuc /// # 42 1095*f4a2713aSLionel Sambuc /// # 42 "file" ('1' | '2')? 1096*f4a2713aSLionel Sambuc /// # 42 "file" ('1' | '2')? '3' '4'? 1097*f4a2713aSLionel Sambuc /// 1098*f4a2713aSLionel Sambuc void Preprocessor::HandleDigitDirective(Token &DigitTok) { 1099*f4a2713aSLionel Sambuc // Validate the number and convert it to an unsigned. GNU does not have a 1100*f4a2713aSLionel Sambuc // line # limit other than it fit in 32-bits. 1101*f4a2713aSLionel Sambuc unsigned LineNo; 1102*f4a2713aSLionel Sambuc if (GetLineValue(DigitTok, LineNo, diag::err_pp_linemarker_requires_integer, 1103*f4a2713aSLionel Sambuc *this, true)) 1104*f4a2713aSLionel Sambuc return; 1105*f4a2713aSLionel Sambuc 1106*f4a2713aSLionel Sambuc Token StrTok; 1107*f4a2713aSLionel Sambuc Lex(StrTok); 1108*f4a2713aSLionel Sambuc 1109*f4a2713aSLionel Sambuc bool IsFileEntry = false, IsFileExit = false; 1110*f4a2713aSLionel Sambuc bool IsSystemHeader = false, IsExternCHeader = false; 1111*f4a2713aSLionel Sambuc int FilenameID = -1; 1112*f4a2713aSLionel Sambuc 1113*f4a2713aSLionel Sambuc // If the StrTok is "eod", then it wasn't present. Otherwise, it must be a 1114*f4a2713aSLionel Sambuc // string followed by eod. 1115*f4a2713aSLionel Sambuc if (StrTok.is(tok::eod)) 1116*f4a2713aSLionel Sambuc ; // ok 1117*f4a2713aSLionel Sambuc else if (StrTok.isNot(tok::string_literal)) { 1118*f4a2713aSLionel Sambuc Diag(StrTok, diag::err_pp_linemarker_invalid_filename); 1119*f4a2713aSLionel Sambuc return DiscardUntilEndOfDirective(); 1120*f4a2713aSLionel Sambuc } else if (StrTok.hasUDSuffix()) { 1121*f4a2713aSLionel Sambuc Diag(StrTok, diag::err_invalid_string_udl); 1122*f4a2713aSLionel Sambuc return DiscardUntilEndOfDirective(); 1123*f4a2713aSLionel Sambuc } else { 1124*f4a2713aSLionel Sambuc // Parse and validate the string, converting it into a unique ID. 1125*f4a2713aSLionel Sambuc StringLiteralParser Literal(&StrTok, 1, *this); 1126*f4a2713aSLionel Sambuc assert(Literal.isAscii() && "Didn't allow wide strings in"); 1127*f4a2713aSLionel Sambuc if (Literal.hadError) 1128*f4a2713aSLionel Sambuc return DiscardUntilEndOfDirective(); 1129*f4a2713aSLionel Sambuc if (Literal.Pascal) { 1130*f4a2713aSLionel Sambuc Diag(StrTok, diag::err_pp_linemarker_invalid_filename); 1131*f4a2713aSLionel Sambuc return DiscardUntilEndOfDirective(); 1132*f4a2713aSLionel Sambuc } 1133*f4a2713aSLionel Sambuc FilenameID = SourceMgr.getLineTableFilenameID(Literal.GetString()); 1134*f4a2713aSLionel Sambuc 1135*f4a2713aSLionel Sambuc // If a filename was present, read any flags that are present. 1136*f4a2713aSLionel Sambuc if (ReadLineMarkerFlags(IsFileEntry, IsFileExit, 1137*f4a2713aSLionel Sambuc IsSystemHeader, IsExternCHeader, *this)) 1138*f4a2713aSLionel Sambuc return; 1139*f4a2713aSLionel Sambuc } 1140*f4a2713aSLionel Sambuc 1141*f4a2713aSLionel Sambuc // Create a line note with this information. 1142*f4a2713aSLionel Sambuc SourceMgr.AddLineNote(DigitTok.getLocation(), LineNo, FilenameID, 1143*f4a2713aSLionel Sambuc IsFileEntry, IsFileExit, 1144*f4a2713aSLionel Sambuc IsSystemHeader, IsExternCHeader); 1145*f4a2713aSLionel Sambuc 1146*f4a2713aSLionel Sambuc // If the preprocessor has callbacks installed, notify them of the #line 1147*f4a2713aSLionel Sambuc // change. This is used so that the line marker comes out in -E mode for 1148*f4a2713aSLionel Sambuc // example. 1149*f4a2713aSLionel Sambuc if (Callbacks) { 1150*f4a2713aSLionel Sambuc PPCallbacks::FileChangeReason Reason = PPCallbacks::RenameFile; 1151*f4a2713aSLionel Sambuc if (IsFileEntry) 1152*f4a2713aSLionel Sambuc Reason = PPCallbacks::EnterFile; 1153*f4a2713aSLionel Sambuc else if (IsFileExit) 1154*f4a2713aSLionel Sambuc Reason = PPCallbacks::ExitFile; 1155*f4a2713aSLionel Sambuc SrcMgr::CharacteristicKind FileKind = SrcMgr::C_User; 1156*f4a2713aSLionel Sambuc if (IsExternCHeader) 1157*f4a2713aSLionel Sambuc FileKind = SrcMgr::C_ExternCSystem; 1158*f4a2713aSLionel Sambuc else if (IsSystemHeader) 1159*f4a2713aSLionel Sambuc FileKind = SrcMgr::C_System; 1160*f4a2713aSLionel Sambuc 1161*f4a2713aSLionel Sambuc Callbacks->FileChanged(CurPPLexer->getSourceLocation(), Reason, FileKind); 1162*f4a2713aSLionel Sambuc } 1163*f4a2713aSLionel Sambuc } 1164*f4a2713aSLionel Sambuc 1165*f4a2713aSLionel Sambuc 1166*f4a2713aSLionel Sambuc /// HandleUserDiagnosticDirective - Handle a #warning or #error directive. 1167*f4a2713aSLionel Sambuc /// 1168*f4a2713aSLionel Sambuc void Preprocessor::HandleUserDiagnosticDirective(Token &Tok, 1169*f4a2713aSLionel Sambuc bool isWarning) { 1170*f4a2713aSLionel Sambuc // PTH doesn't emit #warning or #error directives. 1171*f4a2713aSLionel Sambuc if (CurPTHLexer) 1172*f4a2713aSLionel Sambuc return CurPTHLexer->DiscardToEndOfLine(); 1173*f4a2713aSLionel Sambuc 1174*f4a2713aSLionel Sambuc // Read the rest of the line raw. We do this because we don't want macros 1175*f4a2713aSLionel Sambuc // to be expanded and we don't require that the tokens be valid preprocessing 1176*f4a2713aSLionel Sambuc // tokens. For example, this is allowed: "#warning ` 'foo". GCC does 1177*f4a2713aSLionel Sambuc // collapse multiple consequtive white space between tokens, but this isn't 1178*f4a2713aSLionel Sambuc // specified by the standard. 1179*f4a2713aSLionel Sambuc SmallString<128> Message; 1180*f4a2713aSLionel Sambuc CurLexer->ReadToEndOfLine(&Message); 1181*f4a2713aSLionel Sambuc 1182*f4a2713aSLionel Sambuc // Find the first non-whitespace character, so that we can make the 1183*f4a2713aSLionel Sambuc // diagnostic more succinct. 1184*f4a2713aSLionel Sambuc StringRef Msg = Message.str().ltrim(" "); 1185*f4a2713aSLionel Sambuc 1186*f4a2713aSLionel Sambuc if (isWarning) 1187*f4a2713aSLionel Sambuc Diag(Tok, diag::pp_hash_warning) << Msg; 1188*f4a2713aSLionel Sambuc else 1189*f4a2713aSLionel Sambuc Diag(Tok, diag::err_pp_hash_error) << Msg; 1190*f4a2713aSLionel Sambuc } 1191*f4a2713aSLionel Sambuc 1192*f4a2713aSLionel Sambuc /// HandleIdentSCCSDirective - Handle a #ident/#sccs directive. 1193*f4a2713aSLionel Sambuc /// 1194*f4a2713aSLionel Sambuc void Preprocessor::HandleIdentSCCSDirective(Token &Tok) { 1195*f4a2713aSLionel Sambuc // Yes, this directive is an extension. 1196*f4a2713aSLionel Sambuc Diag(Tok, diag::ext_pp_ident_directive); 1197*f4a2713aSLionel Sambuc 1198*f4a2713aSLionel Sambuc // Read the string argument. 1199*f4a2713aSLionel Sambuc Token StrTok; 1200*f4a2713aSLionel Sambuc Lex(StrTok); 1201*f4a2713aSLionel Sambuc 1202*f4a2713aSLionel Sambuc // If the token kind isn't a string, it's a malformed directive. 1203*f4a2713aSLionel Sambuc if (StrTok.isNot(tok::string_literal) && 1204*f4a2713aSLionel Sambuc StrTok.isNot(tok::wide_string_literal)) { 1205*f4a2713aSLionel Sambuc Diag(StrTok, diag::err_pp_malformed_ident); 1206*f4a2713aSLionel Sambuc if (StrTok.isNot(tok::eod)) 1207*f4a2713aSLionel Sambuc DiscardUntilEndOfDirective(); 1208*f4a2713aSLionel Sambuc return; 1209*f4a2713aSLionel Sambuc } 1210*f4a2713aSLionel Sambuc 1211*f4a2713aSLionel Sambuc if (StrTok.hasUDSuffix()) { 1212*f4a2713aSLionel Sambuc Diag(StrTok, diag::err_invalid_string_udl); 1213*f4a2713aSLionel Sambuc return DiscardUntilEndOfDirective(); 1214*f4a2713aSLionel Sambuc } 1215*f4a2713aSLionel Sambuc 1216*f4a2713aSLionel Sambuc // Verify that there is nothing after the string, other than EOD. 1217*f4a2713aSLionel Sambuc CheckEndOfDirective("ident"); 1218*f4a2713aSLionel Sambuc 1219*f4a2713aSLionel Sambuc if (Callbacks) { 1220*f4a2713aSLionel Sambuc bool Invalid = false; 1221*f4a2713aSLionel Sambuc std::string Str = getSpelling(StrTok, &Invalid); 1222*f4a2713aSLionel Sambuc if (!Invalid) 1223*f4a2713aSLionel Sambuc Callbacks->Ident(Tok.getLocation(), Str); 1224*f4a2713aSLionel Sambuc } 1225*f4a2713aSLionel Sambuc } 1226*f4a2713aSLionel Sambuc 1227*f4a2713aSLionel Sambuc /// \brief Handle a #public directive. 1228*f4a2713aSLionel Sambuc void Preprocessor::HandleMacroPublicDirective(Token &Tok) { 1229*f4a2713aSLionel Sambuc Token MacroNameTok; 1230*f4a2713aSLionel Sambuc ReadMacroName(MacroNameTok, 2); 1231*f4a2713aSLionel Sambuc 1232*f4a2713aSLionel Sambuc // Error reading macro name? If so, diagnostic already issued. 1233*f4a2713aSLionel Sambuc if (MacroNameTok.is(tok::eod)) 1234*f4a2713aSLionel Sambuc return; 1235*f4a2713aSLionel Sambuc 1236*f4a2713aSLionel Sambuc // Check to see if this is the last token on the #__public_macro line. 1237*f4a2713aSLionel Sambuc CheckEndOfDirective("__public_macro"); 1238*f4a2713aSLionel Sambuc 1239*f4a2713aSLionel Sambuc IdentifierInfo *II = MacroNameTok.getIdentifierInfo(); 1240*f4a2713aSLionel Sambuc // Okay, we finally have a valid identifier to undef. 1241*f4a2713aSLionel Sambuc MacroDirective *MD = getMacroDirective(II); 1242*f4a2713aSLionel Sambuc 1243*f4a2713aSLionel Sambuc // If the macro is not defined, this is an error. 1244*f4a2713aSLionel Sambuc if (MD == 0) { 1245*f4a2713aSLionel Sambuc Diag(MacroNameTok, diag::err_pp_visibility_non_macro) << II; 1246*f4a2713aSLionel Sambuc return; 1247*f4a2713aSLionel Sambuc } 1248*f4a2713aSLionel Sambuc 1249*f4a2713aSLionel Sambuc // Note that this macro has now been exported. 1250*f4a2713aSLionel Sambuc appendMacroDirective(II, AllocateVisibilityMacroDirective( 1251*f4a2713aSLionel Sambuc MacroNameTok.getLocation(), /*IsPublic=*/true)); 1252*f4a2713aSLionel Sambuc } 1253*f4a2713aSLionel Sambuc 1254*f4a2713aSLionel Sambuc /// \brief Handle a #private directive. 1255*f4a2713aSLionel Sambuc void Preprocessor::HandleMacroPrivateDirective(Token &Tok) { 1256*f4a2713aSLionel Sambuc Token MacroNameTok; 1257*f4a2713aSLionel Sambuc ReadMacroName(MacroNameTok, 2); 1258*f4a2713aSLionel Sambuc 1259*f4a2713aSLionel Sambuc // Error reading macro name? If so, diagnostic already issued. 1260*f4a2713aSLionel Sambuc if (MacroNameTok.is(tok::eod)) 1261*f4a2713aSLionel Sambuc return; 1262*f4a2713aSLionel Sambuc 1263*f4a2713aSLionel Sambuc // Check to see if this is the last token on the #__private_macro line. 1264*f4a2713aSLionel Sambuc CheckEndOfDirective("__private_macro"); 1265*f4a2713aSLionel Sambuc 1266*f4a2713aSLionel Sambuc IdentifierInfo *II = MacroNameTok.getIdentifierInfo(); 1267*f4a2713aSLionel Sambuc // Okay, we finally have a valid identifier to undef. 1268*f4a2713aSLionel Sambuc MacroDirective *MD = getMacroDirective(II); 1269*f4a2713aSLionel Sambuc 1270*f4a2713aSLionel Sambuc // If the macro is not defined, this is an error. 1271*f4a2713aSLionel Sambuc if (MD == 0) { 1272*f4a2713aSLionel Sambuc Diag(MacroNameTok, diag::err_pp_visibility_non_macro) << II; 1273*f4a2713aSLionel Sambuc return; 1274*f4a2713aSLionel Sambuc } 1275*f4a2713aSLionel Sambuc 1276*f4a2713aSLionel Sambuc // Note that this macro has now been marked private. 1277*f4a2713aSLionel Sambuc appendMacroDirective(II, AllocateVisibilityMacroDirective( 1278*f4a2713aSLionel Sambuc MacroNameTok.getLocation(), /*IsPublic=*/false)); 1279*f4a2713aSLionel Sambuc } 1280*f4a2713aSLionel Sambuc 1281*f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===// 1282*f4a2713aSLionel Sambuc // Preprocessor Include Directive Handling. 1283*f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===// 1284*f4a2713aSLionel Sambuc 1285*f4a2713aSLionel Sambuc /// GetIncludeFilenameSpelling - Turn the specified lexer token into a fully 1286*f4a2713aSLionel Sambuc /// checked and spelled filename, e.g. as an operand of \#include. This returns 1287*f4a2713aSLionel Sambuc /// true if the input filename was in <>'s or false if it were in ""'s. The 1288*f4a2713aSLionel Sambuc /// caller is expected to provide a buffer that is large enough to hold the 1289*f4a2713aSLionel Sambuc /// spelling of the filename, but is also expected to handle the case when 1290*f4a2713aSLionel Sambuc /// this method decides to use a different buffer. 1291*f4a2713aSLionel Sambuc bool Preprocessor::GetIncludeFilenameSpelling(SourceLocation Loc, 1292*f4a2713aSLionel Sambuc StringRef &Buffer) { 1293*f4a2713aSLionel Sambuc // Get the text form of the filename. 1294*f4a2713aSLionel Sambuc assert(!Buffer.empty() && "Can't have tokens with empty spellings!"); 1295*f4a2713aSLionel Sambuc 1296*f4a2713aSLionel Sambuc // Make sure the filename is <x> or "x". 1297*f4a2713aSLionel Sambuc bool isAngled; 1298*f4a2713aSLionel Sambuc if (Buffer[0] == '<') { 1299*f4a2713aSLionel Sambuc if (Buffer.back() != '>') { 1300*f4a2713aSLionel Sambuc Diag(Loc, diag::err_pp_expects_filename); 1301*f4a2713aSLionel Sambuc Buffer = StringRef(); 1302*f4a2713aSLionel Sambuc return true; 1303*f4a2713aSLionel Sambuc } 1304*f4a2713aSLionel Sambuc isAngled = true; 1305*f4a2713aSLionel Sambuc } else if (Buffer[0] == '"') { 1306*f4a2713aSLionel Sambuc if (Buffer.back() != '"') { 1307*f4a2713aSLionel Sambuc Diag(Loc, diag::err_pp_expects_filename); 1308*f4a2713aSLionel Sambuc Buffer = StringRef(); 1309*f4a2713aSLionel Sambuc return true; 1310*f4a2713aSLionel Sambuc } 1311*f4a2713aSLionel Sambuc isAngled = false; 1312*f4a2713aSLionel Sambuc } else { 1313*f4a2713aSLionel Sambuc Diag(Loc, diag::err_pp_expects_filename); 1314*f4a2713aSLionel Sambuc Buffer = StringRef(); 1315*f4a2713aSLionel Sambuc return true; 1316*f4a2713aSLionel Sambuc } 1317*f4a2713aSLionel Sambuc 1318*f4a2713aSLionel Sambuc // Diagnose #include "" as invalid. 1319*f4a2713aSLionel Sambuc if (Buffer.size() <= 2) { 1320*f4a2713aSLionel Sambuc Diag(Loc, diag::err_pp_empty_filename); 1321*f4a2713aSLionel Sambuc Buffer = StringRef(); 1322*f4a2713aSLionel Sambuc return true; 1323*f4a2713aSLionel Sambuc } 1324*f4a2713aSLionel Sambuc 1325*f4a2713aSLionel Sambuc // Skip the brackets. 1326*f4a2713aSLionel Sambuc Buffer = Buffer.substr(1, Buffer.size()-2); 1327*f4a2713aSLionel Sambuc return isAngled; 1328*f4a2713aSLionel Sambuc } 1329*f4a2713aSLionel Sambuc 1330*f4a2713aSLionel Sambuc /// \brief Handle cases where the \#include name is expanded from a macro 1331*f4a2713aSLionel Sambuc /// as multiple tokens, which need to be glued together. 1332*f4a2713aSLionel Sambuc /// 1333*f4a2713aSLionel Sambuc /// This occurs for code like: 1334*f4a2713aSLionel Sambuc /// \code 1335*f4a2713aSLionel Sambuc /// \#define FOO <a/b.h> 1336*f4a2713aSLionel Sambuc /// \#include FOO 1337*f4a2713aSLionel Sambuc /// \endcode 1338*f4a2713aSLionel Sambuc /// because in this case, "<a/b.h>" is returned as 7 tokens, not one. 1339*f4a2713aSLionel Sambuc /// 1340*f4a2713aSLionel Sambuc /// This code concatenates and consumes tokens up to the '>' token. It returns 1341*f4a2713aSLionel Sambuc /// false if the > was found, otherwise it returns true if it finds and consumes 1342*f4a2713aSLionel Sambuc /// the EOD marker. 1343*f4a2713aSLionel Sambuc bool Preprocessor::ConcatenateIncludeName( 1344*f4a2713aSLionel Sambuc SmallString<128> &FilenameBuffer, 1345*f4a2713aSLionel Sambuc SourceLocation &End) { 1346*f4a2713aSLionel Sambuc Token CurTok; 1347*f4a2713aSLionel Sambuc 1348*f4a2713aSLionel Sambuc Lex(CurTok); 1349*f4a2713aSLionel Sambuc while (CurTok.isNot(tok::eod)) { 1350*f4a2713aSLionel Sambuc End = CurTok.getLocation(); 1351*f4a2713aSLionel Sambuc 1352*f4a2713aSLionel Sambuc // FIXME: Provide code completion for #includes. 1353*f4a2713aSLionel Sambuc if (CurTok.is(tok::code_completion)) { 1354*f4a2713aSLionel Sambuc setCodeCompletionReached(); 1355*f4a2713aSLionel Sambuc Lex(CurTok); 1356*f4a2713aSLionel Sambuc continue; 1357*f4a2713aSLionel Sambuc } 1358*f4a2713aSLionel Sambuc 1359*f4a2713aSLionel Sambuc // Append the spelling of this token to the buffer. If there was a space 1360*f4a2713aSLionel Sambuc // before it, add it now. 1361*f4a2713aSLionel Sambuc if (CurTok.hasLeadingSpace()) 1362*f4a2713aSLionel Sambuc FilenameBuffer.push_back(' '); 1363*f4a2713aSLionel Sambuc 1364*f4a2713aSLionel Sambuc // Get the spelling of the token, directly into FilenameBuffer if possible. 1365*f4a2713aSLionel Sambuc unsigned PreAppendSize = FilenameBuffer.size(); 1366*f4a2713aSLionel Sambuc FilenameBuffer.resize(PreAppendSize+CurTok.getLength()); 1367*f4a2713aSLionel Sambuc 1368*f4a2713aSLionel Sambuc const char *BufPtr = &FilenameBuffer[PreAppendSize]; 1369*f4a2713aSLionel Sambuc unsigned ActualLen = getSpelling(CurTok, BufPtr); 1370*f4a2713aSLionel Sambuc 1371*f4a2713aSLionel Sambuc // If the token was spelled somewhere else, copy it into FilenameBuffer. 1372*f4a2713aSLionel Sambuc if (BufPtr != &FilenameBuffer[PreAppendSize]) 1373*f4a2713aSLionel Sambuc memcpy(&FilenameBuffer[PreAppendSize], BufPtr, ActualLen); 1374*f4a2713aSLionel Sambuc 1375*f4a2713aSLionel Sambuc // Resize FilenameBuffer to the correct size. 1376*f4a2713aSLionel Sambuc if (CurTok.getLength() != ActualLen) 1377*f4a2713aSLionel Sambuc FilenameBuffer.resize(PreAppendSize+ActualLen); 1378*f4a2713aSLionel Sambuc 1379*f4a2713aSLionel Sambuc // If we found the '>' marker, return success. 1380*f4a2713aSLionel Sambuc if (CurTok.is(tok::greater)) 1381*f4a2713aSLionel Sambuc return false; 1382*f4a2713aSLionel Sambuc 1383*f4a2713aSLionel Sambuc Lex(CurTok); 1384*f4a2713aSLionel Sambuc } 1385*f4a2713aSLionel Sambuc 1386*f4a2713aSLionel Sambuc // If we hit the eod marker, emit an error and return true so that the caller 1387*f4a2713aSLionel Sambuc // knows the EOD has been read. 1388*f4a2713aSLionel Sambuc Diag(CurTok.getLocation(), diag::err_pp_expects_filename); 1389*f4a2713aSLionel Sambuc return true; 1390*f4a2713aSLionel Sambuc } 1391*f4a2713aSLionel Sambuc 1392*f4a2713aSLionel Sambuc /// HandleIncludeDirective - The "\#include" tokens have just been read, read 1393*f4a2713aSLionel Sambuc /// the file to be included from the lexer, then include it! This is a common 1394*f4a2713aSLionel Sambuc /// routine with functionality shared between \#include, \#include_next and 1395*f4a2713aSLionel Sambuc /// \#import. LookupFrom is set when this is a \#include_next directive, it 1396*f4a2713aSLionel Sambuc /// specifies the file to start searching from. 1397*f4a2713aSLionel Sambuc void Preprocessor::HandleIncludeDirective(SourceLocation HashLoc, 1398*f4a2713aSLionel Sambuc Token &IncludeTok, 1399*f4a2713aSLionel Sambuc const DirectoryLookup *LookupFrom, 1400*f4a2713aSLionel Sambuc bool isImport) { 1401*f4a2713aSLionel Sambuc 1402*f4a2713aSLionel Sambuc Token FilenameTok; 1403*f4a2713aSLionel Sambuc CurPPLexer->LexIncludeFilename(FilenameTok); 1404*f4a2713aSLionel Sambuc 1405*f4a2713aSLionel Sambuc // Reserve a buffer to get the spelling. 1406*f4a2713aSLionel Sambuc SmallString<128> FilenameBuffer; 1407*f4a2713aSLionel Sambuc StringRef Filename; 1408*f4a2713aSLionel Sambuc SourceLocation End; 1409*f4a2713aSLionel Sambuc SourceLocation CharEnd; // the end of this directive, in characters 1410*f4a2713aSLionel Sambuc 1411*f4a2713aSLionel Sambuc switch (FilenameTok.getKind()) { 1412*f4a2713aSLionel Sambuc case tok::eod: 1413*f4a2713aSLionel Sambuc // If the token kind is EOD, the error has already been diagnosed. 1414*f4a2713aSLionel Sambuc return; 1415*f4a2713aSLionel Sambuc 1416*f4a2713aSLionel Sambuc case tok::angle_string_literal: 1417*f4a2713aSLionel Sambuc case tok::string_literal: 1418*f4a2713aSLionel Sambuc Filename = getSpelling(FilenameTok, FilenameBuffer); 1419*f4a2713aSLionel Sambuc End = FilenameTok.getLocation(); 1420*f4a2713aSLionel Sambuc CharEnd = End.getLocWithOffset(FilenameTok.getLength()); 1421*f4a2713aSLionel Sambuc break; 1422*f4a2713aSLionel Sambuc 1423*f4a2713aSLionel Sambuc case tok::less: 1424*f4a2713aSLionel Sambuc // This could be a <foo/bar.h> file coming from a macro expansion. In this 1425*f4a2713aSLionel Sambuc // case, glue the tokens together into FilenameBuffer and interpret those. 1426*f4a2713aSLionel Sambuc FilenameBuffer.push_back('<'); 1427*f4a2713aSLionel Sambuc if (ConcatenateIncludeName(FilenameBuffer, End)) 1428*f4a2713aSLionel Sambuc return; // Found <eod> but no ">"? Diagnostic already emitted. 1429*f4a2713aSLionel Sambuc Filename = FilenameBuffer.str(); 1430*f4a2713aSLionel Sambuc CharEnd = End.getLocWithOffset(1); 1431*f4a2713aSLionel Sambuc break; 1432*f4a2713aSLionel Sambuc default: 1433*f4a2713aSLionel Sambuc Diag(FilenameTok.getLocation(), diag::err_pp_expects_filename); 1434*f4a2713aSLionel Sambuc DiscardUntilEndOfDirective(); 1435*f4a2713aSLionel Sambuc return; 1436*f4a2713aSLionel Sambuc } 1437*f4a2713aSLionel Sambuc 1438*f4a2713aSLionel Sambuc CharSourceRange FilenameRange 1439*f4a2713aSLionel Sambuc = CharSourceRange::getCharRange(FilenameTok.getLocation(), CharEnd); 1440*f4a2713aSLionel Sambuc StringRef OriginalFilename = Filename; 1441*f4a2713aSLionel Sambuc bool isAngled = 1442*f4a2713aSLionel Sambuc GetIncludeFilenameSpelling(FilenameTok.getLocation(), Filename); 1443*f4a2713aSLionel Sambuc // If GetIncludeFilenameSpelling set the start ptr to null, there was an 1444*f4a2713aSLionel Sambuc // error. 1445*f4a2713aSLionel Sambuc if (Filename.empty()) { 1446*f4a2713aSLionel Sambuc DiscardUntilEndOfDirective(); 1447*f4a2713aSLionel Sambuc return; 1448*f4a2713aSLionel Sambuc } 1449*f4a2713aSLionel Sambuc 1450*f4a2713aSLionel Sambuc // Verify that there is nothing after the filename, other than EOD. Note that 1451*f4a2713aSLionel Sambuc // we allow macros that expand to nothing after the filename, because this 1452*f4a2713aSLionel Sambuc // falls into the category of "#include pp-tokens new-line" specified in 1453*f4a2713aSLionel Sambuc // C99 6.10.2p4. 1454*f4a2713aSLionel Sambuc CheckEndOfDirective(IncludeTok.getIdentifierInfo()->getNameStart(), true); 1455*f4a2713aSLionel Sambuc 1456*f4a2713aSLionel Sambuc // Check that we don't have infinite #include recursion. 1457*f4a2713aSLionel Sambuc if (IncludeMacroStack.size() == MaxAllowedIncludeStackDepth-1) { 1458*f4a2713aSLionel Sambuc Diag(FilenameTok, diag::err_pp_include_too_deep); 1459*f4a2713aSLionel Sambuc return; 1460*f4a2713aSLionel Sambuc } 1461*f4a2713aSLionel Sambuc 1462*f4a2713aSLionel Sambuc // Complain about attempts to #include files in an audit pragma. 1463*f4a2713aSLionel Sambuc if (PragmaARCCFCodeAuditedLoc.isValid()) { 1464*f4a2713aSLionel Sambuc Diag(HashLoc, diag::err_pp_include_in_arc_cf_code_audited); 1465*f4a2713aSLionel Sambuc Diag(PragmaARCCFCodeAuditedLoc, diag::note_pragma_entered_here); 1466*f4a2713aSLionel Sambuc 1467*f4a2713aSLionel Sambuc // Immediately leave the pragma. 1468*f4a2713aSLionel Sambuc PragmaARCCFCodeAuditedLoc = SourceLocation(); 1469*f4a2713aSLionel Sambuc } 1470*f4a2713aSLionel Sambuc 1471*f4a2713aSLionel Sambuc if (HeaderInfo.HasIncludeAliasMap()) { 1472*f4a2713aSLionel Sambuc // Map the filename with the brackets still attached. If the name doesn't 1473*f4a2713aSLionel Sambuc // map to anything, fall back on the filename we've already gotten the 1474*f4a2713aSLionel Sambuc // spelling for. 1475*f4a2713aSLionel Sambuc StringRef NewName = HeaderInfo.MapHeaderToIncludeAlias(OriginalFilename); 1476*f4a2713aSLionel Sambuc if (!NewName.empty()) 1477*f4a2713aSLionel Sambuc Filename = NewName; 1478*f4a2713aSLionel Sambuc } 1479*f4a2713aSLionel Sambuc 1480*f4a2713aSLionel Sambuc // Search include directories. 1481*f4a2713aSLionel Sambuc const DirectoryLookup *CurDir; 1482*f4a2713aSLionel Sambuc SmallString<1024> SearchPath; 1483*f4a2713aSLionel Sambuc SmallString<1024> RelativePath; 1484*f4a2713aSLionel Sambuc // We get the raw path only if we have 'Callbacks' to which we later pass 1485*f4a2713aSLionel Sambuc // the path. 1486*f4a2713aSLionel Sambuc ModuleMap::KnownHeader SuggestedModule; 1487*f4a2713aSLionel Sambuc SourceLocation FilenameLoc = FilenameTok.getLocation(); 1488*f4a2713aSLionel Sambuc const FileEntry *File = LookupFile( 1489*f4a2713aSLionel Sambuc FilenameLoc, Filename, isAngled, LookupFrom, CurDir, 1490*f4a2713aSLionel Sambuc Callbacks ? &SearchPath : NULL, Callbacks ? &RelativePath : NULL, 1491*f4a2713aSLionel Sambuc HeaderInfo.getHeaderSearchOpts().ModuleMaps ? &SuggestedModule : 0); 1492*f4a2713aSLionel Sambuc 1493*f4a2713aSLionel Sambuc if (Callbacks) { 1494*f4a2713aSLionel Sambuc if (!File) { 1495*f4a2713aSLionel Sambuc // Give the clients a chance to recover. 1496*f4a2713aSLionel Sambuc SmallString<128> RecoveryPath; 1497*f4a2713aSLionel Sambuc if (Callbacks->FileNotFound(Filename, RecoveryPath)) { 1498*f4a2713aSLionel Sambuc if (const DirectoryEntry *DE = FileMgr.getDirectory(RecoveryPath)) { 1499*f4a2713aSLionel Sambuc // Add the recovery path to the list of search paths. 1500*f4a2713aSLionel Sambuc DirectoryLookup DL(DE, SrcMgr::C_User, false); 1501*f4a2713aSLionel Sambuc HeaderInfo.AddSearchPath(DL, isAngled); 1502*f4a2713aSLionel Sambuc 1503*f4a2713aSLionel Sambuc // Try the lookup again, skipping the cache. 1504*f4a2713aSLionel Sambuc File = LookupFile(FilenameLoc, Filename, isAngled, LookupFrom, CurDir, 1505*f4a2713aSLionel Sambuc 0, 0, HeaderInfo.getHeaderSearchOpts().ModuleMaps 1506*f4a2713aSLionel Sambuc ? &SuggestedModule 1507*f4a2713aSLionel Sambuc : 0, 1508*f4a2713aSLionel Sambuc /*SkipCache*/ true); 1509*f4a2713aSLionel Sambuc } 1510*f4a2713aSLionel Sambuc } 1511*f4a2713aSLionel Sambuc } 1512*f4a2713aSLionel Sambuc 1513*f4a2713aSLionel Sambuc if (!SuggestedModule || !getLangOpts().Modules) { 1514*f4a2713aSLionel Sambuc // Notify the callback object that we've seen an inclusion directive. 1515*f4a2713aSLionel Sambuc Callbacks->InclusionDirective(HashLoc, IncludeTok, Filename, isAngled, 1516*f4a2713aSLionel Sambuc FilenameRange, File, 1517*f4a2713aSLionel Sambuc SearchPath, RelativePath, 1518*f4a2713aSLionel Sambuc /*ImportedModule=*/0); 1519*f4a2713aSLionel Sambuc } 1520*f4a2713aSLionel Sambuc } 1521*f4a2713aSLionel Sambuc 1522*f4a2713aSLionel Sambuc if (File == 0) { 1523*f4a2713aSLionel Sambuc if (!SuppressIncludeNotFoundError) { 1524*f4a2713aSLionel Sambuc // If the file could not be located and it was included via angle 1525*f4a2713aSLionel Sambuc // brackets, we can attempt a lookup as though it were a quoted path to 1526*f4a2713aSLionel Sambuc // provide the user with a possible fixit. 1527*f4a2713aSLionel Sambuc if (isAngled) { 1528*f4a2713aSLionel Sambuc File = LookupFile( 1529*f4a2713aSLionel Sambuc FilenameLoc, Filename, false, LookupFrom, CurDir, 1530*f4a2713aSLionel Sambuc Callbacks ? &SearchPath : 0, Callbacks ? &RelativePath : 0, 1531*f4a2713aSLionel Sambuc HeaderInfo.getHeaderSearchOpts().ModuleMaps ? &SuggestedModule : 0); 1532*f4a2713aSLionel Sambuc if (File) { 1533*f4a2713aSLionel Sambuc SourceRange Range(FilenameTok.getLocation(), CharEnd); 1534*f4a2713aSLionel Sambuc Diag(FilenameTok, diag::err_pp_file_not_found_not_fatal) << 1535*f4a2713aSLionel Sambuc Filename << 1536*f4a2713aSLionel Sambuc FixItHint::CreateReplacement(Range, "\"" + Filename.str() + "\""); 1537*f4a2713aSLionel Sambuc } 1538*f4a2713aSLionel Sambuc } 1539*f4a2713aSLionel Sambuc // If the file is still not found, just go with the vanilla diagnostic 1540*f4a2713aSLionel Sambuc if (!File) 1541*f4a2713aSLionel Sambuc Diag(FilenameTok, diag::err_pp_file_not_found) << Filename; 1542*f4a2713aSLionel Sambuc } 1543*f4a2713aSLionel Sambuc if (!File) 1544*f4a2713aSLionel Sambuc return; 1545*f4a2713aSLionel Sambuc } 1546*f4a2713aSLionel Sambuc 1547*f4a2713aSLionel Sambuc // If we are supposed to import a module rather than including the header, 1548*f4a2713aSLionel Sambuc // do so now. 1549*f4a2713aSLionel Sambuc if (SuggestedModule && getLangOpts().Modules) { 1550*f4a2713aSLionel Sambuc // Compute the module access path corresponding to this module. 1551*f4a2713aSLionel Sambuc // FIXME: Should we have a second loadModule() overload to avoid this 1552*f4a2713aSLionel Sambuc // extra lookup step? 1553*f4a2713aSLionel Sambuc SmallVector<std::pair<IdentifierInfo *, SourceLocation>, 2> Path; 1554*f4a2713aSLionel Sambuc for (Module *Mod = SuggestedModule.getModule(); Mod; Mod = Mod->Parent) 1555*f4a2713aSLionel Sambuc Path.push_back(std::make_pair(getIdentifierInfo(Mod->Name), 1556*f4a2713aSLionel Sambuc FilenameTok.getLocation())); 1557*f4a2713aSLionel Sambuc std::reverse(Path.begin(), Path.end()); 1558*f4a2713aSLionel Sambuc 1559*f4a2713aSLionel Sambuc // Warn that we're replacing the include/import with a module import. 1560*f4a2713aSLionel Sambuc SmallString<128> PathString; 1561*f4a2713aSLionel Sambuc for (unsigned I = 0, N = Path.size(); I != N; ++I) { 1562*f4a2713aSLionel Sambuc if (I) 1563*f4a2713aSLionel Sambuc PathString += '.'; 1564*f4a2713aSLionel Sambuc PathString += Path[I].first->getName(); 1565*f4a2713aSLionel Sambuc } 1566*f4a2713aSLionel Sambuc int IncludeKind = 0; 1567*f4a2713aSLionel Sambuc 1568*f4a2713aSLionel Sambuc switch (IncludeTok.getIdentifierInfo()->getPPKeywordID()) { 1569*f4a2713aSLionel Sambuc case tok::pp_include: 1570*f4a2713aSLionel Sambuc IncludeKind = 0; 1571*f4a2713aSLionel Sambuc break; 1572*f4a2713aSLionel Sambuc 1573*f4a2713aSLionel Sambuc case tok::pp_import: 1574*f4a2713aSLionel Sambuc IncludeKind = 1; 1575*f4a2713aSLionel Sambuc break; 1576*f4a2713aSLionel Sambuc 1577*f4a2713aSLionel Sambuc case tok::pp_include_next: 1578*f4a2713aSLionel Sambuc IncludeKind = 2; 1579*f4a2713aSLionel Sambuc break; 1580*f4a2713aSLionel Sambuc 1581*f4a2713aSLionel Sambuc case tok::pp___include_macros: 1582*f4a2713aSLionel Sambuc IncludeKind = 3; 1583*f4a2713aSLionel Sambuc break; 1584*f4a2713aSLionel Sambuc 1585*f4a2713aSLionel Sambuc default: 1586*f4a2713aSLionel Sambuc llvm_unreachable("unknown include directive kind"); 1587*f4a2713aSLionel Sambuc } 1588*f4a2713aSLionel Sambuc 1589*f4a2713aSLionel Sambuc // Determine whether we are actually building the module that this 1590*f4a2713aSLionel Sambuc // include directive maps to. 1591*f4a2713aSLionel Sambuc bool BuildingImportedModule 1592*f4a2713aSLionel Sambuc = Path[0].first->getName() == getLangOpts().CurrentModule; 1593*f4a2713aSLionel Sambuc 1594*f4a2713aSLionel Sambuc if (!BuildingImportedModule && getLangOpts().ObjC2) { 1595*f4a2713aSLionel Sambuc // If we're not building the imported module, warn that we're going 1596*f4a2713aSLionel Sambuc // to automatically turn this inclusion directive into a module import. 1597*f4a2713aSLionel Sambuc // We only do this in Objective-C, where we have a module-import syntax. 1598*f4a2713aSLionel Sambuc CharSourceRange ReplaceRange(SourceRange(HashLoc, CharEnd), 1599*f4a2713aSLionel Sambuc /*IsTokenRange=*/false); 1600*f4a2713aSLionel Sambuc Diag(HashLoc, diag::warn_auto_module_import) 1601*f4a2713aSLionel Sambuc << IncludeKind << PathString 1602*f4a2713aSLionel Sambuc << FixItHint::CreateReplacement(ReplaceRange, 1603*f4a2713aSLionel Sambuc "@import " + PathString.str().str() + ";"); 1604*f4a2713aSLionel Sambuc } 1605*f4a2713aSLionel Sambuc 1606*f4a2713aSLionel Sambuc // Load the module. Only make macros visible. We'll make the declarations 1607*f4a2713aSLionel Sambuc // visible when the parser gets here. 1608*f4a2713aSLionel Sambuc Module::NameVisibilityKind Visibility = Module::MacrosVisible; 1609*f4a2713aSLionel Sambuc ModuleLoadResult Imported 1610*f4a2713aSLionel Sambuc = TheModuleLoader.loadModule(IncludeTok.getLocation(), Path, Visibility, 1611*f4a2713aSLionel Sambuc /*IsIncludeDirective=*/true); 1612*f4a2713aSLionel Sambuc assert((Imported == 0 || Imported == SuggestedModule.getModule()) && 1613*f4a2713aSLionel Sambuc "the imported module is different than the suggested one"); 1614*f4a2713aSLionel Sambuc 1615*f4a2713aSLionel Sambuc if (!Imported && hadModuleLoaderFatalFailure()) { 1616*f4a2713aSLionel Sambuc // With a fatal failure in the module loader, we abort parsing. 1617*f4a2713aSLionel Sambuc Token &Result = IncludeTok; 1618*f4a2713aSLionel Sambuc if (CurLexer) { 1619*f4a2713aSLionel Sambuc Result.startToken(); 1620*f4a2713aSLionel Sambuc CurLexer->FormTokenWithChars(Result, CurLexer->BufferEnd, tok::eof); 1621*f4a2713aSLionel Sambuc CurLexer->cutOffLexing(); 1622*f4a2713aSLionel Sambuc } else { 1623*f4a2713aSLionel Sambuc assert(CurPTHLexer && "#include but no current lexer set!"); 1624*f4a2713aSLionel Sambuc CurPTHLexer->getEOF(Result); 1625*f4a2713aSLionel Sambuc } 1626*f4a2713aSLionel Sambuc return; 1627*f4a2713aSLionel Sambuc } 1628*f4a2713aSLionel Sambuc 1629*f4a2713aSLionel Sambuc // If this header isn't part of the module we're building, we're done. 1630*f4a2713aSLionel Sambuc if (!BuildingImportedModule && Imported) { 1631*f4a2713aSLionel Sambuc if (Callbacks) { 1632*f4a2713aSLionel Sambuc Callbacks->InclusionDirective(HashLoc, IncludeTok, Filename, isAngled, 1633*f4a2713aSLionel Sambuc FilenameRange, File, 1634*f4a2713aSLionel Sambuc SearchPath, RelativePath, Imported); 1635*f4a2713aSLionel Sambuc } 1636*f4a2713aSLionel Sambuc 1637*f4a2713aSLionel Sambuc if (IncludeKind != 3) { 1638*f4a2713aSLionel Sambuc // Let the parser know that we hit a module import, and it should 1639*f4a2713aSLionel Sambuc // make the module visible. 1640*f4a2713aSLionel Sambuc // FIXME: Produce this as the current token directly, rather than 1641*f4a2713aSLionel Sambuc // allocating a new token for it. 1642*f4a2713aSLionel Sambuc Token *Tok = new Token[1]; 1643*f4a2713aSLionel Sambuc Tok[0].startToken(); 1644*f4a2713aSLionel Sambuc Tok[0].setKind(tok::annot_module_include); 1645*f4a2713aSLionel Sambuc Tok[0].setLocation(HashLoc); 1646*f4a2713aSLionel Sambuc Tok[0].setAnnotationEndLoc(End); 1647*f4a2713aSLionel Sambuc Tok[0].setAnnotationValue(Imported); 1648*f4a2713aSLionel Sambuc EnterTokenStream(Tok, 1, true, true); 1649*f4a2713aSLionel Sambuc } 1650*f4a2713aSLionel Sambuc return; 1651*f4a2713aSLionel Sambuc } 1652*f4a2713aSLionel Sambuc 1653*f4a2713aSLionel Sambuc // If we failed to find a submodule that we expected to find, we can 1654*f4a2713aSLionel Sambuc // continue. Otherwise, there's an error in the included file, so we 1655*f4a2713aSLionel Sambuc // don't want to include it. 1656*f4a2713aSLionel Sambuc if (!BuildingImportedModule && !Imported.isMissingExpected()) { 1657*f4a2713aSLionel Sambuc return; 1658*f4a2713aSLionel Sambuc } 1659*f4a2713aSLionel Sambuc } 1660*f4a2713aSLionel Sambuc 1661*f4a2713aSLionel Sambuc if (Callbacks && SuggestedModule) { 1662*f4a2713aSLionel Sambuc // We didn't notify the callback object that we've seen an inclusion 1663*f4a2713aSLionel Sambuc // directive before. Now that we are parsing the include normally and not 1664*f4a2713aSLionel Sambuc // turning it to a module import, notify the callback object. 1665*f4a2713aSLionel Sambuc Callbacks->InclusionDirective(HashLoc, IncludeTok, Filename, isAngled, 1666*f4a2713aSLionel Sambuc FilenameRange, File, 1667*f4a2713aSLionel Sambuc SearchPath, RelativePath, 1668*f4a2713aSLionel Sambuc /*ImportedModule=*/0); 1669*f4a2713aSLionel Sambuc } 1670*f4a2713aSLionel Sambuc 1671*f4a2713aSLionel Sambuc // The #included file will be considered to be a system header if either it is 1672*f4a2713aSLionel Sambuc // in a system include directory, or if the #includer is a system include 1673*f4a2713aSLionel Sambuc // header. 1674*f4a2713aSLionel Sambuc SrcMgr::CharacteristicKind FileCharacter = 1675*f4a2713aSLionel Sambuc std::max(HeaderInfo.getFileDirFlavor(File), 1676*f4a2713aSLionel Sambuc SourceMgr.getFileCharacteristic(FilenameTok.getLocation())); 1677*f4a2713aSLionel Sambuc 1678*f4a2713aSLionel Sambuc // Ask HeaderInfo if we should enter this #include file. If not, #including 1679*f4a2713aSLionel Sambuc // this file will have no effect. 1680*f4a2713aSLionel Sambuc if (!HeaderInfo.ShouldEnterIncludeFile(File, isImport)) { 1681*f4a2713aSLionel Sambuc if (Callbacks) 1682*f4a2713aSLionel Sambuc Callbacks->FileSkipped(*File, FilenameTok, FileCharacter); 1683*f4a2713aSLionel Sambuc return; 1684*f4a2713aSLionel Sambuc } 1685*f4a2713aSLionel Sambuc 1686*f4a2713aSLionel Sambuc // Look up the file, create a File ID for it. 1687*f4a2713aSLionel Sambuc SourceLocation IncludePos = End; 1688*f4a2713aSLionel Sambuc // If the filename string was the result of macro expansions, set the include 1689*f4a2713aSLionel Sambuc // position on the file where it will be included and after the expansions. 1690*f4a2713aSLionel Sambuc if (IncludePos.isMacroID()) 1691*f4a2713aSLionel Sambuc IncludePos = SourceMgr.getExpansionRange(IncludePos).second; 1692*f4a2713aSLionel Sambuc FileID FID = SourceMgr.createFileID(File, IncludePos, FileCharacter); 1693*f4a2713aSLionel Sambuc assert(!FID.isInvalid() && "Expected valid file ID"); 1694*f4a2713aSLionel Sambuc 1695*f4a2713aSLionel Sambuc // Finally, if all is good, enter the new file! 1696*f4a2713aSLionel Sambuc EnterSourceFile(FID, CurDir, FilenameTok.getLocation()); 1697*f4a2713aSLionel Sambuc } 1698*f4a2713aSLionel Sambuc 1699*f4a2713aSLionel Sambuc /// HandleIncludeNextDirective - Implements \#include_next. 1700*f4a2713aSLionel Sambuc /// 1701*f4a2713aSLionel Sambuc void Preprocessor::HandleIncludeNextDirective(SourceLocation HashLoc, 1702*f4a2713aSLionel Sambuc Token &IncludeNextTok) { 1703*f4a2713aSLionel Sambuc Diag(IncludeNextTok, diag::ext_pp_include_next_directive); 1704*f4a2713aSLionel Sambuc 1705*f4a2713aSLionel Sambuc // #include_next is like #include, except that we start searching after 1706*f4a2713aSLionel Sambuc // the current found directory. If we can't do this, issue a 1707*f4a2713aSLionel Sambuc // diagnostic. 1708*f4a2713aSLionel Sambuc const DirectoryLookup *Lookup = CurDirLookup; 1709*f4a2713aSLionel Sambuc if (isInPrimaryFile()) { 1710*f4a2713aSLionel Sambuc Lookup = 0; 1711*f4a2713aSLionel Sambuc Diag(IncludeNextTok, diag::pp_include_next_in_primary); 1712*f4a2713aSLionel Sambuc } else if (Lookup == 0) { 1713*f4a2713aSLionel Sambuc Diag(IncludeNextTok, diag::pp_include_next_absolute_path); 1714*f4a2713aSLionel Sambuc } else { 1715*f4a2713aSLionel Sambuc // Start looking up in the next directory. 1716*f4a2713aSLionel Sambuc ++Lookup; 1717*f4a2713aSLionel Sambuc } 1718*f4a2713aSLionel Sambuc 1719*f4a2713aSLionel Sambuc return HandleIncludeDirective(HashLoc, IncludeNextTok, Lookup); 1720*f4a2713aSLionel Sambuc } 1721*f4a2713aSLionel Sambuc 1722*f4a2713aSLionel Sambuc /// HandleMicrosoftImportDirective - Implements \#import for Microsoft Mode 1723*f4a2713aSLionel Sambuc void Preprocessor::HandleMicrosoftImportDirective(Token &Tok) { 1724*f4a2713aSLionel Sambuc // The Microsoft #import directive takes a type library and generates header 1725*f4a2713aSLionel Sambuc // files from it, and includes those. This is beyond the scope of what clang 1726*f4a2713aSLionel Sambuc // does, so we ignore it and error out. However, #import can optionally have 1727*f4a2713aSLionel Sambuc // trailing attributes that span multiple lines. We're going to eat those 1728*f4a2713aSLionel Sambuc // so we can continue processing from there. 1729*f4a2713aSLionel Sambuc Diag(Tok, diag::err_pp_import_directive_ms ); 1730*f4a2713aSLionel Sambuc 1731*f4a2713aSLionel Sambuc // Read tokens until we get to the end of the directive. Note that the 1732*f4a2713aSLionel Sambuc // directive can be split over multiple lines using the backslash character. 1733*f4a2713aSLionel Sambuc DiscardUntilEndOfDirective(); 1734*f4a2713aSLionel Sambuc } 1735*f4a2713aSLionel Sambuc 1736*f4a2713aSLionel Sambuc /// HandleImportDirective - Implements \#import. 1737*f4a2713aSLionel Sambuc /// 1738*f4a2713aSLionel Sambuc void Preprocessor::HandleImportDirective(SourceLocation HashLoc, 1739*f4a2713aSLionel Sambuc Token &ImportTok) { 1740*f4a2713aSLionel Sambuc if (!LangOpts.ObjC1) { // #import is standard for ObjC. 1741*f4a2713aSLionel Sambuc if (LangOpts.MicrosoftMode) 1742*f4a2713aSLionel Sambuc return HandleMicrosoftImportDirective(ImportTok); 1743*f4a2713aSLionel Sambuc Diag(ImportTok, diag::ext_pp_import_directive); 1744*f4a2713aSLionel Sambuc } 1745*f4a2713aSLionel Sambuc return HandleIncludeDirective(HashLoc, ImportTok, 0, true); 1746*f4a2713aSLionel Sambuc } 1747*f4a2713aSLionel Sambuc 1748*f4a2713aSLionel Sambuc /// HandleIncludeMacrosDirective - The -imacros command line option turns into a 1749*f4a2713aSLionel Sambuc /// pseudo directive in the predefines buffer. This handles it by sucking all 1750*f4a2713aSLionel Sambuc /// tokens through the preprocessor and discarding them (only keeping the side 1751*f4a2713aSLionel Sambuc /// effects on the preprocessor). 1752*f4a2713aSLionel Sambuc void Preprocessor::HandleIncludeMacrosDirective(SourceLocation HashLoc, 1753*f4a2713aSLionel Sambuc Token &IncludeMacrosTok) { 1754*f4a2713aSLionel Sambuc // This directive should only occur in the predefines buffer. If not, emit an 1755*f4a2713aSLionel Sambuc // error and reject it. 1756*f4a2713aSLionel Sambuc SourceLocation Loc = IncludeMacrosTok.getLocation(); 1757*f4a2713aSLionel Sambuc if (strcmp(SourceMgr.getBufferName(Loc), "<built-in>") != 0) { 1758*f4a2713aSLionel Sambuc Diag(IncludeMacrosTok.getLocation(), 1759*f4a2713aSLionel Sambuc diag::pp_include_macros_out_of_predefines); 1760*f4a2713aSLionel Sambuc DiscardUntilEndOfDirective(); 1761*f4a2713aSLionel Sambuc return; 1762*f4a2713aSLionel Sambuc } 1763*f4a2713aSLionel Sambuc 1764*f4a2713aSLionel Sambuc // Treat this as a normal #include for checking purposes. If this is 1765*f4a2713aSLionel Sambuc // successful, it will push a new lexer onto the include stack. 1766*f4a2713aSLionel Sambuc HandleIncludeDirective(HashLoc, IncludeMacrosTok, 0, false); 1767*f4a2713aSLionel Sambuc 1768*f4a2713aSLionel Sambuc Token TmpTok; 1769*f4a2713aSLionel Sambuc do { 1770*f4a2713aSLionel Sambuc Lex(TmpTok); 1771*f4a2713aSLionel Sambuc assert(TmpTok.isNot(tok::eof) && "Didn't find end of -imacros!"); 1772*f4a2713aSLionel Sambuc } while (TmpTok.isNot(tok::hashhash)); 1773*f4a2713aSLionel Sambuc } 1774*f4a2713aSLionel Sambuc 1775*f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===// 1776*f4a2713aSLionel Sambuc // Preprocessor Macro Directive Handling. 1777*f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===// 1778*f4a2713aSLionel Sambuc 1779*f4a2713aSLionel Sambuc /// ReadMacroDefinitionArgList - The ( starting an argument list of a macro 1780*f4a2713aSLionel Sambuc /// definition has just been read. Lex the rest of the arguments and the 1781*f4a2713aSLionel Sambuc /// closing ), updating MI with what we learn. Return true if an error occurs 1782*f4a2713aSLionel Sambuc /// parsing the arg list. 1783*f4a2713aSLionel Sambuc bool Preprocessor::ReadMacroDefinitionArgList(MacroInfo *MI, Token &Tok) { 1784*f4a2713aSLionel Sambuc SmallVector<IdentifierInfo*, 32> Arguments; 1785*f4a2713aSLionel Sambuc 1786*f4a2713aSLionel Sambuc while (1) { 1787*f4a2713aSLionel Sambuc LexUnexpandedToken(Tok); 1788*f4a2713aSLionel Sambuc switch (Tok.getKind()) { 1789*f4a2713aSLionel Sambuc case tok::r_paren: 1790*f4a2713aSLionel Sambuc // Found the end of the argument list. 1791*f4a2713aSLionel Sambuc if (Arguments.empty()) // #define FOO() 1792*f4a2713aSLionel Sambuc return false; 1793*f4a2713aSLionel Sambuc // Otherwise we have #define FOO(A,) 1794*f4a2713aSLionel Sambuc Diag(Tok, diag::err_pp_expected_ident_in_arg_list); 1795*f4a2713aSLionel Sambuc return true; 1796*f4a2713aSLionel Sambuc case tok::ellipsis: // #define X(... -> C99 varargs 1797*f4a2713aSLionel Sambuc if (!LangOpts.C99) 1798*f4a2713aSLionel Sambuc Diag(Tok, LangOpts.CPlusPlus11 ? 1799*f4a2713aSLionel Sambuc diag::warn_cxx98_compat_variadic_macro : 1800*f4a2713aSLionel Sambuc diag::ext_variadic_macro); 1801*f4a2713aSLionel Sambuc 1802*f4a2713aSLionel Sambuc // OpenCL v1.2 s6.9.e: variadic macros are not supported. 1803*f4a2713aSLionel Sambuc if (LangOpts.OpenCL) { 1804*f4a2713aSLionel Sambuc Diag(Tok, diag::err_pp_opencl_variadic_macros); 1805*f4a2713aSLionel Sambuc return true; 1806*f4a2713aSLionel Sambuc } 1807*f4a2713aSLionel Sambuc 1808*f4a2713aSLionel Sambuc // Lex the token after the identifier. 1809*f4a2713aSLionel Sambuc LexUnexpandedToken(Tok); 1810*f4a2713aSLionel Sambuc if (Tok.isNot(tok::r_paren)) { 1811*f4a2713aSLionel Sambuc Diag(Tok, diag::err_pp_missing_rparen_in_macro_def); 1812*f4a2713aSLionel Sambuc return true; 1813*f4a2713aSLionel Sambuc } 1814*f4a2713aSLionel Sambuc // Add the __VA_ARGS__ identifier as an argument. 1815*f4a2713aSLionel Sambuc Arguments.push_back(Ident__VA_ARGS__); 1816*f4a2713aSLionel Sambuc MI->setIsC99Varargs(); 1817*f4a2713aSLionel Sambuc MI->setArgumentList(&Arguments[0], Arguments.size(), BP); 1818*f4a2713aSLionel Sambuc return false; 1819*f4a2713aSLionel Sambuc case tok::eod: // #define X( 1820*f4a2713aSLionel Sambuc Diag(Tok, diag::err_pp_missing_rparen_in_macro_def); 1821*f4a2713aSLionel Sambuc return true; 1822*f4a2713aSLionel Sambuc default: 1823*f4a2713aSLionel Sambuc // Handle keywords and identifiers here to accept things like 1824*f4a2713aSLionel Sambuc // #define Foo(for) for. 1825*f4a2713aSLionel Sambuc IdentifierInfo *II = Tok.getIdentifierInfo(); 1826*f4a2713aSLionel Sambuc if (II == 0) { 1827*f4a2713aSLionel Sambuc // #define X(1 1828*f4a2713aSLionel Sambuc Diag(Tok, diag::err_pp_invalid_tok_in_arg_list); 1829*f4a2713aSLionel Sambuc return true; 1830*f4a2713aSLionel Sambuc } 1831*f4a2713aSLionel Sambuc 1832*f4a2713aSLionel Sambuc // If this is already used as an argument, it is used multiple times (e.g. 1833*f4a2713aSLionel Sambuc // #define X(A,A. 1834*f4a2713aSLionel Sambuc if (std::find(Arguments.begin(), Arguments.end(), II) != 1835*f4a2713aSLionel Sambuc Arguments.end()) { // C99 6.10.3p6 1836*f4a2713aSLionel Sambuc Diag(Tok, diag::err_pp_duplicate_name_in_arg_list) << II; 1837*f4a2713aSLionel Sambuc return true; 1838*f4a2713aSLionel Sambuc } 1839*f4a2713aSLionel Sambuc 1840*f4a2713aSLionel Sambuc // Add the argument to the macro info. 1841*f4a2713aSLionel Sambuc Arguments.push_back(II); 1842*f4a2713aSLionel Sambuc 1843*f4a2713aSLionel Sambuc // Lex the token after the identifier. 1844*f4a2713aSLionel Sambuc LexUnexpandedToken(Tok); 1845*f4a2713aSLionel Sambuc 1846*f4a2713aSLionel Sambuc switch (Tok.getKind()) { 1847*f4a2713aSLionel Sambuc default: // #define X(A B 1848*f4a2713aSLionel Sambuc Diag(Tok, diag::err_pp_expected_comma_in_arg_list); 1849*f4a2713aSLionel Sambuc return true; 1850*f4a2713aSLionel Sambuc case tok::r_paren: // #define X(A) 1851*f4a2713aSLionel Sambuc MI->setArgumentList(&Arguments[0], Arguments.size(), BP); 1852*f4a2713aSLionel Sambuc return false; 1853*f4a2713aSLionel Sambuc case tok::comma: // #define X(A, 1854*f4a2713aSLionel Sambuc break; 1855*f4a2713aSLionel Sambuc case tok::ellipsis: // #define X(A... -> GCC extension 1856*f4a2713aSLionel Sambuc // Diagnose extension. 1857*f4a2713aSLionel Sambuc Diag(Tok, diag::ext_named_variadic_macro); 1858*f4a2713aSLionel Sambuc 1859*f4a2713aSLionel Sambuc // Lex the token after the identifier. 1860*f4a2713aSLionel Sambuc LexUnexpandedToken(Tok); 1861*f4a2713aSLionel Sambuc if (Tok.isNot(tok::r_paren)) { 1862*f4a2713aSLionel Sambuc Diag(Tok, diag::err_pp_missing_rparen_in_macro_def); 1863*f4a2713aSLionel Sambuc return true; 1864*f4a2713aSLionel Sambuc } 1865*f4a2713aSLionel Sambuc 1866*f4a2713aSLionel Sambuc MI->setIsGNUVarargs(); 1867*f4a2713aSLionel Sambuc MI->setArgumentList(&Arguments[0], Arguments.size(), BP); 1868*f4a2713aSLionel Sambuc return false; 1869*f4a2713aSLionel Sambuc } 1870*f4a2713aSLionel Sambuc } 1871*f4a2713aSLionel Sambuc } 1872*f4a2713aSLionel Sambuc } 1873*f4a2713aSLionel Sambuc 1874*f4a2713aSLionel Sambuc /// HandleDefineDirective - Implements \#define. This consumes the entire macro 1875*f4a2713aSLionel Sambuc /// line then lets the caller lex the next real token. 1876*f4a2713aSLionel Sambuc void Preprocessor::HandleDefineDirective(Token &DefineTok, 1877*f4a2713aSLionel Sambuc bool ImmediatelyAfterHeaderGuard) { 1878*f4a2713aSLionel Sambuc ++NumDefined; 1879*f4a2713aSLionel Sambuc 1880*f4a2713aSLionel Sambuc Token MacroNameTok; 1881*f4a2713aSLionel Sambuc ReadMacroName(MacroNameTok, 1); 1882*f4a2713aSLionel Sambuc 1883*f4a2713aSLionel Sambuc // Error reading macro name? If so, diagnostic already issued. 1884*f4a2713aSLionel Sambuc if (MacroNameTok.is(tok::eod)) 1885*f4a2713aSLionel Sambuc return; 1886*f4a2713aSLionel Sambuc 1887*f4a2713aSLionel Sambuc Token LastTok = MacroNameTok; 1888*f4a2713aSLionel Sambuc 1889*f4a2713aSLionel Sambuc // If we are supposed to keep comments in #defines, reenable comment saving 1890*f4a2713aSLionel Sambuc // mode. 1891*f4a2713aSLionel Sambuc if (CurLexer) CurLexer->SetCommentRetentionState(KeepMacroComments); 1892*f4a2713aSLionel Sambuc 1893*f4a2713aSLionel Sambuc // Create the new macro. 1894*f4a2713aSLionel Sambuc MacroInfo *MI = AllocateMacroInfo(MacroNameTok.getLocation()); 1895*f4a2713aSLionel Sambuc 1896*f4a2713aSLionel Sambuc Token Tok; 1897*f4a2713aSLionel Sambuc LexUnexpandedToken(Tok); 1898*f4a2713aSLionel Sambuc 1899*f4a2713aSLionel Sambuc // If this is a function-like macro definition, parse the argument list, 1900*f4a2713aSLionel Sambuc // marking each of the identifiers as being used as macro arguments. Also, 1901*f4a2713aSLionel Sambuc // check other constraints on the first token of the macro body. 1902*f4a2713aSLionel Sambuc if (Tok.is(tok::eod)) { 1903*f4a2713aSLionel Sambuc if (ImmediatelyAfterHeaderGuard) { 1904*f4a2713aSLionel Sambuc // Save this macro information since it may part of a header guard. 1905*f4a2713aSLionel Sambuc CurPPLexer->MIOpt.SetDefinedMacro(MacroNameTok.getIdentifierInfo(), 1906*f4a2713aSLionel Sambuc MacroNameTok.getLocation()); 1907*f4a2713aSLionel Sambuc } 1908*f4a2713aSLionel Sambuc // If there is no body to this macro, we have no special handling here. 1909*f4a2713aSLionel Sambuc } else if (Tok.hasLeadingSpace()) { 1910*f4a2713aSLionel Sambuc // This is a normal token with leading space. Clear the leading space 1911*f4a2713aSLionel Sambuc // marker on the first token to get proper expansion. 1912*f4a2713aSLionel Sambuc Tok.clearFlag(Token::LeadingSpace); 1913*f4a2713aSLionel Sambuc } else if (Tok.is(tok::l_paren)) { 1914*f4a2713aSLionel Sambuc // This is a function-like macro definition. Read the argument list. 1915*f4a2713aSLionel Sambuc MI->setIsFunctionLike(); 1916*f4a2713aSLionel Sambuc if (ReadMacroDefinitionArgList(MI, LastTok)) { 1917*f4a2713aSLionel Sambuc // Forget about MI. 1918*f4a2713aSLionel Sambuc ReleaseMacroInfo(MI); 1919*f4a2713aSLionel Sambuc // Throw away the rest of the line. 1920*f4a2713aSLionel Sambuc if (CurPPLexer->ParsingPreprocessorDirective) 1921*f4a2713aSLionel Sambuc DiscardUntilEndOfDirective(); 1922*f4a2713aSLionel Sambuc return; 1923*f4a2713aSLionel Sambuc } 1924*f4a2713aSLionel Sambuc 1925*f4a2713aSLionel Sambuc // If this is a definition of a variadic C99 function-like macro, not using 1926*f4a2713aSLionel Sambuc // the GNU named varargs extension, enabled __VA_ARGS__. 1927*f4a2713aSLionel Sambuc 1928*f4a2713aSLionel Sambuc // "Poison" __VA_ARGS__, which can only appear in the expansion of a macro. 1929*f4a2713aSLionel Sambuc // This gets unpoisoned where it is allowed. 1930*f4a2713aSLionel Sambuc assert(Ident__VA_ARGS__->isPoisoned() && "__VA_ARGS__ should be poisoned!"); 1931*f4a2713aSLionel Sambuc if (MI->isC99Varargs()) 1932*f4a2713aSLionel Sambuc Ident__VA_ARGS__->setIsPoisoned(false); 1933*f4a2713aSLionel Sambuc 1934*f4a2713aSLionel Sambuc // Read the first token after the arg list for down below. 1935*f4a2713aSLionel Sambuc LexUnexpandedToken(Tok); 1936*f4a2713aSLionel Sambuc } else if (LangOpts.C99 || LangOpts.CPlusPlus11) { 1937*f4a2713aSLionel Sambuc // C99 requires whitespace between the macro definition and the body. Emit 1938*f4a2713aSLionel Sambuc // a diagnostic for something like "#define X+". 1939*f4a2713aSLionel Sambuc Diag(Tok, diag::ext_c99_whitespace_required_after_macro_name); 1940*f4a2713aSLionel Sambuc } else { 1941*f4a2713aSLionel Sambuc // C90 6.8 TC1 says: "In the definition of an object-like macro, if the 1942*f4a2713aSLionel Sambuc // first character of a replacement list is not a character required by 1943*f4a2713aSLionel Sambuc // subclause 5.2.1, then there shall be white-space separation between the 1944*f4a2713aSLionel Sambuc // identifier and the replacement list.". 5.2.1 lists this set: 1945*f4a2713aSLionel Sambuc // "A-Za-z0-9!"#%&'()*+,_./:;<=>?[\]^_{|}~" as well as whitespace, which 1946*f4a2713aSLionel Sambuc // is irrelevant here. 1947*f4a2713aSLionel Sambuc bool isInvalid = false; 1948*f4a2713aSLionel Sambuc if (Tok.is(tok::at)) // @ is not in the list above. 1949*f4a2713aSLionel Sambuc isInvalid = true; 1950*f4a2713aSLionel Sambuc else if (Tok.is(tok::unknown)) { 1951*f4a2713aSLionel Sambuc // If we have an unknown token, it is something strange like "`". Since 1952*f4a2713aSLionel Sambuc // all of valid characters would have lexed into a single character 1953*f4a2713aSLionel Sambuc // token of some sort, we know this is not a valid case. 1954*f4a2713aSLionel Sambuc isInvalid = true; 1955*f4a2713aSLionel Sambuc } 1956*f4a2713aSLionel Sambuc if (isInvalid) 1957*f4a2713aSLionel Sambuc Diag(Tok, diag::ext_missing_whitespace_after_macro_name); 1958*f4a2713aSLionel Sambuc else 1959*f4a2713aSLionel Sambuc Diag(Tok, diag::warn_missing_whitespace_after_macro_name); 1960*f4a2713aSLionel Sambuc } 1961*f4a2713aSLionel Sambuc 1962*f4a2713aSLionel Sambuc if (!Tok.is(tok::eod)) 1963*f4a2713aSLionel Sambuc LastTok = Tok; 1964*f4a2713aSLionel Sambuc 1965*f4a2713aSLionel Sambuc // Read the rest of the macro body. 1966*f4a2713aSLionel Sambuc if (MI->isObjectLike()) { 1967*f4a2713aSLionel Sambuc // Object-like macros are very simple, just read their body. 1968*f4a2713aSLionel Sambuc while (Tok.isNot(tok::eod)) { 1969*f4a2713aSLionel Sambuc LastTok = Tok; 1970*f4a2713aSLionel Sambuc MI->AddTokenToBody(Tok); 1971*f4a2713aSLionel Sambuc // Get the next token of the macro. 1972*f4a2713aSLionel Sambuc LexUnexpandedToken(Tok); 1973*f4a2713aSLionel Sambuc } 1974*f4a2713aSLionel Sambuc 1975*f4a2713aSLionel Sambuc } else { 1976*f4a2713aSLionel Sambuc // Otherwise, read the body of a function-like macro. While we are at it, 1977*f4a2713aSLionel Sambuc // check C99 6.10.3.2p1: ensure that # operators are followed by macro 1978*f4a2713aSLionel Sambuc // parameters in function-like macro expansions. 1979*f4a2713aSLionel Sambuc while (Tok.isNot(tok::eod)) { 1980*f4a2713aSLionel Sambuc LastTok = Tok; 1981*f4a2713aSLionel Sambuc 1982*f4a2713aSLionel Sambuc if (Tok.isNot(tok::hash) && Tok.isNot(tok::hashhash)) { 1983*f4a2713aSLionel Sambuc MI->AddTokenToBody(Tok); 1984*f4a2713aSLionel Sambuc 1985*f4a2713aSLionel Sambuc // Get the next token of the macro. 1986*f4a2713aSLionel Sambuc LexUnexpandedToken(Tok); 1987*f4a2713aSLionel Sambuc continue; 1988*f4a2713aSLionel Sambuc } 1989*f4a2713aSLionel Sambuc 1990*f4a2713aSLionel Sambuc // If we're in -traditional mode, then we should ignore stringification 1991*f4a2713aSLionel Sambuc // and token pasting. Mark the tokens as unknown so as not to confuse 1992*f4a2713aSLionel Sambuc // things. 1993*f4a2713aSLionel Sambuc if (getLangOpts().TraditionalCPP) { 1994*f4a2713aSLionel Sambuc Tok.setKind(tok::unknown); 1995*f4a2713aSLionel Sambuc MI->AddTokenToBody(Tok); 1996*f4a2713aSLionel Sambuc 1997*f4a2713aSLionel Sambuc // Get the next token of the macro. 1998*f4a2713aSLionel Sambuc LexUnexpandedToken(Tok); 1999*f4a2713aSLionel Sambuc continue; 2000*f4a2713aSLionel Sambuc } 2001*f4a2713aSLionel Sambuc 2002*f4a2713aSLionel Sambuc if (Tok.is(tok::hashhash)) { 2003*f4a2713aSLionel Sambuc 2004*f4a2713aSLionel Sambuc // If we see token pasting, check if it looks like the gcc comma 2005*f4a2713aSLionel Sambuc // pasting extension. We'll use this information to suppress 2006*f4a2713aSLionel Sambuc // diagnostics later on. 2007*f4a2713aSLionel Sambuc 2008*f4a2713aSLionel Sambuc // Get the next token of the macro. 2009*f4a2713aSLionel Sambuc LexUnexpandedToken(Tok); 2010*f4a2713aSLionel Sambuc 2011*f4a2713aSLionel Sambuc if (Tok.is(tok::eod)) { 2012*f4a2713aSLionel Sambuc MI->AddTokenToBody(LastTok); 2013*f4a2713aSLionel Sambuc break; 2014*f4a2713aSLionel Sambuc } 2015*f4a2713aSLionel Sambuc 2016*f4a2713aSLionel Sambuc unsigned NumTokens = MI->getNumTokens(); 2017*f4a2713aSLionel Sambuc if (NumTokens && Tok.getIdentifierInfo() == Ident__VA_ARGS__ && 2018*f4a2713aSLionel Sambuc MI->getReplacementToken(NumTokens-1).is(tok::comma)) 2019*f4a2713aSLionel Sambuc MI->setHasCommaPasting(); 2020*f4a2713aSLionel Sambuc 2021*f4a2713aSLionel Sambuc // Things look ok, add the '##' token to the macro. 2022*f4a2713aSLionel Sambuc MI->AddTokenToBody(LastTok); 2023*f4a2713aSLionel Sambuc continue; 2024*f4a2713aSLionel Sambuc } 2025*f4a2713aSLionel Sambuc 2026*f4a2713aSLionel Sambuc // Get the next token of the macro. 2027*f4a2713aSLionel Sambuc LexUnexpandedToken(Tok); 2028*f4a2713aSLionel Sambuc 2029*f4a2713aSLionel Sambuc // Check for a valid macro arg identifier. 2030*f4a2713aSLionel Sambuc if (Tok.getIdentifierInfo() == 0 || 2031*f4a2713aSLionel Sambuc MI->getArgumentNum(Tok.getIdentifierInfo()) == -1) { 2032*f4a2713aSLionel Sambuc 2033*f4a2713aSLionel Sambuc // If this is assembler-with-cpp mode, we accept random gibberish after 2034*f4a2713aSLionel Sambuc // the '#' because '#' is often a comment character. However, change 2035*f4a2713aSLionel Sambuc // the kind of the token to tok::unknown so that the preprocessor isn't 2036*f4a2713aSLionel Sambuc // confused. 2037*f4a2713aSLionel Sambuc if (getLangOpts().AsmPreprocessor && Tok.isNot(tok::eod)) { 2038*f4a2713aSLionel Sambuc LastTok.setKind(tok::unknown); 2039*f4a2713aSLionel Sambuc MI->AddTokenToBody(LastTok); 2040*f4a2713aSLionel Sambuc continue; 2041*f4a2713aSLionel Sambuc } else { 2042*f4a2713aSLionel Sambuc Diag(Tok, diag::err_pp_stringize_not_parameter); 2043*f4a2713aSLionel Sambuc ReleaseMacroInfo(MI); 2044*f4a2713aSLionel Sambuc 2045*f4a2713aSLionel Sambuc // Disable __VA_ARGS__ again. 2046*f4a2713aSLionel Sambuc Ident__VA_ARGS__->setIsPoisoned(true); 2047*f4a2713aSLionel Sambuc return; 2048*f4a2713aSLionel Sambuc } 2049*f4a2713aSLionel Sambuc } 2050*f4a2713aSLionel Sambuc 2051*f4a2713aSLionel Sambuc // Things look ok, add the '#' and param name tokens to the macro. 2052*f4a2713aSLionel Sambuc MI->AddTokenToBody(LastTok); 2053*f4a2713aSLionel Sambuc MI->AddTokenToBody(Tok); 2054*f4a2713aSLionel Sambuc LastTok = Tok; 2055*f4a2713aSLionel Sambuc 2056*f4a2713aSLionel Sambuc // Get the next token of the macro. 2057*f4a2713aSLionel Sambuc LexUnexpandedToken(Tok); 2058*f4a2713aSLionel Sambuc } 2059*f4a2713aSLionel Sambuc } 2060*f4a2713aSLionel Sambuc 2061*f4a2713aSLionel Sambuc 2062*f4a2713aSLionel Sambuc // Disable __VA_ARGS__ again. 2063*f4a2713aSLionel Sambuc Ident__VA_ARGS__->setIsPoisoned(true); 2064*f4a2713aSLionel Sambuc 2065*f4a2713aSLionel Sambuc // Check that there is no paste (##) operator at the beginning or end of the 2066*f4a2713aSLionel Sambuc // replacement list. 2067*f4a2713aSLionel Sambuc unsigned NumTokens = MI->getNumTokens(); 2068*f4a2713aSLionel Sambuc if (NumTokens != 0) { 2069*f4a2713aSLionel Sambuc if (MI->getReplacementToken(0).is(tok::hashhash)) { 2070*f4a2713aSLionel Sambuc Diag(MI->getReplacementToken(0), diag::err_paste_at_start); 2071*f4a2713aSLionel Sambuc ReleaseMacroInfo(MI); 2072*f4a2713aSLionel Sambuc return; 2073*f4a2713aSLionel Sambuc } 2074*f4a2713aSLionel Sambuc if (MI->getReplacementToken(NumTokens-1).is(tok::hashhash)) { 2075*f4a2713aSLionel Sambuc Diag(MI->getReplacementToken(NumTokens-1), diag::err_paste_at_end); 2076*f4a2713aSLionel Sambuc ReleaseMacroInfo(MI); 2077*f4a2713aSLionel Sambuc return; 2078*f4a2713aSLionel Sambuc } 2079*f4a2713aSLionel Sambuc } 2080*f4a2713aSLionel Sambuc 2081*f4a2713aSLionel Sambuc MI->setDefinitionEndLoc(LastTok.getLocation()); 2082*f4a2713aSLionel Sambuc 2083*f4a2713aSLionel Sambuc // Finally, if this identifier already had a macro defined for it, verify that 2084*f4a2713aSLionel Sambuc // the macro bodies are identical, and issue diagnostics if they are not. 2085*f4a2713aSLionel Sambuc if (const MacroInfo *OtherMI=getMacroInfo(MacroNameTok.getIdentifierInfo())) { 2086*f4a2713aSLionel Sambuc // It is very common for system headers to have tons of macro redefinitions 2087*f4a2713aSLionel Sambuc // and for warnings to be disabled in system headers. If this is the case, 2088*f4a2713aSLionel Sambuc // then don't bother calling MacroInfo::isIdenticalTo. 2089*f4a2713aSLionel Sambuc if (!getDiagnostics().getSuppressSystemWarnings() || 2090*f4a2713aSLionel Sambuc !SourceMgr.isInSystemHeader(DefineTok.getLocation())) { 2091*f4a2713aSLionel Sambuc if (!OtherMI->isUsed() && OtherMI->isWarnIfUnused()) 2092*f4a2713aSLionel Sambuc Diag(OtherMI->getDefinitionLoc(), diag::pp_macro_not_used); 2093*f4a2713aSLionel Sambuc 2094*f4a2713aSLionel Sambuc // Warn if defining "__LINE__" and other builtins, per C99 6.10.8/4 and 2095*f4a2713aSLionel Sambuc // C++ [cpp.predefined]p4, but allow it as an extension. 2096*f4a2713aSLionel Sambuc if (OtherMI->isBuiltinMacro()) 2097*f4a2713aSLionel Sambuc Diag(MacroNameTok, diag::ext_pp_redef_builtin_macro); 2098*f4a2713aSLionel Sambuc // Macros must be identical. This means all tokens and whitespace 2099*f4a2713aSLionel Sambuc // separation must be the same. C99 6.10.3p2. 2100*f4a2713aSLionel Sambuc else if (!OtherMI->isAllowRedefinitionsWithoutWarning() && 2101*f4a2713aSLionel Sambuc !MI->isIdenticalTo(*OtherMI, *this, /*Syntactic=*/LangOpts.MicrosoftExt)) { 2102*f4a2713aSLionel Sambuc Diag(MI->getDefinitionLoc(), diag::ext_pp_macro_redef) 2103*f4a2713aSLionel Sambuc << MacroNameTok.getIdentifierInfo(); 2104*f4a2713aSLionel Sambuc Diag(OtherMI->getDefinitionLoc(), diag::note_previous_definition); 2105*f4a2713aSLionel Sambuc } 2106*f4a2713aSLionel Sambuc } 2107*f4a2713aSLionel Sambuc if (OtherMI->isWarnIfUnused()) 2108*f4a2713aSLionel Sambuc WarnUnusedMacroLocs.erase(OtherMI->getDefinitionLoc()); 2109*f4a2713aSLionel Sambuc } 2110*f4a2713aSLionel Sambuc 2111*f4a2713aSLionel Sambuc DefMacroDirective *MD = 2112*f4a2713aSLionel Sambuc appendDefMacroDirective(MacroNameTok.getIdentifierInfo(), MI); 2113*f4a2713aSLionel Sambuc 2114*f4a2713aSLionel Sambuc assert(!MI->isUsed()); 2115*f4a2713aSLionel Sambuc // If we need warning for not using the macro, add its location in the 2116*f4a2713aSLionel Sambuc // warn-because-unused-macro set. If it gets used it will be removed from set. 2117*f4a2713aSLionel Sambuc if (getSourceManager().isInMainFile(MI->getDefinitionLoc()) && 2118*f4a2713aSLionel Sambuc Diags->getDiagnosticLevel(diag::pp_macro_not_used, 2119*f4a2713aSLionel Sambuc MI->getDefinitionLoc()) != DiagnosticsEngine::Ignored) { 2120*f4a2713aSLionel Sambuc MI->setIsWarnIfUnused(true); 2121*f4a2713aSLionel Sambuc WarnUnusedMacroLocs.insert(MI->getDefinitionLoc()); 2122*f4a2713aSLionel Sambuc } 2123*f4a2713aSLionel Sambuc 2124*f4a2713aSLionel Sambuc // If the callbacks want to know, tell them about the macro definition. 2125*f4a2713aSLionel Sambuc if (Callbacks) 2126*f4a2713aSLionel Sambuc Callbacks->MacroDefined(MacroNameTok, MD); 2127*f4a2713aSLionel Sambuc } 2128*f4a2713aSLionel Sambuc 2129*f4a2713aSLionel Sambuc /// HandleUndefDirective - Implements \#undef. 2130*f4a2713aSLionel Sambuc /// 2131*f4a2713aSLionel Sambuc void Preprocessor::HandleUndefDirective(Token &UndefTok) { 2132*f4a2713aSLionel Sambuc ++NumUndefined; 2133*f4a2713aSLionel Sambuc 2134*f4a2713aSLionel Sambuc Token MacroNameTok; 2135*f4a2713aSLionel Sambuc ReadMacroName(MacroNameTok, 2); 2136*f4a2713aSLionel Sambuc 2137*f4a2713aSLionel Sambuc // Error reading macro name? If so, diagnostic already issued. 2138*f4a2713aSLionel Sambuc if (MacroNameTok.is(tok::eod)) 2139*f4a2713aSLionel Sambuc return; 2140*f4a2713aSLionel Sambuc 2141*f4a2713aSLionel Sambuc // Check to see if this is the last token on the #undef line. 2142*f4a2713aSLionel Sambuc CheckEndOfDirective("undef"); 2143*f4a2713aSLionel Sambuc 2144*f4a2713aSLionel Sambuc // Okay, we finally have a valid identifier to undef. 2145*f4a2713aSLionel Sambuc MacroDirective *MD = getMacroDirective(MacroNameTok.getIdentifierInfo()); 2146*f4a2713aSLionel Sambuc const MacroInfo *MI = MD ? MD->getMacroInfo() : 0; 2147*f4a2713aSLionel Sambuc 2148*f4a2713aSLionel Sambuc // If the callbacks want to know, tell them about the macro #undef. 2149*f4a2713aSLionel Sambuc // Note: no matter if the macro was defined or not. 2150*f4a2713aSLionel Sambuc if (Callbacks) 2151*f4a2713aSLionel Sambuc Callbacks->MacroUndefined(MacroNameTok, MD); 2152*f4a2713aSLionel Sambuc 2153*f4a2713aSLionel Sambuc // If the macro is not defined, this is a noop undef, just return. 2154*f4a2713aSLionel Sambuc if (MI == 0) return; 2155*f4a2713aSLionel Sambuc 2156*f4a2713aSLionel Sambuc if (!MI->isUsed() && MI->isWarnIfUnused()) 2157*f4a2713aSLionel Sambuc Diag(MI->getDefinitionLoc(), diag::pp_macro_not_used); 2158*f4a2713aSLionel Sambuc 2159*f4a2713aSLionel Sambuc if (MI->isWarnIfUnused()) 2160*f4a2713aSLionel Sambuc WarnUnusedMacroLocs.erase(MI->getDefinitionLoc()); 2161*f4a2713aSLionel Sambuc 2162*f4a2713aSLionel Sambuc appendMacroDirective(MacroNameTok.getIdentifierInfo(), 2163*f4a2713aSLionel Sambuc AllocateUndefMacroDirective(MacroNameTok.getLocation())); 2164*f4a2713aSLionel Sambuc } 2165*f4a2713aSLionel Sambuc 2166*f4a2713aSLionel Sambuc 2167*f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===// 2168*f4a2713aSLionel Sambuc // Preprocessor Conditional Directive Handling. 2169*f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===// 2170*f4a2713aSLionel Sambuc 2171*f4a2713aSLionel Sambuc /// HandleIfdefDirective - Implements the \#ifdef/\#ifndef directive. isIfndef 2172*f4a2713aSLionel Sambuc /// is true when this is a \#ifndef directive. ReadAnyTokensBeforeDirective is 2173*f4a2713aSLionel Sambuc /// true if any tokens have been returned or pp-directives activated before this 2174*f4a2713aSLionel Sambuc /// \#ifndef has been lexed. 2175*f4a2713aSLionel Sambuc /// 2176*f4a2713aSLionel Sambuc void Preprocessor::HandleIfdefDirective(Token &Result, bool isIfndef, 2177*f4a2713aSLionel Sambuc bool ReadAnyTokensBeforeDirective) { 2178*f4a2713aSLionel Sambuc ++NumIf; 2179*f4a2713aSLionel Sambuc Token DirectiveTok = Result; 2180*f4a2713aSLionel Sambuc 2181*f4a2713aSLionel Sambuc Token MacroNameTok; 2182*f4a2713aSLionel Sambuc ReadMacroName(MacroNameTok); 2183*f4a2713aSLionel Sambuc 2184*f4a2713aSLionel Sambuc // Error reading macro name? If so, diagnostic already issued. 2185*f4a2713aSLionel Sambuc if (MacroNameTok.is(tok::eod)) { 2186*f4a2713aSLionel Sambuc // Skip code until we get to #endif. This helps with recovery by not 2187*f4a2713aSLionel Sambuc // emitting an error when the #endif is reached. 2188*f4a2713aSLionel Sambuc SkipExcludedConditionalBlock(DirectiveTok.getLocation(), 2189*f4a2713aSLionel Sambuc /*Foundnonskip*/false, /*FoundElse*/false); 2190*f4a2713aSLionel Sambuc return; 2191*f4a2713aSLionel Sambuc } 2192*f4a2713aSLionel Sambuc 2193*f4a2713aSLionel Sambuc // Check to see if this is the last token on the #if[n]def line. 2194*f4a2713aSLionel Sambuc CheckEndOfDirective(isIfndef ? "ifndef" : "ifdef"); 2195*f4a2713aSLionel Sambuc 2196*f4a2713aSLionel Sambuc IdentifierInfo *MII = MacroNameTok.getIdentifierInfo(); 2197*f4a2713aSLionel Sambuc MacroDirective *MD = getMacroDirective(MII); 2198*f4a2713aSLionel Sambuc MacroInfo *MI = MD ? MD->getMacroInfo() : 0; 2199*f4a2713aSLionel Sambuc 2200*f4a2713aSLionel Sambuc if (CurPPLexer->getConditionalStackDepth() == 0) { 2201*f4a2713aSLionel Sambuc // If the start of a top-level #ifdef and if the macro is not defined, 2202*f4a2713aSLionel Sambuc // inform MIOpt that this might be the start of a proper include guard. 2203*f4a2713aSLionel Sambuc // Otherwise it is some other form of unknown conditional which we can't 2204*f4a2713aSLionel Sambuc // handle. 2205*f4a2713aSLionel Sambuc if (!ReadAnyTokensBeforeDirective && MI == 0) { 2206*f4a2713aSLionel Sambuc assert(isIfndef && "#ifdef shouldn't reach here"); 2207*f4a2713aSLionel Sambuc CurPPLexer->MIOpt.EnterTopLevelIfndef(MII, MacroNameTok.getLocation()); 2208*f4a2713aSLionel Sambuc } else 2209*f4a2713aSLionel Sambuc CurPPLexer->MIOpt.EnterTopLevelConditional(); 2210*f4a2713aSLionel Sambuc } 2211*f4a2713aSLionel Sambuc 2212*f4a2713aSLionel Sambuc // If there is a macro, process it. 2213*f4a2713aSLionel Sambuc if (MI) // Mark it used. 2214*f4a2713aSLionel Sambuc markMacroAsUsed(MI); 2215*f4a2713aSLionel Sambuc 2216*f4a2713aSLionel Sambuc if (Callbacks) { 2217*f4a2713aSLionel Sambuc if (isIfndef) 2218*f4a2713aSLionel Sambuc Callbacks->Ifndef(DirectiveTok.getLocation(), MacroNameTok, MD); 2219*f4a2713aSLionel Sambuc else 2220*f4a2713aSLionel Sambuc Callbacks->Ifdef(DirectiveTok.getLocation(), MacroNameTok, MD); 2221*f4a2713aSLionel Sambuc } 2222*f4a2713aSLionel Sambuc 2223*f4a2713aSLionel Sambuc // Should we include the stuff contained by this directive? 2224*f4a2713aSLionel Sambuc if (!MI == isIfndef) { 2225*f4a2713aSLionel Sambuc // Yes, remember that we are inside a conditional, then lex the next token. 2226*f4a2713aSLionel Sambuc CurPPLexer->pushConditionalLevel(DirectiveTok.getLocation(), 2227*f4a2713aSLionel Sambuc /*wasskip*/false, /*foundnonskip*/true, 2228*f4a2713aSLionel Sambuc /*foundelse*/false); 2229*f4a2713aSLionel Sambuc } else { 2230*f4a2713aSLionel Sambuc // No, skip the contents of this block. 2231*f4a2713aSLionel Sambuc SkipExcludedConditionalBlock(DirectiveTok.getLocation(), 2232*f4a2713aSLionel Sambuc /*Foundnonskip*/false, 2233*f4a2713aSLionel Sambuc /*FoundElse*/false); 2234*f4a2713aSLionel Sambuc } 2235*f4a2713aSLionel Sambuc } 2236*f4a2713aSLionel Sambuc 2237*f4a2713aSLionel Sambuc /// HandleIfDirective - Implements the \#if directive. 2238*f4a2713aSLionel Sambuc /// 2239*f4a2713aSLionel Sambuc void Preprocessor::HandleIfDirective(Token &IfToken, 2240*f4a2713aSLionel Sambuc bool ReadAnyTokensBeforeDirective) { 2241*f4a2713aSLionel Sambuc ++NumIf; 2242*f4a2713aSLionel Sambuc 2243*f4a2713aSLionel Sambuc // Parse and evaluate the conditional expression. 2244*f4a2713aSLionel Sambuc IdentifierInfo *IfNDefMacro = 0; 2245*f4a2713aSLionel Sambuc const SourceLocation ConditionalBegin = CurPPLexer->getSourceLocation(); 2246*f4a2713aSLionel Sambuc const bool ConditionalTrue = EvaluateDirectiveExpression(IfNDefMacro); 2247*f4a2713aSLionel Sambuc const SourceLocation ConditionalEnd = CurPPLexer->getSourceLocation(); 2248*f4a2713aSLionel Sambuc 2249*f4a2713aSLionel Sambuc // If this condition is equivalent to #ifndef X, and if this is the first 2250*f4a2713aSLionel Sambuc // directive seen, handle it for the multiple-include optimization. 2251*f4a2713aSLionel Sambuc if (CurPPLexer->getConditionalStackDepth() == 0) { 2252*f4a2713aSLionel Sambuc if (!ReadAnyTokensBeforeDirective && IfNDefMacro && ConditionalTrue) 2253*f4a2713aSLionel Sambuc // FIXME: Pass in the location of the macro name, not the 'if' token. 2254*f4a2713aSLionel Sambuc CurPPLexer->MIOpt.EnterTopLevelIfndef(IfNDefMacro, IfToken.getLocation()); 2255*f4a2713aSLionel Sambuc else 2256*f4a2713aSLionel Sambuc CurPPLexer->MIOpt.EnterTopLevelConditional(); 2257*f4a2713aSLionel Sambuc } 2258*f4a2713aSLionel Sambuc 2259*f4a2713aSLionel Sambuc if (Callbacks) 2260*f4a2713aSLionel Sambuc Callbacks->If(IfToken.getLocation(), 2261*f4a2713aSLionel Sambuc SourceRange(ConditionalBegin, ConditionalEnd), 2262*f4a2713aSLionel Sambuc ConditionalTrue); 2263*f4a2713aSLionel Sambuc 2264*f4a2713aSLionel Sambuc // Should we include the stuff contained by this directive? 2265*f4a2713aSLionel Sambuc if (ConditionalTrue) { 2266*f4a2713aSLionel Sambuc // Yes, remember that we are inside a conditional, then lex the next token. 2267*f4a2713aSLionel Sambuc CurPPLexer->pushConditionalLevel(IfToken.getLocation(), /*wasskip*/false, 2268*f4a2713aSLionel Sambuc /*foundnonskip*/true, /*foundelse*/false); 2269*f4a2713aSLionel Sambuc } else { 2270*f4a2713aSLionel Sambuc // No, skip the contents of this block. 2271*f4a2713aSLionel Sambuc SkipExcludedConditionalBlock(IfToken.getLocation(), /*Foundnonskip*/false, 2272*f4a2713aSLionel Sambuc /*FoundElse*/false); 2273*f4a2713aSLionel Sambuc } 2274*f4a2713aSLionel Sambuc } 2275*f4a2713aSLionel Sambuc 2276*f4a2713aSLionel Sambuc /// HandleEndifDirective - Implements the \#endif directive. 2277*f4a2713aSLionel Sambuc /// 2278*f4a2713aSLionel Sambuc void Preprocessor::HandleEndifDirective(Token &EndifToken) { 2279*f4a2713aSLionel Sambuc ++NumEndif; 2280*f4a2713aSLionel Sambuc 2281*f4a2713aSLionel Sambuc // Check that this is the whole directive. 2282*f4a2713aSLionel Sambuc CheckEndOfDirective("endif"); 2283*f4a2713aSLionel Sambuc 2284*f4a2713aSLionel Sambuc PPConditionalInfo CondInfo; 2285*f4a2713aSLionel Sambuc if (CurPPLexer->popConditionalLevel(CondInfo)) { 2286*f4a2713aSLionel Sambuc // No conditionals on the stack: this is an #endif without an #if. 2287*f4a2713aSLionel Sambuc Diag(EndifToken, diag::err_pp_endif_without_if); 2288*f4a2713aSLionel Sambuc return; 2289*f4a2713aSLionel Sambuc } 2290*f4a2713aSLionel Sambuc 2291*f4a2713aSLionel Sambuc // If this the end of a top-level #endif, inform MIOpt. 2292*f4a2713aSLionel Sambuc if (CurPPLexer->getConditionalStackDepth() == 0) 2293*f4a2713aSLionel Sambuc CurPPLexer->MIOpt.ExitTopLevelConditional(); 2294*f4a2713aSLionel Sambuc 2295*f4a2713aSLionel Sambuc assert(!CondInfo.WasSkipping && !CurPPLexer->LexingRawMode && 2296*f4a2713aSLionel Sambuc "This code should only be reachable in the non-skipping case!"); 2297*f4a2713aSLionel Sambuc 2298*f4a2713aSLionel Sambuc if (Callbacks) 2299*f4a2713aSLionel Sambuc Callbacks->Endif(EndifToken.getLocation(), CondInfo.IfLoc); 2300*f4a2713aSLionel Sambuc } 2301*f4a2713aSLionel Sambuc 2302*f4a2713aSLionel Sambuc /// HandleElseDirective - Implements the \#else directive. 2303*f4a2713aSLionel Sambuc /// 2304*f4a2713aSLionel Sambuc void Preprocessor::HandleElseDirective(Token &Result) { 2305*f4a2713aSLionel Sambuc ++NumElse; 2306*f4a2713aSLionel Sambuc 2307*f4a2713aSLionel Sambuc // #else directive in a non-skipping conditional... start skipping. 2308*f4a2713aSLionel Sambuc CheckEndOfDirective("else"); 2309*f4a2713aSLionel Sambuc 2310*f4a2713aSLionel Sambuc PPConditionalInfo CI; 2311*f4a2713aSLionel Sambuc if (CurPPLexer->popConditionalLevel(CI)) { 2312*f4a2713aSLionel Sambuc Diag(Result, diag::pp_err_else_without_if); 2313*f4a2713aSLionel Sambuc return; 2314*f4a2713aSLionel Sambuc } 2315*f4a2713aSLionel Sambuc 2316*f4a2713aSLionel Sambuc // If this is a top-level #else, inform the MIOpt. 2317*f4a2713aSLionel Sambuc if (CurPPLexer->getConditionalStackDepth() == 0) 2318*f4a2713aSLionel Sambuc CurPPLexer->MIOpt.EnterTopLevelConditional(); 2319*f4a2713aSLionel Sambuc 2320*f4a2713aSLionel Sambuc // If this is a #else with a #else before it, report the error. 2321*f4a2713aSLionel Sambuc if (CI.FoundElse) Diag(Result, diag::pp_err_else_after_else); 2322*f4a2713aSLionel Sambuc 2323*f4a2713aSLionel Sambuc if (Callbacks) 2324*f4a2713aSLionel Sambuc Callbacks->Else(Result.getLocation(), CI.IfLoc); 2325*f4a2713aSLionel Sambuc 2326*f4a2713aSLionel Sambuc // Finally, skip the rest of the contents of this block. 2327*f4a2713aSLionel Sambuc SkipExcludedConditionalBlock(CI.IfLoc, /*Foundnonskip*/true, 2328*f4a2713aSLionel Sambuc /*FoundElse*/true, Result.getLocation()); 2329*f4a2713aSLionel Sambuc } 2330*f4a2713aSLionel Sambuc 2331*f4a2713aSLionel Sambuc /// HandleElifDirective - Implements the \#elif directive. 2332*f4a2713aSLionel Sambuc /// 2333*f4a2713aSLionel Sambuc void Preprocessor::HandleElifDirective(Token &ElifToken) { 2334*f4a2713aSLionel Sambuc ++NumElse; 2335*f4a2713aSLionel Sambuc 2336*f4a2713aSLionel Sambuc // #elif directive in a non-skipping conditional... start skipping. 2337*f4a2713aSLionel Sambuc // We don't care what the condition is, because we will always skip it (since 2338*f4a2713aSLionel Sambuc // the block immediately before it was included). 2339*f4a2713aSLionel Sambuc const SourceLocation ConditionalBegin = CurPPLexer->getSourceLocation(); 2340*f4a2713aSLionel Sambuc DiscardUntilEndOfDirective(); 2341*f4a2713aSLionel Sambuc const SourceLocation ConditionalEnd = CurPPLexer->getSourceLocation(); 2342*f4a2713aSLionel Sambuc 2343*f4a2713aSLionel Sambuc PPConditionalInfo CI; 2344*f4a2713aSLionel Sambuc if (CurPPLexer->popConditionalLevel(CI)) { 2345*f4a2713aSLionel Sambuc Diag(ElifToken, diag::pp_err_elif_without_if); 2346*f4a2713aSLionel Sambuc return; 2347*f4a2713aSLionel Sambuc } 2348*f4a2713aSLionel Sambuc 2349*f4a2713aSLionel Sambuc // If this is a top-level #elif, inform the MIOpt. 2350*f4a2713aSLionel Sambuc if (CurPPLexer->getConditionalStackDepth() == 0) 2351*f4a2713aSLionel Sambuc CurPPLexer->MIOpt.EnterTopLevelConditional(); 2352*f4a2713aSLionel Sambuc 2353*f4a2713aSLionel Sambuc // If this is a #elif with a #else before it, report the error. 2354*f4a2713aSLionel Sambuc if (CI.FoundElse) Diag(ElifToken, diag::pp_err_elif_after_else); 2355*f4a2713aSLionel Sambuc 2356*f4a2713aSLionel Sambuc if (Callbacks) 2357*f4a2713aSLionel Sambuc Callbacks->Elif(ElifToken.getLocation(), 2358*f4a2713aSLionel Sambuc SourceRange(ConditionalBegin, ConditionalEnd), 2359*f4a2713aSLionel Sambuc true, CI.IfLoc); 2360*f4a2713aSLionel Sambuc 2361*f4a2713aSLionel Sambuc // Finally, skip the rest of the contents of this block. 2362*f4a2713aSLionel Sambuc SkipExcludedConditionalBlock(CI.IfLoc, /*Foundnonskip*/true, 2363*f4a2713aSLionel Sambuc /*FoundElse*/CI.FoundElse, 2364*f4a2713aSLionel Sambuc ElifToken.getLocation()); 2365*f4a2713aSLionel Sambuc } 2366