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