Preprocessor.cpp revision 872a45e91778eb0b706ff57272fe547d4512eb19
1//===--- Preprocess.cpp - C Language Family Preprocessor Implementation ---===// 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 the Preprocessor interface. 11// 12//===----------------------------------------------------------------------===// 13// 14// Options to support: 15// -H - Print the name of each header file used. 16// -d[DNI] - Dump various things. 17// -fworking-directory - #line's with preprocessor's working dir. 18// -fpreprocessed 19// -dependency-file,-M,-MM,-MF,-MG,-MP,-MT,-MQ,-MD,-MMD 20// -W* 21// -w 22// 23// Messages to emit: 24// "Multiple include guards may be useful for:\n" 25// 26//===----------------------------------------------------------------------===// 27 28#include "clang/Lex/Preprocessor.h" 29#include "MacroArgs.h" 30#include "clang/Lex/ExternalPreprocessorSource.h" 31#include "clang/Lex/HeaderSearch.h" 32#include "clang/Lex/MacroInfo.h" 33#include "clang/Lex/Pragma.h" 34#include "clang/Lex/PreprocessingRecord.h" 35#include "clang/Lex/ScratchBuffer.h" 36#include "clang/Lex/LexDiagnostic.h" 37#include "clang/Lex/CodeCompletionHandler.h" 38#include "clang/Basic/SourceManager.h" 39#include "clang/Basic/FileManager.h" 40#include "clang/Basic/TargetInfo.h" 41#include "llvm/ADT/APFloat.h" 42#include "llvm/ADT/SmallVector.h" 43#include "llvm/Support/MemoryBuffer.h" 44#include "llvm/Support/raw_ostream.h" 45using namespace clang; 46 47//===----------------------------------------------------------------------===// 48ExternalPreprocessorSource::~ExternalPreprocessorSource() { } 49 50Preprocessor::Preprocessor(Diagnostic &diags, const LangOptions &opts, 51 const TargetInfo &target, SourceManager &SM, 52 HeaderSearch &Headers, 53 IdentifierInfoLookup* IILookup, 54 bool OwnsHeaders) 55 : Diags(&diags), Features(opts), Target(target),FileMgr(Headers.getFileMgr()), 56 FileSystemOpts(Headers.getFileSystemOpts()), SourceMgr(SM), 57 HeaderInfo(Headers), ExternalSource(0), 58 Identifiers(opts, IILookup), BuiltinInfo(Target), CodeComplete(0), 59 CodeCompletionFile(0), SkipMainFilePreamble(0, true), CurPPLexer(0), 60 CurDirLookup(0), Callbacks(0), MacroArgCache(0), Record(0), MIChainHead(0), 61 MICache(0) { 62 ScratchBuf = new ScratchBuffer(SourceMgr); 63 CounterValue = 0; // __COUNTER__ starts at 0. 64 OwnsHeaderSearch = OwnsHeaders; 65 66 // Clear stats. 67 NumDirectives = NumDefined = NumUndefined = NumPragma = 0; 68 NumIf = NumElse = NumEndif = 0; 69 NumEnteredSourceFiles = 0; 70 NumMacroExpanded = NumFnMacroExpanded = NumBuiltinMacroExpanded = 0; 71 NumFastMacroExpanded = NumTokenPaste = NumFastTokenPaste = 0; 72 MaxIncludeStackDepth = 0; 73 NumSkipped = 0; 74 75 // Default to discarding comments. 76 KeepComments = false; 77 KeepMacroComments = false; 78 79 // Macro expansion is enabled. 80 DisableMacroExpansion = false; 81 InMacroArgs = false; 82 NumCachedTokenLexers = 0; 83 84 CachedLexPos = 0; 85 86 // We haven't read anything from the external source. 87 ReadMacrosFromExternalSource = false; 88 89 // "Poison" __VA_ARGS__, which can only appear in the expansion of a macro. 90 // This gets unpoisoned where it is allowed. 91 (Ident__VA_ARGS__ = getIdentifierInfo("__VA_ARGS__"))->setIsPoisoned(); 92 93 // Initialize the pragma handlers. 94 PragmaHandlers = new PragmaNamespace(llvm::StringRef()); 95 RegisterBuiltinPragmas(); 96 97 // Initialize builtin macros like __LINE__ and friends. 98 RegisterBuiltinMacros(); 99} 100 101Preprocessor::~Preprocessor() { 102 assert(BacktrackPositions.empty() && "EnableBacktrack/Backtrack imbalance!"); 103 104 while (!IncludeMacroStack.empty()) { 105 delete IncludeMacroStack.back().TheLexer; 106 delete IncludeMacroStack.back().TheTokenLexer; 107 IncludeMacroStack.pop_back(); 108 } 109 110 // Free any macro definitions. 111 for (MacroInfoChain *I = MIChainHead ; I ; I = I->Next) 112 I->MI.Destroy(); 113 114 // Free any cached macro expanders. 115 for (unsigned i = 0, e = NumCachedTokenLexers; i != e; ++i) 116 delete TokenLexerCache[i]; 117 118 // Free any cached MacroArgs. 119 for (MacroArgs *ArgList = MacroArgCache; ArgList; ) 120 ArgList = ArgList->deallocate(); 121 122 // Release pragma information. 123 delete PragmaHandlers; 124 125 // Delete the scratch buffer info. 126 delete ScratchBuf; 127 128 // Delete the header search info, if we own it. 129 if (OwnsHeaderSearch) 130 delete &HeaderInfo; 131 132 delete Callbacks; 133} 134 135void Preprocessor::setPTHManager(PTHManager* pm) { 136 PTH.reset(pm); 137 FileMgr.addStatCache(PTH->createStatCache()); 138} 139 140void Preprocessor::DumpToken(const Token &Tok, bool DumpFlags) const { 141 llvm::errs() << tok::getTokenName(Tok.getKind()) << " '" 142 << getSpelling(Tok) << "'"; 143 144 if (!DumpFlags) return; 145 146 llvm::errs() << "\t"; 147 if (Tok.isAtStartOfLine()) 148 llvm::errs() << " [StartOfLine]"; 149 if (Tok.hasLeadingSpace()) 150 llvm::errs() << " [LeadingSpace]"; 151 if (Tok.isExpandDisabled()) 152 llvm::errs() << " [ExpandDisabled]"; 153 if (Tok.needsCleaning()) { 154 const char *Start = SourceMgr.getCharacterData(Tok.getLocation()); 155 llvm::errs() << " [UnClean='" << llvm::StringRef(Start, Tok.getLength()) 156 << "']"; 157 } 158 159 llvm::errs() << "\tLoc=<"; 160 DumpLocation(Tok.getLocation()); 161 llvm::errs() << ">"; 162} 163 164void Preprocessor::DumpLocation(SourceLocation Loc) const { 165 Loc.dump(SourceMgr); 166} 167 168void Preprocessor::DumpMacro(const MacroInfo &MI) const { 169 llvm::errs() << "MACRO: "; 170 for (unsigned i = 0, e = MI.getNumTokens(); i != e; ++i) { 171 DumpToken(MI.getReplacementToken(i)); 172 llvm::errs() << " "; 173 } 174 llvm::errs() << "\n"; 175} 176 177void Preprocessor::PrintStats() { 178 llvm::errs() << "\n*** Preprocessor Stats:\n"; 179 llvm::errs() << NumDirectives << " directives found:\n"; 180 llvm::errs() << " " << NumDefined << " #define.\n"; 181 llvm::errs() << " " << NumUndefined << " #undef.\n"; 182 llvm::errs() << " #include/#include_next/#import:\n"; 183 llvm::errs() << " " << NumEnteredSourceFiles << " source files entered.\n"; 184 llvm::errs() << " " << MaxIncludeStackDepth << " max include stack depth\n"; 185 llvm::errs() << " " << NumIf << " #if/#ifndef/#ifdef.\n"; 186 llvm::errs() << " " << NumElse << " #else/#elif.\n"; 187 llvm::errs() << " " << NumEndif << " #endif.\n"; 188 llvm::errs() << " " << NumPragma << " #pragma.\n"; 189 llvm::errs() << NumSkipped << " #if/#ifndef#ifdef regions skipped\n"; 190 191 llvm::errs() << NumMacroExpanded << "/" << NumFnMacroExpanded << "/" 192 << NumBuiltinMacroExpanded << " obj/fn/builtin macros expanded, " 193 << NumFastMacroExpanded << " on the fast path.\n"; 194 llvm::errs() << (NumFastTokenPaste+NumTokenPaste) 195 << " token paste (##) operations performed, " 196 << NumFastTokenPaste << " on the fast path.\n"; 197} 198 199Preprocessor::macro_iterator 200Preprocessor::macro_begin(bool IncludeExternalMacros) const { 201 if (IncludeExternalMacros && ExternalSource && 202 !ReadMacrosFromExternalSource) { 203 ReadMacrosFromExternalSource = true; 204 ExternalSource->ReadDefinedMacros(); 205 } 206 207 return Macros.begin(); 208} 209 210Preprocessor::macro_iterator 211Preprocessor::macro_end(bool IncludeExternalMacros) const { 212 if (IncludeExternalMacros && ExternalSource && 213 !ReadMacrosFromExternalSource) { 214 ReadMacrosFromExternalSource = true; 215 ExternalSource->ReadDefinedMacros(); 216 } 217 218 return Macros.end(); 219} 220 221bool Preprocessor::SetCodeCompletionPoint(const FileEntry *File, 222 unsigned TruncateAtLine, 223 unsigned TruncateAtColumn) { 224 using llvm::MemoryBuffer; 225 226 CodeCompletionFile = File; 227 228 // Okay to clear out the code-completion point by passing NULL. 229 if (!CodeCompletionFile) 230 return false; 231 232 // Load the actual file's contents. 233 bool Invalid = false; 234 const MemoryBuffer *Buffer = SourceMgr.getMemoryBufferForFile(File, &Invalid); 235 if (Invalid) 236 return true; 237 238 // Find the byte position of the truncation point. 239 const char *Position = Buffer->getBufferStart(); 240 for (unsigned Line = 1; Line < TruncateAtLine; ++Line) { 241 for (; *Position; ++Position) { 242 if (*Position != '\r' && *Position != '\n') 243 continue; 244 245 // Eat \r\n or \n\r as a single line. 246 if ((Position[1] == '\r' || Position[1] == '\n') && 247 Position[0] != Position[1]) 248 ++Position; 249 ++Position; 250 break; 251 } 252 } 253 254 Position += TruncateAtColumn - 1; 255 256 // Truncate the buffer. 257 if (Position < Buffer->getBufferEnd()) { 258 llvm::StringRef Data(Buffer->getBufferStart(), 259 Position-Buffer->getBufferStart()); 260 MemoryBuffer *TruncatedBuffer 261 = MemoryBuffer::getMemBufferCopy(Data, Buffer->getBufferIdentifier()); 262 SourceMgr.overrideFileContents(File, TruncatedBuffer); 263 } 264 265 return false; 266} 267 268bool Preprocessor::isCodeCompletionFile(SourceLocation FileLoc) const { 269 return CodeCompletionFile && FileLoc.isFileID() && 270 SourceMgr.getFileEntryForID(SourceMgr.getFileID(FileLoc)) 271 == CodeCompletionFile; 272} 273 274void Preprocessor::CodeCompleteNaturalLanguage() { 275 SetCodeCompletionPoint(0, 0, 0); 276 getDiagnostics().setSuppressAllDiagnostics(true); 277 if (CodeComplete) 278 CodeComplete->CodeCompleteNaturalLanguage(); 279} 280 281//===----------------------------------------------------------------------===// 282// Token Spelling 283//===----------------------------------------------------------------------===// 284 285/// getSpelling() - Return the 'spelling' of this token. The spelling of a 286/// token are the characters used to represent the token in the source file 287/// after trigraph expansion and escaped-newline folding. In particular, this 288/// wants to get the true, uncanonicalized, spelling of things like digraphs 289/// UCNs, etc. 290std::string Preprocessor::getSpelling(const Token &Tok, 291 const SourceManager &SourceMgr, 292 const LangOptions &Features, 293 bool *Invalid) { 294 assert((int)Tok.getLength() >= 0 && "Token character range is bogus!"); 295 296 // If this token contains nothing interesting, return it directly. 297 bool CharDataInvalid = false; 298 const char* TokStart = SourceMgr.getCharacterData(Tok.getLocation(), 299 &CharDataInvalid); 300 if (Invalid) 301 *Invalid = CharDataInvalid; 302 if (CharDataInvalid) 303 return std::string(); 304 305 if (!Tok.needsCleaning()) 306 return std::string(TokStart, TokStart+Tok.getLength()); 307 308 std::string Result; 309 Result.reserve(Tok.getLength()); 310 311 // Otherwise, hard case, relex the characters into the string. 312 for (const char *Ptr = TokStart, *End = TokStart+Tok.getLength(); 313 Ptr != End; ) { 314 unsigned CharSize; 315 Result.push_back(Lexer::getCharAndSizeNoWarn(Ptr, CharSize, Features)); 316 Ptr += CharSize; 317 } 318 assert(Result.size() != unsigned(Tok.getLength()) && 319 "NeedsCleaning flag set on something that didn't need cleaning!"); 320 return Result; 321} 322 323/// getSpelling() - Return the 'spelling' of this token. The spelling of a 324/// token are the characters used to represent the token in the source file 325/// after trigraph expansion and escaped-newline folding. In particular, this 326/// wants to get the true, uncanonicalized, spelling of things like digraphs 327/// UCNs, etc. 328std::string Preprocessor::getSpelling(const Token &Tok, bool *Invalid) const { 329 return getSpelling(Tok, SourceMgr, Features, Invalid); 330} 331 332/// getSpelling - This method is used to get the spelling of a token into a 333/// preallocated buffer, instead of as an std::string. The caller is required 334/// to allocate enough space for the token, which is guaranteed to be at least 335/// Tok.getLength() bytes long. The actual length of the token is returned. 336/// 337/// Note that this method may do two possible things: it may either fill in 338/// the buffer specified with characters, or it may *change the input pointer* 339/// to point to a constant buffer with the data already in it (avoiding a 340/// copy). The caller is not allowed to modify the returned buffer pointer 341/// if an internal buffer is returned. 342unsigned Preprocessor::getSpelling(const Token &Tok, const char *&Buffer, 343 const SourceManager &SourceMgr, 344 const LangOptions &Features, 345 bool *Invalid) { 346 assert((int)Tok.getLength() >= 0 && "Token character range is bogus!"); 347 348 // If this token is an identifier, just return the string from the identifier 349 // table, which is very quick. 350 if (const IdentifierInfo *II = Tok.getIdentifierInfo()) { 351 Buffer = II->getNameStart(); 352 return II->getLength(); 353 } 354 355 // Otherwise, compute the start of the token in the input lexer buffer. 356 const char *TokStart = 0; 357 358 if (Tok.isLiteral()) 359 TokStart = Tok.getLiteralData(); 360 361 if (TokStart == 0) { 362 bool CharDataInvalid = false; 363 TokStart = SourceMgr.getCharacterData(Tok.getLocation(), &CharDataInvalid); 364 if (Invalid) 365 *Invalid = CharDataInvalid; 366 if (CharDataInvalid) { 367 Buffer = ""; 368 return 0; 369 } 370 } 371 372 // If this token contains nothing interesting, return it directly. 373 if (!Tok.needsCleaning()) { 374 Buffer = TokStart; 375 return Tok.getLength(); 376 } 377 378 // Otherwise, hard case, relex the characters into the string. 379 char *OutBuf = const_cast<char*>(Buffer); 380 for (const char *Ptr = TokStart, *End = TokStart+Tok.getLength(); 381 Ptr != End; ) { 382 unsigned CharSize; 383 *OutBuf++ = Lexer::getCharAndSizeNoWarn(Ptr, CharSize, Features); 384 Ptr += CharSize; 385 } 386 assert(unsigned(OutBuf-Buffer) != Tok.getLength() && 387 "NeedsCleaning flag set on something that didn't need cleaning!"); 388 389 return OutBuf-Buffer; 390} 391 392/// getSpelling - This method is used to get the spelling of a token into a 393/// SmallVector. Note that the returned StringRef may not point to the 394/// supplied buffer if a copy can be avoided. 395llvm::StringRef Preprocessor::getSpelling(const Token &Tok, 396 llvm::SmallVectorImpl<char> &Buffer, 397 bool *Invalid) const { 398 // Try the fast path. 399 if (const IdentifierInfo *II = Tok.getIdentifierInfo()) 400 return II->getName(); 401 402 // Resize the buffer if we need to copy into it. 403 if (Tok.needsCleaning()) 404 Buffer.resize(Tok.getLength()); 405 406 const char *Ptr = Buffer.data(); 407 unsigned Len = getSpelling(Tok, Ptr, Invalid); 408 return llvm::StringRef(Ptr, Len); 409} 410 411/// CreateString - Plop the specified string into a scratch buffer and return a 412/// location for it. If specified, the source location provides a source 413/// location for the token. 414void Preprocessor::CreateString(const char *Buf, unsigned Len, Token &Tok, 415 SourceLocation InstantiationLoc) { 416 Tok.setLength(Len); 417 418 const char *DestPtr; 419 SourceLocation Loc = ScratchBuf->getToken(Buf, Len, DestPtr); 420 421 if (InstantiationLoc.isValid()) 422 Loc = SourceMgr.createInstantiationLoc(Loc, InstantiationLoc, 423 InstantiationLoc, Len); 424 Tok.setLocation(Loc); 425 426 // If this is a literal token, set the pointer data. 427 if (Tok.isLiteral()) 428 Tok.setLiteralData(DestPtr); 429} 430 431 432/// AdvanceToTokenCharacter - Given a location that specifies the start of a 433/// token, return a new location that specifies a character within the token. 434FullSourceLoc Preprocessor::AdvanceToTokenCharacter(FullSourceLoc TokStart, 435 unsigned CharNo, 436 const LangOptions &Features) { 437 // Figure out how many physical characters away the specified instantiation 438 // character is. This needs to take into consideration newlines and 439 // trigraphs. 440 bool Invalid = false; 441 const char *TokPtr = TokStart.getCharacterData(&Invalid); 442 443 // If they request the first char of the token, we're trivially done. 444 if (Invalid || (CharNo == 0 && Lexer::isObviouslySimpleCharacter(*TokPtr))) 445 return TokStart; 446 447 unsigned PhysOffset = 0; 448 449 // The usual case is that tokens don't contain anything interesting. Skip 450 // over the uninteresting characters. If a token only consists of simple 451 // chars, this method is extremely fast. 452 while (Lexer::isObviouslySimpleCharacter(*TokPtr)) { 453 if (CharNo == 0) 454 return FullSourceLoc(TokStart.getFileLocWithOffset(PhysOffset), 455 TokStart.getManager()); 456 ++TokPtr, --CharNo, ++PhysOffset; 457 } 458 459 // If we have a character that may be a trigraph or escaped newline, use a 460 // lexer to parse it correctly. 461 for (; CharNo; --CharNo) { 462 unsigned Size; 463 Lexer::getCharAndSizeNoWarn(TokPtr, Size, Features); 464 TokPtr += Size; 465 PhysOffset += Size; 466 } 467 468 // Final detail: if we end up on an escaped newline, we want to return the 469 // location of the actual byte of the token. For example foo\<newline>bar 470 // advanced by 3 should return the location of b, not of \\. One compounding 471 // detail of this is that the escape may be made by a trigraph. 472 if (!Lexer::isObviouslySimpleCharacter(*TokPtr)) 473 PhysOffset += Lexer::SkipEscapedNewLines(TokPtr)-TokPtr; 474 475 return FullSourceLoc(TokStart.getFileLocWithOffset(PhysOffset), 476 TokStart.getManager()); 477} 478 479SourceLocation Preprocessor::getLocForEndOfToken(SourceLocation Loc, 480 unsigned Offset) { 481 if (Loc.isInvalid() || !Loc.isFileID()) 482 return SourceLocation(); 483 484 unsigned Len = Lexer::MeasureTokenLength(Loc, getSourceManager(), Features); 485 if (Len > Offset) 486 Len = Len - Offset; 487 else 488 return Loc; 489 490 return AdvanceToTokenCharacter(Loc, Len); 491} 492 493 494 495//===----------------------------------------------------------------------===// 496// Preprocessor Initialization Methods 497//===----------------------------------------------------------------------===// 498 499 500/// EnterMainSourceFile - Enter the specified FileID as the main source file, 501/// which implicitly adds the builtin defines etc. 502void Preprocessor::EnterMainSourceFile() { 503 // We do not allow the preprocessor to reenter the main file. Doing so will 504 // cause FileID's to accumulate information from both runs (e.g. #line 505 // information) and predefined macros aren't guaranteed to be set properly. 506 assert(NumEnteredSourceFiles == 0 && "Cannot reenter the main file!"); 507 FileID MainFileID = SourceMgr.getMainFileID(); 508 509 // Enter the main file source buffer. 510 EnterSourceFile(MainFileID, 0, SourceLocation()); 511 512 // If we've been asked to skip bytes in the main file (e.g., as part of a 513 // precompiled preamble), do so now. 514 if (SkipMainFilePreamble.first > 0) 515 CurLexer->SkipBytes(SkipMainFilePreamble.first, 516 SkipMainFilePreamble.second); 517 518 // Tell the header info that the main file was entered. If the file is later 519 // #imported, it won't be re-entered. 520 if (const FileEntry *FE = SourceMgr.getFileEntryForID(MainFileID)) 521 HeaderInfo.IncrementIncludeCount(FE); 522 523 // Preprocess Predefines to populate the initial preprocessor state. 524 llvm::MemoryBuffer *SB = 525 llvm::MemoryBuffer::getMemBufferCopy(Predefines, "<built-in>"); 526 assert(SB && "Cannot create predefined source buffer"); 527 FileID FID = SourceMgr.createFileIDForMemBuffer(SB); 528 assert(!FID.isInvalid() && "Could not create FileID for predefines?"); 529 530 // Start parsing the predefines. 531 EnterSourceFile(FID, 0, SourceLocation()); 532} 533 534void Preprocessor::EndSourceFile() { 535 // Notify the client that we reached the end of the source file. 536 if (Callbacks) 537 Callbacks->EndOfMainFile(); 538} 539 540//===----------------------------------------------------------------------===// 541// Lexer Event Handling. 542//===----------------------------------------------------------------------===// 543 544/// LookUpIdentifierInfo - Given a tok::identifier token, look up the 545/// identifier information for the token and install it into the token. 546IdentifierInfo *Preprocessor::LookUpIdentifierInfo(Token &Identifier, 547 const char *BufPtr) const { 548 assert(Identifier.is(tok::identifier) && "Not an identifier!"); 549 assert(Identifier.getIdentifierInfo() == 0 && "Identinfo already exists!"); 550 551 // Look up this token, see if it is a macro, or if it is a language keyword. 552 IdentifierInfo *II; 553 if (BufPtr && !Identifier.needsCleaning()) { 554 // No cleaning needed, just use the characters from the lexed buffer. 555 II = getIdentifierInfo(llvm::StringRef(BufPtr, Identifier.getLength())); 556 } else { 557 // Cleaning needed, alloca a buffer, clean into it, then use the buffer. 558 llvm::SmallString<64> IdentifierBuffer; 559 llvm::StringRef CleanedStr = getSpelling(Identifier, IdentifierBuffer); 560 II = getIdentifierInfo(CleanedStr); 561 } 562 Identifier.setIdentifierInfo(II); 563 return II; 564} 565 566 567/// HandleIdentifier - This callback is invoked when the lexer reads an 568/// identifier. This callback looks up the identifier in the map and/or 569/// potentially macro expands it or turns it into a named token (like 'for'). 570/// 571/// Note that callers of this method are guarded by checking the 572/// IdentifierInfo's 'isHandleIdentifierCase' bit. If this method changes, the 573/// IdentifierInfo methods that compute these properties will need to change to 574/// match. 575void Preprocessor::HandleIdentifier(Token &Identifier) { 576 assert(Identifier.getIdentifierInfo() && 577 "Can't handle identifiers without identifier info!"); 578 579 IdentifierInfo &II = *Identifier.getIdentifierInfo(); 580 581 // If this identifier was poisoned, and if it was not produced from a macro 582 // expansion, emit an error. 583 if (II.isPoisoned() && CurPPLexer) { 584 if (&II != Ident__VA_ARGS__) // We warn about __VA_ARGS__ with poisoning. 585 Diag(Identifier, diag::err_pp_used_poisoned_id); 586 else 587 Diag(Identifier, diag::ext_pp_bad_vaargs_use); 588 } 589 590 // If this is a macro to be expanded, do it. 591 if (MacroInfo *MI = getMacroInfo(&II)) { 592 if (!DisableMacroExpansion && !Identifier.isExpandDisabled()) { 593 if (MI->isEnabled()) { 594 if (!HandleMacroExpandedIdentifier(Identifier, MI)) 595 return; 596 } else { 597 // C99 6.10.3.4p2 says that a disabled macro may never again be 598 // expanded, even if it's in a context where it could be expanded in the 599 // future. 600 Identifier.setFlag(Token::DisableExpand); 601 } 602 } 603 } 604 605 // C++ 2.11p2: If this is an alternative representation of a C++ operator, 606 // then we act as if it is the actual operator and not the textual 607 // representation of it. 608 if (II.isCPlusPlusOperatorKeyword()) 609 Identifier.setIdentifierInfo(0); 610 611 // If this is an extension token, diagnose its use. 612 // We avoid diagnosing tokens that originate from macro definitions. 613 // FIXME: This warning is disabled in cases where it shouldn't be, 614 // like "#define TY typeof", "TY(1) x". 615 if (II.isExtensionToken() && !DisableMacroExpansion) 616 Diag(Identifier, diag::ext_token_used); 617} 618 619void Preprocessor::AddCommentHandler(CommentHandler *Handler) { 620 assert(Handler && "NULL comment handler"); 621 assert(std::find(CommentHandlers.begin(), CommentHandlers.end(), Handler) == 622 CommentHandlers.end() && "Comment handler already registered"); 623 CommentHandlers.push_back(Handler); 624} 625 626void Preprocessor::RemoveCommentHandler(CommentHandler *Handler) { 627 std::vector<CommentHandler *>::iterator Pos 628 = std::find(CommentHandlers.begin(), CommentHandlers.end(), Handler); 629 assert(Pos != CommentHandlers.end() && "Comment handler not registered"); 630 CommentHandlers.erase(Pos); 631} 632 633bool Preprocessor::HandleComment(Token &result, SourceRange Comment) { 634 bool AnyPendingTokens = false; 635 for (std::vector<CommentHandler *>::iterator H = CommentHandlers.begin(), 636 HEnd = CommentHandlers.end(); 637 H != HEnd; ++H) { 638 if ((*H)->HandleComment(*this, Comment)) 639 AnyPendingTokens = true; 640 } 641 if (!AnyPendingTokens || getCommentRetentionState()) 642 return false; 643 Lex(result); 644 return true; 645} 646 647CommentHandler::~CommentHandler() { } 648 649CodeCompletionHandler::~CodeCompletionHandler() { } 650 651void Preprocessor::createPreprocessingRecord() { 652 if (Record) 653 return; 654 655 Record = new PreprocessingRecord; 656 addPPCallbacks(Record); 657} 658