10b57cec5SDimitry Andric //===--- PrintPreprocessedOutput.cpp - Implement the -E mode --------------===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric // 90b57cec5SDimitry Andric // This code simply runs the preprocessor on the input file and prints out the 100b57cec5SDimitry Andric // result. This is the traditional behavior of the -E option. 110b57cec5SDimitry Andric // 120b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 130b57cec5SDimitry Andric 140b57cec5SDimitry Andric #include "clang/Frontend/Utils.h" 150b57cec5SDimitry Andric #include "clang/Basic/CharInfo.h" 160b57cec5SDimitry Andric #include "clang/Basic/Diagnostic.h" 170b57cec5SDimitry Andric #include "clang/Basic/SourceManager.h" 180b57cec5SDimitry Andric #include "clang/Frontend/PreprocessorOutputOptions.h" 190b57cec5SDimitry Andric #include "clang/Lex/MacroInfo.h" 200b57cec5SDimitry Andric #include "clang/Lex/PPCallbacks.h" 210b57cec5SDimitry Andric #include "clang/Lex/Pragma.h" 220b57cec5SDimitry Andric #include "clang/Lex/Preprocessor.h" 230b57cec5SDimitry Andric #include "clang/Lex/TokenConcatenation.h" 240b57cec5SDimitry Andric #include "llvm/ADT/STLExtras.h" 250b57cec5SDimitry Andric #include "llvm/ADT/SmallString.h" 260b57cec5SDimitry Andric #include "llvm/ADT/StringRef.h" 270b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h" 280b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h" 290b57cec5SDimitry Andric #include <cstdio> 300b57cec5SDimitry Andric using namespace clang; 310b57cec5SDimitry Andric 320b57cec5SDimitry Andric /// PrintMacroDefinition - Print a macro definition in a form that will be 330b57cec5SDimitry Andric /// properly accepted back as a definition. 340b57cec5SDimitry Andric static void PrintMacroDefinition(const IdentifierInfo &II, const MacroInfo &MI, 350b57cec5SDimitry Andric Preprocessor &PP, raw_ostream &OS) { 360b57cec5SDimitry Andric OS << "#define " << II.getName(); 370b57cec5SDimitry Andric 380b57cec5SDimitry Andric if (MI.isFunctionLike()) { 390b57cec5SDimitry Andric OS << '('; 400b57cec5SDimitry Andric if (!MI.param_empty()) { 410b57cec5SDimitry Andric MacroInfo::param_iterator AI = MI.param_begin(), E = MI.param_end(); 420b57cec5SDimitry Andric for (; AI+1 != E; ++AI) { 430b57cec5SDimitry Andric OS << (*AI)->getName(); 440b57cec5SDimitry Andric OS << ','; 450b57cec5SDimitry Andric } 460b57cec5SDimitry Andric 470b57cec5SDimitry Andric // Last argument. 480b57cec5SDimitry Andric if ((*AI)->getName() == "__VA_ARGS__") 490b57cec5SDimitry Andric OS << "..."; 500b57cec5SDimitry Andric else 510b57cec5SDimitry Andric OS << (*AI)->getName(); 520b57cec5SDimitry Andric } 530b57cec5SDimitry Andric 540b57cec5SDimitry Andric if (MI.isGNUVarargs()) 550b57cec5SDimitry Andric OS << "..."; // #define foo(x...) 560b57cec5SDimitry Andric 570b57cec5SDimitry Andric OS << ')'; 580b57cec5SDimitry Andric } 590b57cec5SDimitry Andric 600b57cec5SDimitry Andric // GCC always emits a space, even if the macro body is empty. However, do not 610b57cec5SDimitry Andric // want to emit two spaces if the first token has a leading space. 620b57cec5SDimitry Andric if (MI.tokens_empty() || !MI.tokens_begin()->hasLeadingSpace()) 630b57cec5SDimitry Andric OS << ' '; 640b57cec5SDimitry Andric 650b57cec5SDimitry Andric SmallString<128> SpellingBuffer; 660b57cec5SDimitry Andric for (const auto &T : MI.tokens()) { 670b57cec5SDimitry Andric if (T.hasLeadingSpace()) 680b57cec5SDimitry Andric OS << ' '; 690b57cec5SDimitry Andric 700b57cec5SDimitry Andric OS << PP.getSpelling(T, SpellingBuffer); 710b57cec5SDimitry Andric } 720b57cec5SDimitry Andric } 730b57cec5SDimitry Andric 740b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 750b57cec5SDimitry Andric // Preprocessed token printer 760b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 770b57cec5SDimitry Andric 780b57cec5SDimitry Andric namespace { 790b57cec5SDimitry Andric class PrintPPOutputPPCallbacks : public PPCallbacks { 800b57cec5SDimitry Andric Preprocessor &PP; 810b57cec5SDimitry Andric SourceManager &SM; 820b57cec5SDimitry Andric TokenConcatenation ConcatInfo; 830b57cec5SDimitry Andric public: 840b57cec5SDimitry Andric raw_ostream &OS; 850b57cec5SDimitry Andric private: 860b57cec5SDimitry Andric unsigned CurLine; 870b57cec5SDimitry Andric 880b57cec5SDimitry Andric bool EmittedTokensOnThisLine; 890b57cec5SDimitry Andric bool EmittedDirectiveOnThisLine; 900b57cec5SDimitry Andric SrcMgr::CharacteristicKind FileType; 910b57cec5SDimitry Andric SmallString<512> CurFilename; 920b57cec5SDimitry Andric bool Initialized; 930b57cec5SDimitry Andric bool DisableLineMarkers; 940b57cec5SDimitry Andric bool DumpDefines; 950b57cec5SDimitry Andric bool DumpIncludeDirectives; 960b57cec5SDimitry Andric bool UseLineDirectives; 970b57cec5SDimitry Andric bool IsFirstFileEntered; 98349cc55cSDimitry Andric bool MinimizeWhitespace; 9981ad6265SDimitry Andric bool DirectivesOnly; 100349cc55cSDimitry Andric 101349cc55cSDimitry Andric Token PrevTok; 102349cc55cSDimitry Andric Token PrevPrevTok; 103349cc55cSDimitry Andric 1040b57cec5SDimitry Andric public: 1050b57cec5SDimitry Andric PrintPPOutputPPCallbacks(Preprocessor &pp, raw_ostream &os, bool lineMarkers, 1060b57cec5SDimitry Andric bool defines, bool DumpIncludeDirectives, 10781ad6265SDimitry Andric bool UseLineDirectives, bool MinimizeWhitespace, 10881ad6265SDimitry Andric bool DirectivesOnly) 1090b57cec5SDimitry Andric : PP(pp), SM(PP.getSourceManager()), ConcatInfo(PP), OS(os), 1100b57cec5SDimitry Andric DisableLineMarkers(lineMarkers), DumpDefines(defines), 1110b57cec5SDimitry Andric DumpIncludeDirectives(DumpIncludeDirectives), 112349cc55cSDimitry Andric UseLineDirectives(UseLineDirectives), 11381ad6265SDimitry Andric MinimizeWhitespace(MinimizeWhitespace), DirectivesOnly(DirectivesOnly) { 1140b57cec5SDimitry Andric CurLine = 0; 1150b57cec5SDimitry Andric CurFilename += "<uninit>"; 1160b57cec5SDimitry Andric EmittedTokensOnThisLine = false; 1170b57cec5SDimitry Andric EmittedDirectiveOnThisLine = false; 1180b57cec5SDimitry Andric FileType = SrcMgr::C_User; 1190b57cec5SDimitry Andric Initialized = false; 1200b57cec5SDimitry Andric IsFirstFileEntered = false; 121349cc55cSDimitry Andric 122349cc55cSDimitry Andric PrevTok.startToken(); 123349cc55cSDimitry Andric PrevPrevTok.startToken(); 1240b57cec5SDimitry Andric } 1250b57cec5SDimitry Andric 126349cc55cSDimitry Andric bool isMinimizeWhitespace() const { return MinimizeWhitespace; } 127349cc55cSDimitry Andric 1280b57cec5SDimitry Andric void setEmittedTokensOnThisLine() { EmittedTokensOnThisLine = true; } 1290b57cec5SDimitry Andric bool hasEmittedTokensOnThisLine() const { return EmittedTokensOnThisLine; } 1300b57cec5SDimitry Andric 1310b57cec5SDimitry Andric void setEmittedDirectiveOnThisLine() { EmittedDirectiveOnThisLine = true; } 1320b57cec5SDimitry Andric bool hasEmittedDirectiveOnThisLine() const { 1330b57cec5SDimitry Andric return EmittedDirectiveOnThisLine; 1340b57cec5SDimitry Andric } 1350b57cec5SDimitry Andric 136349cc55cSDimitry Andric /// Ensure that the output stream position is at the beginning of a new line 137349cc55cSDimitry Andric /// and inserts one if it does not. It is intended to ensure that directives 138349cc55cSDimitry Andric /// inserted by the directives not from the input source (such as #line) are 139349cc55cSDimitry Andric /// in the first column. To insert newlines that represent the input, use 140349cc55cSDimitry Andric /// MoveToLine(/*...*/, /*RequireStartOfLine=*/true). 141349cc55cSDimitry Andric void startNewLineIfNeeded(); 1420b57cec5SDimitry Andric 1430b57cec5SDimitry Andric void FileChanged(SourceLocation Loc, FileChangeReason Reason, 1440b57cec5SDimitry Andric SrcMgr::CharacteristicKind FileType, 1450b57cec5SDimitry Andric FileID PrevFID) override; 1460b57cec5SDimitry Andric void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok, 1470b57cec5SDimitry Andric StringRef FileName, bool IsAngled, 14881ad6265SDimitry Andric CharSourceRange FilenameRange, 149*bdd1243dSDimitry Andric OptionalFileEntryRef File, StringRef SearchPath, 15081ad6265SDimitry Andric StringRef RelativePath, const Module *Imported, 1510b57cec5SDimitry Andric SrcMgr::CharacteristicKind FileType) override; 1520b57cec5SDimitry Andric void Ident(SourceLocation Loc, StringRef str) override; 1530b57cec5SDimitry Andric void PragmaMessage(SourceLocation Loc, StringRef Namespace, 1540b57cec5SDimitry Andric PragmaMessageKind Kind, StringRef Str) override; 1550b57cec5SDimitry Andric void PragmaDebug(SourceLocation Loc, StringRef DebugType) override; 1560b57cec5SDimitry Andric void PragmaDiagnosticPush(SourceLocation Loc, StringRef Namespace) override; 1570b57cec5SDimitry Andric void PragmaDiagnosticPop(SourceLocation Loc, StringRef Namespace) override; 1580b57cec5SDimitry Andric void PragmaDiagnostic(SourceLocation Loc, StringRef Namespace, 1590b57cec5SDimitry Andric diag::Severity Map, StringRef Str) override; 160349cc55cSDimitry Andric void PragmaWarning(SourceLocation Loc, PragmaWarningSpecifier WarningSpec, 1610b57cec5SDimitry Andric ArrayRef<int> Ids) override; 1620b57cec5SDimitry Andric void PragmaWarningPush(SourceLocation Loc, int Level) override; 1630b57cec5SDimitry Andric void PragmaWarningPop(SourceLocation Loc) override; 1640b57cec5SDimitry Andric void PragmaExecCharsetPush(SourceLocation Loc, StringRef Str) override; 1650b57cec5SDimitry Andric void PragmaExecCharsetPop(SourceLocation Loc) override; 1660b57cec5SDimitry Andric void PragmaAssumeNonNullBegin(SourceLocation Loc) override; 1670b57cec5SDimitry Andric void PragmaAssumeNonNullEnd(SourceLocation Loc) override; 1680b57cec5SDimitry Andric 169349cc55cSDimitry Andric /// Insert whitespace before emitting the next token. 170349cc55cSDimitry Andric /// 171349cc55cSDimitry Andric /// @param Tok Next token to be emitted. 172349cc55cSDimitry Andric /// @param RequireSpace Ensure at least one whitespace is emitted. Useful 173349cc55cSDimitry Andric /// if non-tokens have been emitted to the stream. 174349cc55cSDimitry Andric /// @param RequireSameLine Never emit newlines. Useful when semantics depend 175349cc55cSDimitry Andric /// on being on the same line, such as directives. 176349cc55cSDimitry Andric void HandleWhitespaceBeforeTok(const Token &Tok, bool RequireSpace, 177349cc55cSDimitry Andric bool RequireSameLine); 1780b57cec5SDimitry Andric 1790b57cec5SDimitry Andric /// Move to the line of the provided source location. This will 180349cc55cSDimitry Andric /// return true if a newline was inserted or if 181349cc55cSDimitry Andric /// the requested location is the first token on the first line. 182349cc55cSDimitry Andric /// In these cases the next output will be the first column on the line and 183349cc55cSDimitry Andric /// make it possible to insert indention. The newline was inserted 184349cc55cSDimitry Andric /// implicitly when at the beginning of the file. 185349cc55cSDimitry Andric /// 186349cc55cSDimitry Andric /// @param Tok Token where to move to. 187349cc55cSDimitry Andric /// @param RequireStartOfLine Whether the next line depends on being in the 188349cc55cSDimitry Andric /// first column, such as a directive. 189349cc55cSDimitry Andric /// 190349cc55cSDimitry Andric /// @return Whether column adjustments are necessary. 191349cc55cSDimitry Andric bool MoveToLine(const Token &Tok, bool RequireStartOfLine) { 192349cc55cSDimitry Andric PresumedLoc PLoc = SM.getPresumedLoc(Tok.getLocation()); 193349cc55cSDimitry Andric unsigned TargetLine = PLoc.isValid() ? PLoc.getLine() : CurLine; 19481ad6265SDimitry Andric bool IsFirstInFile = 19581ad6265SDimitry Andric Tok.isAtStartOfLine() && PLoc.isValid() && PLoc.getLine() == 1; 196349cc55cSDimitry Andric return MoveToLine(TargetLine, RequireStartOfLine) || IsFirstInFile; 1970b57cec5SDimitry Andric } 198349cc55cSDimitry Andric 199349cc55cSDimitry Andric /// Move to the line of the provided source location. Returns true if a new 200349cc55cSDimitry Andric /// line was inserted. 201349cc55cSDimitry Andric bool MoveToLine(SourceLocation Loc, bool RequireStartOfLine) { 202349cc55cSDimitry Andric PresumedLoc PLoc = SM.getPresumedLoc(Loc); 203349cc55cSDimitry Andric unsigned TargetLine = PLoc.isValid() ? PLoc.getLine() : CurLine; 204349cc55cSDimitry Andric return MoveToLine(TargetLine, RequireStartOfLine); 205349cc55cSDimitry Andric } 206349cc55cSDimitry Andric bool MoveToLine(unsigned LineNo, bool RequireStartOfLine); 2070b57cec5SDimitry Andric 2080b57cec5SDimitry Andric bool AvoidConcat(const Token &PrevPrevTok, const Token &PrevTok, 2090b57cec5SDimitry Andric const Token &Tok) { 2100b57cec5SDimitry Andric return ConcatInfo.AvoidConcat(PrevPrevTok, PrevTok, Tok); 2110b57cec5SDimitry Andric } 2120b57cec5SDimitry Andric void WriteLineInfo(unsigned LineNo, const char *Extra=nullptr, 2130b57cec5SDimitry Andric unsigned ExtraLen=0); 2140b57cec5SDimitry Andric bool LineMarkersAreDisabled() const { return DisableLineMarkers; } 2150b57cec5SDimitry Andric void HandleNewlinesInToken(const char *TokStr, unsigned Len); 2160b57cec5SDimitry Andric 2170b57cec5SDimitry Andric /// MacroDefined - This hook is called whenever a macro definition is seen. 2180b57cec5SDimitry Andric void MacroDefined(const Token &MacroNameTok, 2190b57cec5SDimitry Andric const MacroDirective *MD) override; 2200b57cec5SDimitry Andric 2210b57cec5SDimitry Andric /// MacroUndefined - This hook is called whenever a macro #undef is seen. 2220b57cec5SDimitry Andric void MacroUndefined(const Token &MacroNameTok, 2230b57cec5SDimitry Andric const MacroDefinition &MD, 2240b57cec5SDimitry Andric const MacroDirective *Undef) override; 2250b57cec5SDimitry Andric 2260b57cec5SDimitry Andric void BeginModule(const Module *M); 2270b57cec5SDimitry Andric void EndModule(const Module *M); 2280b57cec5SDimitry Andric }; 2290b57cec5SDimitry Andric } // end anonymous namespace 2300b57cec5SDimitry Andric 2310b57cec5SDimitry Andric void PrintPPOutputPPCallbacks::WriteLineInfo(unsigned LineNo, 2320b57cec5SDimitry Andric const char *Extra, 2330b57cec5SDimitry Andric unsigned ExtraLen) { 234349cc55cSDimitry Andric startNewLineIfNeeded(); 2350b57cec5SDimitry Andric 2360b57cec5SDimitry Andric // Emit #line directives or GNU line markers depending on what mode we're in. 2370b57cec5SDimitry Andric if (UseLineDirectives) { 2380b57cec5SDimitry Andric OS << "#line" << ' ' << LineNo << ' ' << '"'; 2390b57cec5SDimitry Andric OS.write_escaped(CurFilename); 2400b57cec5SDimitry Andric OS << '"'; 2410b57cec5SDimitry Andric } else { 2420b57cec5SDimitry Andric OS << '#' << ' ' << LineNo << ' ' << '"'; 2430b57cec5SDimitry Andric OS.write_escaped(CurFilename); 2440b57cec5SDimitry Andric OS << '"'; 2450b57cec5SDimitry Andric 2460b57cec5SDimitry Andric if (ExtraLen) 2470b57cec5SDimitry Andric OS.write(Extra, ExtraLen); 2480b57cec5SDimitry Andric 2490b57cec5SDimitry Andric if (FileType == SrcMgr::C_System) 2500b57cec5SDimitry Andric OS.write(" 3", 2); 2510b57cec5SDimitry Andric else if (FileType == SrcMgr::C_ExternCSystem) 2520b57cec5SDimitry Andric OS.write(" 3 4", 4); 2530b57cec5SDimitry Andric } 2540b57cec5SDimitry Andric OS << '\n'; 2550b57cec5SDimitry Andric } 2560b57cec5SDimitry Andric 2570b57cec5SDimitry Andric /// MoveToLine - Move the output to the source line specified by the location 2580b57cec5SDimitry Andric /// object. We can do this by emitting some number of \n's, or be emitting a 2590b57cec5SDimitry Andric /// #line directive. This returns false if already at the specified line, true 2600b57cec5SDimitry Andric /// if some newlines were emitted. 261349cc55cSDimitry Andric bool PrintPPOutputPPCallbacks::MoveToLine(unsigned LineNo, 262349cc55cSDimitry Andric bool RequireStartOfLine) { 263349cc55cSDimitry Andric // If it is required to start a new line or finish the current, insert 264349cc55cSDimitry Andric // vertical whitespace now and take it into account when moving to the 265349cc55cSDimitry Andric // expected line. 266349cc55cSDimitry Andric bool StartedNewLine = false; 267349cc55cSDimitry Andric if ((RequireStartOfLine && EmittedTokensOnThisLine) || 268349cc55cSDimitry Andric EmittedDirectiveOnThisLine) { 269349cc55cSDimitry Andric OS << '\n'; 270349cc55cSDimitry Andric StartedNewLine = true; 271349cc55cSDimitry Andric CurLine += 1; 272349cc55cSDimitry Andric EmittedTokensOnThisLine = false; 273349cc55cSDimitry Andric EmittedDirectiveOnThisLine = false; 274349cc55cSDimitry Andric } 275349cc55cSDimitry Andric 2760b57cec5SDimitry Andric // If this line is "close enough" to the original line, just print newlines, 2770b57cec5SDimitry Andric // otherwise print a #line directive. 278349cc55cSDimitry Andric if (CurLine == LineNo) { 279349cc55cSDimitry Andric // Nothing to do if we are already on the correct line. 280349cc55cSDimitry Andric } else if (MinimizeWhitespace && DisableLineMarkers) { 281349cc55cSDimitry Andric // With -E -P -fminimize-whitespace, don't emit anything if not necessary. 282349cc55cSDimitry Andric } else if (!StartedNewLine && LineNo - CurLine == 1) { 283349cc55cSDimitry Andric // Printing a single line has priority over printing a #line directive, even 284349cc55cSDimitry Andric // when minimizing whitespace which otherwise would print #line directives 285349cc55cSDimitry Andric // for every single line. 2860b57cec5SDimitry Andric OS << '\n'; 287349cc55cSDimitry Andric StartedNewLine = true; 288349cc55cSDimitry Andric } else if (!DisableLineMarkers) { 289349cc55cSDimitry Andric if (LineNo - CurLine <= 8) { 2900b57cec5SDimitry Andric const char *NewLines = "\n\n\n\n\n\n\n\n"; 2910b57cec5SDimitry Andric OS.write(NewLines, LineNo - CurLine); 292349cc55cSDimitry Andric } else { 2930b57cec5SDimitry Andric // Emit a #line or line marker. 2940b57cec5SDimitry Andric WriteLineInfo(LineNo, nullptr, 0); 295349cc55cSDimitry Andric } 296349cc55cSDimitry Andric StartedNewLine = true; 297349cc55cSDimitry Andric } else if (EmittedTokensOnThisLine) { 298349cc55cSDimitry Andric // If we are not on the correct line and don't need to be line-correct, 299349cc55cSDimitry Andric // at least ensure we start on a new line. 300349cc55cSDimitry Andric OS << '\n'; 301349cc55cSDimitry Andric StartedNewLine = true; 302349cc55cSDimitry Andric } 303349cc55cSDimitry Andric 304349cc55cSDimitry Andric if (StartedNewLine) { 305349cc55cSDimitry Andric EmittedTokensOnThisLine = false; 306349cc55cSDimitry Andric EmittedDirectiveOnThisLine = false; 3070b57cec5SDimitry Andric } 3080b57cec5SDimitry Andric 3090b57cec5SDimitry Andric CurLine = LineNo; 310349cc55cSDimitry Andric return StartedNewLine; 3110b57cec5SDimitry Andric } 3120b57cec5SDimitry Andric 313349cc55cSDimitry Andric void PrintPPOutputPPCallbacks::startNewLineIfNeeded() { 3140b57cec5SDimitry Andric if (EmittedTokensOnThisLine || EmittedDirectiveOnThisLine) { 3150b57cec5SDimitry Andric OS << '\n'; 3160b57cec5SDimitry Andric EmittedTokensOnThisLine = false; 3170b57cec5SDimitry Andric EmittedDirectiveOnThisLine = false; 3180b57cec5SDimitry Andric } 3190b57cec5SDimitry Andric } 3200b57cec5SDimitry Andric 3210b57cec5SDimitry Andric /// FileChanged - Whenever the preprocessor enters or exits a #include file 3220b57cec5SDimitry Andric /// it invokes this handler. Update our conception of the current source 3230b57cec5SDimitry Andric /// position. 3240b57cec5SDimitry Andric void PrintPPOutputPPCallbacks::FileChanged(SourceLocation Loc, 3250b57cec5SDimitry Andric FileChangeReason Reason, 3260b57cec5SDimitry Andric SrcMgr::CharacteristicKind NewFileType, 3270b57cec5SDimitry Andric FileID PrevFID) { 3280b57cec5SDimitry Andric // Unless we are exiting a #include, make sure to skip ahead to the line the 3290b57cec5SDimitry Andric // #include directive was at. 3300b57cec5SDimitry Andric SourceManager &SourceMgr = SM; 3310b57cec5SDimitry Andric 3320b57cec5SDimitry Andric PresumedLoc UserLoc = SourceMgr.getPresumedLoc(Loc); 3330b57cec5SDimitry Andric if (UserLoc.isInvalid()) 3340b57cec5SDimitry Andric return; 3350b57cec5SDimitry Andric 3360b57cec5SDimitry Andric unsigned NewLine = UserLoc.getLine(); 3370b57cec5SDimitry Andric 3380b57cec5SDimitry Andric if (Reason == PPCallbacks::EnterFile) { 3390b57cec5SDimitry Andric SourceLocation IncludeLoc = UserLoc.getIncludeLoc(); 3400b57cec5SDimitry Andric if (IncludeLoc.isValid()) 341349cc55cSDimitry Andric MoveToLine(IncludeLoc, /*RequireStartOfLine=*/false); 3420b57cec5SDimitry Andric } else if (Reason == PPCallbacks::SystemHeaderPragma) { 3430b57cec5SDimitry Andric // GCC emits the # directive for this directive on the line AFTER the 3440b57cec5SDimitry Andric // directive and emits a bunch of spaces that aren't needed. This is because 3450b57cec5SDimitry Andric // otherwise we will emit a line marker for THIS line, which requires an 3460b57cec5SDimitry Andric // extra blank line after the directive to avoid making all following lines 3470b57cec5SDimitry Andric // off by one. We can do better by simply incrementing NewLine here. 3480b57cec5SDimitry Andric NewLine += 1; 3490b57cec5SDimitry Andric } 3500b57cec5SDimitry Andric 3510b57cec5SDimitry Andric CurLine = NewLine; 3520b57cec5SDimitry Andric 3530b57cec5SDimitry Andric CurFilename.clear(); 3540b57cec5SDimitry Andric CurFilename += UserLoc.getFilename(); 3550b57cec5SDimitry Andric FileType = NewFileType; 3560b57cec5SDimitry Andric 3570b57cec5SDimitry Andric if (DisableLineMarkers) { 358349cc55cSDimitry Andric if (!MinimizeWhitespace) 359349cc55cSDimitry Andric startNewLineIfNeeded(); 3600b57cec5SDimitry Andric return; 3610b57cec5SDimitry Andric } 3620b57cec5SDimitry Andric 3630b57cec5SDimitry Andric if (!Initialized) { 3640b57cec5SDimitry Andric WriteLineInfo(CurLine); 3650b57cec5SDimitry Andric Initialized = true; 3660b57cec5SDimitry Andric } 3670b57cec5SDimitry Andric 3680b57cec5SDimitry Andric // Do not emit an enter marker for the main file (which we expect is the first 3690b57cec5SDimitry Andric // entered file). This matches gcc, and improves compatibility with some tools 3700b57cec5SDimitry Andric // which track the # line markers as a way to determine when the preprocessed 3710b57cec5SDimitry Andric // output is in the context of the main file. 3720b57cec5SDimitry Andric if (Reason == PPCallbacks::EnterFile && !IsFirstFileEntered) { 3730b57cec5SDimitry Andric IsFirstFileEntered = true; 3740b57cec5SDimitry Andric return; 3750b57cec5SDimitry Andric } 3760b57cec5SDimitry Andric 3770b57cec5SDimitry Andric switch (Reason) { 3780b57cec5SDimitry Andric case PPCallbacks::EnterFile: 3790b57cec5SDimitry Andric WriteLineInfo(CurLine, " 1", 2); 3800b57cec5SDimitry Andric break; 3810b57cec5SDimitry Andric case PPCallbacks::ExitFile: 3820b57cec5SDimitry Andric WriteLineInfo(CurLine, " 2", 2); 3830b57cec5SDimitry Andric break; 3840b57cec5SDimitry Andric case PPCallbacks::SystemHeaderPragma: 3850b57cec5SDimitry Andric case PPCallbacks::RenameFile: 3860b57cec5SDimitry Andric WriteLineInfo(CurLine); 3870b57cec5SDimitry Andric break; 3880b57cec5SDimitry Andric } 3890b57cec5SDimitry Andric } 3900b57cec5SDimitry Andric 3910b57cec5SDimitry Andric void PrintPPOutputPPCallbacks::InclusionDirective( 392*bdd1243dSDimitry Andric SourceLocation HashLoc, const Token &IncludeTok, StringRef FileName, 393*bdd1243dSDimitry Andric bool IsAngled, CharSourceRange FilenameRange, OptionalFileEntryRef File, 394*bdd1243dSDimitry Andric StringRef SearchPath, StringRef RelativePath, const Module *Imported, 3950b57cec5SDimitry Andric SrcMgr::CharacteristicKind FileType) { 3960b57cec5SDimitry Andric // In -dI mode, dump #include directives prior to dumping their content or 3970b57cec5SDimitry Andric // interpretation. 3980b57cec5SDimitry Andric if (DumpIncludeDirectives) { 399349cc55cSDimitry Andric MoveToLine(HashLoc, /*RequireStartOfLine=*/true); 4000b57cec5SDimitry Andric const std::string TokenText = PP.getSpelling(IncludeTok); 4010b57cec5SDimitry Andric assert(!TokenText.empty()); 4020b57cec5SDimitry Andric OS << "#" << TokenText << " " 4030b57cec5SDimitry Andric << (IsAngled ? '<' : '"') << FileName << (IsAngled ? '>' : '"') 4040b57cec5SDimitry Andric << " /* clang -E -dI */"; 4050b57cec5SDimitry Andric setEmittedDirectiveOnThisLine(); 4060b57cec5SDimitry Andric } 4070b57cec5SDimitry Andric 4080b57cec5SDimitry Andric // When preprocessing, turn implicit imports into module import pragmas. 4090b57cec5SDimitry Andric if (Imported) { 4100b57cec5SDimitry Andric switch (IncludeTok.getIdentifierInfo()->getPPKeywordID()) { 4110b57cec5SDimitry Andric case tok::pp_include: 4120b57cec5SDimitry Andric case tok::pp_import: 4130b57cec5SDimitry Andric case tok::pp_include_next: 414349cc55cSDimitry Andric MoveToLine(HashLoc, /*RequireStartOfLine=*/true); 4150b57cec5SDimitry Andric OS << "#pragma clang module import " << Imported->getFullModuleName(true) 4160b57cec5SDimitry Andric << " /* clang -E: implicit import for " 4170b57cec5SDimitry Andric << "#" << PP.getSpelling(IncludeTok) << " " 4180b57cec5SDimitry Andric << (IsAngled ? '<' : '"') << FileName << (IsAngled ? '>' : '"') 4190b57cec5SDimitry Andric << " */"; 420349cc55cSDimitry Andric setEmittedDirectiveOnThisLine(); 4210b57cec5SDimitry Andric break; 4220b57cec5SDimitry Andric 4230b57cec5SDimitry Andric case tok::pp___include_macros: 4240b57cec5SDimitry Andric // #__include_macros has no effect on a user of a preprocessed source 4250b57cec5SDimitry Andric // file; the only effect is on preprocessing. 4260b57cec5SDimitry Andric // 4270b57cec5SDimitry Andric // FIXME: That's not *quite* true: it causes the module in question to 4280b57cec5SDimitry Andric // be loaded, which can affect downstream diagnostics. 4290b57cec5SDimitry Andric break; 4300b57cec5SDimitry Andric 4310b57cec5SDimitry Andric default: 4320b57cec5SDimitry Andric llvm_unreachable("unknown include directive kind"); 4330b57cec5SDimitry Andric break; 4340b57cec5SDimitry Andric } 4350b57cec5SDimitry Andric } 4360b57cec5SDimitry Andric } 4370b57cec5SDimitry Andric 4380b57cec5SDimitry Andric /// Handle entering the scope of a module during a module compilation. 4390b57cec5SDimitry Andric void PrintPPOutputPPCallbacks::BeginModule(const Module *M) { 4400b57cec5SDimitry Andric startNewLineIfNeeded(); 4410b57cec5SDimitry Andric OS << "#pragma clang module begin " << M->getFullModuleName(true); 4420b57cec5SDimitry Andric setEmittedDirectiveOnThisLine(); 4430b57cec5SDimitry Andric } 4440b57cec5SDimitry Andric 4450b57cec5SDimitry Andric /// Handle leaving the scope of a module during a module compilation. 4460b57cec5SDimitry Andric void PrintPPOutputPPCallbacks::EndModule(const Module *M) { 4470b57cec5SDimitry Andric startNewLineIfNeeded(); 4480b57cec5SDimitry Andric OS << "#pragma clang module end /*" << M->getFullModuleName(true) << "*/"; 4490b57cec5SDimitry Andric setEmittedDirectiveOnThisLine(); 4500b57cec5SDimitry Andric } 4510b57cec5SDimitry Andric 4520b57cec5SDimitry Andric /// Ident - Handle #ident directives when read by the preprocessor. 4530b57cec5SDimitry Andric /// 4540b57cec5SDimitry Andric void PrintPPOutputPPCallbacks::Ident(SourceLocation Loc, StringRef S) { 455349cc55cSDimitry Andric MoveToLine(Loc, /*RequireStartOfLine=*/true); 4560b57cec5SDimitry Andric 4570b57cec5SDimitry Andric OS.write("#ident ", strlen("#ident ")); 4580b57cec5SDimitry Andric OS.write(S.begin(), S.size()); 459349cc55cSDimitry Andric setEmittedTokensOnThisLine(); 4600b57cec5SDimitry Andric } 4610b57cec5SDimitry Andric 4620b57cec5SDimitry Andric /// MacroDefined - This hook is called whenever a macro definition is seen. 4630b57cec5SDimitry Andric void PrintPPOutputPPCallbacks::MacroDefined(const Token &MacroNameTok, 4640b57cec5SDimitry Andric const MacroDirective *MD) { 4650b57cec5SDimitry Andric const MacroInfo *MI = MD->getMacroInfo(); 46681ad6265SDimitry Andric // Print out macro definitions in -dD mode and when we have -fdirectives-only 46781ad6265SDimitry Andric // for C++20 header units. 46881ad6265SDimitry Andric if ((!DumpDefines && !DirectivesOnly) || 4690b57cec5SDimitry Andric // Ignore __FILE__ etc. 47081ad6265SDimitry Andric MI->isBuiltinMacro()) 47181ad6265SDimitry Andric return; 4720b57cec5SDimitry Andric 47381ad6265SDimitry Andric SourceLocation DefLoc = MI->getDefinitionLoc(); 47481ad6265SDimitry Andric if (DirectivesOnly && !MI->isUsed()) { 47581ad6265SDimitry Andric SourceManager &SM = PP.getSourceManager(); 47681ad6265SDimitry Andric if (SM.isWrittenInBuiltinFile(DefLoc) || 47781ad6265SDimitry Andric SM.isWrittenInCommandLineFile(DefLoc)) 47881ad6265SDimitry Andric return; 47981ad6265SDimitry Andric } 48081ad6265SDimitry Andric MoveToLine(DefLoc, /*RequireStartOfLine=*/true); 4810b57cec5SDimitry Andric PrintMacroDefinition(*MacroNameTok.getIdentifierInfo(), *MI, PP, OS); 4820b57cec5SDimitry Andric setEmittedDirectiveOnThisLine(); 4830b57cec5SDimitry Andric } 4840b57cec5SDimitry Andric 4850b57cec5SDimitry Andric void PrintPPOutputPPCallbacks::MacroUndefined(const Token &MacroNameTok, 4860b57cec5SDimitry Andric const MacroDefinition &MD, 4870b57cec5SDimitry Andric const MacroDirective *Undef) { 48881ad6265SDimitry Andric // Print out macro definitions in -dD mode and when we have -fdirectives-only 48981ad6265SDimitry Andric // for C++20 header units. 49081ad6265SDimitry Andric if (!DumpDefines && !DirectivesOnly) 49181ad6265SDimitry Andric return; 4920b57cec5SDimitry Andric 493349cc55cSDimitry Andric MoveToLine(MacroNameTok.getLocation(), /*RequireStartOfLine=*/true); 4940b57cec5SDimitry Andric OS << "#undef " << MacroNameTok.getIdentifierInfo()->getName(); 4950b57cec5SDimitry Andric setEmittedDirectiveOnThisLine(); 4960b57cec5SDimitry Andric } 4970b57cec5SDimitry Andric 4980b57cec5SDimitry Andric static void outputPrintable(raw_ostream &OS, StringRef Str) { 4990b57cec5SDimitry Andric for (unsigned char Char : Str) { 5000b57cec5SDimitry Andric if (isPrintable(Char) && Char != '\\' && Char != '"') 5010b57cec5SDimitry Andric OS << (char)Char; 5020b57cec5SDimitry Andric else // Output anything hard as an octal escape. 5030b57cec5SDimitry Andric OS << '\\' 5040b57cec5SDimitry Andric << (char)('0' + ((Char >> 6) & 7)) 5050b57cec5SDimitry Andric << (char)('0' + ((Char >> 3) & 7)) 5060b57cec5SDimitry Andric << (char)('0' + ((Char >> 0) & 7)); 5070b57cec5SDimitry Andric } 5080b57cec5SDimitry Andric } 5090b57cec5SDimitry Andric 5100b57cec5SDimitry Andric void PrintPPOutputPPCallbacks::PragmaMessage(SourceLocation Loc, 5110b57cec5SDimitry Andric StringRef Namespace, 5120b57cec5SDimitry Andric PragmaMessageKind Kind, 5130b57cec5SDimitry Andric StringRef Str) { 514349cc55cSDimitry Andric MoveToLine(Loc, /*RequireStartOfLine=*/true); 5150b57cec5SDimitry Andric OS << "#pragma "; 5160b57cec5SDimitry Andric if (!Namespace.empty()) 5170b57cec5SDimitry Andric OS << Namespace << ' '; 5180b57cec5SDimitry Andric switch (Kind) { 5190b57cec5SDimitry Andric case PMK_Message: 5200b57cec5SDimitry Andric OS << "message(\""; 5210b57cec5SDimitry Andric break; 5220b57cec5SDimitry Andric case PMK_Warning: 5230b57cec5SDimitry Andric OS << "warning \""; 5240b57cec5SDimitry Andric break; 5250b57cec5SDimitry Andric case PMK_Error: 5260b57cec5SDimitry Andric OS << "error \""; 5270b57cec5SDimitry Andric break; 5280b57cec5SDimitry Andric } 5290b57cec5SDimitry Andric 5300b57cec5SDimitry Andric outputPrintable(OS, Str); 5310b57cec5SDimitry Andric OS << '"'; 5320b57cec5SDimitry Andric if (Kind == PMK_Message) 5330b57cec5SDimitry Andric OS << ')'; 5340b57cec5SDimitry Andric setEmittedDirectiveOnThisLine(); 5350b57cec5SDimitry Andric } 5360b57cec5SDimitry Andric 5370b57cec5SDimitry Andric void PrintPPOutputPPCallbacks::PragmaDebug(SourceLocation Loc, 5380b57cec5SDimitry Andric StringRef DebugType) { 539349cc55cSDimitry Andric MoveToLine(Loc, /*RequireStartOfLine=*/true); 5400b57cec5SDimitry Andric 5410b57cec5SDimitry Andric OS << "#pragma clang __debug "; 5420b57cec5SDimitry Andric OS << DebugType; 5430b57cec5SDimitry Andric 5440b57cec5SDimitry Andric setEmittedDirectiveOnThisLine(); 5450b57cec5SDimitry Andric } 5460b57cec5SDimitry Andric 5470b57cec5SDimitry Andric void PrintPPOutputPPCallbacks:: 5480b57cec5SDimitry Andric PragmaDiagnosticPush(SourceLocation Loc, StringRef Namespace) { 549349cc55cSDimitry Andric MoveToLine(Loc, /*RequireStartOfLine=*/true); 5500b57cec5SDimitry Andric OS << "#pragma " << Namespace << " diagnostic push"; 5510b57cec5SDimitry Andric setEmittedDirectiveOnThisLine(); 5520b57cec5SDimitry Andric } 5530b57cec5SDimitry Andric 5540b57cec5SDimitry Andric void PrintPPOutputPPCallbacks:: 5550b57cec5SDimitry Andric PragmaDiagnosticPop(SourceLocation Loc, StringRef Namespace) { 556349cc55cSDimitry Andric MoveToLine(Loc, /*RequireStartOfLine=*/true); 5570b57cec5SDimitry Andric OS << "#pragma " << Namespace << " diagnostic pop"; 5580b57cec5SDimitry Andric setEmittedDirectiveOnThisLine(); 5590b57cec5SDimitry Andric } 5600b57cec5SDimitry Andric 5610b57cec5SDimitry Andric void PrintPPOutputPPCallbacks::PragmaDiagnostic(SourceLocation Loc, 5620b57cec5SDimitry Andric StringRef Namespace, 5630b57cec5SDimitry Andric diag::Severity Map, 5640b57cec5SDimitry Andric StringRef Str) { 565349cc55cSDimitry Andric MoveToLine(Loc, /*RequireStartOfLine=*/true); 5660b57cec5SDimitry Andric OS << "#pragma " << Namespace << " diagnostic "; 5670b57cec5SDimitry Andric switch (Map) { 5680b57cec5SDimitry Andric case diag::Severity::Remark: 5690b57cec5SDimitry Andric OS << "remark"; 5700b57cec5SDimitry Andric break; 5710b57cec5SDimitry Andric case diag::Severity::Warning: 5720b57cec5SDimitry Andric OS << "warning"; 5730b57cec5SDimitry Andric break; 5740b57cec5SDimitry Andric case diag::Severity::Error: 5750b57cec5SDimitry Andric OS << "error"; 5760b57cec5SDimitry Andric break; 5770b57cec5SDimitry Andric case diag::Severity::Ignored: 5780b57cec5SDimitry Andric OS << "ignored"; 5790b57cec5SDimitry Andric break; 5800b57cec5SDimitry Andric case diag::Severity::Fatal: 5810b57cec5SDimitry Andric OS << "fatal"; 5820b57cec5SDimitry Andric break; 5830b57cec5SDimitry Andric } 5840b57cec5SDimitry Andric OS << " \"" << Str << '"'; 5850b57cec5SDimitry Andric setEmittedDirectiveOnThisLine(); 5860b57cec5SDimitry Andric } 5870b57cec5SDimitry Andric 5880b57cec5SDimitry Andric void PrintPPOutputPPCallbacks::PragmaWarning(SourceLocation Loc, 589349cc55cSDimitry Andric PragmaWarningSpecifier WarningSpec, 5900b57cec5SDimitry Andric ArrayRef<int> Ids) { 591349cc55cSDimitry Andric MoveToLine(Loc, /*RequireStartOfLine=*/true); 592349cc55cSDimitry Andric 593349cc55cSDimitry Andric OS << "#pragma warning("; 594349cc55cSDimitry Andric switch(WarningSpec) { 595349cc55cSDimitry Andric case PWS_Default: OS << "default"; break; 596349cc55cSDimitry Andric case PWS_Disable: OS << "disable"; break; 597349cc55cSDimitry Andric case PWS_Error: OS << "error"; break; 598349cc55cSDimitry Andric case PWS_Once: OS << "once"; break; 599349cc55cSDimitry Andric case PWS_Suppress: OS << "suppress"; break; 600349cc55cSDimitry Andric case PWS_Level1: OS << '1'; break; 601349cc55cSDimitry Andric case PWS_Level2: OS << '2'; break; 602349cc55cSDimitry Andric case PWS_Level3: OS << '3'; break; 603349cc55cSDimitry Andric case PWS_Level4: OS << '4'; break; 604349cc55cSDimitry Andric } 605349cc55cSDimitry Andric OS << ':'; 606349cc55cSDimitry Andric 6070b57cec5SDimitry Andric for (ArrayRef<int>::iterator I = Ids.begin(), E = Ids.end(); I != E; ++I) 6080b57cec5SDimitry Andric OS << ' ' << *I; 6090b57cec5SDimitry Andric OS << ')'; 6100b57cec5SDimitry Andric setEmittedDirectiveOnThisLine(); 6110b57cec5SDimitry Andric } 6120b57cec5SDimitry Andric 6130b57cec5SDimitry Andric void PrintPPOutputPPCallbacks::PragmaWarningPush(SourceLocation Loc, 6140b57cec5SDimitry Andric int Level) { 615349cc55cSDimitry Andric MoveToLine(Loc, /*RequireStartOfLine=*/true); 6160b57cec5SDimitry Andric OS << "#pragma warning(push"; 6170b57cec5SDimitry Andric if (Level >= 0) 6180b57cec5SDimitry Andric OS << ", " << Level; 6190b57cec5SDimitry Andric OS << ')'; 6200b57cec5SDimitry Andric setEmittedDirectiveOnThisLine(); 6210b57cec5SDimitry Andric } 6220b57cec5SDimitry Andric 6230b57cec5SDimitry Andric void PrintPPOutputPPCallbacks::PragmaWarningPop(SourceLocation Loc) { 624349cc55cSDimitry Andric MoveToLine(Loc, /*RequireStartOfLine=*/true); 6250b57cec5SDimitry Andric OS << "#pragma warning(pop)"; 6260b57cec5SDimitry Andric setEmittedDirectiveOnThisLine(); 6270b57cec5SDimitry Andric } 6280b57cec5SDimitry Andric 6290b57cec5SDimitry Andric void PrintPPOutputPPCallbacks::PragmaExecCharsetPush(SourceLocation Loc, 6300b57cec5SDimitry Andric StringRef Str) { 631349cc55cSDimitry Andric MoveToLine(Loc, /*RequireStartOfLine=*/true); 6320b57cec5SDimitry Andric OS << "#pragma character_execution_set(push"; 6330b57cec5SDimitry Andric if (!Str.empty()) 6340b57cec5SDimitry Andric OS << ", " << Str; 6350b57cec5SDimitry Andric OS << ')'; 6360b57cec5SDimitry Andric setEmittedDirectiveOnThisLine(); 6370b57cec5SDimitry Andric } 6380b57cec5SDimitry Andric 6390b57cec5SDimitry Andric void PrintPPOutputPPCallbacks::PragmaExecCharsetPop(SourceLocation Loc) { 640349cc55cSDimitry Andric MoveToLine(Loc, /*RequireStartOfLine=*/true); 6410b57cec5SDimitry Andric OS << "#pragma character_execution_set(pop)"; 6420b57cec5SDimitry Andric setEmittedDirectiveOnThisLine(); 6430b57cec5SDimitry Andric } 6440b57cec5SDimitry Andric 6450b57cec5SDimitry Andric void PrintPPOutputPPCallbacks:: 6460b57cec5SDimitry Andric PragmaAssumeNonNullBegin(SourceLocation Loc) { 647349cc55cSDimitry Andric MoveToLine(Loc, /*RequireStartOfLine=*/true); 6480b57cec5SDimitry Andric OS << "#pragma clang assume_nonnull begin"; 6490b57cec5SDimitry Andric setEmittedDirectiveOnThisLine(); 6500b57cec5SDimitry Andric } 6510b57cec5SDimitry Andric 6520b57cec5SDimitry Andric void PrintPPOutputPPCallbacks:: 6530b57cec5SDimitry Andric PragmaAssumeNonNullEnd(SourceLocation Loc) { 654349cc55cSDimitry Andric MoveToLine(Loc, /*RequireStartOfLine=*/true); 6550b57cec5SDimitry Andric OS << "#pragma clang assume_nonnull end"; 6560b57cec5SDimitry Andric setEmittedDirectiveOnThisLine(); 6570b57cec5SDimitry Andric } 6580b57cec5SDimitry Andric 659349cc55cSDimitry Andric void PrintPPOutputPPCallbacks::HandleWhitespaceBeforeTok(const Token &Tok, 660349cc55cSDimitry Andric bool RequireSpace, 661349cc55cSDimitry Andric bool RequireSameLine) { 662349cc55cSDimitry Andric // These tokens are not expanded to anything and don't need whitespace before 663349cc55cSDimitry Andric // them. 664349cc55cSDimitry Andric if (Tok.is(tok::eof) || 665349cc55cSDimitry Andric (Tok.isAnnotation() && !Tok.is(tok::annot_header_unit) && 666349cc55cSDimitry Andric !Tok.is(tok::annot_module_begin) && !Tok.is(tok::annot_module_end))) 667349cc55cSDimitry Andric return; 6680b57cec5SDimitry Andric 669349cc55cSDimitry Andric // EmittedDirectiveOnThisLine takes priority over RequireSameLine. 670349cc55cSDimitry Andric if ((!RequireSameLine || EmittedDirectiveOnThisLine) && 671349cc55cSDimitry Andric MoveToLine(Tok, /*RequireStartOfLine=*/EmittedDirectiveOnThisLine)) { 672349cc55cSDimitry Andric if (MinimizeWhitespace) { 673349cc55cSDimitry Andric // Avoid interpreting hash as a directive under -fpreprocessed. 674349cc55cSDimitry Andric if (Tok.is(tok::hash)) 675349cc55cSDimitry Andric OS << ' '; 676349cc55cSDimitry Andric } else { 6770b57cec5SDimitry Andric // Print out space characters so that the first token on a line is 6780b57cec5SDimitry Andric // indented for easy reading. 6790b57cec5SDimitry Andric unsigned ColNo = SM.getExpansionColumnNumber(Tok.getLocation()); 6800b57cec5SDimitry Andric 681349cc55cSDimitry Andric // The first token on a line can have a column number of 1, yet still 682349cc55cSDimitry Andric // expect leading white space, if a macro expansion in column 1 starts 683349cc55cSDimitry Andric // with an empty macro argument, or an empty nested macro expansion. In 684349cc55cSDimitry Andric // this case, move the token to column 2. 6850b57cec5SDimitry Andric if (ColNo == 1 && Tok.hasLeadingSpace()) 6860b57cec5SDimitry Andric ColNo = 2; 6870b57cec5SDimitry Andric 6880b57cec5SDimitry Andric // This hack prevents stuff like: 6890b57cec5SDimitry Andric // #define HASH # 6900b57cec5SDimitry Andric // HASH define foo bar 6910b57cec5SDimitry Andric // From having the # character end up at column 1, which makes it so it 6920b57cec5SDimitry Andric // is not handled as a #define next time through the preprocessor if in 6930b57cec5SDimitry Andric // -fpreprocessed mode. 6940b57cec5SDimitry Andric if (ColNo <= 1 && Tok.is(tok::hash)) 6950b57cec5SDimitry Andric OS << ' '; 6960b57cec5SDimitry Andric 6970b57cec5SDimitry Andric // Otherwise, indent the appropriate number of spaces. 6980b57cec5SDimitry Andric for (; ColNo > 1; --ColNo) 6990b57cec5SDimitry Andric OS << ' '; 700349cc55cSDimitry Andric } 701349cc55cSDimitry Andric } else { 702349cc55cSDimitry Andric // Insert whitespace between the previous and next token if either 703349cc55cSDimitry Andric // - The caller requires it 704349cc55cSDimitry Andric // - The input had whitespace between them and we are not in 705349cc55cSDimitry Andric // whitespace-minimization mode 706349cc55cSDimitry Andric // - The whitespace is necessary to keep the tokens apart and there is not 707349cc55cSDimitry Andric // already a newline between them 708349cc55cSDimitry Andric if (RequireSpace || (!MinimizeWhitespace && Tok.hasLeadingSpace()) || 709349cc55cSDimitry Andric ((EmittedTokensOnThisLine || EmittedDirectiveOnThisLine) && 710349cc55cSDimitry Andric AvoidConcat(PrevPrevTok, PrevTok, Tok))) 711349cc55cSDimitry Andric OS << ' '; 712349cc55cSDimitry Andric } 7130b57cec5SDimitry Andric 714349cc55cSDimitry Andric PrevPrevTok = PrevTok; 715349cc55cSDimitry Andric PrevTok = Tok; 7160b57cec5SDimitry Andric } 7170b57cec5SDimitry Andric 7180b57cec5SDimitry Andric void PrintPPOutputPPCallbacks::HandleNewlinesInToken(const char *TokStr, 7190b57cec5SDimitry Andric unsigned Len) { 7200b57cec5SDimitry Andric unsigned NumNewlines = 0; 7210b57cec5SDimitry Andric for (; Len; --Len, ++TokStr) { 7220b57cec5SDimitry Andric if (*TokStr != '\n' && 7230b57cec5SDimitry Andric *TokStr != '\r') 7240b57cec5SDimitry Andric continue; 7250b57cec5SDimitry Andric 7260b57cec5SDimitry Andric ++NumNewlines; 7270b57cec5SDimitry Andric 7280b57cec5SDimitry Andric // If we have \n\r or \r\n, skip both and count as one line. 7290b57cec5SDimitry Andric if (Len != 1 && 7300b57cec5SDimitry Andric (TokStr[1] == '\n' || TokStr[1] == '\r') && 7310b57cec5SDimitry Andric TokStr[0] != TokStr[1]) { 7320b57cec5SDimitry Andric ++TokStr; 7330b57cec5SDimitry Andric --Len; 7340b57cec5SDimitry Andric } 7350b57cec5SDimitry Andric } 7360b57cec5SDimitry Andric 7370b57cec5SDimitry Andric if (NumNewlines == 0) return; 7380b57cec5SDimitry Andric 7390b57cec5SDimitry Andric CurLine += NumNewlines; 7400b57cec5SDimitry Andric } 7410b57cec5SDimitry Andric 7420b57cec5SDimitry Andric 7430b57cec5SDimitry Andric namespace { 7440b57cec5SDimitry Andric struct UnknownPragmaHandler : public PragmaHandler { 7450b57cec5SDimitry Andric const char *Prefix; 7460b57cec5SDimitry Andric PrintPPOutputPPCallbacks *Callbacks; 7470b57cec5SDimitry Andric 7480b57cec5SDimitry Andric // Set to true if tokens should be expanded 7490b57cec5SDimitry Andric bool ShouldExpandTokens; 7500b57cec5SDimitry Andric 7510b57cec5SDimitry Andric UnknownPragmaHandler(const char *prefix, PrintPPOutputPPCallbacks *callbacks, 7520b57cec5SDimitry Andric bool RequireTokenExpansion) 7530b57cec5SDimitry Andric : Prefix(prefix), Callbacks(callbacks), 7540b57cec5SDimitry Andric ShouldExpandTokens(RequireTokenExpansion) {} 7550b57cec5SDimitry Andric void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer, 7560b57cec5SDimitry Andric Token &PragmaTok) override { 7570b57cec5SDimitry Andric // Figure out what line we went to and insert the appropriate number of 7580b57cec5SDimitry Andric // newline characters. 759349cc55cSDimitry Andric Callbacks->MoveToLine(PragmaTok.getLocation(), /*RequireStartOfLine=*/true); 7600b57cec5SDimitry Andric Callbacks->OS.write(Prefix, strlen(Prefix)); 761349cc55cSDimitry Andric Callbacks->setEmittedTokensOnThisLine(); 7620b57cec5SDimitry Andric 7630b57cec5SDimitry Andric if (ShouldExpandTokens) { 7640b57cec5SDimitry Andric // The first token does not have expanded macros. Expand them, if 7650b57cec5SDimitry Andric // required. 766a7dea167SDimitry Andric auto Toks = std::make_unique<Token[]>(1); 7670b57cec5SDimitry Andric Toks[0] = PragmaTok; 7680b57cec5SDimitry Andric PP.EnterTokenStream(std::move(Toks), /*NumToks=*/1, 7690b57cec5SDimitry Andric /*DisableMacroExpansion=*/false, 7700b57cec5SDimitry Andric /*IsReinject=*/false); 7710b57cec5SDimitry Andric PP.Lex(PragmaTok); 7720b57cec5SDimitry Andric } 7730b57cec5SDimitry Andric 7740b57cec5SDimitry Andric // Read and print all of the pragma tokens. 775349cc55cSDimitry Andric bool IsFirst = true; 7760b57cec5SDimitry Andric while (PragmaTok.isNot(tok::eod)) { 777349cc55cSDimitry Andric Callbacks->HandleWhitespaceBeforeTok(PragmaTok, /*RequireSpace=*/IsFirst, 778349cc55cSDimitry Andric /*RequireSameLine=*/true); 779349cc55cSDimitry Andric IsFirst = false; 7800b57cec5SDimitry Andric std::string TokSpell = PP.getSpelling(PragmaTok); 7810b57cec5SDimitry Andric Callbacks->OS.write(&TokSpell[0], TokSpell.size()); 782349cc55cSDimitry Andric Callbacks->setEmittedTokensOnThisLine(); 7830b57cec5SDimitry Andric 7840b57cec5SDimitry Andric if (ShouldExpandTokens) 7850b57cec5SDimitry Andric PP.Lex(PragmaTok); 7860b57cec5SDimitry Andric else 7870b57cec5SDimitry Andric PP.LexUnexpandedToken(PragmaTok); 7880b57cec5SDimitry Andric } 7890b57cec5SDimitry Andric Callbacks->setEmittedDirectiveOnThisLine(); 7900b57cec5SDimitry Andric } 7910b57cec5SDimitry Andric }; 7920b57cec5SDimitry Andric } // end anonymous namespace 7930b57cec5SDimitry Andric 7940b57cec5SDimitry Andric 7950b57cec5SDimitry Andric static void PrintPreprocessedTokens(Preprocessor &PP, Token &Tok, 7960b57cec5SDimitry Andric PrintPPOutputPPCallbacks *Callbacks, 7970b57cec5SDimitry Andric raw_ostream &OS) { 7980b57cec5SDimitry Andric bool DropComments = PP.getLangOpts().TraditionalCPP && 7990b57cec5SDimitry Andric !PP.getCommentRetentionState(); 8000b57cec5SDimitry Andric 801349cc55cSDimitry Andric bool IsStartOfLine = false; 8020b57cec5SDimitry Andric char Buffer[256]; 80304eeddc0SDimitry Andric while (true) { 804349cc55cSDimitry Andric // Two lines joined with line continuation ('\' as last character on the 805349cc55cSDimitry Andric // line) must be emitted as one line even though Tok.getLine() returns two 806349cc55cSDimitry Andric // different values. In this situation Tok.isAtStartOfLine() is false even 807349cc55cSDimitry Andric // though it may be the first token on the lexical line. When 808349cc55cSDimitry Andric // dropping/skipping a token that is at the start of a line, propagate the 809349cc55cSDimitry Andric // start-of-line-ness to the next token to not append it to the previous 810349cc55cSDimitry Andric // line. 811349cc55cSDimitry Andric IsStartOfLine = IsStartOfLine || Tok.isAtStartOfLine(); 8120b57cec5SDimitry Andric 813349cc55cSDimitry Andric Callbacks->HandleWhitespaceBeforeTok(Tok, /*RequireSpace=*/false, 814349cc55cSDimitry Andric /*RequireSameLine=*/!IsStartOfLine); 8150b57cec5SDimitry Andric 8160b57cec5SDimitry Andric if (DropComments && Tok.is(tok::comment)) { 8170b57cec5SDimitry Andric // Skip comments. Normally the preprocessor does not generate 8180b57cec5SDimitry Andric // tok::comment nodes at all when not keeping comments, but under 8190b57cec5SDimitry Andric // -traditional-cpp the lexer keeps /all/ whitespace, including comments. 820349cc55cSDimitry Andric PP.Lex(Tok); 821349cc55cSDimitry Andric continue; 8220b57cec5SDimitry Andric } else if (Tok.is(tok::eod)) { 8230b57cec5SDimitry Andric // Don't print end of directive tokens, since they are typically newlines 8240b57cec5SDimitry Andric // that mess up our line tracking. These come from unknown pre-processor 8250b57cec5SDimitry Andric // directives or hash-prefixed comments in standalone assembly files. 8260b57cec5SDimitry Andric PP.Lex(Tok); 827349cc55cSDimitry Andric // FIXME: The token on the next line after #include should have 828349cc55cSDimitry Andric // Tok.isAtStartOfLine() set. 829349cc55cSDimitry Andric IsStartOfLine = true; 8300b57cec5SDimitry Andric continue; 8310b57cec5SDimitry Andric } else if (Tok.is(tok::annot_module_include)) { 8320b57cec5SDimitry Andric // PrintPPOutputPPCallbacks::InclusionDirective handles producing 8330b57cec5SDimitry Andric // appropriate output here. Ignore this token entirely. 8340b57cec5SDimitry Andric PP.Lex(Tok); 835349cc55cSDimitry Andric IsStartOfLine = true; 8360b57cec5SDimitry Andric continue; 8370b57cec5SDimitry Andric } else if (Tok.is(tok::annot_module_begin)) { 8380b57cec5SDimitry Andric // FIXME: We retrieve this token after the FileChanged callback, and 8390b57cec5SDimitry Andric // retrieve the module_end token before the FileChanged callback, so 8400b57cec5SDimitry Andric // we render this within the file and render the module end outside the 8410b57cec5SDimitry Andric // file, but this is backwards from the token locations: the module_begin 8420b57cec5SDimitry Andric // token is at the include location (outside the file) and the module_end 8430b57cec5SDimitry Andric // token is at the EOF location (within the file). 8440b57cec5SDimitry Andric Callbacks->BeginModule( 8450b57cec5SDimitry Andric reinterpret_cast<Module *>(Tok.getAnnotationValue())); 8460b57cec5SDimitry Andric PP.Lex(Tok); 847349cc55cSDimitry Andric IsStartOfLine = true; 8480b57cec5SDimitry Andric continue; 8490b57cec5SDimitry Andric } else if (Tok.is(tok::annot_module_end)) { 8500b57cec5SDimitry Andric Callbacks->EndModule( 8510b57cec5SDimitry Andric reinterpret_cast<Module *>(Tok.getAnnotationValue())); 8520b57cec5SDimitry Andric PP.Lex(Tok); 853349cc55cSDimitry Andric IsStartOfLine = true; 8540b57cec5SDimitry Andric continue; 8550b57cec5SDimitry Andric } else if (Tok.is(tok::annot_header_unit)) { 8560b57cec5SDimitry Andric // This is a header-name that has been (effectively) converted into a 8570b57cec5SDimitry Andric // module-name. 8580b57cec5SDimitry Andric // FIXME: The module name could contain non-identifier module name 8590b57cec5SDimitry Andric // components. We don't have a good way to round-trip those. 8600b57cec5SDimitry Andric Module *M = reinterpret_cast<Module *>(Tok.getAnnotationValue()); 8610b57cec5SDimitry Andric std::string Name = M->getFullModuleName(); 8620b57cec5SDimitry Andric OS.write(Name.data(), Name.size()); 8630b57cec5SDimitry Andric Callbacks->HandleNewlinesInToken(Name.data(), Name.size()); 8640b57cec5SDimitry Andric } else if (Tok.isAnnotation()) { 8650b57cec5SDimitry Andric // Ignore annotation tokens created by pragmas - the pragmas themselves 8660b57cec5SDimitry Andric // will be reproduced in the preprocessed output. 8670b57cec5SDimitry Andric PP.Lex(Tok); 8680b57cec5SDimitry Andric continue; 8690b57cec5SDimitry Andric } else if (IdentifierInfo *II = Tok.getIdentifierInfo()) { 8700b57cec5SDimitry Andric OS << II->getName(); 8710b57cec5SDimitry Andric } else if (Tok.isLiteral() && !Tok.needsCleaning() && 8720b57cec5SDimitry Andric Tok.getLiteralData()) { 8730b57cec5SDimitry Andric OS.write(Tok.getLiteralData(), Tok.getLength()); 874*bdd1243dSDimitry Andric } else if (Tok.getLength() < std::size(Buffer)) { 8750b57cec5SDimitry Andric const char *TokPtr = Buffer; 8760b57cec5SDimitry Andric unsigned Len = PP.getSpelling(Tok, TokPtr); 8770b57cec5SDimitry Andric OS.write(TokPtr, Len); 8780b57cec5SDimitry Andric 8790b57cec5SDimitry Andric // Tokens that can contain embedded newlines need to adjust our current 8800b57cec5SDimitry Andric // line number. 881349cc55cSDimitry Andric // FIXME: The token may end with a newline in which case 882349cc55cSDimitry Andric // setEmittedDirectiveOnThisLine/setEmittedTokensOnThisLine afterwards is 883349cc55cSDimitry Andric // wrong. 8840b57cec5SDimitry Andric if (Tok.getKind() == tok::comment || Tok.getKind() == tok::unknown) 8850b57cec5SDimitry Andric Callbacks->HandleNewlinesInToken(TokPtr, Len); 886349cc55cSDimitry Andric if (Tok.is(tok::comment) && Len >= 2 && TokPtr[0] == '/' && 887349cc55cSDimitry Andric TokPtr[1] == '/') { 888349cc55cSDimitry Andric // It's a line comment; 889349cc55cSDimitry Andric // Ensure that we don't concatenate anything behind it. 890349cc55cSDimitry Andric Callbacks->setEmittedDirectiveOnThisLine(); 891349cc55cSDimitry Andric } 8920b57cec5SDimitry Andric } else { 8930b57cec5SDimitry Andric std::string S = PP.getSpelling(Tok); 8940b57cec5SDimitry Andric OS.write(S.data(), S.size()); 8950b57cec5SDimitry Andric 8960b57cec5SDimitry Andric // Tokens that can contain embedded newlines need to adjust our current 8970b57cec5SDimitry Andric // line number. 8980b57cec5SDimitry Andric if (Tok.getKind() == tok::comment || Tok.getKind() == tok::unknown) 8990b57cec5SDimitry Andric Callbacks->HandleNewlinesInToken(S.data(), S.size()); 900349cc55cSDimitry Andric if (Tok.is(tok::comment) && S.size() >= 2 && S[0] == '/' && S[1] == '/') { 901349cc55cSDimitry Andric // It's a line comment; 902349cc55cSDimitry Andric // Ensure that we don't concatenate anything behind it. 903349cc55cSDimitry Andric Callbacks->setEmittedDirectiveOnThisLine(); 904349cc55cSDimitry Andric } 9050b57cec5SDimitry Andric } 9060b57cec5SDimitry Andric Callbacks->setEmittedTokensOnThisLine(); 907349cc55cSDimitry Andric IsStartOfLine = false; 9080b57cec5SDimitry Andric 9090b57cec5SDimitry Andric if (Tok.is(tok::eof)) break; 9100b57cec5SDimitry Andric 9110b57cec5SDimitry Andric PP.Lex(Tok); 9120b57cec5SDimitry Andric } 9130b57cec5SDimitry Andric } 9140b57cec5SDimitry Andric 9150b57cec5SDimitry Andric typedef std::pair<const IdentifierInfo *, MacroInfo *> id_macro_pair; 9160b57cec5SDimitry Andric static int MacroIDCompare(const id_macro_pair *LHS, const id_macro_pair *RHS) { 9170b57cec5SDimitry Andric return LHS->first->getName().compare(RHS->first->getName()); 9180b57cec5SDimitry Andric } 9190b57cec5SDimitry Andric 9200b57cec5SDimitry Andric static void DoPrintMacros(Preprocessor &PP, raw_ostream *OS) { 9210b57cec5SDimitry Andric // Ignore unknown pragmas. 9220b57cec5SDimitry Andric PP.IgnorePragmas(); 9230b57cec5SDimitry Andric 9240b57cec5SDimitry Andric // -dM mode just scans and ignores all tokens in the files, then dumps out 9250b57cec5SDimitry Andric // the macro table at the end. 9260b57cec5SDimitry Andric PP.EnterMainSourceFile(); 9270b57cec5SDimitry Andric 9280b57cec5SDimitry Andric Token Tok; 9290b57cec5SDimitry Andric do PP.Lex(Tok); 9300b57cec5SDimitry Andric while (Tok.isNot(tok::eof)); 9310b57cec5SDimitry Andric 9320b57cec5SDimitry Andric SmallVector<id_macro_pair, 128> MacrosByID; 9330b57cec5SDimitry Andric for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end(); 9340b57cec5SDimitry Andric I != E; ++I) { 9350b57cec5SDimitry Andric auto *MD = I->second.getLatest(); 9360b57cec5SDimitry Andric if (MD && MD->isDefined()) 9370b57cec5SDimitry Andric MacrosByID.push_back(id_macro_pair(I->first, MD->getMacroInfo())); 9380b57cec5SDimitry Andric } 9390b57cec5SDimitry Andric llvm::array_pod_sort(MacrosByID.begin(), MacrosByID.end(), MacroIDCompare); 9400b57cec5SDimitry Andric 9410b57cec5SDimitry Andric for (unsigned i = 0, e = MacrosByID.size(); i != e; ++i) { 9420b57cec5SDimitry Andric MacroInfo &MI = *MacrosByID[i].second; 9430b57cec5SDimitry Andric // Ignore computed macros like __LINE__ and friends. 9440b57cec5SDimitry Andric if (MI.isBuiltinMacro()) continue; 9450b57cec5SDimitry Andric 9460b57cec5SDimitry Andric PrintMacroDefinition(*MacrosByID[i].first, MI, PP, *OS); 9470b57cec5SDimitry Andric *OS << '\n'; 9480b57cec5SDimitry Andric } 9490b57cec5SDimitry Andric } 9500b57cec5SDimitry Andric 9510b57cec5SDimitry Andric /// DoPrintPreprocessedInput - This implements -E mode. 9520b57cec5SDimitry Andric /// 9530b57cec5SDimitry Andric void clang::DoPrintPreprocessedInput(Preprocessor &PP, raw_ostream *OS, 9540b57cec5SDimitry Andric const PreprocessorOutputOptions &Opts) { 9550b57cec5SDimitry Andric // Show macros with no output is handled specially. 9560b57cec5SDimitry Andric if (!Opts.ShowCPP) { 9570b57cec5SDimitry Andric assert(Opts.ShowMacros && "Not yet implemented!"); 9580b57cec5SDimitry Andric DoPrintMacros(PP, OS); 9590b57cec5SDimitry Andric return; 9600b57cec5SDimitry Andric } 9610b57cec5SDimitry Andric 9620b57cec5SDimitry Andric // Inform the preprocessor whether we want it to retain comments or not, due 9630b57cec5SDimitry Andric // to -C or -CC. 9640b57cec5SDimitry Andric PP.SetCommentRetentionState(Opts.ShowComments, Opts.ShowMacroComments); 9650b57cec5SDimitry Andric 9660b57cec5SDimitry Andric PrintPPOutputPPCallbacks *Callbacks = new PrintPPOutputPPCallbacks( 9670b57cec5SDimitry Andric PP, *OS, !Opts.ShowLineMarkers, Opts.ShowMacros, 968349cc55cSDimitry Andric Opts.ShowIncludeDirectives, Opts.UseLineDirectives, 96981ad6265SDimitry Andric Opts.MinimizeWhitespace, Opts.DirectivesOnly); 9700b57cec5SDimitry Andric 9710b57cec5SDimitry Andric // Expand macros in pragmas with -fms-extensions. The assumption is that 9720b57cec5SDimitry Andric // the majority of pragmas in such a file will be Microsoft pragmas. 9730b57cec5SDimitry Andric // Remember the handlers we will add so that we can remove them later. 9740b57cec5SDimitry Andric std::unique_ptr<UnknownPragmaHandler> MicrosoftExtHandler( 9750b57cec5SDimitry Andric new UnknownPragmaHandler( 9760b57cec5SDimitry Andric "#pragma", Callbacks, 9770b57cec5SDimitry Andric /*RequireTokenExpansion=*/PP.getLangOpts().MicrosoftExt)); 9780b57cec5SDimitry Andric 9790b57cec5SDimitry Andric std::unique_ptr<UnknownPragmaHandler> GCCHandler(new UnknownPragmaHandler( 9800b57cec5SDimitry Andric "#pragma GCC", Callbacks, 9810b57cec5SDimitry Andric /*RequireTokenExpansion=*/PP.getLangOpts().MicrosoftExt)); 9820b57cec5SDimitry Andric 9830b57cec5SDimitry Andric std::unique_ptr<UnknownPragmaHandler> ClangHandler(new UnknownPragmaHandler( 9840b57cec5SDimitry Andric "#pragma clang", Callbacks, 9850b57cec5SDimitry Andric /*RequireTokenExpansion=*/PP.getLangOpts().MicrosoftExt)); 9860b57cec5SDimitry Andric 9870b57cec5SDimitry Andric PP.AddPragmaHandler(MicrosoftExtHandler.get()); 9880b57cec5SDimitry Andric PP.AddPragmaHandler("GCC", GCCHandler.get()); 9890b57cec5SDimitry Andric PP.AddPragmaHandler("clang", ClangHandler.get()); 9900b57cec5SDimitry Andric 9910b57cec5SDimitry Andric // The tokens after pragma omp need to be expanded. 9920b57cec5SDimitry Andric // 9930b57cec5SDimitry Andric // OpenMP [2.1, Directive format] 9940b57cec5SDimitry Andric // Preprocessing tokens following the #pragma omp are subject to macro 9950b57cec5SDimitry Andric // replacement. 9960b57cec5SDimitry Andric std::unique_ptr<UnknownPragmaHandler> OpenMPHandler( 9970b57cec5SDimitry Andric new UnknownPragmaHandler("#pragma omp", Callbacks, 9980b57cec5SDimitry Andric /*RequireTokenExpansion=*/true)); 9990b57cec5SDimitry Andric PP.AddPragmaHandler("omp", OpenMPHandler.get()); 10000b57cec5SDimitry Andric 10010b57cec5SDimitry Andric PP.addPPCallbacks(std::unique_ptr<PPCallbacks>(Callbacks)); 10020b57cec5SDimitry Andric 10030b57cec5SDimitry Andric // After we have configured the preprocessor, enter the main file. 10040b57cec5SDimitry Andric PP.EnterMainSourceFile(); 100581ad6265SDimitry Andric if (Opts.DirectivesOnly) 100681ad6265SDimitry Andric PP.SetMacroExpansionOnlyInDirectives(); 10070b57cec5SDimitry Andric 10080b57cec5SDimitry Andric // Consume all of the tokens that come from the predefines buffer. Those 10090b57cec5SDimitry Andric // should not be emitted into the output and are guaranteed to be at the 10100b57cec5SDimitry Andric // start. 10110b57cec5SDimitry Andric const SourceManager &SourceMgr = PP.getSourceManager(); 10120b57cec5SDimitry Andric Token Tok; 10130b57cec5SDimitry Andric do { 10140b57cec5SDimitry Andric PP.Lex(Tok); 10150b57cec5SDimitry Andric if (Tok.is(tok::eof) || !Tok.getLocation().isFileID()) 10160b57cec5SDimitry Andric break; 10170b57cec5SDimitry Andric 10180b57cec5SDimitry Andric PresumedLoc PLoc = SourceMgr.getPresumedLoc(Tok.getLocation()); 10190b57cec5SDimitry Andric if (PLoc.isInvalid()) 10200b57cec5SDimitry Andric break; 10210b57cec5SDimitry Andric 10220b57cec5SDimitry Andric if (strcmp(PLoc.getFilename(), "<built-in>")) 10230b57cec5SDimitry Andric break; 10240b57cec5SDimitry Andric } while (true); 10250b57cec5SDimitry Andric 10260b57cec5SDimitry Andric // Read all the preprocessed tokens, printing them out to the stream. 10270b57cec5SDimitry Andric PrintPreprocessedTokens(PP, Tok, Callbacks, *OS); 10280b57cec5SDimitry Andric *OS << '\n'; 10290b57cec5SDimitry Andric 10300b57cec5SDimitry Andric // Remove the handlers we just added to leave the preprocessor in a sane state 10310b57cec5SDimitry Andric // so that it can be reused (for example by a clang::Parser instance). 10320b57cec5SDimitry Andric PP.RemovePragmaHandler(MicrosoftExtHandler.get()); 10330b57cec5SDimitry Andric PP.RemovePragmaHandler("GCC", GCCHandler.get()); 10340b57cec5SDimitry Andric PP.RemovePragmaHandler("clang", ClangHandler.get()); 10350b57cec5SDimitry Andric PP.RemovePragmaHandler("omp", OpenMPHandler.get()); 10360b57cec5SDimitry Andric } 1037