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