clang API Documentation
00001 //===--- PrintPreprocessedOutput.cpp - Implement the -E mode --------------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This code simply runs the preprocessor on the input file and prints out the 00011 // result. This is the traditional behavior of the -E option. 00012 // 00013 //===----------------------------------------------------------------------===// 00014 00015 #include "clang/Frontend/Utils.h" 00016 #include "clang/Basic/Diagnostic.h" 00017 #include "clang/Basic/SourceManager.h" 00018 #include "clang/Frontend/PreprocessorOutputOptions.h" 00019 #include "clang/Lex/MacroInfo.h" 00020 #include "clang/Lex/PPCallbacks.h" 00021 #include "clang/Lex/Pragma.h" 00022 #include "clang/Lex/Preprocessor.h" 00023 #include "clang/Lex/TokenConcatenation.h" 00024 #include "llvm/ADT/SmallString.h" 00025 #include "llvm/ADT/STLExtras.h" 00026 #include "llvm/ADT/StringRef.h" 00027 #include "llvm/Support/raw_ostream.h" 00028 #include "llvm/Support/ErrorHandling.h" 00029 #include <cstdio> 00030 using namespace clang; 00031 00032 /// PrintMacroDefinition - Print a macro definition in a form that will be 00033 /// properly accepted back as a definition. 00034 static void PrintMacroDefinition(const IdentifierInfo &II, const MacroInfo &MI, 00035 Preprocessor &PP, raw_ostream &OS) { 00036 OS << "#define " << II.getName(); 00037 00038 if (MI.isFunctionLike()) { 00039 OS << '('; 00040 if (!MI.arg_empty()) { 00041 MacroInfo::arg_iterator AI = MI.arg_begin(), E = MI.arg_end(); 00042 for (; AI+1 != E; ++AI) { 00043 OS << (*AI)->getName(); 00044 OS << ','; 00045 } 00046 00047 // Last argument. 00048 if ((*AI)->getName() == "__VA_ARGS__") 00049 OS << "..."; 00050 else 00051 OS << (*AI)->getName(); 00052 } 00053 00054 if (MI.isGNUVarargs()) 00055 OS << "..."; // #define foo(x...) 00056 00057 OS << ')'; 00058 } 00059 00060 // GCC always emits a space, even if the macro body is empty. However, do not 00061 // want to emit two spaces if the first token has a leading space. 00062 if (MI.tokens_empty() || !MI.tokens_begin()->hasLeadingSpace()) 00063 OS << ' '; 00064 00065 SmallString<128> SpellingBuffer; 00066 for (MacroInfo::tokens_iterator I = MI.tokens_begin(), E = MI.tokens_end(); 00067 I != E; ++I) { 00068 if (I->hasLeadingSpace()) 00069 OS << ' '; 00070 00071 OS << PP.getSpelling(*I, SpellingBuffer); 00072 } 00073 } 00074 00075 //===----------------------------------------------------------------------===// 00076 // Preprocessed token printer 00077 //===----------------------------------------------------------------------===// 00078 00079 namespace { 00080 class PrintPPOutputPPCallbacks : public PPCallbacks { 00081 Preprocessor &PP; 00082 SourceManager &SM; 00083 TokenConcatenation ConcatInfo; 00084 public: 00085 raw_ostream &OS; 00086 private: 00087 unsigned CurLine; 00088 00089 bool EmittedTokensOnThisLine; 00090 bool EmittedMacroOnThisLine; 00091 SrcMgr::CharacteristicKind FileType; 00092 SmallString<512> CurFilename; 00093 bool Initialized; 00094 bool DisableLineMarkers; 00095 bool DumpDefines; 00096 bool UseLineDirective; 00097 public: 00098 PrintPPOutputPPCallbacks(Preprocessor &pp, raw_ostream &os, 00099 bool lineMarkers, bool defines) 00100 : PP(pp), SM(PP.getSourceManager()), 00101 ConcatInfo(PP), OS(os), DisableLineMarkers(lineMarkers), 00102 DumpDefines(defines) { 00103 CurLine = 0; 00104 CurFilename += "<uninit>"; 00105 EmittedTokensOnThisLine = false; 00106 EmittedMacroOnThisLine = false; 00107 FileType = SrcMgr::C_User; 00108 Initialized = false; 00109 00110 // If we're in microsoft mode, use normal #line instead of line markers. 00111 UseLineDirective = PP.getLangOpts().MicrosoftExt; 00112 } 00113 00114 void SetEmittedTokensOnThisLine() { EmittedTokensOnThisLine = true; } 00115 bool hasEmittedTokensOnThisLine() const { return EmittedTokensOnThisLine; } 00116 00117 bool StartNewLineIfNeeded(); 00118 00119 virtual void FileChanged(SourceLocation Loc, FileChangeReason Reason, 00120 SrcMgr::CharacteristicKind FileType, 00121 FileID PrevFID); 00122 virtual void Ident(SourceLocation Loc, const std::string &str); 00123 virtual void PragmaComment(SourceLocation Loc, const IdentifierInfo *Kind, 00124 const std::string &Str); 00125 virtual void PragmaMessage(SourceLocation Loc, StringRef Str); 00126 virtual void PragmaDiagnosticPush(SourceLocation Loc, 00127 StringRef Namespace); 00128 virtual void PragmaDiagnosticPop(SourceLocation Loc, 00129 StringRef Namespace); 00130 virtual void PragmaDiagnostic(SourceLocation Loc, StringRef Namespace, 00131 diag::Mapping Map, StringRef Str); 00132 00133 bool HandleFirstTokOnLine(Token &Tok); 00134 bool MoveToLine(SourceLocation Loc) { 00135 PresumedLoc PLoc = SM.getPresumedLoc(Loc); 00136 if (PLoc.isInvalid()) 00137 return false; 00138 return MoveToLine(PLoc.getLine()); 00139 } 00140 bool MoveToLine(unsigned LineNo); 00141 00142 bool AvoidConcat(const Token &PrevPrevTok, const Token &PrevTok, 00143 const Token &Tok) { 00144 return ConcatInfo.AvoidConcat(PrevPrevTok, PrevTok, Tok); 00145 } 00146 void WriteLineInfo(unsigned LineNo, const char *Extra=0, unsigned ExtraLen=0); 00147 bool LineMarkersAreDisabled() const { return DisableLineMarkers; } 00148 void HandleNewlinesInToken(const char *TokStr, unsigned Len); 00149 00150 /// MacroDefined - This hook is called whenever a macro definition is seen. 00151 void MacroDefined(const Token &MacroNameTok, const MacroInfo *MI); 00152 00153 /// MacroUndefined - This hook is called whenever a macro #undef is seen. 00154 void MacroUndefined(const Token &MacroNameTok, const MacroInfo *MI); 00155 }; 00156 } // end anonymous namespace 00157 00158 void PrintPPOutputPPCallbacks::WriteLineInfo(unsigned LineNo, 00159 const char *Extra, 00160 unsigned ExtraLen) { 00161 if (EmittedTokensOnThisLine || EmittedMacroOnThisLine) { 00162 OS << '\n'; 00163 EmittedTokensOnThisLine = false; 00164 EmittedMacroOnThisLine = false; 00165 } 00166 00167 // Emit #line directives or GNU line markers depending on what mode we're in. 00168 if (UseLineDirective) { 00169 OS << "#line" << ' ' << LineNo << ' ' << '"'; 00170 OS.write(CurFilename.data(), CurFilename.size()); 00171 OS << '"'; 00172 } else { 00173 OS << '#' << ' ' << LineNo << ' ' << '"'; 00174 OS.write(CurFilename.data(), CurFilename.size()); 00175 OS << '"'; 00176 00177 if (ExtraLen) 00178 OS.write(Extra, ExtraLen); 00179 00180 if (FileType == SrcMgr::C_System) 00181 OS.write(" 3", 2); 00182 else if (FileType == SrcMgr::C_ExternCSystem) 00183 OS.write(" 3 4", 4); 00184 } 00185 OS << '\n'; 00186 } 00187 00188 /// MoveToLine - Move the output to the source line specified by the location 00189 /// object. We can do this by emitting some number of \n's, or be emitting a 00190 /// #line directive. This returns false if already at the specified line, true 00191 /// if some newlines were emitted. 00192 bool PrintPPOutputPPCallbacks::MoveToLine(unsigned LineNo) { 00193 // If this line is "close enough" to the original line, just print newlines, 00194 // otherwise print a #line directive. 00195 if (LineNo-CurLine <= 8) { 00196 if (LineNo-CurLine == 1) 00197 OS << '\n'; 00198 else if (LineNo == CurLine) 00199 return false; // Spelling line moved, but expansion line didn't. 00200 else { 00201 const char *NewLines = "\n\n\n\n\n\n\n\n"; 00202 OS.write(NewLines, LineNo-CurLine); 00203 } 00204 } else if (!DisableLineMarkers) { 00205 // Emit a #line or line marker. 00206 WriteLineInfo(LineNo, 0, 0); 00207 } else { 00208 // Okay, we're in -P mode, which turns off line markers. However, we still 00209 // need to emit a newline between tokens on different lines. 00210 if (EmittedTokensOnThisLine || EmittedMacroOnThisLine) { 00211 OS << '\n'; 00212 EmittedTokensOnThisLine = false; 00213 EmittedMacroOnThisLine = false; 00214 } 00215 } 00216 00217 CurLine = LineNo; 00218 return true; 00219 } 00220 00221 bool PrintPPOutputPPCallbacks::StartNewLineIfNeeded() { 00222 if (EmittedTokensOnThisLine || EmittedMacroOnThisLine) { 00223 OS << '\n'; 00224 EmittedTokensOnThisLine = false; 00225 EmittedMacroOnThisLine = false; 00226 ++CurLine; 00227 return true; 00228 } 00229 00230 return false; 00231 } 00232 00233 /// FileChanged - Whenever the preprocessor enters or exits a #include file 00234 /// it invokes this handler. Update our conception of the current source 00235 /// position. 00236 void PrintPPOutputPPCallbacks::FileChanged(SourceLocation Loc, 00237 FileChangeReason Reason, 00238 SrcMgr::CharacteristicKind NewFileType, 00239 FileID PrevFID) { 00240 // Unless we are exiting a #include, make sure to skip ahead to the line the 00241 // #include directive was at. 00242 SourceManager &SourceMgr = SM; 00243 00244 PresumedLoc UserLoc = SourceMgr.getPresumedLoc(Loc); 00245 if (UserLoc.isInvalid()) 00246 return; 00247 00248 unsigned NewLine = UserLoc.getLine(); 00249 00250 if (Reason == PPCallbacks::EnterFile) { 00251 SourceLocation IncludeLoc = UserLoc.getIncludeLoc(); 00252 if (IncludeLoc.isValid()) 00253 MoveToLine(IncludeLoc); 00254 } else if (Reason == PPCallbacks::SystemHeaderPragma) { 00255 MoveToLine(NewLine); 00256 00257 // TODO GCC emits the # directive for this directive on the line AFTER the 00258 // directive and emits a bunch of spaces that aren't needed. Emulate this 00259 // strange behavior. 00260 } 00261 00262 CurLine = NewLine; 00263 00264 CurFilename.clear(); 00265 CurFilename += UserLoc.getFilename(); 00266 Lexer::Stringify(CurFilename); 00267 FileType = NewFileType; 00268 00269 if (DisableLineMarkers) return; 00270 00271 if (!Initialized) { 00272 WriteLineInfo(CurLine); 00273 Initialized = true; 00274 } 00275 00276 switch (Reason) { 00277 case PPCallbacks::EnterFile: 00278 WriteLineInfo(CurLine, " 1", 2); 00279 break; 00280 case PPCallbacks::ExitFile: 00281 WriteLineInfo(CurLine, " 2", 2); 00282 break; 00283 case PPCallbacks::SystemHeaderPragma: 00284 case PPCallbacks::RenameFile: 00285 WriteLineInfo(CurLine); 00286 break; 00287 } 00288 } 00289 00290 /// Ident - Handle #ident directives when read by the preprocessor. 00291 /// 00292 void PrintPPOutputPPCallbacks::Ident(SourceLocation Loc, const std::string &S) { 00293 MoveToLine(Loc); 00294 00295 OS.write("#ident ", strlen("#ident ")); 00296 OS.write(&S[0], S.size()); 00297 EmittedTokensOnThisLine = true; 00298 } 00299 00300 /// MacroDefined - This hook is called whenever a macro definition is seen. 00301 void PrintPPOutputPPCallbacks::MacroDefined(const Token &MacroNameTok, 00302 const MacroInfo *MI) { 00303 // Only print out macro definitions in -dD mode. 00304 if (!DumpDefines || 00305 // Ignore __FILE__ etc. 00306 MI->isBuiltinMacro()) return; 00307 00308 MoveToLine(MI->getDefinitionLoc()); 00309 PrintMacroDefinition(*MacroNameTok.getIdentifierInfo(), *MI, PP, OS); 00310 EmittedMacroOnThisLine = true; 00311 } 00312 00313 void PrintPPOutputPPCallbacks::MacroUndefined(const Token &MacroNameTok, 00314 const MacroInfo *MI) { 00315 // Only print out macro definitions in -dD mode. 00316 if (!DumpDefines) return; 00317 00318 MoveToLine(MacroNameTok.getLocation()); 00319 OS << "#undef " << MacroNameTok.getIdentifierInfo()->getName(); 00320 EmittedMacroOnThisLine = true; 00321 } 00322 00323 void PrintPPOutputPPCallbacks::PragmaComment(SourceLocation Loc, 00324 const IdentifierInfo *Kind, 00325 const std::string &Str) { 00326 MoveToLine(Loc); 00327 OS << "#pragma comment(" << Kind->getName(); 00328 00329 if (!Str.empty()) { 00330 OS << ", \""; 00331 00332 for (unsigned i = 0, e = Str.size(); i != e; ++i) { 00333 unsigned char Char = Str[i]; 00334 if (isprint(Char) && Char != '\\' && Char != '"') 00335 OS << (char)Char; 00336 else // Output anything hard as an octal escape. 00337 OS << '\\' 00338 << (char)('0'+ ((Char >> 6) & 7)) 00339 << (char)('0'+ ((Char >> 3) & 7)) 00340 << (char)('0'+ ((Char >> 0) & 7)); 00341 } 00342 OS << '"'; 00343 } 00344 00345 OS << ')'; 00346 EmittedTokensOnThisLine = true; 00347 } 00348 00349 void PrintPPOutputPPCallbacks::PragmaMessage(SourceLocation Loc, 00350 StringRef Str) { 00351 MoveToLine(Loc); 00352 OS << "#pragma message("; 00353 00354 OS << '"'; 00355 00356 for (unsigned i = 0, e = Str.size(); i != e; ++i) { 00357 unsigned char Char = Str[i]; 00358 if (isprint(Char) && Char != '\\' && Char != '"') 00359 OS << (char)Char; 00360 else // Output anything hard as an octal escape. 00361 OS << '\\' 00362 << (char)('0'+ ((Char >> 6) & 7)) 00363 << (char)('0'+ ((Char >> 3) & 7)) 00364 << (char)('0'+ ((Char >> 0) & 7)); 00365 } 00366 OS << '"'; 00367 00368 OS << ')'; 00369 EmittedTokensOnThisLine = true; 00370 } 00371 00372 void PrintPPOutputPPCallbacks:: 00373 PragmaDiagnosticPush(SourceLocation Loc, StringRef Namespace) { 00374 MoveToLine(Loc); 00375 OS << "#pragma " << Namespace << " diagnostic push"; 00376 EmittedTokensOnThisLine = true; 00377 } 00378 00379 void PrintPPOutputPPCallbacks:: 00380 PragmaDiagnosticPop(SourceLocation Loc, StringRef Namespace) { 00381 MoveToLine(Loc); 00382 OS << "#pragma " << Namespace << " diagnostic pop"; 00383 EmittedTokensOnThisLine = true; 00384 } 00385 00386 void PrintPPOutputPPCallbacks:: 00387 PragmaDiagnostic(SourceLocation Loc, StringRef Namespace, 00388 diag::Mapping Map, StringRef Str) { 00389 MoveToLine(Loc); 00390 OS << "#pragma " << Namespace << " diagnostic "; 00391 switch (Map) { 00392 case diag::MAP_WARNING: 00393 OS << "warning"; 00394 break; 00395 case diag::MAP_ERROR: 00396 OS << "error"; 00397 break; 00398 case diag::MAP_IGNORE: 00399 OS << "ignored"; 00400 break; 00401 case diag::MAP_FATAL: 00402 OS << "fatal"; 00403 break; 00404 } 00405 OS << " \"" << Str << '"'; 00406 EmittedTokensOnThisLine = true; 00407 } 00408 00409 /// HandleFirstTokOnLine - When emitting a preprocessed file in -E mode, this 00410 /// is called for the first token on each new line. If this really is the start 00411 /// of a new logical line, handle it and return true, otherwise return false. 00412 /// This may not be the start of a logical line because the "start of line" 00413 /// marker is set for spelling lines, not expansion ones. 00414 bool PrintPPOutputPPCallbacks::HandleFirstTokOnLine(Token &Tok) { 00415 // Figure out what line we went to and insert the appropriate number of 00416 // newline characters. 00417 if (!MoveToLine(Tok.getLocation())) 00418 return false; 00419 00420 // Print out space characters so that the first token on a line is 00421 // indented for easy reading. 00422 unsigned ColNo = SM.getExpansionColumnNumber(Tok.getLocation()); 00423 00424 // This hack prevents stuff like: 00425 // #define HASH # 00426 // HASH define foo bar 00427 // From having the # character end up at column 1, which makes it so it 00428 // is not handled as a #define next time through the preprocessor if in 00429 // -fpreprocessed mode. 00430 if (ColNo <= 1 && Tok.is(tok::hash)) 00431 OS << ' '; 00432 00433 // Otherwise, indent the appropriate number of spaces. 00434 for (; ColNo > 1; --ColNo) 00435 OS << ' '; 00436 00437 return true; 00438 } 00439 00440 void PrintPPOutputPPCallbacks::HandleNewlinesInToken(const char *TokStr, 00441 unsigned Len) { 00442 unsigned NumNewlines = 0; 00443 for (; Len; --Len, ++TokStr) { 00444 if (*TokStr != '\n' && 00445 *TokStr != '\r') 00446 continue; 00447 00448 ++NumNewlines; 00449 00450 // If we have \n\r or \r\n, skip both and count as one line. 00451 if (Len != 1 && 00452 (TokStr[1] == '\n' || TokStr[1] == '\r') && 00453 TokStr[0] != TokStr[1]) 00454 ++TokStr, --Len; 00455 } 00456 00457 if (NumNewlines == 0) return; 00458 00459 CurLine += NumNewlines; 00460 } 00461 00462 00463 namespace { 00464 struct UnknownPragmaHandler : public PragmaHandler { 00465 const char *Prefix; 00466 PrintPPOutputPPCallbacks *Callbacks; 00467 00468 UnknownPragmaHandler(const char *prefix, PrintPPOutputPPCallbacks *callbacks) 00469 : Prefix(prefix), Callbacks(callbacks) {} 00470 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, 00471 Token &PragmaTok) { 00472 // Figure out what line we went to and insert the appropriate number of 00473 // newline characters. 00474 Callbacks->StartNewLineIfNeeded(); 00475 Callbacks->MoveToLine(PragmaTok.getLocation()); 00476 Callbacks->OS.write(Prefix, strlen(Prefix)); 00477 Callbacks->SetEmittedTokensOnThisLine(); 00478 // Read and print all of the pragma tokens. 00479 while (PragmaTok.isNot(tok::eod)) { 00480 if (PragmaTok.hasLeadingSpace()) 00481 Callbacks->OS << ' '; 00482 std::string TokSpell = PP.getSpelling(PragmaTok); 00483 Callbacks->OS.write(&TokSpell[0], TokSpell.size()); 00484 PP.LexUnexpandedToken(PragmaTok); 00485 } 00486 Callbacks->StartNewLineIfNeeded(); 00487 } 00488 }; 00489 } // end anonymous namespace 00490 00491 00492 static void PrintPreprocessedTokens(Preprocessor &PP, Token &Tok, 00493 PrintPPOutputPPCallbacks *Callbacks, 00494 raw_ostream &OS) { 00495 char Buffer[256]; 00496 Token PrevPrevTok, PrevTok; 00497 PrevPrevTok.startToken(); 00498 PrevTok.startToken(); 00499 while (1) { 00500 00501 // If this token is at the start of a line, emit newlines if needed. 00502 if (Tok.isAtStartOfLine() && Callbacks->HandleFirstTokOnLine(Tok)) { 00503 // done. 00504 } else if (Tok.hasLeadingSpace() || 00505 // If we haven't emitted a token on this line yet, PrevTok isn't 00506 // useful to look at and no concatenation could happen anyway. 00507 (Callbacks->hasEmittedTokensOnThisLine() && 00508 // Don't print "-" next to "-", it would form "--". 00509 Callbacks->AvoidConcat(PrevPrevTok, PrevTok, Tok))) { 00510 OS << ' '; 00511 } 00512 00513 if (IdentifierInfo *II = Tok.getIdentifierInfo()) { 00514 OS << II->getName(); 00515 } else if (Tok.isLiteral() && !Tok.needsCleaning() && 00516 Tok.getLiteralData()) { 00517 OS.write(Tok.getLiteralData(), Tok.getLength()); 00518 } else if (Tok.getLength() < 256) { 00519 const char *TokPtr = Buffer; 00520 unsigned Len = PP.getSpelling(Tok, TokPtr); 00521 OS.write(TokPtr, Len); 00522 00523 // Tokens that can contain embedded newlines need to adjust our current 00524 // line number. 00525 if (Tok.getKind() == tok::comment) 00526 Callbacks->HandleNewlinesInToken(TokPtr, Len); 00527 } else { 00528 std::string S = PP.getSpelling(Tok); 00529 OS.write(&S[0], S.size()); 00530 00531 // Tokens that can contain embedded newlines need to adjust our current 00532 // line number. 00533 if (Tok.getKind() == tok::comment) 00534 Callbacks->HandleNewlinesInToken(&S[0], S.size()); 00535 } 00536 Callbacks->SetEmittedTokensOnThisLine(); 00537 00538 if (Tok.is(tok::eof)) break; 00539 00540 PrevPrevTok = PrevTok; 00541 PrevTok = Tok; 00542 PP.Lex(Tok); 00543 } 00544 } 00545 00546 typedef std::pair<IdentifierInfo*, MacroInfo*> id_macro_pair; 00547 static int MacroIDCompare(const void* a, const void* b) { 00548 const id_macro_pair *LHS = static_cast<const id_macro_pair*>(a); 00549 const id_macro_pair *RHS = static_cast<const id_macro_pair*>(b); 00550 return LHS->first->getName().compare(RHS->first->getName()); 00551 } 00552 00553 static void DoPrintMacros(Preprocessor &PP, raw_ostream *OS) { 00554 // Ignore unknown pragmas. 00555 PP.AddPragmaHandler(new EmptyPragmaHandler()); 00556 00557 // -dM mode just scans and ignores all tokens in the files, then dumps out 00558 // the macro table at the end. 00559 PP.EnterMainSourceFile(); 00560 00561 Token Tok; 00562 do PP.Lex(Tok); 00563 while (Tok.isNot(tok::eof)); 00564 00565 SmallVector<id_macro_pair, 128> 00566 MacrosByID(PP.macro_begin(), PP.macro_end()); 00567 llvm::array_pod_sort(MacrosByID.begin(), MacrosByID.end(), MacroIDCompare); 00568 00569 for (unsigned i = 0, e = MacrosByID.size(); i != e; ++i) { 00570 MacroInfo &MI = *MacrosByID[i].second; 00571 // Ignore computed macros like __LINE__ and friends. 00572 if (MI.isBuiltinMacro()) continue; 00573 00574 PrintMacroDefinition(*MacrosByID[i].first, MI, PP, *OS); 00575 *OS << '\n'; 00576 } 00577 } 00578 00579 /// DoPrintPreprocessedInput - This implements -E mode. 00580 /// 00581 void clang::DoPrintPreprocessedInput(Preprocessor &PP, raw_ostream *OS, 00582 const PreprocessorOutputOptions &Opts) { 00583 // Show macros with no output is handled specially. 00584 if (!Opts.ShowCPP) { 00585 assert(Opts.ShowMacros && "Not yet implemented!"); 00586 DoPrintMacros(PP, OS); 00587 return; 00588 } 00589 00590 // Inform the preprocessor whether we want it to retain comments or not, due 00591 // to -C or -CC. 00592 PP.SetCommentRetentionState(Opts.ShowComments, Opts.ShowMacroComments); 00593 00594 PrintPPOutputPPCallbacks *Callbacks = 00595 new PrintPPOutputPPCallbacks(PP, *OS, !Opts.ShowLineMarkers, 00596 Opts.ShowMacros); 00597 PP.AddPragmaHandler(new UnknownPragmaHandler("#pragma", Callbacks)); 00598 PP.AddPragmaHandler("GCC", new UnknownPragmaHandler("#pragma GCC",Callbacks)); 00599 PP.AddPragmaHandler("clang", 00600 new UnknownPragmaHandler("#pragma clang", Callbacks)); 00601 00602 PP.addPPCallbacks(Callbacks); 00603 00604 // After we have configured the preprocessor, enter the main file. 00605 PP.EnterMainSourceFile(); 00606 00607 // Consume all of the tokens that come from the predefines buffer. Those 00608 // should not be emitted into the output and are guaranteed to be at the 00609 // start. 00610 const SourceManager &SourceMgr = PP.getSourceManager(); 00611 Token Tok; 00612 do { 00613 PP.Lex(Tok); 00614 if (Tok.is(tok::eof) || !Tok.getLocation().isFileID()) 00615 break; 00616 00617 PresumedLoc PLoc = SourceMgr.getPresumedLoc(Tok.getLocation()); 00618 if (PLoc.isInvalid()) 00619 break; 00620 00621 if (strcmp(PLoc.getFilename(), "<built-in>")) 00622 break; 00623 } while (true); 00624 00625 // Read all the preprocessed tokens, printing them out to the stream. 00626 PrintPreprocessedTokens(PP, Tok, Callbacks, *OS); 00627 *OS << '\n'; 00628 }