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; 99*81ad6265SDimitry 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, 107*81ad6265SDimitry Andric bool UseLineDirectives, bool MinimizeWhitespace, 108*81ad6265SDimitry 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), 113*81ad6265SDimitry 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, 148*81ad6265SDimitry Andric CharSourceRange FilenameRange, 149*81ad6265SDimitry Andric Optional<FileEntryRef> File, StringRef SearchPath, 150*81ad6265SDimitry 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; 194*81ad6265SDimitry Andric bool IsFirstInFile = 195*81ad6265SDimitry 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( 3920b57cec5SDimitry Andric SourceLocation HashLoc, 3930b57cec5SDimitry Andric const Token &IncludeTok, 3940b57cec5SDimitry Andric StringRef FileName, 3950b57cec5SDimitry Andric bool IsAngled, 3960b57cec5SDimitry Andric CharSourceRange FilenameRange, 397*81ad6265SDimitry Andric Optional<FileEntryRef> File, 3980b57cec5SDimitry Andric StringRef SearchPath, 3990b57cec5SDimitry Andric StringRef RelativePath, 4000b57cec5SDimitry Andric const Module *Imported, 4010b57cec5SDimitry Andric SrcMgr::CharacteristicKind FileType) { 4020b57cec5SDimitry Andric // In -dI mode, dump #include directives prior to dumping their content or 4030b57cec5SDimitry Andric // interpretation. 4040b57cec5SDimitry Andric if (DumpIncludeDirectives) { 405349cc55cSDimitry Andric MoveToLine(HashLoc, /*RequireStartOfLine=*/true); 4060b57cec5SDimitry Andric const std::string TokenText = PP.getSpelling(IncludeTok); 4070b57cec5SDimitry Andric assert(!TokenText.empty()); 4080b57cec5SDimitry Andric OS << "#" << TokenText << " " 4090b57cec5SDimitry Andric << (IsAngled ? '<' : '"') << FileName << (IsAngled ? '>' : '"') 4100b57cec5SDimitry Andric << " /* clang -E -dI */"; 4110b57cec5SDimitry Andric setEmittedDirectiveOnThisLine(); 4120b57cec5SDimitry Andric } 4130b57cec5SDimitry Andric 4140b57cec5SDimitry Andric // When preprocessing, turn implicit imports into module import pragmas. 4150b57cec5SDimitry Andric if (Imported) { 4160b57cec5SDimitry Andric switch (IncludeTok.getIdentifierInfo()->getPPKeywordID()) { 4170b57cec5SDimitry Andric case tok::pp_include: 4180b57cec5SDimitry Andric case tok::pp_import: 4190b57cec5SDimitry Andric case tok::pp_include_next: 420349cc55cSDimitry Andric MoveToLine(HashLoc, /*RequireStartOfLine=*/true); 4210b57cec5SDimitry Andric OS << "#pragma clang module import " << Imported->getFullModuleName(true) 4220b57cec5SDimitry Andric << " /* clang -E: implicit import for " 4230b57cec5SDimitry Andric << "#" << PP.getSpelling(IncludeTok) << " " 4240b57cec5SDimitry Andric << (IsAngled ? '<' : '"') << FileName << (IsAngled ? '>' : '"') 4250b57cec5SDimitry Andric << " */"; 426349cc55cSDimitry Andric setEmittedDirectiveOnThisLine(); 4270b57cec5SDimitry Andric break; 4280b57cec5SDimitry Andric 4290b57cec5SDimitry Andric case tok::pp___include_macros: 4300b57cec5SDimitry Andric // #__include_macros has no effect on a user of a preprocessed source 4310b57cec5SDimitry Andric // file; the only effect is on preprocessing. 4320b57cec5SDimitry Andric // 4330b57cec5SDimitry Andric // FIXME: That's not *quite* true: it causes the module in question to 4340b57cec5SDimitry Andric // be loaded, which can affect downstream diagnostics. 4350b57cec5SDimitry Andric break; 4360b57cec5SDimitry Andric 4370b57cec5SDimitry Andric default: 4380b57cec5SDimitry Andric llvm_unreachable("unknown include directive kind"); 4390b57cec5SDimitry Andric break; 4400b57cec5SDimitry Andric } 4410b57cec5SDimitry Andric } 4420b57cec5SDimitry Andric } 4430b57cec5SDimitry Andric 4440b57cec5SDimitry Andric /// Handle entering the scope of a module during a module compilation. 4450b57cec5SDimitry Andric void PrintPPOutputPPCallbacks::BeginModule(const Module *M) { 4460b57cec5SDimitry Andric startNewLineIfNeeded(); 4470b57cec5SDimitry Andric OS << "#pragma clang module begin " << M->getFullModuleName(true); 4480b57cec5SDimitry Andric setEmittedDirectiveOnThisLine(); 4490b57cec5SDimitry Andric } 4500b57cec5SDimitry Andric 4510b57cec5SDimitry Andric /// Handle leaving the scope of a module during a module compilation. 4520b57cec5SDimitry Andric void PrintPPOutputPPCallbacks::EndModule(const Module *M) { 4530b57cec5SDimitry Andric startNewLineIfNeeded(); 4540b57cec5SDimitry Andric OS << "#pragma clang module end /*" << M->getFullModuleName(true) << "*/"; 4550b57cec5SDimitry Andric setEmittedDirectiveOnThisLine(); 4560b57cec5SDimitry Andric } 4570b57cec5SDimitry Andric 4580b57cec5SDimitry Andric /// Ident - Handle #ident directives when read by the preprocessor. 4590b57cec5SDimitry Andric /// 4600b57cec5SDimitry Andric void PrintPPOutputPPCallbacks::Ident(SourceLocation Loc, StringRef S) { 461349cc55cSDimitry Andric MoveToLine(Loc, /*RequireStartOfLine=*/true); 4620b57cec5SDimitry Andric 4630b57cec5SDimitry Andric OS.write("#ident ", strlen("#ident ")); 4640b57cec5SDimitry Andric OS.write(S.begin(), S.size()); 465349cc55cSDimitry Andric setEmittedTokensOnThisLine(); 4660b57cec5SDimitry Andric } 4670b57cec5SDimitry Andric 4680b57cec5SDimitry Andric /// MacroDefined - This hook is called whenever a macro definition is seen. 4690b57cec5SDimitry Andric void PrintPPOutputPPCallbacks::MacroDefined(const Token &MacroNameTok, 4700b57cec5SDimitry Andric const MacroDirective *MD) { 4710b57cec5SDimitry Andric const MacroInfo *MI = MD->getMacroInfo(); 472*81ad6265SDimitry Andric // Print out macro definitions in -dD mode and when we have -fdirectives-only 473*81ad6265SDimitry Andric // for C++20 header units. 474*81ad6265SDimitry Andric if ((!DumpDefines && !DirectivesOnly) || 4750b57cec5SDimitry Andric // Ignore __FILE__ etc. 476*81ad6265SDimitry Andric MI->isBuiltinMacro()) 477*81ad6265SDimitry Andric return; 4780b57cec5SDimitry Andric 479*81ad6265SDimitry Andric SourceLocation DefLoc = MI->getDefinitionLoc(); 480*81ad6265SDimitry Andric if (DirectivesOnly && !MI->isUsed()) { 481*81ad6265SDimitry Andric SourceManager &SM = PP.getSourceManager(); 482*81ad6265SDimitry Andric if (SM.isWrittenInBuiltinFile(DefLoc) || 483*81ad6265SDimitry Andric SM.isWrittenInCommandLineFile(DefLoc)) 484*81ad6265SDimitry Andric return; 485*81ad6265SDimitry Andric } 486*81ad6265SDimitry Andric MoveToLine(DefLoc, /*RequireStartOfLine=*/true); 4870b57cec5SDimitry Andric PrintMacroDefinition(*MacroNameTok.getIdentifierInfo(), *MI, PP, OS); 4880b57cec5SDimitry Andric setEmittedDirectiveOnThisLine(); 4890b57cec5SDimitry Andric } 4900b57cec5SDimitry Andric 4910b57cec5SDimitry Andric void PrintPPOutputPPCallbacks::MacroUndefined(const Token &MacroNameTok, 4920b57cec5SDimitry Andric const MacroDefinition &MD, 4930b57cec5SDimitry Andric const MacroDirective *Undef) { 494*81ad6265SDimitry Andric // Print out macro definitions in -dD mode and when we have -fdirectives-only 495*81ad6265SDimitry Andric // for C++20 header units. 496*81ad6265SDimitry Andric if (!DumpDefines && !DirectivesOnly) 497*81ad6265SDimitry Andric return; 4980b57cec5SDimitry Andric 499349cc55cSDimitry Andric MoveToLine(MacroNameTok.getLocation(), /*RequireStartOfLine=*/true); 5000b57cec5SDimitry Andric OS << "#undef " << MacroNameTok.getIdentifierInfo()->getName(); 5010b57cec5SDimitry Andric setEmittedDirectiveOnThisLine(); 5020b57cec5SDimitry Andric } 5030b57cec5SDimitry Andric 5040b57cec5SDimitry Andric static void outputPrintable(raw_ostream &OS, StringRef Str) { 5050b57cec5SDimitry Andric for (unsigned char Char : Str) { 5060b57cec5SDimitry Andric if (isPrintable(Char) && Char != '\\' && Char != '"') 5070b57cec5SDimitry Andric OS << (char)Char; 5080b57cec5SDimitry Andric else // Output anything hard as an octal escape. 5090b57cec5SDimitry Andric OS << '\\' 5100b57cec5SDimitry Andric << (char)('0' + ((Char >> 6) & 7)) 5110b57cec5SDimitry Andric << (char)('0' + ((Char >> 3) & 7)) 5120b57cec5SDimitry Andric << (char)('0' + ((Char >> 0) & 7)); 5130b57cec5SDimitry Andric } 5140b57cec5SDimitry Andric } 5150b57cec5SDimitry Andric 5160b57cec5SDimitry Andric void PrintPPOutputPPCallbacks::PragmaMessage(SourceLocation Loc, 5170b57cec5SDimitry Andric StringRef Namespace, 5180b57cec5SDimitry Andric PragmaMessageKind Kind, 5190b57cec5SDimitry Andric StringRef Str) { 520349cc55cSDimitry Andric MoveToLine(Loc, /*RequireStartOfLine=*/true); 5210b57cec5SDimitry Andric OS << "#pragma "; 5220b57cec5SDimitry Andric if (!Namespace.empty()) 5230b57cec5SDimitry Andric OS << Namespace << ' '; 5240b57cec5SDimitry Andric switch (Kind) { 5250b57cec5SDimitry Andric case PMK_Message: 5260b57cec5SDimitry Andric OS << "message(\""; 5270b57cec5SDimitry Andric break; 5280b57cec5SDimitry Andric case PMK_Warning: 5290b57cec5SDimitry Andric OS << "warning \""; 5300b57cec5SDimitry Andric break; 5310b57cec5SDimitry Andric case PMK_Error: 5320b57cec5SDimitry Andric OS << "error \""; 5330b57cec5SDimitry Andric break; 5340b57cec5SDimitry Andric } 5350b57cec5SDimitry Andric 5360b57cec5SDimitry Andric outputPrintable(OS, Str); 5370b57cec5SDimitry Andric OS << '"'; 5380b57cec5SDimitry Andric if (Kind == PMK_Message) 5390b57cec5SDimitry Andric OS << ')'; 5400b57cec5SDimitry Andric setEmittedDirectiveOnThisLine(); 5410b57cec5SDimitry Andric } 5420b57cec5SDimitry Andric 5430b57cec5SDimitry Andric void PrintPPOutputPPCallbacks::PragmaDebug(SourceLocation Loc, 5440b57cec5SDimitry Andric StringRef DebugType) { 545349cc55cSDimitry Andric MoveToLine(Loc, /*RequireStartOfLine=*/true); 5460b57cec5SDimitry Andric 5470b57cec5SDimitry Andric OS << "#pragma clang __debug "; 5480b57cec5SDimitry Andric OS << DebugType; 5490b57cec5SDimitry Andric 5500b57cec5SDimitry Andric setEmittedDirectiveOnThisLine(); 5510b57cec5SDimitry Andric } 5520b57cec5SDimitry Andric 5530b57cec5SDimitry Andric void PrintPPOutputPPCallbacks:: 5540b57cec5SDimitry Andric PragmaDiagnosticPush(SourceLocation Loc, StringRef Namespace) { 555349cc55cSDimitry Andric MoveToLine(Loc, /*RequireStartOfLine=*/true); 5560b57cec5SDimitry Andric OS << "#pragma " << Namespace << " diagnostic push"; 5570b57cec5SDimitry Andric setEmittedDirectiveOnThisLine(); 5580b57cec5SDimitry Andric } 5590b57cec5SDimitry Andric 5600b57cec5SDimitry Andric void PrintPPOutputPPCallbacks:: 5610b57cec5SDimitry Andric PragmaDiagnosticPop(SourceLocation Loc, StringRef Namespace) { 562349cc55cSDimitry Andric MoveToLine(Loc, /*RequireStartOfLine=*/true); 5630b57cec5SDimitry Andric OS << "#pragma " << Namespace << " diagnostic pop"; 5640b57cec5SDimitry Andric setEmittedDirectiveOnThisLine(); 5650b57cec5SDimitry Andric } 5660b57cec5SDimitry Andric 5670b57cec5SDimitry Andric void PrintPPOutputPPCallbacks::PragmaDiagnostic(SourceLocation Loc, 5680b57cec5SDimitry Andric StringRef Namespace, 5690b57cec5SDimitry Andric diag::Severity Map, 5700b57cec5SDimitry Andric StringRef Str) { 571349cc55cSDimitry Andric MoveToLine(Loc, /*RequireStartOfLine=*/true); 5720b57cec5SDimitry Andric OS << "#pragma " << Namespace << " diagnostic "; 5730b57cec5SDimitry Andric switch (Map) { 5740b57cec5SDimitry Andric case diag::Severity::Remark: 5750b57cec5SDimitry Andric OS << "remark"; 5760b57cec5SDimitry Andric break; 5770b57cec5SDimitry Andric case diag::Severity::Warning: 5780b57cec5SDimitry Andric OS << "warning"; 5790b57cec5SDimitry Andric break; 5800b57cec5SDimitry Andric case diag::Severity::Error: 5810b57cec5SDimitry Andric OS << "error"; 5820b57cec5SDimitry Andric break; 5830b57cec5SDimitry Andric case diag::Severity::Ignored: 5840b57cec5SDimitry Andric OS << "ignored"; 5850b57cec5SDimitry Andric break; 5860b57cec5SDimitry Andric case diag::Severity::Fatal: 5870b57cec5SDimitry Andric OS << "fatal"; 5880b57cec5SDimitry Andric break; 5890b57cec5SDimitry Andric } 5900b57cec5SDimitry Andric OS << " \"" << Str << '"'; 5910b57cec5SDimitry Andric setEmittedDirectiveOnThisLine(); 5920b57cec5SDimitry Andric } 5930b57cec5SDimitry Andric 5940b57cec5SDimitry Andric void PrintPPOutputPPCallbacks::PragmaWarning(SourceLocation Loc, 595349cc55cSDimitry Andric PragmaWarningSpecifier WarningSpec, 5960b57cec5SDimitry Andric ArrayRef<int> Ids) { 597349cc55cSDimitry Andric MoveToLine(Loc, /*RequireStartOfLine=*/true); 598349cc55cSDimitry Andric 599349cc55cSDimitry Andric OS << "#pragma warning("; 600349cc55cSDimitry Andric switch(WarningSpec) { 601349cc55cSDimitry Andric case PWS_Default: OS << "default"; break; 602349cc55cSDimitry Andric case PWS_Disable: OS << "disable"; break; 603349cc55cSDimitry Andric case PWS_Error: OS << "error"; break; 604349cc55cSDimitry Andric case PWS_Once: OS << "once"; break; 605349cc55cSDimitry Andric case PWS_Suppress: OS << "suppress"; break; 606349cc55cSDimitry Andric case PWS_Level1: OS << '1'; break; 607349cc55cSDimitry Andric case PWS_Level2: OS << '2'; break; 608349cc55cSDimitry Andric case PWS_Level3: OS << '3'; break; 609349cc55cSDimitry Andric case PWS_Level4: OS << '4'; break; 610349cc55cSDimitry Andric } 611349cc55cSDimitry Andric OS << ':'; 612349cc55cSDimitry Andric 6130b57cec5SDimitry Andric for (ArrayRef<int>::iterator I = Ids.begin(), E = Ids.end(); I != E; ++I) 6140b57cec5SDimitry Andric OS << ' ' << *I; 6150b57cec5SDimitry Andric OS << ')'; 6160b57cec5SDimitry Andric setEmittedDirectiveOnThisLine(); 6170b57cec5SDimitry Andric } 6180b57cec5SDimitry Andric 6190b57cec5SDimitry Andric void PrintPPOutputPPCallbacks::PragmaWarningPush(SourceLocation Loc, 6200b57cec5SDimitry Andric int Level) { 621349cc55cSDimitry Andric MoveToLine(Loc, /*RequireStartOfLine=*/true); 6220b57cec5SDimitry Andric OS << "#pragma warning(push"; 6230b57cec5SDimitry Andric if (Level >= 0) 6240b57cec5SDimitry Andric OS << ", " << Level; 6250b57cec5SDimitry Andric OS << ')'; 6260b57cec5SDimitry Andric setEmittedDirectiveOnThisLine(); 6270b57cec5SDimitry Andric } 6280b57cec5SDimitry Andric 6290b57cec5SDimitry Andric void PrintPPOutputPPCallbacks::PragmaWarningPop(SourceLocation Loc) { 630349cc55cSDimitry Andric MoveToLine(Loc, /*RequireStartOfLine=*/true); 6310b57cec5SDimitry Andric OS << "#pragma warning(pop)"; 6320b57cec5SDimitry Andric setEmittedDirectiveOnThisLine(); 6330b57cec5SDimitry Andric } 6340b57cec5SDimitry Andric 6350b57cec5SDimitry Andric void PrintPPOutputPPCallbacks::PragmaExecCharsetPush(SourceLocation Loc, 6360b57cec5SDimitry Andric StringRef Str) { 637349cc55cSDimitry Andric MoveToLine(Loc, /*RequireStartOfLine=*/true); 6380b57cec5SDimitry Andric OS << "#pragma character_execution_set(push"; 6390b57cec5SDimitry Andric if (!Str.empty()) 6400b57cec5SDimitry Andric OS << ", " << Str; 6410b57cec5SDimitry Andric OS << ')'; 6420b57cec5SDimitry Andric setEmittedDirectiveOnThisLine(); 6430b57cec5SDimitry Andric } 6440b57cec5SDimitry Andric 6450b57cec5SDimitry Andric void PrintPPOutputPPCallbacks::PragmaExecCharsetPop(SourceLocation Loc) { 646349cc55cSDimitry Andric MoveToLine(Loc, /*RequireStartOfLine=*/true); 6470b57cec5SDimitry Andric OS << "#pragma character_execution_set(pop)"; 6480b57cec5SDimitry Andric setEmittedDirectiveOnThisLine(); 6490b57cec5SDimitry Andric } 6500b57cec5SDimitry Andric 6510b57cec5SDimitry Andric void PrintPPOutputPPCallbacks:: 6520b57cec5SDimitry Andric PragmaAssumeNonNullBegin(SourceLocation Loc) { 653349cc55cSDimitry Andric MoveToLine(Loc, /*RequireStartOfLine=*/true); 6540b57cec5SDimitry Andric OS << "#pragma clang assume_nonnull begin"; 6550b57cec5SDimitry Andric setEmittedDirectiveOnThisLine(); 6560b57cec5SDimitry Andric } 6570b57cec5SDimitry Andric 6580b57cec5SDimitry Andric void PrintPPOutputPPCallbacks:: 6590b57cec5SDimitry Andric PragmaAssumeNonNullEnd(SourceLocation Loc) { 660349cc55cSDimitry Andric MoveToLine(Loc, /*RequireStartOfLine=*/true); 6610b57cec5SDimitry Andric OS << "#pragma clang assume_nonnull end"; 6620b57cec5SDimitry Andric setEmittedDirectiveOnThisLine(); 6630b57cec5SDimitry Andric } 6640b57cec5SDimitry Andric 665349cc55cSDimitry Andric void PrintPPOutputPPCallbacks::HandleWhitespaceBeforeTok(const Token &Tok, 666349cc55cSDimitry Andric bool RequireSpace, 667349cc55cSDimitry Andric bool RequireSameLine) { 668349cc55cSDimitry Andric // These tokens are not expanded to anything and don't need whitespace before 669349cc55cSDimitry Andric // them. 670349cc55cSDimitry Andric if (Tok.is(tok::eof) || 671349cc55cSDimitry Andric (Tok.isAnnotation() && !Tok.is(tok::annot_header_unit) && 672349cc55cSDimitry Andric !Tok.is(tok::annot_module_begin) && !Tok.is(tok::annot_module_end))) 673349cc55cSDimitry Andric return; 6740b57cec5SDimitry Andric 675349cc55cSDimitry Andric // EmittedDirectiveOnThisLine takes priority over RequireSameLine. 676349cc55cSDimitry Andric if ((!RequireSameLine || EmittedDirectiveOnThisLine) && 677349cc55cSDimitry Andric MoveToLine(Tok, /*RequireStartOfLine=*/EmittedDirectiveOnThisLine)) { 678349cc55cSDimitry Andric if (MinimizeWhitespace) { 679349cc55cSDimitry Andric // Avoid interpreting hash as a directive under -fpreprocessed. 680349cc55cSDimitry Andric if (Tok.is(tok::hash)) 681349cc55cSDimitry Andric OS << ' '; 682349cc55cSDimitry Andric } else { 6830b57cec5SDimitry Andric // Print out space characters so that the first token on a line is 6840b57cec5SDimitry Andric // indented for easy reading. 6850b57cec5SDimitry Andric unsigned ColNo = SM.getExpansionColumnNumber(Tok.getLocation()); 6860b57cec5SDimitry Andric 687349cc55cSDimitry Andric // The first token on a line can have a column number of 1, yet still 688349cc55cSDimitry Andric // expect leading white space, if a macro expansion in column 1 starts 689349cc55cSDimitry Andric // with an empty macro argument, or an empty nested macro expansion. In 690349cc55cSDimitry Andric // this case, move the token to column 2. 6910b57cec5SDimitry Andric if (ColNo == 1 && Tok.hasLeadingSpace()) 6920b57cec5SDimitry Andric ColNo = 2; 6930b57cec5SDimitry Andric 6940b57cec5SDimitry Andric // This hack prevents stuff like: 6950b57cec5SDimitry Andric // #define HASH # 6960b57cec5SDimitry Andric // HASH define foo bar 6970b57cec5SDimitry Andric // From having the # character end up at column 1, which makes it so it 6980b57cec5SDimitry Andric // is not handled as a #define next time through the preprocessor if in 6990b57cec5SDimitry Andric // -fpreprocessed mode. 7000b57cec5SDimitry Andric if (ColNo <= 1 && Tok.is(tok::hash)) 7010b57cec5SDimitry Andric OS << ' '; 7020b57cec5SDimitry Andric 7030b57cec5SDimitry Andric // Otherwise, indent the appropriate number of spaces. 7040b57cec5SDimitry Andric for (; ColNo > 1; --ColNo) 7050b57cec5SDimitry Andric OS << ' '; 706349cc55cSDimitry Andric } 707349cc55cSDimitry Andric } else { 708349cc55cSDimitry Andric // Insert whitespace between the previous and next token if either 709349cc55cSDimitry Andric // - The caller requires it 710349cc55cSDimitry Andric // - The input had whitespace between them and we are not in 711349cc55cSDimitry Andric // whitespace-minimization mode 712349cc55cSDimitry Andric // - The whitespace is necessary to keep the tokens apart and there is not 713349cc55cSDimitry Andric // already a newline between them 714349cc55cSDimitry Andric if (RequireSpace || (!MinimizeWhitespace && Tok.hasLeadingSpace()) || 715349cc55cSDimitry Andric ((EmittedTokensOnThisLine || EmittedDirectiveOnThisLine) && 716349cc55cSDimitry Andric AvoidConcat(PrevPrevTok, PrevTok, Tok))) 717349cc55cSDimitry Andric OS << ' '; 718349cc55cSDimitry Andric } 7190b57cec5SDimitry Andric 720349cc55cSDimitry Andric PrevPrevTok = PrevTok; 721349cc55cSDimitry Andric PrevTok = Tok; 7220b57cec5SDimitry Andric } 7230b57cec5SDimitry Andric 7240b57cec5SDimitry Andric void PrintPPOutputPPCallbacks::HandleNewlinesInToken(const char *TokStr, 7250b57cec5SDimitry Andric unsigned Len) { 7260b57cec5SDimitry Andric unsigned NumNewlines = 0; 7270b57cec5SDimitry Andric for (; Len; --Len, ++TokStr) { 7280b57cec5SDimitry Andric if (*TokStr != '\n' && 7290b57cec5SDimitry Andric *TokStr != '\r') 7300b57cec5SDimitry Andric continue; 7310b57cec5SDimitry Andric 7320b57cec5SDimitry Andric ++NumNewlines; 7330b57cec5SDimitry Andric 7340b57cec5SDimitry Andric // If we have \n\r or \r\n, skip both and count as one line. 7350b57cec5SDimitry Andric if (Len != 1 && 7360b57cec5SDimitry Andric (TokStr[1] == '\n' || TokStr[1] == '\r') && 7370b57cec5SDimitry Andric TokStr[0] != TokStr[1]) { 7380b57cec5SDimitry Andric ++TokStr; 7390b57cec5SDimitry Andric --Len; 7400b57cec5SDimitry Andric } 7410b57cec5SDimitry Andric } 7420b57cec5SDimitry Andric 7430b57cec5SDimitry Andric if (NumNewlines == 0) return; 7440b57cec5SDimitry Andric 7450b57cec5SDimitry Andric CurLine += NumNewlines; 7460b57cec5SDimitry Andric } 7470b57cec5SDimitry Andric 7480b57cec5SDimitry Andric 7490b57cec5SDimitry Andric namespace { 7500b57cec5SDimitry Andric struct UnknownPragmaHandler : public PragmaHandler { 7510b57cec5SDimitry Andric const char *Prefix; 7520b57cec5SDimitry Andric PrintPPOutputPPCallbacks *Callbacks; 7530b57cec5SDimitry Andric 7540b57cec5SDimitry Andric // Set to true if tokens should be expanded 7550b57cec5SDimitry Andric bool ShouldExpandTokens; 7560b57cec5SDimitry Andric 7570b57cec5SDimitry Andric UnknownPragmaHandler(const char *prefix, PrintPPOutputPPCallbacks *callbacks, 7580b57cec5SDimitry Andric bool RequireTokenExpansion) 7590b57cec5SDimitry Andric : Prefix(prefix), Callbacks(callbacks), 7600b57cec5SDimitry Andric ShouldExpandTokens(RequireTokenExpansion) {} 7610b57cec5SDimitry Andric void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer, 7620b57cec5SDimitry Andric Token &PragmaTok) override { 7630b57cec5SDimitry Andric // Figure out what line we went to and insert the appropriate number of 7640b57cec5SDimitry Andric // newline characters. 765349cc55cSDimitry Andric Callbacks->MoveToLine(PragmaTok.getLocation(), /*RequireStartOfLine=*/true); 7660b57cec5SDimitry Andric Callbacks->OS.write(Prefix, strlen(Prefix)); 767349cc55cSDimitry Andric Callbacks->setEmittedTokensOnThisLine(); 7680b57cec5SDimitry Andric 7690b57cec5SDimitry Andric if (ShouldExpandTokens) { 7700b57cec5SDimitry Andric // The first token does not have expanded macros. Expand them, if 7710b57cec5SDimitry Andric // required. 772a7dea167SDimitry Andric auto Toks = std::make_unique<Token[]>(1); 7730b57cec5SDimitry Andric Toks[0] = PragmaTok; 7740b57cec5SDimitry Andric PP.EnterTokenStream(std::move(Toks), /*NumToks=*/1, 7750b57cec5SDimitry Andric /*DisableMacroExpansion=*/false, 7760b57cec5SDimitry Andric /*IsReinject=*/false); 7770b57cec5SDimitry Andric PP.Lex(PragmaTok); 7780b57cec5SDimitry Andric } 7790b57cec5SDimitry Andric 7800b57cec5SDimitry Andric // Read and print all of the pragma tokens. 781349cc55cSDimitry Andric bool IsFirst = true; 7820b57cec5SDimitry Andric while (PragmaTok.isNot(tok::eod)) { 783349cc55cSDimitry Andric Callbacks->HandleWhitespaceBeforeTok(PragmaTok, /*RequireSpace=*/IsFirst, 784349cc55cSDimitry Andric /*RequireSameLine=*/true); 785349cc55cSDimitry Andric IsFirst = false; 7860b57cec5SDimitry Andric std::string TokSpell = PP.getSpelling(PragmaTok); 7870b57cec5SDimitry Andric Callbacks->OS.write(&TokSpell[0], TokSpell.size()); 788349cc55cSDimitry Andric Callbacks->setEmittedTokensOnThisLine(); 7890b57cec5SDimitry Andric 7900b57cec5SDimitry Andric if (ShouldExpandTokens) 7910b57cec5SDimitry Andric PP.Lex(PragmaTok); 7920b57cec5SDimitry Andric else 7930b57cec5SDimitry Andric PP.LexUnexpandedToken(PragmaTok); 7940b57cec5SDimitry Andric } 7950b57cec5SDimitry Andric Callbacks->setEmittedDirectiveOnThisLine(); 7960b57cec5SDimitry Andric } 7970b57cec5SDimitry Andric }; 7980b57cec5SDimitry Andric } // end anonymous namespace 7990b57cec5SDimitry Andric 8000b57cec5SDimitry Andric 8010b57cec5SDimitry Andric static void PrintPreprocessedTokens(Preprocessor &PP, Token &Tok, 8020b57cec5SDimitry Andric PrintPPOutputPPCallbacks *Callbacks, 8030b57cec5SDimitry Andric raw_ostream &OS) { 8040b57cec5SDimitry Andric bool DropComments = PP.getLangOpts().TraditionalCPP && 8050b57cec5SDimitry Andric !PP.getCommentRetentionState(); 8060b57cec5SDimitry Andric 807349cc55cSDimitry Andric bool IsStartOfLine = false; 8080b57cec5SDimitry Andric char Buffer[256]; 80904eeddc0SDimitry Andric while (true) { 810349cc55cSDimitry Andric // Two lines joined with line continuation ('\' as last character on the 811349cc55cSDimitry Andric // line) must be emitted as one line even though Tok.getLine() returns two 812349cc55cSDimitry Andric // different values. In this situation Tok.isAtStartOfLine() is false even 813349cc55cSDimitry Andric // though it may be the first token on the lexical line. When 814349cc55cSDimitry Andric // dropping/skipping a token that is at the start of a line, propagate the 815349cc55cSDimitry Andric // start-of-line-ness to the next token to not append it to the previous 816349cc55cSDimitry Andric // line. 817349cc55cSDimitry Andric IsStartOfLine = IsStartOfLine || Tok.isAtStartOfLine(); 8180b57cec5SDimitry Andric 819349cc55cSDimitry Andric Callbacks->HandleWhitespaceBeforeTok(Tok, /*RequireSpace=*/false, 820349cc55cSDimitry Andric /*RequireSameLine=*/!IsStartOfLine); 8210b57cec5SDimitry Andric 8220b57cec5SDimitry Andric if (DropComments && Tok.is(tok::comment)) { 8230b57cec5SDimitry Andric // Skip comments. Normally the preprocessor does not generate 8240b57cec5SDimitry Andric // tok::comment nodes at all when not keeping comments, but under 8250b57cec5SDimitry Andric // -traditional-cpp the lexer keeps /all/ whitespace, including comments. 826349cc55cSDimitry Andric PP.Lex(Tok); 827349cc55cSDimitry Andric continue; 8280b57cec5SDimitry Andric } else if (Tok.is(tok::eod)) { 8290b57cec5SDimitry Andric // Don't print end of directive tokens, since they are typically newlines 8300b57cec5SDimitry Andric // that mess up our line tracking. These come from unknown pre-processor 8310b57cec5SDimitry Andric // directives or hash-prefixed comments in standalone assembly files. 8320b57cec5SDimitry Andric PP.Lex(Tok); 833349cc55cSDimitry Andric // FIXME: The token on the next line after #include should have 834349cc55cSDimitry Andric // Tok.isAtStartOfLine() set. 835349cc55cSDimitry Andric IsStartOfLine = true; 8360b57cec5SDimitry Andric continue; 8370b57cec5SDimitry Andric } else if (Tok.is(tok::annot_module_include)) { 8380b57cec5SDimitry Andric // PrintPPOutputPPCallbacks::InclusionDirective handles producing 8390b57cec5SDimitry Andric // appropriate output here. Ignore this token entirely. 8400b57cec5SDimitry Andric PP.Lex(Tok); 841349cc55cSDimitry Andric IsStartOfLine = true; 8420b57cec5SDimitry Andric continue; 8430b57cec5SDimitry Andric } else if (Tok.is(tok::annot_module_begin)) { 8440b57cec5SDimitry Andric // FIXME: We retrieve this token after the FileChanged callback, and 8450b57cec5SDimitry Andric // retrieve the module_end token before the FileChanged callback, so 8460b57cec5SDimitry Andric // we render this within the file and render the module end outside the 8470b57cec5SDimitry Andric // file, but this is backwards from the token locations: the module_begin 8480b57cec5SDimitry Andric // token is at the include location (outside the file) and the module_end 8490b57cec5SDimitry Andric // token is at the EOF location (within the file). 8500b57cec5SDimitry Andric Callbacks->BeginModule( 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_module_end)) { 8560b57cec5SDimitry Andric Callbacks->EndModule( 8570b57cec5SDimitry Andric reinterpret_cast<Module *>(Tok.getAnnotationValue())); 8580b57cec5SDimitry Andric PP.Lex(Tok); 859349cc55cSDimitry Andric IsStartOfLine = true; 8600b57cec5SDimitry Andric continue; 8610b57cec5SDimitry Andric } else if (Tok.is(tok::annot_header_unit)) { 8620b57cec5SDimitry Andric // This is a header-name that has been (effectively) converted into a 8630b57cec5SDimitry Andric // module-name. 8640b57cec5SDimitry Andric // FIXME: The module name could contain non-identifier module name 8650b57cec5SDimitry Andric // components. We don't have a good way to round-trip those. 8660b57cec5SDimitry Andric Module *M = reinterpret_cast<Module *>(Tok.getAnnotationValue()); 8670b57cec5SDimitry Andric std::string Name = M->getFullModuleName(); 8680b57cec5SDimitry Andric OS.write(Name.data(), Name.size()); 8690b57cec5SDimitry Andric Callbacks->HandleNewlinesInToken(Name.data(), Name.size()); 8700b57cec5SDimitry Andric } else if (Tok.isAnnotation()) { 8710b57cec5SDimitry Andric // Ignore annotation tokens created by pragmas - the pragmas themselves 8720b57cec5SDimitry Andric // will be reproduced in the preprocessed output. 8730b57cec5SDimitry Andric PP.Lex(Tok); 8740b57cec5SDimitry Andric continue; 8750b57cec5SDimitry Andric } else if (IdentifierInfo *II = Tok.getIdentifierInfo()) { 8760b57cec5SDimitry Andric OS << II->getName(); 8770b57cec5SDimitry Andric } else if (Tok.isLiteral() && !Tok.needsCleaning() && 8780b57cec5SDimitry Andric Tok.getLiteralData()) { 8790b57cec5SDimitry Andric OS.write(Tok.getLiteralData(), Tok.getLength()); 8800b57cec5SDimitry Andric } else if (Tok.getLength() < llvm::array_lengthof(Buffer)) { 8810b57cec5SDimitry Andric const char *TokPtr = Buffer; 8820b57cec5SDimitry Andric unsigned Len = PP.getSpelling(Tok, TokPtr); 8830b57cec5SDimitry Andric OS.write(TokPtr, Len); 8840b57cec5SDimitry Andric 8850b57cec5SDimitry Andric // Tokens that can contain embedded newlines need to adjust our current 8860b57cec5SDimitry Andric // line number. 887349cc55cSDimitry Andric // FIXME: The token may end with a newline in which case 888349cc55cSDimitry Andric // setEmittedDirectiveOnThisLine/setEmittedTokensOnThisLine afterwards is 889349cc55cSDimitry Andric // wrong. 8900b57cec5SDimitry Andric if (Tok.getKind() == tok::comment || Tok.getKind() == tok::unknown) 8910b57cec5SDimitry Andric Callbacks->HandleNewlinesInToken(TokPtr, Len); 892349cc55cSDimitry Andric if (Tok.is(tok::comment) && Len >= 2 && TokPtr[0] == '/' && 893349cc55cSDimitry Andric TokPtr[1] == '/') { 894349cc55cSDimitry Andric // It's a line comment; 895349cc55cSDimitry Andric // Ensure that we don't concatenate anything behind it. 896349cc55cSDimitry Andric Callbacks->setEmittedDirectiveOnThisLine(); 897349cc55cSDimitry Andric } 8980b57cec5SDimitry Andric } else { 8990b57cec5SDimitry Andric std::string S = PP.getSpelling(Tok); 9000b57cec5SDimitry Andric OS.write(S.data(), S.size()); 9010b57cec5SDimitry Andric 9020b57cec5SDimitry Andric // Tokens that can contain embedded newlines need to adjust our current 9030b57cec5SDimitry Andric // line number. 9040b57cec5SDimitry Andric if (Tok.getKind() == tok::comment || Tok.getKind() == tok::unknown) 9050b57cec5SDimitry Andric Callbacks->HandleNewlinesInToken(S.data(), S.size()); 906349cc55cSDimitry Andric if (Tok.is(tok::comment) && S.size() >= 2 && S[0] == '/' && S[1] == '/') { 907349cc55cSDimitry Andric // It's a line comment; 908349cc55cSDimitry Andric // Ensure that we don't concatenate anything behind it. 909349cc55cSDimitry Andric Callbacks->setEmittedDirectiveOnThisLine(); 910349cc55cSDimitry Andric } 9110b57cec5SDimitry Andric } 9120b57cec5SDimitry Andric Callbacks->setEmittedTokensOnThisLine(); 913349cc55cSDimitry Andric IsStartOfLine = false; 9140b57cec5SDimitry Andric 9150b57cec5SDimitry Andric if (Tok.is(tok::eof)) break; 9160b57cec5SDimitry Andric 9170b57cec5SDimitry Andric PP.Lex(Tok); 9180b57cec5SDimitry Andric } 9190b57cec5SDimitry Andric } 9200b57cec5SDimitry Andric 9210b57cec5SDimitry Andric typedef std::pair<const IdentifierInfo *, MacroInfo *> id_macro_pair; 9220b57cec5SDimitry Andric static int MacroIDCompare(const id_macro_pair *LHS, const id_macro_pair *RHS) { 9230b57cec5SDimitry Andric return LHS->first->getName().compare(RHS->first->getName()); 9240b57cec5SDimitry Andric } 9250b57cec5SDimitry Andric 9260b57cec5SDimitry Andric static void DoPrintMacros(Preprocessor &PP, raw_ostream *OS) { 9270b57cec5SDimitry Andric // Ignore unknown pragmas. 9280b57cec5SDimitry Andric PP.IgnorePragmas(); 9290b57cec5SDimitry Andric 9300b57cec5SDimitry Andric // -dM mode just scans and ignores all tokens in the files, then dumps out 9310b57cec5SDimitry Andric // the macro table at the end. 9320b57cec5SDimitry Andric PP.EnterMainSourceFile(); 9330b57cec5SDimitry Andric 9340b57cec5SDimitry Andric Token Tok; 9350b57cec5SDimitry Andric do PP.Lex(Tok); 9360b57cec5SDimitry Andric while (Tok.isNot(tok::eof)); 9370b57cec5SDimitry Andric 9380b57cec5SDimitry Andric SmallVector<id_macro_pair, 128> MacrosByID; 9390b57cec5SDimitry Andric for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end(); 9400b57cec5SDimitry Andric I != E; ++I) { 9410b57cec5SDimitry Andric auto *MD = I->second.getLatest(); 9420b57cec5SDimitry Andric if (MD && MD->isDefined()) 9430b57cec5SDimitry Andric MacrosByID.push_back(id_macro_pair(I->first, MD->getMacroInfo())); 9440b57cec5SDimitry Andric } 9450b57cec5SDimitry Andric llvm::array_pod_sort(MacrosByID.begin(), MacrosByID.end(), MacroIDCompare); 9460b57cec5SDimitry Andric 9470b57cec5SDimitry Andric for (unsigned i = 0, e = MacrosByID.size(); i != e; ++i) { 9480b57cec5SDimitry Andric MacroInfo &MI = *MacrosByID[i].second; 9490b57cec5SDimitry Andric // Ignore computed macros like __LINE__ and friends. 9500b57cec5SDimitry Andric if (MI.isBuiltinMacro()) continue; 9510b57cec5SDimitry Andric 9520b57cec5SDimitry Andric PrintMacroDefinition(*MacrosByID[i].first, MI, PP, *OS); 9530b57cec5SDimitry Andric *OS << '\n'; 9540b57cec5SDimitry Andric } 9550b57cec5SDimitry Andric } 9560b57cec5SDimitry Andric 9570b57cec5SDimitry Andric /// DoPrintPreprocessedInput - This implements -E mode. 9580b57cec5SDimitry Andric /// 9590b57cec5SDimitry Andric void clang::DoPrintPreprocessedInput(Preprocessor &PP, raw_ostream *OS, 9600b57cec5SDimitry Andric const PreprocessorOutputOptions &Opts) { 9610b57cec5SDimitry Andric // Show macros with no output is handled specially. 9620b57cec5SDimitry Andric if (!Opts.ShowCPP) { 9630b57cec5SDimitry Andric assert(Opts.ShowMacros && "Not yet implemented!"); 9640b57cec5SDimitry Andric DoPrintMacros(PP, OS); 9650b57cec5SDimitry Andric return; 9660b57cec5SDimitry Andric } 9670b57cec5SDimitry Andric 9680b57cec5SDimitry Andric // Inform the preprocessor whether we want it to retain comments or not, due 9690b57cec5SDimitry Andric // to -C or -CC. 9700b57cec5SDimitry Andric PP.SetCommentRetentionState(Opts.ShowComments, Opts.ShowMacroComments); 9710b57cec5SDimitry Andric 9720b57cec5SDimitry Andric PrintPPOutputPPCallbacks *Callbacks = new PrintPPOutputPPCallbacks( 9730b57cec5SDimitry Andric PP, *OS, !Opts.ShowLineMarkers, Opts.ShowMacros, 974349cc55cSDimitry Andric Opts.ShowIncludeDirectives, Opts.UseLineDirectives, 975*81ad6265SDimitry Andric Opts.MinimizeWhitespace, Opts.DirectivesOnly); 9760b57cec5SDimitry Andric 9770b57cec5SDimitry Andric // Expand macros in pragmas with -fms-extensions. The assumption is that 9780b57cec5SDimitry Andric // the majority of pragmas in such a file will be Microsoft pragmas. 9790b57cec5SDimitry Andric // Remember the handlers we will add so that we can remove them later. 9800b57cec5SDimitry Andric std::unique_ptr<UnknownPragmaHandler> MicrosoftExtHandler( 9810b57cec5SDimitry Andric new UnknownPragmaHandler( 9820b57cec5SDimitry Andric "#pragma", Callbacks, 9830b57cec5SDimitry Andric /*RequireTokenExpansion=*/PP.getLangOpts().MicrosoftExt)); 9840b57cec5SDimitry Andric 9850b57cec5SDimitry Andric std::unique_ptr<UnknownPragmaHandler> GCCHandler(new UnknownPragmaHandler( 9860b57cec5SDimitry Andric "#pragma GCC", Callbacks, 9870b57cec5SDimitry Andric /*RequireTokenExpansion=*/PP.getLangOpts().MicrosoftExt)); 9880b57cec5SDimitry Andric 9890b57cec5SDimitry Andric std::unique_ptr<UnknownPragmaHandler> ClangHandler(new UnknownPragmaHandler( 9900b57cec5SDimitry Andric "#pragma clang", Callbacks, 9910b57cec5SDimitry Andric /*RequireTokenExpansion=*/PP.getLangOpts().MicrosoftExt)); 9920b57cec5SDimitry Andric 9930b57cec5SDimitry Andric PP.AddPragmaHandler(MicrosoftExtHandler.get()); 9940b57cec5SDimitry Andric PP.AddPragmaHandler("GCC", GCCHandler.get()); 9950b57cec5SDimitry Andric PP.AddPragmaHandler("clang", ClangHandler.get()); 9960b57cec5SDimitry Andric 9970b57cec5SDimitry Andric // The tokens after pragma omp need to be expanded. 9980b57cec5SDimitry Andric // 9990b57cec5SDimitry Andric // OpenMP [2.1, Directive format] 10000b57cec5SDimitry Andric // Preprocessing tokens following the #pragma omp are subject to macro 10010b57cec5SDimitry Andric // replacement. 10020b57cec5SDimitry Andric std::unique_ptr<UnknownPragmaHandler> OpenMPHandler( 10030b57cec5SDimitry Andric new UnknownPragmaHandler("#pragma omp", Callbacks, 10040b57cec5SDimitry Andric /*RequireTokenExpansion=*/true)); 10050b57cec5SDimitry Andric PP.AddPragmaHandler("omp", OpenMPHandler.get()); 10060b57cec5SDimitry Andric 10070b57cec5SDimitry Andric PP.addPPCallbacks(std::unique_ptr<PPCallbacks>(Callbacks)); 10080b57cec5SDimitry Andric 10090b57cec5SDimitry Andric // After we have configured the preprocessor, enter the main file. 10100b57cec5SDimitry Andric PP.EnterMainSourceFile(); 1011*81ad6265SDimitry Andric if (Opts.DirectivesOnly) 1012*81ad6265SDimitry Andric PP.SetMacroExpansionOnlyInDirectives(); 10130b57cec5SDimitry Andric 10140b57cec5SDimitry Andric // Consume all of the tokens that come from the predefines buffer. Those 10150b57cec5SDimitry Andric // should not be emitted into the output and are guaranteed to be at the 10160b57cec5SDimitry Andric // start. 10170b57cec5SDimitry Andric const SourceManager &SourceMgr = PP.getSourceManager(); 10180b57cec5SDimitry Andric Token Tok; 10190b57cec5SDimitry Andric do { 10200b57cec5SDimitry Andric PP.Lex(Tok); 10210b57cec5SDimitry Andric if (Tok.is(tok::eof) || !Tok.getLocation().isFileID()) 10220b57cec5SDimitry Andric break; 10230b57cec5SDimitry Andric 10240b57cec5SDimitry Andric PresumedLoc PLoc = SourceMgr.getPresumedLoc(Tok.getLocation()); 10250b57cec5SDimitry Andric if (PLoc.isInvalid()) 10260b57cec5SDimitry Andric break; 10270b57cec5SDimitry Andric 10280b57cec5SDimitry Andric if (strcmp(PLoc.getFilename(), "<built-in>")) 10290b57cec5SDimitry Andric break; 10300b57cec5SDimitry Andric } while (true); 10310b57cec5SDimitry Andric 10320b57cec5SDimitry Andric // Read all the preprocessed tokens, printing them out to the stream. 10330b57cec5SDimitry Andric PrintPreprocessedTokens(PP, Tok, Callbacks, *OS); 10340b57cec5SDimitry Andric *OS << '\n'; 10350b57cec5SDimitry Andric 10360b57cec5SDimitry Andric // Remove the handlers we just added to leave the preprocessor in a sane state 10370b57cec5SDimitry Andric // so that it can be reused (for example by a clang::Parser instance). 10380b57cec5SDimitry Andric PP.RemovePragmaHandler(MicrosoftExtHandler.get()); 10390b57cec5SDimitry Andric PP.RemovePragmaHandler("GCC", GCCHandler.get()); 10400b57cec5SDimitry Andric PP.RemovePragmaHandler("clang", ClangHandler.get()); 10410b57cec5SDimitry Andric PP.RemovePragmaHandler("omp", OpenMPHandler.get()); 10420b57cec5SDimitry Andric } 1043