1 //===--- PrintPreprocessedOutput.cpp - Implement the -E mode --------------===// 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 code simply runs the preprocessor on the input file and prints out the 11 // result. This is the traditional behavior of the -E option. 12 // 13 //===----------------------------------------------------------------------===// 14 15 #include "clang/Frontend/Utils.h" 16 #include "clang/Basic/Diagnostic.h" 17 #include "clang/Basic/SourceManager.h" 18 #include "clang/Frontend/PreprocessorOutputOptions.h" 19 #include "clang/Lex/MacroInfo.h" 20 #include "clang/Lex/PPCallbacks.h" 21 #include "clang/Lex/Pragma.h" 22 #include "clang/Lex/Preprocessor.h" 23 #include "clang/Lex/TokenConcatenation.h" 24 #include "llvm/ADT/STLExtras.h" 25 #include "llvm/ADT/SmallString.h" 26 #include "llvm/ADT/StringRef.h" 27 #include "llvm/Support/ErrorHandling.h" 28 #include "llvm/Support/raw_ostream.h" 29 #include <cctype> 30 #include <cstdio> 31 using namespace clang; 32 33 /// PrintMacroDefinition - Print a macro definition in a form that will be 34 /// properly accepted back as a definition. 35 static void PrintMacroDefinition(const IdentifierInfo &II, const MacroInfo &MI, 36 Preprocessor &PP, raw_ostream &OS) { 37 OS << "#define " << II.getName(); 38 39 if (MI.isFunctionLike()) { 40 OS << '('; 41 if (!MI.arg_empty()) { 42 MacroInfo::arg_iterator AI = MI.arg_begin(), E = MI.arg_end(); 43 for (; AI+1 != E; ++AI) { 44 OS << (*AI)->getName(); 45 OS << ','; 46 } 47 48 // Last argument. 49 if ((*AI)->getName() == "__VA_ARGS__") 50 OS << "..."; 51 else 52 OS << (*AI)->getName(); 53 } 54 55 if (MI.isGNUVarargs()) 56 OS << "..."; // #define foo(x...) 57 58 OS << ')'; 59 } 60 61 // GCC always emits a space, even if the macro body is empty. However, do not 62 // want to emit two spaces if the first token has a leading space. 63 if (MI.tokens_empty() || !MI.tokens_begin()->hasLeadingSpace()) 64 OS << ' '; 65 66 SmallString<128> SpellingBuffer; 67 for (MacroInfo::tokens_iterator I = MI.tokens_begin(), E = MI.tokens_end(); 68 I != E; ++I) { 69 if (I->hasLeadingSpace()) 70 OS << ' '; 71 72 OS << PP.getSpelling(*I, SpellingBuffer); 73 } 74 } 75 76 //===----------------------------------------------------------------------===// 77 // Preprocessed token printer 78 //===----------------------------------------------------------------------===// 79 80 namespace { 81 class PrintPPOutputPPCallbacks : public PPCallbacks { 82 Preprocessor &PP; 83 SourceManager &SM; 84 TokenConcatenation ConcatInfo; 85 public: 86 raw_ostream &OS; 87 private: 88 unsigned CurLine; 89 90 bool EmittedTokensOnThisLine; 91 bool EmittedDirectiveOnThisLine; 92 SrcMgr::CharacteristicKind FileType; 93 SmallString<512> CurFilename; 94 bool Initialized; 95 bool DisableLineMarkers; 96 bool DumpDefines; 97 bool UseLineDirective; 98 bool IsFirstFileEntered; 99 public: 100 PrintPPOutputPPCallbacks(Preprocessor &pp, raw_ostream &os, 101 bool lineMarkers, bool defines) 102 : PP(pp), SM(PP.getSourceManager()), 103 ConcatInfo(PP), OS(os), DisableLineMarkers(lineMarkers), 104 DumpDefines(defines) { 105 CurLine = 0; 106 CurFilename += "<uninit>"; 107 EmittedTokensOnThisLine = false; 108 EmittedDirectiveOnThisLine = false; 109 FileType = SrcMgr::C_User; 110 Initialized = false; 111 IsFirstFileEntered = false; 112 113 // If we're in microsoft mode, use normal #line instead of line markers. 114 UseLineDirective = PP.getLangOpts().MicrosoftExt; 115 } 116 117 void setEmittedTokensOnThisLine() { EmittedTokensOnThisLine = true; } 118 bool hasEmittedTokensOnThisLine() const { return EmittedTokensOnThisLine; } 119 120 void setEmittedDirectiveOnThisLine() { EmittedDirectiveOnThisLine = true; } 121 bool hasEmittedDirectiveOnThisLine() const { 122 return EmittedDirectiveOnThisLine; 123 } 124 125 bool startNewLineIfNeeded(bool ShouldUpdateCurrentLine = true); 126 127 virtual void FileChanged(SourceLocation Loc, FileChangeReason Reason, 128 SrcMgr::CharacteristicKind FileType, 129 FileID PrevFID); 130 virtual void Ident(SourceLocation Loc, const std::string &str); 131 virtual void PragmaComment(SourceLocation Loc, const IdentifierInfo *Kind, 132 const std::string &Str); 133 virtual void PragmaMessage(SourceLocation Loc, StringRef Str); 134 virtual void PragmaDiagnosticPush(SourceLocation Loc, 135 StringRef Namespace); 136 virtual void PragmaDiagnosticPop(SourceLocation Loc, 137 StringRef Namespace); 138 virtual void PragmaDiagnostic(SourceLocation Loc, StringRef Namespace, 139 diag::Mapping Map, StringRef Str); 140 141 bool HandleFirstTokOnLine(Token &Tok); 142 bool MoveToLine(SourceLocation Loc) { 143 PresumedLoc PLoc = SM.getPresumedLoc(Loc); 144 if (PLoc.isInvalid()) 145 return false; 146 return MoveToLine(PLoc.getLine()); 147 } 148 bool MoveToLine(unsigned LineNo); 149 150 bool AvoidConcat(const Token &PrevPrevTok, const Token &PrevTok, 151 const Token &Tok) { 152 return ConcatInfo.AvoidConcat(PrevPrevTok, PrevTok, Tok); 153 } 154 void WriteLineInfo(unsigned LineNo, const char *Extra=0, unsigned ExtraLen=0); 155 bool LineMarkersAreDisabled() const { return DisableLineMarkers; } 156 void HandleNewlinesInToken(const char *TokStr, unsigned Len); 157 158 /// MacroDefined - This hook is called whenever a macro definition is seen. 159 void MacroDefined(const Token &MacroNameTok, const MacroInfo *MI); 160 161 /// MacroUndefined - This hook is called whenever a macro #undef is seen. 162 void MacroUndefined(const Token &MacroNameTok, const MacroInfo *MI); 163 }; 164 } // end anonymous namespace 165 166 void PrintPPOutputPPCallbacks::WriteLineInfo(unsigned LineNo, 167 const char *Extra, 168 unsigned ExtraLen) { 169 startNewLineIfNeeded(/*ShouldUpdateCurrentLine=*/false); 170 171 // Emit #line directives or GNU line markers depending on what mode we're in. 172 if (UseLineDirective) { 173 OS << "#line" << ' ' << LineNo << ' ' << '"'; 174 OS.write(CurFilename.data(), CurFilename.size()); 175 OS << '"'; 176 } else { 177 OS << '#' << ' ' << LineNo << ' ' << '"'; 178 OS.write(CurFilename.data(), CurFilename.size()); 179 OS << '"'; 180 181 if (ExtraLen) 182 OS.write(Extra, ExtraLen); 183 184 if (FileType == SrcMgr::C_System) 185 OS.write(" 3", 2); 186 else if (FileType == SrcMgr::C_ExternCSystem) 187 OS.write(" 3 4", 4); 188 } 189 OS << '\n'; 190 } 191 192 /// MoveToLine - Move the output to the source line specified by the location 193 /// object. We can do this by emitting some number of \n's, or be emitting a 194 /// #line directive. This returns false if already at the specified line, true 195 /// if some newlines were emitted. 196 bool PrintPPOutputPPCallbacks::MoveToLine(unsigned LineNo) { 197 // If this line is "close enough" to the original line, just print newlines, 198 // otherwise print a #line directive. 199 if (LineNo-CurLine <= 8) { 200 if (LineNo-CurLine == 1) 201 OS << '\n'; 202 else if (LineNo == CurLine) 203 return false; // Spelling line moved, but expansion line didn't. 204 else { 205 const char *NewLines = "\n\n\n\n\n\n\n\n"; 206 OS.write(NewLines, LineNo-CurLine); 207 } 208 } else if (!DisableLineMarkers) { 209 // Emit a #line or line marker. 210 WriteLineInfo(LineNo, 0, 0); 211 } else { 212 // Okay, we're in -P mode, which turns off line markers. However, we still 213 // need to emit a newline between tokens on different lines. 214 startNewLineIfNeeded(/*ShouldUpdateCurrentLine=*/false); 215 } 216 217 CurLine = LineNo; 218 return true; 219 } 220 221 bool 222 PrintPPOutputPPCallbacks::startNewLineIfNeeded(bool ShouldUpdateCurrentLine) { 223 if (EmittedTokensOnThisLine || EmittedDirectiveOnThisLine) { 224 OS << '\n'; 225 EmittedTokensOnThisLine = false; 226 EmittedDirectiveOnThisLine = false; 227 if (ShouldUpdateCurrentLine) 228 ++CurLine; 229 return true; 230 } 231 232 return false; 233 } 234 235 /// FileChanged - Whenever the preprocessor enters or exits a #include file 236 /// it invokes this handler. Update our conception of the current source 237 /// position. 238 void PrintPPOutputPPCallbacks::FileChanged(SourceLocation Loc, 239 FileChangeReason Reason, 240 SrcMgr::CharacteristicKind NewFileType, 241 FileID PrevFID) { 242 // Unless we are exiting a #include, make sure to skip ahead to the line the 243 // #include directive was at. 244 SourceManager &SourceMgr = SM; 245 246 PresumedLoc UserLoc = SourceMgr.getPresumedLoc(Loc); 247 if (UserLoc.isInvalid()) 248 return; 249 250 unsigned NewLine = UserLoc.getLine(); 251 252 if (Reason == PPCallbacks::EnterFile) { 253 SourceLocation IncludeLoc = UserLoc.getIncludeLoc(); 254 if (IncludeLoc.isValid()) 255 MoveToLine(IncludeLoc); 256 } else if (Reason == PPCallbacks::SystemHeaderPragma) { 257 MoveToLine(NewLine); 258 259 // TODO GCC emits the # directive for this directive on the line AFTER the 260 // directive and emits a bunch of spaces that aren't needed. Emulate this 261 // strange behavior. 262 } 263 264 CurLine = NewLine; 265 266 CurFilename.clear(); 267 CurFilename += UserLoc.getFilename(); 268 Lexer::Stringify(CurFilename); 269 FileType = NewFileType; 270 271 if (DisableLineMarkers) { 272 startNewLineIfNeeded(/*ShouldUpdateCurrentLine=*/false); 273 return; 274 } 275 276 if (!Initialized) { 277 WriteLineInfo(CurLine); 278 Initialized = true; 279 } 280 281 // Do not emit an enter marker for the main file (which we expect is the first 282 // entered file). This matches gcc, and improves compatibility with some tools 283 // which track the # line markers as a way to determine when the preprocessed 284 // output is in the context of the main file. 285 if (Reason == PPCallbacks::EnterFile && !IsFirstFileEntered) { 286 IsFirstFileEntered = true; 287 return; 288 } 289 290 switch (Reason) { 291 case PPCallbacks::EnterFile: 292 WriteLineInfo(CurLine, " 1", 2); 293 break; 294 case PPCallbacks::ExitFile: 295 WriteLineInfo(CurLine, " 2", 2); 296 break; 297 case PPCallbacks::SystemHeaderPragma: 298 case PPCallbacks::RenameFile: 299 WriteLineInfo(CurLine); 300 break; 301 } 302 } 303 304 /// Ident - Handle #ident directives when read by the preprocessor. 305 /// 306 void PrintPPOutputPPCallbacks::Ident(SourceLocation Loc, const std::string &S) { 307 MoveToLine(Loc); 308 309 OS.write("#ident ", strlen("#ident ")); 310 OS.write(&S[0], S.size()); 311 EmittedTokensOnThisLine = true; 312 } 313 314 /// MacroDefined - This hook is called whenever a macro definition is seen. 315 void PrintPPOutputPPCallbacks::MacroDefined(const Token &MacroNameTok, 316 const MacroInfo *MI) { 317 // Only print out macro definitions in -dD mode. 318 if (!DumpDefines || 319 // Ignore __FILE__ etc. 320 MI->isBuiltinMacro()) return; 321 322 MoveToLine(MI->getDefinitionLoc()); 323 PrintMacroDefinition(*MacroNameTok.getIdentifierInfo(), *MI, PP, OS); 324 setEmittedDirectiveOnThisLine(); 325 } 326 327 void PrintPPOutputPPCallbacks::MacroUndefined(const Token &MacroNameTok, 328 const MacroInfo *MI) { 329 // Only print out macro definitions in -dD mode. 330 if (!DumpDefines) return; 331 332 MoveToLine(MacroNameTok.getLocation()); 333 OS << "#undef " << MacroNameTok.getIdentifierInfo()->getName(); 334 setEmittedDirectiveOnThisLine(); 335 } 336 337 void PrintPPOutputPPCallbacks::PragmaComment(SourceLocation Loc, 338 const IdentifierInfo *Kind, 339 const std::string &Str) { 340 startNewLineIfNeeded(); 341 MoveToLine(Loc); 342 OS << "#pragma comment(" << Kind->getName(); 343 344 if (!Str.empty()) { 345 OS << ", \""; 346 347 for (unsigned i = 0, e = Str.size(); i != e; ++i) { 348 unsigned char Char = Str[i]; 349 if (isprint(Char) && Char != '\\' && Char != '"') 350 OS << (char)Char; 351 else // Output anything hard as an octal escape. 352 OS << '\\' 353 << (char)('0'+ ((Char >> 6) & 7)) 354 << (char)('0'+ ((Char >> 3) & 7)) 355 << (char)('0'+ ((Char >> 0) & 7)); 356 } 357 OS << '"'; 358 } 359 360 OS << ')'; 361 setEmittedDirectiveOnThisLine(); 362 } 363 364 void PrintPPOutputPPCallbacks::PragmaMessage(SourceLocation Loc, 365 StringRef Str) { 366 startNewLineIfNeeded(); 367 MoveToLine(Loc); 368 OS << "#pragma message("; 369 370 OS << '"'; 371 372 for (unsigned i = 0, e = Str.size(); i != e; ++i) { 373 unsigned char Char = Str[i]; 374 if (isprint(Char) && Char != '\\' && Char != '"') 375 OS << (char)Char; 376 else // Output anything hard as an octal escape. 377 OS << '\\' 378 << (char)('0'+ ((Char >> 6) & 7)) 379 << (char)('0'+ ((Char >> 3) & 7)) 380 << (char)('0'+ ((Char >> 0) & 7)); 381 } 382 OS << '"'; 383 384 OS << ')'; 385 setEmittedDirectiveOnThisLine(); 386 } 387 388 void PrintPPOutputPPCallbacks:: 389 PragmaDiagnosticPush(SourceLocation Loc, StringRef Namespace) { 390 startNewLineIfNeeded(); 391 MoveToLine(Loc); 392 OS << "#pragma " << Namespace << " diagnostic push"; 393 setEmittedDirectiveOnThisLine(); 394 } 395 396 void PrintPPOutputPPCallbacks:: 397 PragmaDiagnosticPop(SourceLocation Loc, StringRef Namespace) { 398 startNewLineIfNeeded(); 399 MoveToLine(Loc); 400 OS << "#pragma " << Namespace << " diagnostic pop"; 401 setEmittedDirectiveOnThisLine(); 402 } 403 404 void PrintPPOutputPPCallbacks:: 405 PragmaDiagnostic(SourceLocation Loc, StringRef Namespace, 406 diag::Mapping Map, StringRef Str) { 407 startNewLineIfNeeded(); 408 MoveToLine(Loc); 409 OS << "#pragma " << Namespace << " diagnostic "; 410 switch (Map) { 411 case diag::MAP_WARNING: 412 OS << "warning"; 413 break; 414 case diag::MAP_ERROR: 415 OS << "error"; 416 break; 417 case diag::MAP_IGNORE: 418 OS << "ignored"; 419 break; 420 case diag::MAP_FATAL: 421 OS << "fatal"; 422 break; 423 } 424 OS << " \"" << Str << '"'; 425 setEmittedDirectiveOnThisLine(); 426 } 427 428 /// HandleFirstTokOnLine - When emitting a preprocessed file in -E mode, this 429 /// is called for the first token on each new line. If this really is the start 430 /// of a new logical line, handle it and return true, otherwise return false. 431 /// This may not be the start of a logical line because the "start of line" 432 /// marker is set for spelling lines, not expansion ones. 433 bool PrintPPOutputPPCallbacks::HandleFirstTokOnLine(Token &Tok) { 434 // Figure out what line we went to and insert the appropriate number of 435 // newline characters. 436 if (!MoveToLine(Tok.getLocation())) 437 return false; 438 439 // Print out space characters so that the first token on a line is 440 // indented for easy reading. 441 unsigned ColNo = SM.getExpansionColumnNumber(Tok.getLocation()); 442 443 // This hack prevents stuff like: 444 // #define HASH # 445 // HASH define foo bar 446 // From having the # character end up at column 1, which makes it so it 447 // is not handled as a #define next time through the preprocessor if in 448 // -fpreprocessed mode. 449 if (ColNo <= 1 && Tok.is(tok::hash)) 450 OS << ' '; 451 452 // Otherwise, indent the appropriate number of spaces. 453 for (; ColNo > 1; --ColNo) 454 OS << ' '; 455 456 return true; 457 } 458 459 void PrintPPOutputPPCallbacks::HandleNewlinesInToken(const char *TokStr, 460 unsigned Len) { 461 unsigned NumNewlines = 0; 462 for (; Len; --Len, ++TokStr) { 463 if (*TokStr != '\n' && 464 *TokStr != '\r') 465 continue; 466 467 ++NumNewlines; 468 469 // If we have \n\r or \r\n, skip both and count as one line. 470 if (Len != 1 && 471 (TokStr[1] == '\n' || TokStr[1] == '\r') && 472 TokStr[0] != TokStr[1]) 473 ++TokStr, --Len; 474 } 475 476 if (NumNewlines == 0) return; 477 478 CurLine += NumNewlines; 479 } 480 481 482 namespace { 483 struct UnknownPragmaHandler : public PragmaHandler { 484 const char *Prefix; 485 PrintPPOutputPPCallbacks *Callbacks; 486 487 UnknownPragmaHandler(const char *prefix, PrintPPOutputPPCallbacks *callbacks) 488 : Prefix(prefix), Callbacks(callbacks) {} 489 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, 490 Token &PragmaTok) { 491 // Figure out what line we went to and insert the appropriate number of 492 // newline characters. 493 Callbacks->startNewLineIfNeeded(); 494 Callbacks->MoveToLine(PragmaTok.getLocation()); 495 Callbacks->OS.write(Prefix, strlen(Prefix)); 496 // Read and print all of the pragma tokens. 497 while (PragmaTok.isNot(tok::eod)) { 498 if (PragmaTok.hasLeadingSpace()) 499 Callbacks->OS << ' '; 500 std::string TokSpell = PP.getSpelling(PragmaTok); 501 Callbacks->OS.write(&TokSpell[0], TokSpell.size()); 502 PP.LexUnexpandedToken(PragmaTok); 503 } 504 Callbacks->setEmittedDirectiveOnThisLine(); 505 } 506 }; 507 } // end anonymous namespace 508 509 510 static void PrintPreprocessedTokens(Preprocessor &PP, Token &Tok, 511 PrintPPOutputPPCallbacks *Callbacks, 512 raw_ostream &OS) { 513 char Buffer[256]; 514 Token PrevPrevTok, PrevTok; 515 PrevPrevTok.startToken(); 516 PrevTok.startToken(); 517 while (1) { 518 if (Callbacks->hasEmittedDirectiveOnThisLine()) { 519 Callbacks->startNewLineIfNeeded(); 520 Callbacks->MoveToLine(Tok.getLocation()); 521 } 522 523 // If this token is at the start of a line, emit newlines if needed. 524 if (Tok.isAtStartOfLine() && Callbacks->HandleFirstTokOnLine(Tok)) { 525 // done. 526 } else if (Tok.hasLeadingSpace() || 527 // If we haven't emitted a token on this line yet, PrevTok isn't 528 // useful to look at and no concatenation could happen anyway. 529 (Callbacks->hasEmittedTokensOnThisLine() && 530 // Don't print "-" next to "-", it would form "--". 531 Callbacks->AvoidConcat(PrevPrevTok, PrevTok, Tok))) { 532 OS << ' '; 533 } 534 535 if (IdentifierInfo *II = Tok.getIdentifierInfo()) { 536 OS << II->getName(); 537 } else if (Tok.isLiteral() && !Tok.needsCleaning() && 538 Tok.getLiteralData()) { 539 OS.write(Tok.getLiteralData(), Tok.getLength()); 540 } else if (Tok.getLength() < 256) { 541 const char *TokPtr = Buffer; 542 unsigned Len = PP.getSpelling(Tok, TokPtr); 543 OS.write(TokPtr, Len); 544 545 // Tokens that can contain embedded newlines need to adjust our current 546 // line number. 547 if (Tok.getKind() == tok::comment) 548 Callbacks->HandleNewlinesInToken(TokPtr, Len); 549 } else { 550 std::string S = PP.getSpelling(Tok); 551 OS.write(&S[0], S.size()); 552 553 // Tokens that can contain embedded newlines need to adjust our current 554 // line number. 555 if (Tok.getKind() == tok::comment) 556 Callbacks->HandleNewlinesInToken(&S[0], S.size()); 557 } 558 Callbacks->setEmittedTokensOnThisLine(); 559 560 if (Tok.is(tok::eof)) break; 561 562 PrevPrevTok = PrevTok; 563 PrevTok = Tok; 564 PP.Lex(Tok); 565 } 566 } 567 568 typedef std::pair<const IdentifierInfo *, MacroInfo *> id_macro_pair; 569 static int MacroIDCompare(const void* a, const void* b) { 570 const id_macro_pair *LHS = static_cast<const id_macro_pair*>(a); 571 const id_macro_pair *RHS = static_cast<const id_macro_pair*>(b); 572 return LHS->first->getName().compare(RHS->first->getName()); 573 } 574 575 static void DoPrintMacros(Preprocessor &PP, raw_ostream *OS) { 576 // Ignore unknown pragmas. 577 PP.AddPragmaHandler(new EmptyPragmaHandler()); 578 579 // -dM mode just scans and ignores all tokens in the files, then dumps out 580 // the macro table at the end. 581 PP.EnterMainSourceFile(); 582 583 Token Tok; 584 do PP.Lex(Tok); 585 while (Tok.isNot(tok::eof)); 586 587 SmallVector<id_macro_pair, 128> MacrosByID; 588 for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end(); 589 I != E; ++I) { 590 if (I->first->hasMacroDefinition()) 591 MacrosByID.push_back(id_macro_pair(I->first, I->second)); 592 } 593 llvm::array_pod_sort(MacrosByID.begin(), MacrosByID.end(), MacroIDCompare); 594 595 for (unsigned i = 0, e = MacrosByID.size(); i != e; ++i) { 596 MacroInfo &MI = *MacrosByID[i].second; 597 // Ignore computed macros like __LINE__ and friends. 598 if (MI.isBuiltinMacro()) continue; 599 600 PrintMacroDefinition(*MacrosByID[i].first, MI, PP, *OS); 601 *OS << '\n'; 602 } 603 } 604 605 /// DoPrintPreprocessedInput - This implements -E mode. 606 /// 607 void clang::DoPrintPreprocessedInput(Preprocessor &PP, raw_ostream *OS, 608 const PreprocessorOutputOptions &Opts) { 609 // Show macros with no output is handled specially. 610 if (!Opts.ShowCPP) { 611 assert(Opts.ShowMacros && "Not yet implemented!"); 612 DoPrintMacros(PP, OS); 613 return; 614 } 615 616 // Inform the preprocessor whether we want it to retain comments or not, due 617 // to -C or -CC. 618 PP.SetCommentRetentionState(Opts.ShowComments, Opts.ShowMacroComments); 619 620 PrintPPOutputPPCallbacks *Callbacks = 621 new PrintPPOutputPPCallbacks(PP, *OS, !Opts.ShowLineMarkers, 622 Opts.ShowMacros); 623 PP.AddPragmaHandler(new UnknownPragmaHandler("#pragma", Callbacks)); 624 PP.AddPragmaHandler("GCC", new UnknownPragmaHandler("#pragma GCC",Callbacks)); 625 PP.AddPragmaHandler("clang", 626 new UnknownPragmaHandler("#pragma clang", Callbacks)); 627 628 PP.addPPCallbacks(Callbacks); 629 630 // After we have configured the preprocessor, enter the main file. 631 PP.EnterMainSourceFile(); 632 633 // Consume all of the tokens that come from the predefines buffer. Those 634 // should not be emitted into the output and are guaranteed to be at the 635 // start. 636 const SourceManager &SourceMgr = PP.getSourceManager(); 637 Token Tok; 638 do { 639 PP.Lex(Tok); 640 if (Tok.is(tok::eof) || !Tok.getLocation().isFileID()) 641 break; 642 643 PresumedLoc PLoc = SourceMgr.getPresumedLoc(Tok.getLocation()); 644 if (PLoc.isInvalid()) 645 break; 646 647 if (strcmp(PLoc.getFilename(), "<built-in>")) 648 break; 649 } while (true); 650 651 // Read all the preprocessed tokens, printing them out to the stream. 652 PrintPreprocessedTokens(PP, Tok, Callbacks, *OS); 653 *OS << '\n'; 654 } 655