PPDirectives.cpp revision a28cc090f265d5bdaef9ebb24fb9d579928d3a18
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/Basic/SourceManager.h" 20#include "llvm/ADT/APInt.h" 21using namespace clang; 22 23//===----------------------------------------------------------------------===// 24// Utility Methods for Preprocessor Directive Handling. 25//===----------------------------------------------------------------------===// 26 27MacroInfo *Preprocessor::AllocateMacroInfo(SourceLocation L) { 28 MacroInfo *MI; 29 30 if (!MICache.empty()) { 31 MI = MICache.back(); 32 MICache.pop_back(); 33 } else 34 MI = (MacroInfo*) BP.Allocate<MacroInfo>(); 35 new (MI) MacroInfo(L); 36 return MI; 37} 38 39/// ReleaseMacroInfo - Release the specified MacroInfo. This memory will 40/// be reused for allocating new MacroInfo objects. 41void Preprocessor::ReleaseMacroInfo(MacroInfo* MI) { 42 MICache.push_back(MI); 43 MI->FreeArgumentList(BP); 44} 45 46 47/// DiscardUntilEndOfDirective - Read and discard all tokens remaining on the 48/// current line until the tok::eom token is found. 49void Preprocessor::DiscardUntilEndOfDirective() { 50 Token Tmp; 51 do { 52 LexUnexpandedToken(Tmp); 53 } while (Tmp.isNot(tok::eom)); 54} 55 56/// ReadMacroName - Lex and validate a macro name, which occurs after a 57/// #define or #undef. This sets the token kind to eom and discards the rest 58/// of the macro line if the macro name is invalid. isDefineUndef is 1 if 59/// this is due to a a #define, 2 if #undef directive, 0 if it is something 60/// else (e.g. #ifdef). 61void Preprocessor::ReadMacroName(Token &MacroNameTok, char isDefineUndef) { 62 // Read the token, don't allow macro expansion on it. 63 LexUnexpandedToken(MacroNameTok); 64 65 // Missing macro name? 66 if (MacroNameTok.is(tok::eom)) { 67 Diag(MacroNameTok, diag::err_pp_missing_macro_name); 68 return; 69 } 70 71 IdentifierInfo *II = MacroNameTok.getIdentifierInfo(); 72 if (II == 0) { 73 std::string Spelling = getSpelling(MacroNameTok); 74 const IdentifierInfo &Info = Identifiers.get(Spelling); 75 if (Info.isCPlusPlusOperatorKeyword()) 76 // C++ 2.5p2: Alternative tokens behave the same as its primary token 77 // except for their spellings. 78 Diag(MacroNameTok, diag::err_pp_operator_used_as_macro_name) << Spelling; 79 else 80 Diag(MacroNameTok, diag::err_pp_macro_not_identifier); 81 // Fall through on error. 82 } else if (isDefineUndef && II->getPPKeywordID() == tok::pp_defined) { 83 // Error if defining "defined": C99 6.10.8.4. 84 Diag(MacroNameTok, diag::err_defined_macro_name); 85 } else if (isDefineUndef && II->hasMacroDefinition() && 86 getMacroInfo(II)->isBuiltinMacro()) { 87 // Error if defining "__LINE__" and other builtins: C99 6.10.8.4. 88 if (isDefineUndef == 1) 89 Diag(MacroNameTok, diag::pp_redef_builtin_macro); 90 else 91 Diag(MacroNameTok, diag::pp_undef_builtin_macro); 92 } else { 93 // Okay, we got a good identifier node. Return it. 94 return; 95 } 96 97 // Invalid macro name, read and discard the rest of the line. Then set the 98 // token kind to tok::eom. 99 MacroNameTok.setKind(tok::eom); 100 return DiscardUntilEndOfDirective(); 101} 102 103/// CheckEndOfDirective - Ensure that the next token is a tok::eom token. If 104/// not, emit a diagnostic and consume up until the eom. If EnableMacros is 105/// true, then we consider macros that expand to zero tokens as being ok. 106void Preprocessor::CheckEndOfDirective(const char *DirType, bool EnableMacros) { 107 Token Tmp; 108 // Lex unexpanded tokens for most directives: macros might expand to zero 109 // tokens, causing us to miss diagnosing invalid lines. Some directives (like 110 // #line) allow empty macros. 111 if (EnableMacros) 112 Lex(Tmp); 113 else 114 LexUnexpandedToken(Tmp); 115 116 // There should be no tokens after the directive, but we allow them as an 117 // extension. 118 while (Tmp.is(tok::comment)) // Skip comments in -C mode. 119 LexUnexpandedToken(Tmp); 120 121 if (Tmp.isNot(tok::eom)) { 122 // Add a fixit in GNU/C99/C++ mode. Don't offer a fixit for strict-C89, 123 // because it is more trouble than it is worth to insert /**/ and check that 124 // there is no /**/ in the range also. 125 CodeModificationHint FixItHint; 126 if (Features.GNUMode || Features.C99 || Features.CPlusPlus) 127 FixItHint = CodeModificationHint::CreateInsertion(Tmp.getLocation(),"//"); 128 Diag(Tmp, diag::ext_pp_extra_tokens_at_eol) << DirType << FixItHint; 129 DiscardUntilEndOfDirective(); 130 } 131} 132 133 134 135/// SkipExcludedConditionalBlock - We just read a #if or related directive and 136/// decided that the subsequent tokens are in the #if'd out portion of the 137/// file. Lex the rest of the file, until we see an #endif. If 138/// FoundNonSkipPortion is true, then we have already emitted code for part of 139/// this #if directive, so #else/#elif blocks should never be entered. If ElseOk 140/// is true, then #else directives are ok, if not, then we have already seen one 141/// so a #else directive is a duplicate. When this returns, the caller can lex 142/// the first valid token. 143void Preprocessor::SkipExcludedConditionalBlock(SourceLocation IfTokenLoc, 144 bool FoundNonSkipPortion, 145 bool FoundElse) { 146 ++NumSkipped; 147 assert(CurTokenLexer == 0 && CurPPLexer && "Lexing a macro, not a file?"); 148 149 CurPPLexer->pushConditionalLevel(IfTokenLoc, /*isSkipping*/false, 150 FoundNonSkipPortion, FoundElse); 151 152 if (CurPTHLexer) { 153 PTHSkipExcludedConditionalBlock(); 154 return; 155 } 156 157 // Enter raw mode to disable identifier lookup (and thus macro expansion), 158 // disabling warnings, etc. 159 CurPPLexer->LexingRawMode = true; 160 Token Tok; 161 while (1) { 162 if (CurLexer) 163 CurLexer->Lex(Tok); 164 else 165 CurPTHLexer->Lex(Tok); 166 167 // If this is the end of the buffer, we have an error. 168 if (Tok.is(tok::eof)) { 169 // Emit errors for each unterminated conditional on the stack, including 170 // the current one. 171 while (!CurPPLexer->ConditionalStack.empty()) { 172 Diag(CurPPLexer->ConditionalStack.back().IfLoc, 173 diag::err_pp_unterminated_conditional); 174 CurPPLexer->ConditionalStack.pop_back(); 175 } 176 177 // Just return and let the caller lex after this #include. 178 break; 179 } 180 181 // If this token is not a preprocessor directive, just skip it. 182 if (Tok.isNot(tok::hash) || !Tok.isAtStartOfLine()) 183 continue; 184 185 // We just parsed a # character at the start of a line, so we're in 186 // directive mode. Tell the lexer this so any newlines we see will be 187 // converted into an EOM token (this terminates the macro). 188 CurPPLexer->ParsingPreprocessorDirective = true; 189 if (CurLexer) CurLexer->SetCommentRetentionState(false); 190 191 192 // Read the next token, the directive flavor. 193 LexUnexpandedToken(Tok); 194 195 // If this isn't an identifier directive (e.g. is "# 1\n" or "#\n", or 196 // something bogus), skip it. 197 if (Tok.isNot(tok::identifier)) { 198 CurPPLexer->ParsingPreprocessorDirective = false; 199 // Restore comment saving mode. 200 if (CurLexer) CurLexer->SetCommentRetentionState(KeepComments); 201 continue; 202 } 203 204 // If the first letter isn't i or e, it isn't intesting to us. We know that 205 // this is safe in the face of spelling differences, because there is no way 206 // to spell an i/e in a strange way that is another letter. Skipping this 207 // allows us to avoid looking up the identifier info for #define/#undef and 208 // other common directives. 209 const char *RawCharData = SourceMgr.getCharacterData(Tok.getLocation()); 210 char FirstChar = RawCharData[0]; 211 if (FirstChar >= 'a' && FirstChar <= 'z' && 212 FirstChar != 'i' && FirstChar != 'e') { 213 CurPPLexer->ParsingPreprocessorDirective = false; 214 // Restore comment saving mode. 215 if (CurLexer) CurLexer->SetCommentRetentionState(KeepComments); 216 continue; 217 } 218 219 // Get the identifier name without trigraphs or embedded newlines. Note 220 // that we can't use Tok.getIdentifierInfo() because its lookup is disabled 221 // when skipping. 222 // TODO: could do this with zero copies in the no-clean case by using 223 // strncmp below. 224 char Directive[20]; 225 unsigned IdLen; 226 if (!Tok.needsCleaning() && Tok.getLength() < 20) { 227 IdLen = Tok.getLength(); 228 memcpy(Directive, RawCharData, IdLen); 229 Directive[IdLen] = 0; 230 } else { 231 std::string DirectiveStr = getSpelling(Tok); 232 IdLen = DirectiveStr.size(); 233 if (IdLen >= 20) { 234 CurPPLexer->ParsingPreprocessorDirective = false; 235 // Restore comment saving mode. 236 if (CurLexer) CurLexer->SetCommentRetentionState(KeepComments); 237 continue; 238 } 239 memcpy(Directive, &DirectiveStr[0], IdLen); 240 Directive[IdLen] = 0; 241 FirstChar = Directive[0]; 242 } 243 244 if (FirstChar == 'i' && Directive[1] == 'f') { 245 if ((IdLen == 2) || // "if" 246 (IdLen == 5 && !strcmp(Directive+2, "def")) || // "ifdef" 247 (IdLen == 6 && !strcmp(Directive+2, "ndef"))) { // "ifndef" 248 // We know the entire #if/#ifdef/#ifndef block will be skipped, don't 249 // bother parsing the condition. 250 DiscardUntilEndOfDirective(); 251 CurPPLexer->pushConditionalLevel(Tok.getLocation(), /*wasskipping*/true, 252 /*foundnonskip*/false, 253 /*fnddelse*/false); 254 } 255 } else if (FirstChar == 'e') { 256 if (IdLen == 5 && !strcmp(Directive+1, "ndif")) { // "endif" 257 CheckEndOfDirective("endif"); 258 PPConditionalInfo CondInfo; 259 CondInfo.WasSkipping = true; // Silence bogus warning. 260 bool InCond = CurPPLexer->popConditionalLevel(CondInfo); 261 InCond = InCond; // Silence warning in no-asserts mode. 262 assert(!InCond && "Can't be skipping if not in a conditional!"); 263 264 // If we popped the outermost skipping block, we're done skipping! 265 if (!CondInfo.WasSkipping) 266 break; 267 } else if (IdLen == 4 && !strcmp(Directive+1, "lse")) { // "else". 268 // #else directive in a skipping conditional. If not in some other 269 // skipping conditional, and if #else hasn't already been seen, enter it 270 // as a non-skipping conditional. 271 DiscardUntilEndOfDirective(); // C99 6.10p4. 272 PPConditionalInfo &CondInfo = CurPPLexer->peekConditionalLevel(); 273 274 // If this is a #else with a #else before it, report the error. 275 if (CondInfo.FoundElse) Diag(Tok, diag::pp_err_else_after_else); 276 277 // Note that we've seen a #else in this conditional. 278 CondInfo.FoundElse = true; 279 280 // If the conditional is at the top level, and the #if block wasn't 281 // entered, enter the #else block now. 282 if (!CondInfo.WasSkipping && !CondInfo.FoundNonSkip) { 283 CondInfo.FoundNonSkip = true; 284 break; 285 } 286 } else if (IdLen == 4 && !strcmp(Directive+1, "lif")) { // "elif". 287 PPConditionalInfo &CondInfo = CurPPLexer->peekConditionalLevel(); 288 289 bool ShouldEnter; 290 // If this is in a skipping block or if we're already handled this #if 291 // block, don't bother parsing the condition. 292 if (CondInfo.WasSkipping || CondInfo.FoundNonSkip) { 293 DiscardUntilEndOfDirective(); 294 ShouldEnter = false; 295 } else { 296 // Restore the value of LexingRawMode so that identifiers are 297 // looked up, etc, inside the #elif expression. 298 assert(CurPPLexer->LexingRawMode && "We have to be skipping here!"); 299 CurPPLexer->LexingRawMode = false; 300 IdentifierInfo *IfNDefMacro = 0; 301 ShouldEnter = EvaluateDirectiveExpression(IfNDefMacro); 302 CurPPLexer->LexingRawMode = true; 303 } 304 305 // If this is a #elif with a #else before it, report the error. 306 if (CondInfo.FoundElse) Diag(Tok, diag::pp_err_elif_after_else); 307 308 // If this condition is true, enter it! 309 if (ShouldEnter) { 310 CondInfo.FoundNonSkip = true; 311 break; 312 } 313 } 314 } 315 316 CurPPLexer->ParsingPreprocessorDirective = false; 317 // Restore comment saving mode. 318 if (CurLexer) CurLexer->SetCommentRetentionState(KeepComments); 319 } 320 321 // Finally, if we are out of the conditional (saw an #endif or ran off the end 322 // of the file, just stop skipping and return to lexing whatever came after 323 // the #if block. 324 CurPPLexer->LexingRawMode = false; 325} 326 327void Preprocessor::PTHSkipExcludedConditionalBlock() { 328 329 while (1) { 330 assert(CurPTHLexer); 331 assert(CurPTHLexer->LexingRawMode == false); 332 333 // Skip to the next '#else', '#elif', or #endif. 334 if (CurPTHLexer->SkipBlock()) { 335 // We have reached an #endif. Both the '#' and 'endif' tokens 336 // have been consumed by the PTHLexer. Just pop off the condition level. 337 PPConditionalInfo CondInfo; 338 bool InCond = CurPTHLexer->popConditionalLevel(CondInfo); 339 InCond = InCond; // Silence warning in no-asserts mode. 340 assert(!InCond && "Can't be skipping if not in a conditional!"); 341 break; 342 } 343 344 // We have reached a '#else' or '#elif'. Lex the next token to get 345 // the directive flavor. 346 Token Tok; 347 LexUnexpandedToken(Tok); 348 349 // We can actually look up the IdentifierInfo here since we aren't in 350 // raw mode. 351 tok::PPKeywordKind K = Tok.getIdentifierInfo()->getPPKeywordID(); 352 353 if (K == tok::pp_else) { 354 // #else: Enter the else condition. We aren't in a nested condition 355 // since we skip those. We're always in the one matching the last 356 // blocked we skipped. 357 PPConditionalInfo &CondInfo = CurPTHLexer->peekConditionalLevel(); 358 // Note that we've seen a #else in this conditional. 359 CondInfo.FoundElse = true; 360 361 // If the #if block wasn't entered then enter the #else block now. 362 if (!CondInfo.FoundNonSkip) { 363 CondInfo.FoundNonSkip = true; 364 365 // Scan until the eom token. 366 CurPTHLexer->ParsingPreprocessorDirective = true; 367 DiscardUntilEndOfDirective(); 368 CurPTHLexer->ParsingPreprocessorDirective = false; 369 370 break; 371 } 372 373 // Otherwise skip this block. 374 continue; 375 } 376 377 assert(K == tok::pp_elif); 378 PPConditionalInfo &CondInfo = CurPTHLexer->peekConditionalLevel(); 379 380 // If this is a #elif with a #else before it, report the error. 381 if (CondInfo.FoundElse) 382 Diag(Tok, diag::pp_err_elif_after_else); 383 384 // If this is in a skipping block or if we're already handled this #if 385 // block, don't bother parsing the condition. We just skip this block. 386 if (CondInfo.FoundNonSkip) 387 continue; 388 389 // Evaluate the condition of the #elif. 390 IdentifierInfo *IfNDefMacro = 0; 391 CurPTHLexer->ParsingPreprocessorDirective = true; 392 bool ShouldEnter = EvaluateDirectiveExpression(IfNDefMacro); 393 CurPTHLexer->ParsingPreprocessorDirective = false; 394 395 // If this condition is true, enter it! 396 if (ShouldEnter) { 397 CondInfo.FoundNonSkip = true; 398 break; 399 } 400 401 // Otherwise, skip this block and go to the next one. 402 continue; 403 } 404} 405 406/// LookupFile - Given a "foo" or <foo> reference, look up the indicated file, 407/// return null on failure. isAngled indicates whether the file reference is 408/// for system #include's or not (i.e. using <> instead of ""). 409const FileEntry *Preprocessor::LookupFile(const char *FilenameStart, 410 const char *FilenameEnd, 411 bool isAngled, 412 const DirectoryLookup *FromDir, 413 const DirectoryLookup *&CurDir) { 414 // If the header lookup mechanism may be relative to the current file, pass in 415 // info about where the current file is. 416 const FileEntry *CurFileEnt = 0; 417 if (!FromDir) { 418 FileID FID = getCurrentFileLexer()->getFileID(); 419 CurFileEnt = SourceMgr.getFileEntryForID(FID); 420 421 // If there is no file entry associated with this file, it must be the 422 // predefines buffer. Any other file is not lexed with a normal lexer, so 423 // it won't be scanned for preprocessor directives. If we have the 424 // predefines buffer, resolve #include references (which come from the 425 // -include command line argument) as if they came from the main file, this 426 // affects file lookup etc. 427 if (CurFileEnt == 0) { 428 FID = SourceMgr.getMainFileID(); 429 CurFileEnt = SourceMgr.getFileEntryForID(FID); 430 } 431 } 432 433 // Do a standard file entry lookup. 434 CurDir = CurDirLookup; 435 const FileEntry *FE = 436 HeaderInfo.LookupFile(FilenameStart, FilenameEnd, 437 isAngled, FromDir, CurDir, CurFileEnt); 438 if (FE) return FE; 439 440 // Otherwise, see if this is a subframework header. If so, this is relative 441 // to one of the headers on the #include stack. Walk the list of the current 442 // headers on the #include stack and pass them to HeaderInfo. 443 if (IsFileLexer()) { 444 if ((CurFileEnt = SourceMgr.getFileEntryForID(CurPPLexer->getFileID()))) 445 if ((FE = HeaderInfo.LookupSubframeworkHeader(FilenameStart, FilenameEnd, 446 CurFileEnt))) 447 return FE; 448 } 449 450 for (unsigned i = 0, e = IncludeMacroStack.size(); i != e; ++i) { 451 IncludeStackInfo &ISEntry = IncludeMacroStack[e-i-1]; 452 if (IsFileLexer(ISEntry)) { 453 if ((CurFileEnt = 454 SourceMgr.getFileEntryForID(ISEntry.ThePPLexer->getFileID()))) 455 if ((FE = HeaderInfo.LookupSubframeworkHeader(FilenameStart, 456 FilenameEnd, CurFileEnt))) 457 return FE; 458 } 459 } 460 461 // Otherwise, we really couldn't find the file. 462 return 0; 463} 464 465 466//===----------------------------------------------------------------------===// 467// Preprocessor Directive Handling. 468//===----------------------------------------------------------------------===// 469 470/// HandleDirective - This callback is invoked when the lexer sees a # token 471/// at the start of a line. This consumes the directive, modifies the 472/// lexer/preprocessor state, and advances the lexer(s) so that the next token 473/// read is the correct one. 474void Preprocessor::HandleDirective(Token &Result) { 475 // FIXME: Traditional: # with whitespace before it not recognized by K&R? 476 477 // We just parsed a # character at the start of a line, so we're in directive 478 // mode. Tell the lexer this so any newlines we see will be converted into an 479 // EOM token (which terminates the directive). 480 CurPPLexer->ParsingPreprocessorDirective = true; 481 482 ++NumDirectives; 483 484 // We are about to read a token. For the multiple-include optimization FA to 485 // work, we have to remember if we had read any tokens *before* this 486 // pp-directive. 487 bool ReadAnyTokensBeforeDirective = CurPPLexer->MIOpt.getHasReadAnyTokensVal(); 488 489 // Save the '#' token in case we need to return it later. 490 Token SavedHash = Result; 491 492 // Read the next token, the directive flavor. This isn't expanded due to 493 // C99 6.10.3p8. 494 LexUnexpandedToken(Result); 495 496 // C99 6.10.3p11: Is this preprocessor directive in macro invocation? e.g.: 497 // #define A(x) #x 498 // A(abc 499 // #warning blah 500 // def) 501 // If so, the user is relying on non-portable behavior, emit a diagnostic. 502 if (InMacroArgs) 503 Diag(Result, diag::ext_embedded_directive); 504 505TryAgain: 506 switch (Result.getKind()) { 507 case tok::eom: 508 return; // null directive. 509 case tok::comment: 510 // Handle stuff like "# /*foo*/ define X" in -E -C mode. 511 LexUnexpandedToken(Result); 512 goto TryAgain; 513 514 case tok::numeric_constant: // # 7 GNU line marker directive. 515 if (getLangOptions().AsmPreprocessor) 516 break; // # 4 is not a preprocessor directive in .S files. 517 return HandleDigitDirective(Result); 518 default: 519 IdentifierInfo *II = Result.getIdentifierInfo(); 520 if (II == 0) break; // Not an identifier. 521 522 // Ask what the preprocessor keyword ID is. 523 switch (II->getPPKeywordID()) { 524 default: break; 525 // C99 6.10.1 - Conditional Inclusion. 526 case tok::pp_if: 527 return HandleIfDirective(Result, ReadAnyTokensBeforeDirective); 528 case tok::pp_ifdef: 529 return HandleIfdefDirective(Result, false, true/*not valid for miopt*/); 530 case tok::pp_ifndef: 531 return HandleIfdefDirective(Result, true, ReadAnyTokensBeforeDirective); 532 case tok::pp_elif: 533 return HandleElifDirective(Result); 534 case tok::pp_else: 535 return HandleElseDirective(Result); 536 case tok::pp_endif: 537 return HandleEndifDirective(Result); 538 539 // C99 6.10.2 - Source File Inclusion. 540 case tok::pp_include: 541 return HandleIncludeDirective(Result); // Handle #include. 542 case tok::pp___include_macros: 543 return HandleIncludeMacrosDirective(Result); // Handle -imacros. 544 545 // C99 6.10.3 - Macro Replacement. 546 case tok::pp_define: 547 return HandleDefineDirective(Result); 548 case tok::pp_undef: 549 return HandleUndefDirective(Result); 550 551 // C99 6.10.4 - Line Control. 552 case tok::pp_line: 553 return HandleLineDirective(Result); 554 555 // C99 6.10.5 - Error Directive. 556 case tok::pp_error: 557 return HandleUserDiagnosticDirective(Result, false); 558 559 // C99 6.10.6 - Pragma Directive. 560 case tok::pp_pragma: 561 return HandlePragmaDirective(); 562 563 // GNU Extensions. 564 case tok::pp_import: 565 return HandleImportDirective(Result); 566 case tok::pp_include_next: 567 return HandleIncludeNextDirective(Result); 568 569 case tok::pp_warning: 570 Diag(Result, diag::ext_pp_warning_directive); 571 return HandleUserDiagnosticDirective(Result, true); 572 case tok::pp_ident: 573 return HandleIdentSCCSDirective(Result); 574 case tok::pp_sccs: 575 return HandleIdentSCCSDirective(Result); 576 case tok::pp_assert: 577 //isExtension = true; // FIXME: implement #assert 578 break; 579 case tok::pp_unassert: 580 //isExtension = true; // FIXME: implement #unassert 581 break; 582 } 583 break; 584 } 585 586 // If this is a .S file, treat unknown # directives as non-preprocessor 587 // directives. This is important because # may be a comment or introduce 588 // various pseudo-ops. Just return the # token and push back the following 589 // token to be lexed next time. 590 if (getLangOptions().AsmPreprocessor) { 591 Token *Toks = new Token[2]; 592 // Return the # and the token after it. 593 Toks[0] = SavedHash; 594 Toks[1] = Result; 595 // Enter this token stream so that we re-lex the tokens. Make sure to 596 // enable macro expansion, in case the token after the # is an identifier 597 // that is expanded. 598 EnterTokenStream(Toks, 2, false, true); 599 return; 600 } 601 602 // If we reached here, the preprocessing token is not valid! 603 Diag(Result, diag::err_pp_invalid_directive); 604 605 // Read the rest of the PP line. 606 DiscardUntilEndOfDirective(); 607 608 // Okay, we're done parsing the directive. 609} 610 611/// GetLineValue - Convert a numeric token into an unsigned value, emitting 612/// Diagnostic DiagID if it is invalid, and returning the value in Val. 613static bool GetLineValue(Token &DigitTok, unsigned &Val, 614 unsigned DiagID, Preprocessor &PP) { 615 if (DigitTok.isNot(tok::numeric_constant)) { 616 PP.Diag(DigitTok, DiagID); 617 618 if (DigitTok.isNot(tok::eom)) 619 PP.DiscardUntilEndOfDirective(); 620 return true; 621 } 622 623 llvm::SmallString<64> IntegerBuffer; 624 IntegerBuffer.resize(DigitTok.getLength()); 625 const char *DigitTokBegin = &IntegerBuffer[0]; 626 unsigned ActualLength = PP.getSpelling(DigitTok, DigitTokBegin); 627 628 // Verify that we have a simple digit-sequence, and compute the value. This 629 // is always a simple digit string computed in decimal, so we do this manually 630 // here. 631 Val = 0; 632 for (unsigned i = 0; i != ActualLength; ++i) { 633 if (!isdigit(DigitTokBegin[i])) { 634 PP.Diag(PP.AdvanceToTokenCharacter(DigitTok.getLocation(), i), 635 diag::err_pp_line_digit_sequence); 636 PP.DiscardUntilEndOfDirective(); 637 return true; 638 } 639 640 unsigned NextVal = Val*10+(DigitTokBegin[i]-'0'); 641 if (NextVal < Val) { // overflow. 642 PP.Diag(DigitTok, DiagID); 643 PP.DiscardUntilEndOfDirective(); 644 return true; 645 } 646 Val = NextVal; 647 } 648 649 // Reject 0, this is needed both by #line numbers and flags. 650 if (Val == 0) { 651 PP.Diag(DigitTok, DiagID); 652 PP.DiscardUntilEndOfDirective(); 653 return true; 654 } 655 656 if (DigitTokBegin[0] == '0') 657 PP.Diag(DigitTok.getLocation(), diag::warn_pp_line_decimal); 658 659 return false; 660} 661 662/// HandleLineDirective - Handle #line directive: C99 6.10.4. The two 663/// acceptable forms are: 664/// # line digit-sequence 665/// # line digit-sequence "s-char-sequence" 666void Preprocessor::HandleLineDirective(Token &Tok) { 667 // Read the line # and string argument. Per C99 6.10.4p5, these tokens are 668 // expanded. 669 Token DigitTok; 670 Lex(DigitTok); 671 672 // Validate the number and convert it to an unsigned. 673 unsigned LineNo; 674 if (GetLineValue(DigitTok, LineNo, diag::err_pp_line_requires_integer,*this)) 675 return; 676 677 // Enforce C99 6.10.4p3: "The digit sequence shall not specify ... a 678 // number greater than 2147483647". C90 requires that the line # be <= 32767. 679 unsigned LineLimit = Features.C99 ? 2147483648U : 32768U; 680 if (LineNo >= LineLimit) 681 Diag(DigitTok, diag::ext_pp_line_too_big) << LineLimit; 682 683 int FilenameID = -1; 684 Token StrTok; 685 Lex(StrTok); 686 687 // If the StrTok is "eom", then it wasn't present. Otherwise, it must be a 688 // string followed by eom. 689 if (StrTok.is(tok::eom)) 690 ; // ok 691 else if (StrTok.isNot(tok::string_literal)) { 692 Diag(StrTok, diag::err_pp_line_invalid_filename); 693 DiscardUntilEndOfDirective(); 694 return; 695 } else { 696 // Parse and validate the string, converting it into a unique ID. 697 StringLiteralParser Literal(&StrTok, 1, *this); 698 assert(!Literal.AnyWide && "Didn't allow wide strings in"); 699 if (Literal.hadError) 700 return DiscardUntilEndOfDirective(); 701 if (Literal.Pascal) { 702 Diag(StrTok, diag::err_pp_linemarker_invalid_filename); 703 return DiscardUntilEndOfDirective(); 704 } 705 FilenameID = SourceMgr.getLineTableFilenameID(Literal.GetString(), 706 Literal.GetStringLength()); 707 708 // Verify that there is nothing after the string, other than EOM. Because 709 // of C99 6.10.4p5, macros that expand to empty tokens are ok. 710 CheckEndOfDirective("line", true); 711 } 712 713 SourceMgr.AddLineNote(DigitTok.getLocation(), LineNo, FilenameID); 714 715 if (Callbacks) 716 Callbacks->FileChanged(DigitTok.getLocation(), PPCallbacks::RenameFile, 717 SrcMgr::C_User); 718} 719 720/// ReadLineMarkerFlags - Parse and validate any flags at the end of a GNU line 721/// marker directive. 722static bool ReadLineMarkerFlags(bool &IsFileEntry, bool &IsFileExit, 723 bool &IsSystemHeader, bool &IsExternCHeader, 724 Preprocessor &PP) { 725 unsigned FlagVal; 726 Token FlagTok; 727 PP.Lex(FlagTok); 728 if (FlagTok.is(tok::eom)) return false; 729 if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag, PP)) 730 return true; 731 732 if (FlagVal == 1) { 733 IsFileEntry = true; 734 735 PP.Lex(FlagTok); 736 if (FlagTok.is(tok::eom)) return false; 737 if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag,PP)) 738 return true; 739 } else if (FlagVal == 2) { 740 IsFileExit = true; 741 742 SourceManager &SM = PP.getSourceManager(); 743 // If we are leaving the current presumed file, check to make sure the 744 // presumed include stack isn't empty! 745 FileID CurFileID = 746 SM.getDecomposedInstantiationLoc(FlagTok.getLocation()).first; 747 PresumedLoc PLoc = SM.getPresumedLoc(FlagTok.getLocation()); 748 749 // If there is no include loc (main file) or if the include loc is in a 750 // different physical file, then we aren't in a "1" line marker flag region. 751 SourceLocation IncLoc = PLoc.getIncludeLoc(); 752 if (IncLoc.isInvalid() || 753 SM.getDecomposedInstantiationLoc(IncLoc).first != CurFileID) { 754 PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_pop); 755 PP.DiscardUntilEndOfDirective(); 756 return true; 757 } 758 759 PP.Lex(FlagTok); 760 if (FlagTok.is(tok::eom)) return false; 761 if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag,PP)) 762 return true; 763 } 764 765 // We must have 3 if there are still flags. 766 if (FlagVal != 3) { 767 PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_flag); 768 PP.DiscardUntilEndOfDirective(); 769 return true; 770 } 771 772 IsSystemHeader = true; 773 774 PP.Lex(FlagTok); 775 if (FlagTok.is(tok::eom)) return false; 776 if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag, PP)) 777 return true; 778 779 // We must have 4 if there is yet another flag. 780 if (FlagVal != 4) { 781 PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_flag); 782 PP.DiscardUntilEndOfDirective(); 783 return true; 784 } 785 786 IsExternCHeader = true; 787 788 PP.Lex(FlagTok); 789 if (FlagTok.is(tok::eom)) return false; 790 791 // There are no more valid flags here. 792 PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_flag); 793 PP.DiscardUntilEndOfDirective(); 794 return true; 795} 796 797/// HandleDigitDirective - Handle a GNU line marker directive, whose syntax is 798/// one of the following forms: 799/// 800/// # 42 801/// # 42 "file" ('1' | '2')? 802/// # 42 "file" ('1' | '2')? '3' '4'? 803/// 804void Preprocessor::HandleDigitDirective(Token &DigitTok) { 805 // Validate the number and convert it to an unsigned. GNU does not have a 806 // line # limit other than it fit in 32-bits. 807 unsigned LineNo; 808 if (GetLineValue(DigitTok, LineNo, diag::err_pp_linemarker_requires_integer, 809 *this)) 810 return; 811 812 Token StrTok; 813 Lex(StrTok); 814 815 bool IsFileEntry = false, IsFileExit = false; 816 bool IsSystemHeader = false, IsExternCHeader = false; 817 int FilenameID = -1; 818 819 // If the StrTok is "eom", then it wasn't present. Otherwise, it must be a 820 // string followed by eom. 821 if (StrTok.is(tok::eom)) 822 ; // ok 823 else if (StrTok.isNot(tok::string_literal)) { 824 Diag(StrTok, diag::err_pp_linemarker_invalid_filename); 825 return DiscardUntilEndOfDirective(); 826 } else { 827 // Parse and validate the string, converting it into a unique ID. 828 StringLiteralParser Literal(&StrTok, 1, *this); 829 assert(!Literal.AnyWide && "Didn't allow wide strings in"); 830 if (Literal.hadError) 831 return DiscardUntilEndOfDirective(); 832 if (Literal.Pascal) { 833 Diag(StrTok, diag::err_pp_linemarker_invalid_filename); 834 return DiscardUntilEndOfDirective(); 835 } 836 FilenameID = SourceMgr.getLineTableFilenameID(Literal.GetString(), 837 Literal.GetStringLength()); 838 839 // If a filename was present, read any flags that are present. 840 if (ReadLineMarkerFlags(IsFileEntry, IsFileExit, 841 IsSystemHeader, IsExternCHeader, *this)) 842 return; 843 } 844 845 // Create a line note with this information. 846 SourceMgr.AddLineNote(DigitTok.getLocation(), LineNo, FilenameID, 847 IsFileEntry, IsFileExit, 848 IsSystemHeader, IsExternCHeader); 849 850 // If the preprocessor has callbacks installed, notify them of the #line 851 // change. This is used so that the line marker comes out in -E mode for 852 // example. 853 if (Callbacks) { 854 PPCallbacks::FileChangeReason Reason = PPCallbacks::RenameFile; 855 if (IsFileEntry) 856 Reason = PPCallbacks::EnterFile; 857 else if (IsFileExit) 858 Reason = PPCallbacks::ExitFile; 859 SrcMgr::CharacteristicKind FileKind = SrcMgr::C_User; 860 if (IsExternCHeader) 861 FileKind = SrcMgr::C_ExternCSystem; 862 else if (IsSystemHeader) 863 FileKind = SrcMgr::C_System; 864 865 Callbacks->FileChanged(DigitTok.getLocation(), Reason, FileKind); 866 } 867} 868 869 870/// HandleUserDiagnosticDirective - Handle a #warning or #error directive. 871/// 872void Preprocessor::HandleUserDiagnosticDirective(Token &Tok, 873 bool isWarning) { 874 // PTH doesn't emit #warning or #error directives. 875 if (CurPTHLexer) 876 return CurPTHLexer->DiscardToEndOfLine(); 877 878 // Read the rest of the line raw. We do this because we don't want macros 879 // to be expanded and we don't require that the tokens be valid preprocessing 880 // tokens. For example, this is allowed: "#warning ` 'foo". GCC does 881 // collapse multiple consequtive white space between tokens, but this isn't 882 // specified by the standard. 883 std::string Message = CurLexer->ReadToEndOfLine(); 884 if (isWarning) 885 Diag(Tok, diag::pp_hash_warning) << Message; 886 else 887 Diag(Tok, diag::err_pp_hash_error) << Message; 888} 889 890/// HandleIdentSCCSDirective - Handle a #ident/#sccs directive. 891/// 892void Preprocessor::HandleIdentSCCSDirective(Token &Tok) { 893 // Yes, this directive is an extension. 894 Diag(Tok, diag::ext_pp_ident_directive); 895 896 // Read the string argument. 897 Token StrTok; 898 Lex(StrTok); 899 900 // If the token kind isn't a string, it's a malformed directive. 901 if (StrTok.isNot(tok::string_literal) && 902 StrTok.isNot(tok::wide_string_literal)) { 903 Diag(StrTok, diag::err_pp_malformed_ident); 904 if (StrTok.isNot(tok::eom)) 905 DiscardUntilEndOfDirective(); 906 return; 907 } 908 909 // Verify that there is nothing after the string, other than EOM. 910 CheckEndOfDirective("ident"); 911 912 if (Callbacks) 913 Callbacks->Ident(Tok.getLocation(), getSpelling(StrTok)); 914} 915 916//===----------------------------------------------------------------------===// 917// Preprocessor Include Directive Handling. 918//===----------------------------------------------------------------------===// 919 920/// GetIncludeFilenameSpelling - Turn the specified lexer token into a fully 921/// checked and spelled filename, e.g. as an operand of #include. This returns 922/// true if the input filename was in <>'s or false if it were in ""'s. The 923/// caller is expected to provide a buffer that is large enough to hold the 924/// spelling of the filename, but is also expected to handle the case when 925/// this method decides to use a different buffer. 926bool Preprocessor::GetIncludeFilenameSpelling(SourceLocation Loc, 927 const char *&BufStart, 928 const char *&BufEnd) { 929 // Get the text form of the filename. 930 assert(BufStart != BufEnd && "Can't have tokens with empty spellings!"); 931 932 // Make sure the filename is <x> or "x". 933 bool isAngled; 934 if (BufStart[0] == '<') { 935 if (BufEnd[-1] != '>') { 936 Diag(Loc, diag::err_pp_expects_filename); 937 BufStart = 0; 938 return true; 939 } 940 isAngled = true; 941 } else if (BufStart[0] == '"') { 942 if (BufEnd[-1] != '"') { 943 Diag(Loc, diag::err_pp_expects_filename); 944 BufStart = 0; 945 return true; 946 } 947 isAngled = false; 948 } else { 949 Diag(Loc, diag::err_pp_expects_filename); 950 BufStart = 0; 951 return true; 952 } 953 954 // Diagnose #include "" as invalid. 955 if (BufEnd-BufStart <= 2) { 956 Diag(Loc, diag::err_pp_empty_filename); 957 BufStart = 0; 958 return ""; 959 } 960 961 // Skip the brackets. 962 ++BufStart; 963 --BufEnd; 964 return isAngled; 965} 966 967/// ConcatenateIncludeName - Handle cases where the #include name is expanded 968/// from a macro as multiple tokens, which need to be glued together. This 969/// occurs for code like: 970/// #define FOO <a/b.h> 971/// #include FOO 972/// because in this case, "<a/b.h>" is returned as 7 tokens, not one. 973/// 974/// This code concatenates and consumes tokens up to the '>' token. It returns 975/// false if the > was found, otherwise it returns true if it finds and consumes 976/// the EOM marker. 977bool Preprocessor::ConcatenateIncludeName( 978 llvm::SmallVector<char, 128> &FilenameBuffer) { 979 Token CurTok; 980 981 Lex(CurTok); 982 while (CurTok.isNot(tok::eom)) { 983 // Append the spelling of this token to the buffer. If there was a space 984 // before it, add it now. 985 if (CurTok.hasLeadingSpace()) 986 FilenameBuffer.push_back(' '); 987 988 // Get the spelling of the token, directly into FilenameBuffer if possible. 989 unsigned PreAppendSize = FilenameBuffer.size(); 990 FilenameBuffer.resize(PreAppendSize+CurTok.getLength()); 991 992 const char *BufPtr = &FilenameBuffer[PreAppendSize]; 993 unsigned ActualLen = getSpelling(CurTok, BufPtr); 994 995 // If the token was spelled somewhere else, copy it into FilenameBuffer. 996 if (BufPtr != &FilenameBuffer[PreAppendSize]) 997 memcpy(&FilenameBuffer[PreAppendSize], BufPtr, ActualLen); 998 999 // Resize FilenameBuffer to the correct size. 1000 if (CurTok.getLength() != ActualLen) 1001 FilenameBuffer.resize(PreAppendSize+ActualLen); 1002 1003 // If we found the '>' marker, return success. 1004 if (CurTok.is(tok::greater)) 1005 return false; 1006 1007 Lex(CurTok); 1008 } 1009 1010 // If we hit the eom marker, emit an error and return true so that the caller 1011 // knows the EOM has been read. 1012 Diag(CurTok.getLocation(), diag::err_pp_expects_filename); 1013 return true; 1014} 1015 1016/// HandleIncludeDirective - The "#include" tokens have just been read, read the 1017/// file to be included from the lexer, then include it! This is a common 1018/// routine with functionality shared between #include, #include_next and 1019/// #import. LookupFrom is set when this is a #include_next directive, it 1020/// specifies the file to start searching from. 1021void Preprocessor::HandleIncludeDirective(Token &IncludeTok, 1022 const DirectoryLookup *LookupFrom, 1023 bool isImport) { 1024 1025 Token FilenameTok; 1026 CurPPLexer->LexIncludeFilename(FilenameTok); 1027 1028 // Reserve a buffer to get the spelling. 1029 llvm::SmallVector<char, 128> FilenameBuffer; 1030 const char *FilenameStart, *FilenameEnd; 1031 1032 switch (FilenameTok.getKind()) { 1033 case tok::eom: 1034 // If the token kind is EOM, the error has already been diagnosed. 1035 return; 1036 1037 case tok::angle_string_literal: 1038 case tok::string_literal: { 1039 FilenameBuffer.resize(FilenameTok.getLength()); 1040 FilenameStart = &FilenameBuffer[0]; 1041 unsigned Len = getSpelling(FilenameTok, FilenameStart); 1042 FilenameEnd = FilenameStart+Len; 1043 break; 1044 } 1045 1046 case tok::less: 1047 // This could be a <foo/bar.h> file coming from a macro expansion. In this 1048 // case, glue the tokens together into FilenameBuffer and interpret those. 1049 FilenameBuffer.push_back('<'); 1050 if (ConcatenateIncludeName(FilenameBuffer)) 1051 return; // Found <eom> but no ">"? Diagnostic already emitted. 1052 FilenameStart = FilenameBuffer.data(); 1053 FilenameEnd = FilenameStart + FilenameBuffer.size(); 1054 break; 1055 default: 1056 Diag(FilenameTok.getLocation(), diag::err_pp_expects_filename); 1057 DiscardUntilEndOfDirective(); 1058 return; 1059 } 1060 1061 bool isAngled = GetIncludeFilenameSpelling(FilenameTok.getLocation(), 1062 FilenameStart, FilenameEnd); 1063 // If GetIncludeFilenameSpelling set the start ptr to null, there was an 1064 // error. 1065 if (FilenameStart == 0) { 1066 DiscardUntilEndOfDirective(); 1067 return; 1068 } 1069 1070 // Verify that there is nothing after the filename, other than EOM. Note that 1071 // we allow macros that expand to nothing after the filename, because this 1072 // falls into the category of "#include pp-tokens new-line" specified in 1073 // C99 6.10.2p4. 1074 CheckEndOfDirective(IncludeTok.getIdentifierInfo()->getNameStart(), true); 1075 1076 // Check that we don't have infinite #include recursion. 1077 if (IncludeMacroStack.size() == MaxAllowedIncludeStackDepth-1) { 1078 Diag(FilenameTok, diag::err_pp_include_too_deep); 1079 return; 1080 } 1081 1082 // Search include directories. 1083 const DirectoryLookup *CurDir; 1084 const FileEntry *File = LookupFile(FilenameStart, FilenameEnd, 1085 isAngled, LookupFrom, CurDir); 1086 if (File == 0) { 1087 Diag(FilenameTok, diag::err_pp_file_not_found) 1088 << std::string(FilenameStart, FilenameEnd); 1089 return; 1090 } 1091 1092 // Ask HeaderInfo if we should enter this #include file. If not, #including 1093 // this file will have no effect. 1094 if (!HeaderInfo.ShouldEnterIncludeFile(File, isImport)) 1095 return; 1096 1097 // The #included file will be considered to be a system header if either it is 1098 // in a system include directory, or if the #includer is a system include 1099 // header. 1100 SrcMgr::CharacteristicKind FileCharacter = 1101 std::max(HeaderInfo.getFileDirFlavor(File), 1102 SourceMgr.getFileCharacteristic(FilenameTok.getLocation())); 1103 1104 // Look up the file, create a File ID for it. 1105 FileID FID = SourceMgr.createFileID(File, FilenameTok.getLocation(), 1106 FileCharacter); 1107 if (FID.isInvalid()) { 1108 Diag(FilenameTok, diag::err_pp_file_not_found) 1109 << std::string(FilenameStart, FilenameEnd); 1110 return; 1111 } 1112 1113 // Finally, if all is good, enter the new file! 1114 EnterSourceFile(FID, CurDir); 1115} 1116 1117/// HandleIncludeNextDirective - Implements #include_next. 1118/// 1119void Preprocessor::HandleIncludeNextDirective(Token &IncludeNextTok) { 1120 Diag(IncludeNextTok, diag::ext_pp_include_next_directive); 1121 1122 // #include_next is like #include, except that we start searching after 1123 // the current found directory. If we can't do this, issue a 1124 // diagnostic. 1125 const DirectoryLookup *Lookup = CurDirLookup; 1126 if (isInPrimaryFile()) { 1127 Lookup = 0; 1128 Diag(IncludeNextTok, diag::pp_include_next_in_primary); 1129 } else if (Lookup == 0) { 1130 Diag(IncludeNextTok, diag::pp_include_next_absolute_path); 1131 } else { 1132 // Start looking up in the next directory. 1133 ++Lookup; 1134 } 1135 1136 return HandleIncludeDirective(IncludeNextTok, Lookup); 1137} 1138 1139/// HandleImportDirective - Implements #import. 1140/// 1141void Preprocessor::HandleImportDirective(Token &ImportTok) { 1142 if (!Features.ObjC1) // #import is standard for ObjC. 1143 Diag(ImportTok, diag::ext_pp_import_directive); 1144 1145 return HandleIncludeDirective(ImportTok, 0, true); 1146} 1147 1148/// HandleIncludeMacrosDirective - The -imacros command line option turns into a 1149/// pseudo directive in the predefines buffer. This handles it by sucking all 1150/// tokens through the preprocessor and discarding them (only keeping the side 1151/// effects on the preprocessor). 1152void Preprocessor::HandleIncludeMacrosDirective(Token &IncludeMacrosTok) { 1153 // This directive should only occur in the predefines buffer. If not, emit an 1154 // error and reject it. 1155 SourceLocation Loc = IncludeMacrosTok.getLocation(); 1156 if (strcmp(SourceMgr.getBufferName(Loc), "<built-in>") != 0) { 1157 Diag(IncludeMacrosTok.getLocation(), 1158 diag::pp_include_macros_out_of_predefines); 1159 DiscardUntilEndOfDirective(); 1160 return; 1161 } 1162 1163 // Treat this as a normal #include for checking purposes. If this is 1164 // successful, it will push a new lexer onto the include stack. 1165 HandleIncludeDirective(IncludeMacrosTok, 0, false); 1166 1167 Token TmpTok; 1168 do { 1169 Lex(TmpTok); 1170 assert(TmpTok.isNot(tok::eof) && "Didn't find end of -imacros!"); 1171 } while (TmpTok.isNot(tok::hashhash)); 1172} 1173 1174//===----------------------------------------------------------------------===// 1175// Preprocessor Macro Directive Handling. 1176//===----------------------------------------------------------------------===// 1177 1178/// ReadMacroDefinitionArgList - The ( starting an argument list of a macro 1179/// definition has just been read. Lex the rest of the arguments and the 1180/// closing ), updating MI with what we learn. Return true if an error occurs 1181/// parsing the arg list. 1182bool Preprocessor::ReadMacroDefinitionArgList(MacroInfo *MI) { 1183 llvm::SmallVector<IdentifierInfo*, 32> Arguments; 1184 1185 Token Tok; 1186 while (1) { 1187 LexUnexpandedToken(Tok); 1188 switch (Tok.getKind()) { 1189 case tok::r_paren: 1190 // Found the end of the argument list. 1191 if (Arguments.empty()) // #define FOO() 1192 return false; 1193 // Otherwise we have #define FOO(A,) 1194 Diag(Tok, diag::err_pp_expected_ident_in_arg_list); 1195 return true; 1196 case tok::ellipsis: // #define X(... -> C99 varargs 1197 // Warn if use of C99 feature in non-C99 mode. 1198 if (!Features.C99) Diag(Tok, diag::ext_variadic_macro); 1199 1200 // Lex the token after the identifier. 1201 LexUnexpandedToken(Tok); 1202 if (Tok.isNot(tok::r_paren)) { 1203 Diag(Tok, diag::err_pp_missing_rparen_in_macro_def); 1204 return true; 1205 } 1206 // Add the __VA_ARGS__ identifier as an argument. 1207 Arguments.push_back(Ident__VA_ARGS__); 1208 MI->setIsC99Varargs(); 1209 MI->setArgumentList(&Arguments[0], Arguments.size(), BP); 1210 return false; 1211 case tok::eom: // #define X( 1212 Diag(Tok, diag::err_pp_missing_rparen_in_macro_def); 1213 return true; 1214 default: 1215 // Handle keywords and identifiers here to accept things like 1216 // #define Foo(for) for. 1217 IdentifierInfo *II = Tok.getIdentifierInfo(); 1218 if (II == 0) { 1219 // #define X(1 1220 Diag(Tok, diag::err_pp_invalid_tok_in_arg_list); 1221 return true; 1222 } 1223 1224 // If this is already used as an argument, it is used multiple times (e.g. 1225 // #define X(A,A. 1226 if (std::find(Arguments.begin(), Arguments.end(), II) != 1227 Arguments.end()) { // C99 6.10.3p6 1228 Diag(Tok, diag::err_pp_duplicate_name_in_arg_list) << II; 1229 return true; 1230 } 1231 1232 // Add the argument to the macro info. 1233 Arguments.push_back(II); 1234 1235 // Lex the token after the identifier. 1236 LexUnexpandedToken(Tok); 1237 1238 switch (Tok.getKind()) { 1239 default: // #define X(A B 1240 Diag(Tok, diag::err_pp_expected_comma_in_arg_list); 1241 return true; 1242 case tok::r_paren: // #define X(A) 1243 MI->setArgumentList(&Arguments[0], Arguments.size(), BP); 1244 return false; 1245 case tok::comma: // #define X(A, 1246 break; 1247 case tok::ellipsis: // #define X(A... -> GCC extension 1248 // Diagnose extension. 1249 Diag(Tok, diag::ext_named_variadic_macro); 1250 1251 // Lex the token after the identifier. 1252 LexUnexpandedToken(Tok); 1253 if (Tok.isNot(tok::r_paren)) { 1254 Diag(Tok, diag::err_pp_missing_rparen_in_macro_def); 1255 return true; 1256 } 1257 1258 MI->setIsGNUVarargs(); 1259 MI->setArgumentList(&Arguments[0], Arguments.size(), BP); 1260 return false; 1261 } 1262 } 1263 } 1264} 1265 1266/// HandleDefineDirective - Implements #define. This consumes the entire macro 1267/// line then lets the caller lex the next real token. 1268void Preprocessor::HandleDefineDirective(Token &DefineTok) { 1269 ++NumDefined; 1270 1271 Token MacroNameTok; 1272 ReadMacroName(MacroNameTok, 1); 1273 1274 // Error reading macro name? If so, diagnostic already issued. 1275 if (MacroNameTok.is(tok::eom)) 1276 return; 1277 1278 Token LastTok = MacroNameTok; 1279 1280 // If we are supposed to keep comments in #defines, reenable comment saving 1281 // mode. 1282 if (CurLexer) CurLexer->SetCommentRetentionState(KeepMacroComments); 1283 1284 // Create the new macro. 1285 MacroInfo *MI = AllocateMacroInfo(MacroNameTok.getLocation()); 1286 1287 Token Tok; 1288 LexUnexpandedToken(Tok); 1289 1290 // If this is a function-like macro definition, parse the argument list, 1291 // marking each of the identifiers as being used as macro arguments. Also, 1292 // check other constraints on the first token of the macro body. 1293 if (Tok.is(tok::eom)) { 1294 // If there is no body to this macro, we have no special handling here. 1295 } else if (Tok.hasLeadingSpace()) { 1296 // This is a normal token with leading space. Clear the leading space 1297 // marker on the first token to get proper expansion. 1298 Tok.clearFlag(Token::LeadingSpace); 1299 } else if (Tok.is(tok::l_paren)) { 1300 // This is a function-like macro definition. Read the argument list. 1301 MI->setIsFunctionLike(); 1302 if (ReadMacroDefinitionArgList(MI)) { 1303 // Forget about MI. 1304 ReleaseMacroInfo(MI); 1305 // Throw away the rest of the line. 1306 if (CurPPLexer->ParsingPreprocessorDirective) 1307 DiscardUntilEndOfDirective(); 1308 return; 1309 } 1310 1311 // If this is a definition of a variadic C99 function-like macro, not using 1312 // the GNU named varargs extension, enabled __VA_ARGS__. 1313 1314 // "Poison" __VA_ARGS__, which can only appear in the expansion of a macro. 1315 // This gets unpoisoned where it is allowed. 1316 assert(Ident__VA_ARGS__->isPoisoned() && "__VA_ARGS__ should be poisoned!"); 1317 if (MI->isC99Varargs()) 1318 Ident__VA_ARGS__->setIsPoisoned(false); 1319 1320 // Read the first token after the arg list for down below. 1321 LexUnexpandedToken(Tok); 1322 } else if (Features.C99) { 1323 // C99 requires whitespace between the macro definition and the body. Emit 1324 // a diagnostic for something like "#define X+". 1325 Diag(Tok, diag::ext_c99_whitespace_required_after_macro_name); 1326 } else { 1327 // C90 6.8 TC1 says: "In the definition of an object-like macro, if the 1328 // first character of a replacement list is not a character required by 1329 // subclause 5.2.1, then there shall be white-space separation between the 1330 // identifier and the replacement list.". 5.2.1 lists this set: 1331 // "A-Za-z0-9!"#%&'()*+,_./:;<=>?[\]^_{|}~" as well as whitespace, which 1332 // is irrelevant here. 1333 bool isInvalid = false; 1334 if (Tok.is(tok::at)) // @ is not in the list above. 1335 isInvalid = true; 1336 else if (Tok.is(tok::unknown)) { 1337 // If we have an unknown token, it is something strange like "`". Since 1338 // all of valid characters would have lexed into a single character 1339 // token of some sort, we know this is not a valid case. 1340 isInvalid = true; 1341 } 1342 if (isInvalid) 1343 Diag(Tok, diag::ext_missing_whitespace_after_macro_name); 1344 else 1345 Diag(Tok, diag::warn_missing_whitespace_after_macro_name); 1346 } 1347 1348 if (!Tok.is(tok::eom)) 1349 LastTok = Tok; 1350 1351 // Read the rest of the macro body. 1352 if (MI->isObjectLike()) { 1353 // Object-like macros are very simple, just read their body. 1354 while (Tok.isNot(tok::eom)) { 1355 LastTok = Tok; 1356 MI->AddTokenToBody(Tok); 1357 // Get the next token of the macro. 1358 LexUnexpandedToken(Tok); 1359 } 1360 1361 } else { 1362 // Otherwise, read the body of a function-like macro. While we are at it, 1363 // check C99 6.10.3.2p1: ensure that # operators are followed by macro 1364 // parameters in function-like macro expansions. 1365 while (Tok.isNot(tok::eom)) { 1366 LastTok = Tok; 1367 1368 if (Tok.isNot(tok::hash)) { 1369 MI->AddTokenToBody(Tok); 1370 1371 // Get the next token of the macro. 1372 LexUnexpandedToken(Tok); 1373 continue; 1374 } 1375 1376 // Get the next token of the macro. 1377 LexUnexpandedToken(Tok); 1378 1379 // Check for a valid macro arg identifier. 1380 if (Tok.getIdentifierInfo() == 0 || 1381 MI->getArgumentNum(Tok.getIdentifierInfo()) == -1) { 1382 1383 // If this is assembler-with-cpp mode, we accept random gibberish after 1384 // the '#' because '#' is often a comment character. However, change 1385 // the kind of the token to tok::unknown so that the preprocessor isn't 1386 // confused. 1387 if (getLangOptions().AsmPreprocessor && Tok.isNot(tok::eom)) { 1388 LastTok.setKind(tok::unknown); 1389 } else { 1390 Diag(Tok, diag::err_pp_stringize_not_parameter); 1391 ReleaseMacroInfo(MI); 1392 1393 // Disable __VA_ARGS__ again. 1394 Ident__VA_ARGS__->setIsPoisoned(true); 1395 return; 1396 } 1397 } 1398 1399 // Things look ok, add the '#' and param name tokens to the macro. 1400 MI->AddTokenToBody(LastTok); 1401 MI->AddTokenToBody(Tok); 1402 LastTok = Tok; 1403 1404 // Get the next token of the macro. 1405 LexUnexpandedToken(Tok); 1406 } 1407 } 1408 1409 1410 // Disable __VA_ARGS__ again. 1411 Ident__VA_ARGS__->setIsPoisoned(true); 1412 1413 // Check that there is no paste (##) operator at the begining or end of the 1414 // replacement list. 1415 unsigned NumTokens = MI->getNumTokens(); 1416 if (NumTokens != 0) { 1417 if (MI->getReplacementToken(0).is(tok::hashhash)) { 1418 Diag(MI->getReplacementToken(0), diag::err_paste_at_start); 1419 ReleaseMacroInfo(MI); 1420 return; 1421 } 1422 if (MI->getReplacementToken(NumTokens-1).is(tok::hashhash)) { 1423 Diag(MI->getReplacementToken(NumTokens-1), diag::err_paste_at_end); 1424 ReleaseMacroInfo(MI); 1425 return; 1426 } 1427 } 1428 1429 // If this is the primary source file, remember that this macro hasn't been 1430 // used yet. 1431 if (isInPrimaryFile()) 1432 MI->setIsUsed(false); 1433 1434 MI->setDefinitionEndLoc(LastTok.getLocation()); 1435 1436 // Finally, if this identifier already had a macro defined for it, verify that 1437 // the macro bodies are identical and free the old definition. 1438 if (MacroInfo *OtherMI = getMacroInfo(MacroNameTok.getIdentifierInfo())) { 1439 // It is very common for system headers to have tons of macro redefinitions 1440 // and for warnings to be disabled in system headers. If this is the case, 1441 // then don't bother calling MacroInfo::isIdenticalTo. 1442 if (!getDiagnostics().getSuppressSystemWarnings() || 1443 !SourceMgr.isInSystemHeader(DefineTok.getLocation())) { 1444 if (!OtherMI->isUsed()) 1445 Diag(OtherMI->getDefinitionLoc(), diag::pp_macro_not_used); 1446 1447 // Macros must be identical. This means all tokes and whitespace 1448 // separation must be the same. C99 6.10.3.2. 1449 if (!MI->isIdenticalTo(*OtherMI, *this)) { 1450 Diag(MI->getDefinitionLoc(), diag::ext_pp_macro_redef) 1451 << MacroNameTok.getIdentifierInfo(); 1452 Diag(OtherMI->getDefinitionLoc(), diag::note_previous_definition); 1453 } 1454 } 1455 1456 ReleaseMacroInfo(OtherMI); 1457 } 1458 1459 setMacroInfo(MacroNameTok.getIdentifierInfo(), MI); 1460 1461 // If the callbacks want to know, tell them about the macro definition. 1462 if (Callbacks) 1463 Callbacks->MacroDefined(MacroNameTok.getIdentifierInfo(), MI); 1464} 1465 1466/// HandleUndefDirective - Implements #undef. 1467/// 1468void Preprocessor::HandleUndefDirective(Token &UndefTok) { 1469 ++NumUndefined; 1470 1471 Token MacroNameTok; 1472 ReadMacroName(MacroNameTok, 2); 1473 1474 // Error reading macro name? If so, diagnostic already issued. 1475 if (MacroNameTok.is(tok::eom)) 1476 return; 1477 1478 // Check to see if this is the last token on the #undef line. 1479 CheckEndOfDirective("undef"); 1480 1481 // Okay, we finally have a valid identifier to undef. 1482 MacroInfo *MI = getMacroInfo(MacroNameTok.getIdentifierInfo()); 1483 1484 // If the macro is not defined, this is a noop undef, just return. 1485 if (MI == 0) return; 1486 1487 if (!MI->isUsed()) 1488 Diag(MI->getDefinitionLoc(), diag::pp_macro_not_used); 1489 1490 // If the callbacks want to know, tell them about the macro #undef. 1491 if (Callbacks) 1492 Callbacks->MacroUndefined(MacroNameTok.getIdentifierInfo(), MI); 1493 1494 // Free macro definition. 1495 ReleaseMacroInfo(MI); 1496 setMacroInfo(MacroNameTok.getIdentifierInfo(), 0); 1497} 1498 1499 1500//===----------------------------------------------------------------------===// 1501// Preprocessor Conditional Directive Handling. 1502//===----------------------------------------------------------------------===// 1503 1504/// HandleIfdefDirective - Implements the #ifdef/#ifndef directive. isIfndef is 1505/// true when this is a #ifndef directive. ReadAnyTokensBeforeDirective is true 1506/// if any tokens have been returned or pp-directives activated before this 1507/// #ifndef has been lexed. 1508/// 1509void Preprocessor::HandleIfdefDirective(Token &Result, bool isIfndef, 1510 bool ReadAnyTokensBeforeDirective) { 1511 ++NumIf; 1512 Token DirectiveTok = Result; 1513 1514 Token MacroNameTok; 1515 ReadMacroName(MacroNameTok); 1516 1517 // Error reading macro name? If so, diagnostic already issued. 1518 if (MacroNameTok.is(tok::eom)) { 1519 // Skip code until we get to #endif. This helps with recovery by not 1520 // emitting an error when the #endif is reached. 1521 SkipExcludedConditionalBlock(DirectiveTok.getLocation(), 1522 /*Foundnonskip*/false, /*FoundElse*/false); 1523 return; 1524 } 1525 1526 // Check to see if this is the last token on the #if[n]def line. 1527 CheckEndOfDirective(isIfndef ? "ifndef" : "ifdef"); 1528 1529 if (CurPPLexer->getConditionalStackDepth() == 0) { 1530 // If the start of a top-level #ifdef, inform MIOpt. 1531 if (!ReadAnyTokensBeforeDirective) { 1532 assert(isIfndef && "#ifdef shouldn't reach here"); 1533 CurPPLexer->MIOpt.EnterTopLevelIFNDEF(MacroNameTok.getIdentifierInfo()); 1534 } else 1535 CurPPLexer->MIOpt.EnterTopLevelConditional(); 1536 } 1537 1538 IdentifierInfo *MII = MacroNameTok.getIdentifierInfo(); 1539 MacroInfo *MI = getMacroInfo(MII); 1540 1541 // If there is a macro, process it. 1542 if (MI) // Mark it used. 1543 MI->setIsUsed(true); 1544 1545 // Should we include the stuff contained by this directive? 1546 if (!MI == isIfndef) { 1547 // Yes, remember that we are inside a conditional, then lex the next token. 1548 CurPPLexer->pushConditionalLevel(DirectiveTok.getLocation(), /*wasskip*/false, 1549 /*foundnonskip*/true, /*foundelse*/false); 1550 } else { 1551 // No, skip the contents of this block and return the first token after it. 1552 SkipExcludedConditionalBlock(DirectiveTok.getLocation(), 1553 /*Foundnonskip*/false, 1554 /*FoundElse*/false); 1555 } 1556} 1557 1558/// HandleIfDirective - Implements the #if directive. 1559/// 1560void Preprocessor::HandleIfDirective(Token &IfToken, 1561 bool ReadAnyTokensBeforeDirective) { 1562 ++NumIf; 1563 1564 // Parse and evaluation the conditional expression. 1565 IdentifierInfo *IfNDefMacro = 0; 1566 bool ConditionalTrue = EvaluateDirectiveExpression(IfNDefMacro); 1567 1568 1569 // If this condition is equivalent to #ifndef X, and if this is the first 1570 // directive seen, handle it for the multiple-include optimization. 1571 if (CurPPLexer->getConditionalStackDepth() == 0) { 1572 if (!ReadAnyTokensBeforeDirective && IfNDefMacro) 1573 CurPPLexer->MIOpt.EnterTopLevelIFNDEF(IfNDefMacro); 1574 else 1575 CurPPLexer->MIOpt.EnterTopLevelConditional(); 1576 } 1577 1578 // Should we include the stuff contained by this directive? 1579 if (ConditionalTrue) { 1580 // Yes, remember that we are inside a conditional, then lex the next token. 1581 CurPPLexer->pushConditionalLevel(IfToken.getLocation(), /*wasskip*/false, 1582 /*foundnonskip*/true, /*foundelse*/false); 1583 } else { 1584 // No, skip the contents of this block and return the first token after it. 1585 SkipExcludedConditionalBlock(IfToken.getLocation(), /*Foundnonskip*/false, 1586 /*FoundElse*/false); 1587 } 1588} 1589 1590/// HandleEndifDirective - Implements the #endif directive. 1591/// 1592void Preprocessor::HandleEndifDirective(Token &EndifToken) { 1593 ++NumEndif; 1594 1595 // Check that this is the whole directive. 1596 CheckEndOfDirective("endif"); 1597 1598 PPConditionalInfo CondInfo; 1599 if (CurPPLexer->popConditionalLevel(CondInfo)) { 1600 // No conditionals on the stack: this is an #endif without an #if. 1601 Diag(EndifToken, diag::err_pp_endif_without_if); 1602 return; 1603 } 1604 1605 // If this the end of a top-level #endif, inform MIOpt. 1606 if (CurPPLexer->getConditionalStackDepth() == 0) 1607 CurPPLexer->MIOpt.ExitTopLevelConditional(); 1608 1609 assert(!CondInfo.WasSkipping && !CurPPLexer->LexingRawMode && 1610 "This code should only be reachable in the non-skipping case!"); 1611} 1612 1613 1614void Preprocessor::HandleElseDirective(Token &Result) { 1615 ++NumElse; 1616 1617 // #else directive in a non-skipping conditional... start skipping. 1618 CheckEndOfDirective("else"); 1619 1620 PPConditionalInfo CI; 1621 if (CurPPLexer->popConditionalLevel(CI)) { 1622 Diag(Result, diag::pp_err_else_without_if); 1623 return; 1624 } 1625 1626 // If this is a top-level #else, inform the MIOpt. 1627 if (CurPPLexer->getConditionalStackDepth() == 0) 1628 CurPPLexer->MIOpt.EnterTopLevelConditional(); 1629 1630 // If this is a #else with a #else before it, report the error. 1631 if (CI.FoundElse) Diag(Result, diag::pp_err_else_after_else); 1632 1633 // Finally, skip the rest of the contents of this block and return the first 1634 // token after it. 1635 return SkipExcludedConditionalBlock(CI.IfLoc, /*Foundnonskip*/true, 1636 /*FoundElse*/true); 1637} 1638 1639void Preprocessor::HandleElifDirective(Token &ElifToken) { 1640 ++NumElse; 1641 1642 // #elif directive in a non-skipping conditional... start skipping. 1643 // We don't care what the condition is, because we will always skip it (since 1644 // the block immediately before it was included). 1645 DiscardUntilEndOfDirective(); 1646 1647 PPConditionalInfo CI; 1648 if (CurPPLexer->popConditionalLevel(CI)) { 1649 Diag(ElifToken, diag::pp_err_elif_without_if); 1650 return; 1651 } 1652 1653 // If this is a top-level #elif, inform the MIOpt. 1654 if (CurPPLexer->getConditionalStackDepth() == 0) 1655 CurPPLexer->MIOpt.EnterTopLevelConditional(); 1656 1657 // If this is a #elif with a #else before it, report the error. 1658 if (CI.FoundElse) Diag(ElifToken, diag::pp_err_elif_after_else); 1659 1660 // Finally, skip the rest of the contents of this block and return the first 1661 // token after it. 1662 return SkipExcludedConditionalBlock(CI.IfLoc, /*Foundnonskip*/true, 1663 /*FoundElse*/CI.FoundElse); 1664} 1665 1666