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