1e5dd7070Spatrick //===--- PrintPreprocessedOutput.cpp - Implement the -E mode --------------===//
2e5dd7070Spatrick //
3e5dd7070Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e5dd7070Spatrick // See https://llvm.org/LICENSE.txt for license information.
5e5dd7070Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6e5dd7070Spatrick //
7e5dd7070Spatrick //===----------------------------------------------------------------------===//
8e5dd7070Spatrick //
9e5dd7070Spatrick // This code simply runs the preprocessor on the input file and prints out the
10e5dd7070Spatrick // result. This is the traditional behavior of the -E option.
11e5dd7070Spatrick //
12e5dd7070Spatrick //===----------------------------------------------------------------------===//
13e5dd7070Spatrick
14e5dd7070Spatrick #include "clang/Frontend/Utils.h"
15e5dd7070Spatrick #include "clang/Basic/CharInfo.h"
16e5dd7070Spatrick #include "clang/Basic/Diagnostic.h"
17e5dd7070Spatrick #include "clang/Basic/SourceManager.h"
18e5dd7070Spatrick #include "clang/Frontend/PreprocessorOutputOptions.h"
19e5dd7070Spatrick #include "clang/Lex/MacroInfo.h"
20e5dd7070Spatrick #include "clang/Lex/PPCallbacks.h"
21e5dd7070Spatrick #include "clang/Lex/Pragma.h"
22e5dd7070Spatrick #include "clang/Lex/Preprocessor.h"
23e5dd7070Spatrick #include "clang/Lex/TokenConcatenation.h"
24e5dd7070Spatrick #include "llvm/ADT/STLExtras.h"
25e5dd7070Spatrick #include "llvm/ADT/SmallString.h"
26e5dd7070Spatrick #include "llvm/ADT/StringRef.h"
27e5dd7070Spatrick #include "llvm/Support/ErrorHandling.h"
28e5dd7070Spatrick #include "llvm/Support/raw_ostream.h"
29e5dd7070Spatrick #include <cstdio>
30e5dd7070Spatrick using namespace clang;
31e5dd7070Spatrick
32e5dd7070Spatrick /// PrintMacroDefinition - Print a macro definition in a form that will be
33e5dd7070Spatrick /// properly accepted back as a definition.
PrintMacroDefinition(const IdentifierInfo & II,const MacroInfo & MI,Preprocessor & PP,raw_ostream & OS)34e5dd7070Spatrick static void PrintMacroDefinition(const IdentifierInfo &II, const MacroInfo &MI,
35e5dd7070Spatrick Preprocessor &PP, raw_ostream &OS) {
36e5dd7070Spatrick OS << "#define " << II.getName();
37e5dd7070Spatrick
38e5dd7070Spatrick if (MI.isFunctionLike()) {
39e5dd7070Spatrick OS << '(';
40e5dd7070Spatrick if (!MI.param_empty()) {
41e5dd7070Spatrick MacroInfo::param_iterator AI = MI.param_begin(), E = MI.param_end();
42e5dd7070Spatrick for (; AI+1 != E; ++AI) {
43e5dd7070Spatrick OS << (*AI)->getName();
44e5dd7070Spatrick OS << ',';
45e5dd7070Spatrick }
46e5dd7070Spatrick
47e5dd7070Spatrick // Last argument.
48e5dd7070Spatrick if ((*AI)->getName() == "__VA_ARGS__")
49e5dd7070Spatrick OS << "...";
50e5dd7070Spatrick else
51e5dd7070Spatrick OS << (*AI)->getName();
52e5dd7070Spatrick }
53e5dd7070Spatrick
54e5dd7070Spatrick if (MI.isGNUVarargs())
55e5dd7070Spatrick OS << "..."; // #define foo(x...)
56e5dd7070Spatrick
57e5dd7070Spatrick OS << ')';
58e5dd7070Spatrick }
59e5dd7070Spatrick
60e5dd7070Spatrick // GCC always emits a space, even if the macro body is empty. However, do not
61e5dd7070Spatrick // want to emit two spaces if the first token has a leading space.
62e5dd7070Spatrick if (MI.tokens_empty() || !MI.tokens_begin()->hasLeadingSpace())
63e5dd7070Spatrick OS << ' ';
64e5dd7070Spatrick
65e5dd7070Spatrick SmallString<128> SpellingBuffer;
66e5dd7070Spatrick for (const auto &T : MI.tokens()) {
67e5dd7070Spatrick if (T.hasLeadingSpace())
68e5dd7070Spatrick OS << ' ';
69e5dd7070Spatrick
70e5dd7070Spatrick OS << PP.getSpelling(T, SpellingBuffer);
71e5dd7070Spatrick }
72e5dd7070Spatrick }
73e5dd7070Spatrick
74e5dd7070Spatrick //===----------------------------------------------------------------------===//
75e5dd7070Spatrick // Preprocessed token printer
76e5dd7070Spatrick //===----------------------------------------------------------------------===//
77e5dd7070Spatrick
78e5dd7070Spatrick namespace {
79e5dd7070Spatrick class PrintPPOutputPPCallbacks : public PPCallbacks {
80e5dd7070Spatrick Preprocessor &PP;
81e5dd7070Spatrick SourceManager &SM;
82e5dd7070Spatrick TokenConcatenation ConcatInfo;
83e5dd7070Spatrick public:
84e5dd7070Spatrick raw_ostream &OS;
85e5dd7070Spatrick private:
86e5dd7070Spatrick unsigned CurLine;
87e5dd7070Spatrick
88e5dd7070Spatrick bool EmittedTokensOnThisLine;
89e5dd7070Spatrick bool EmittedDirectiveOnThisLine;
90e5dd7070Spatrick SrcMgr::CharacteristicKind FileType;
91e5dd7070Spatrick SmallString<512> CurFilename;
92e5dd7070Spatrick bool Initialized;
93e5dd7070Spatrick bool DisableLineMarkers;
94e5dd7070Spatrick bool DumpDefines;
95e5dd7070Spatrick bool DumpIncludeDirectives;
96e5dd7070Spatrick bool UseLineDirectives;
97e5dd7070Spatrick bool IsFirstFileEntered;
98*12c85518Srobert bool MinimizeWhitespace;
99*12c85518Srobert bool DirectivesOnly;
100*12c85518Srobert
101*12c85518Srobert Token PrevTok;
102*12c85518Srobert Token PrevPrevTok;
103*12c85518Srobert
104e5dd7070Spatrick public:
PrintPPOutputPPCallbacks(Preprocessor & pp,raw_ostream & os,bool lineMarkers,bool defines,bool DumpIncludeDirectives,bool UseLineDirectives,bool MinimizeWhitespace,bool DirectivesOnly)105e5dd7070Spatrick PrintPPOutputPPCallbacks(Preprocessor &pp, raw_ostream &os, bool lineMarkers,
106e5dd7070Spatrick bool defines, bool DumpIncludeDirectives,
107*12c85518Srobert bool UseLineDirectives, bool MinimizeWhitespace,
108*12c85518Srobert bool DirectivesOnly)
109e5dd7070Spatrick : PP(pp), SM(PP.getSourceManager()), ConcatInfo(PP), OS(os),
110e5dd7070Spatrick DisableLineMarkers(lineMarkers), DumpDefines(defines),
111e5dd7070Spatrick DumpIncludeDirectives(DumpIncludeDirectives),
112*12c85518Srobert UseLineDirectives(UseLineDirectives),
113*12c85518Srobert MinimizeWhitespace(MinimizeWhitespace), DirectivesOnly(DirectivesOnly) {
114e5dd7070Spatrick CurLine = 0;
115e5dd7070Spatrick CurFilename += "<uninit>";
116e5dd7070Spatrick EmittedTokensOnThisLine = false;
117e5dd7070Spatrick EmittedDirectiveOnThisLine = false;
118e5dd7070Spatrick FileType = SrcMgr::C_User;
119e5dd7070Spatrick Initialized = false;
120e5dd7070Spatrick IsFirstFileEntered = false;
121*12c85518Srobert
122*12c85518Srobert PrevTok.startToken();
123*12c85518Srobert PrevPrevTok.startToken();
124e5dd7070Spatrick }
125e5dd7070Spatrick
isMinimizeWhitespace() const126*12c85518Srobert bool isMinimizeWhitespace() const { return MinimizeWhitespace; }
127*12c85518Srobert
setEmittedTokensOnThisLine()128e5dd7070Spatrick void setEmittedTokensOnThisLine() { EmittedTokensOnThisLine = true; }
hasEmittedTokensOnThisLine() const129e5dd7070Spatrick bool hasEmittedTokensOnThisLine() const { return EmittedTokensOnThisLine; }
130e5dd7070Spatrick
setEmittedDirectiveOnThisLine()131e5dd7070Spatrick void setEmittedDirectiveOnThisLine() { EmittedDirectiveOnThisLine = true; }
hasEmittedDirectiveOnThisLine() const132e5dd7070Spatrick bool hasEmittedDirectiveOnThisLine() const {
133e5dd7070Spatrick return EmittedDirectiveOnThisLine;
134e5dd7070Spatrick }
135e5dd7070Spatrick
136*12c85518Srobert /// Ensure that the output stream position is at the beginning of a new line
137*12c85518Srobert /// and inserts one if it does not. It is intended to ensure that directives
138*12c85518Srobert /// inserted by the directives not from the input source (such as #line) are
139*12c85518Srobert /// in the first column. To insert newlines that represent the input, use
140*12c85518Srobert /// MoveToLine(/*...*/, /*RequireStartOfLine=*/true).
141*12c85518Srobert void startNewLineIfNeeded();
142e5dd7070Spatrick
143e5dd7070Spatrick void FileChanged(SourceLocation Loc, FileChangeReason Reason,
144e5dd7070Spatrick SrcMgr::CharacteristicKind FileType,
145e5dd7070Spatrick FileID PrevFID) override;
146e5dd7070Spatrick void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok,
147e5dd7070Spatrick StringRef FileName, bool IsAngled,
148*12c85518Srobert CharSourceRange FilenameRange,
149*12c85518Srobert OptionalFileEntryRef File, StringRef SearchPath,
150*12c85518Srobert StringRef RelativePath, const Module *Imported,
151e5dd7070Spatrick SrcMgr::CharacteristicKind FileType) override;
152e5dd7070Spatrick void Ident(SourceLocation Loc, StringRef str) override;
153e5dd7070Spatrick void PragmaMessage(SourceLocation Loc, StringRef Namespace,
154e5dd7070Spatrick PragmaMessageKind Kind, StringRef Str) override;
155e5dd7070Spatrick void PragmaDebug(SourceLocation Loc, StringRef DebugType) override;
156e5dd7070Spatrick void PragmaDiagnosticPush(SourceLocation Loc, StringRef Namespace) override;
157e5dd7070Spatrick void PragmaDiagnosticPop(SourceLocation Loc, StringRef Namespace) override;
158e5dd7070Spatrick void PragmaDiagnostic(SourceLocation Loc, StringRef Namespace,
159e5dd7070Spatrick diag::Severity Map, StringRef Str) override;
160*12c85518Srobert void PragmaWarning(SourceLocation Loc, PragmaWarningSpecifier WarningSpec,
161e5dd7070Spatrick ArrayRef<int> Ids) override;
162e5dd7070Spatrick void PragmaWarningPush(SourceLocation Loc, int Level) override;
163e5dd7070Spatrick void PragmaWarningPop(SourceLocation Loc) override;
164e5dd7070Spatrick void PragmaExecCharsetPush(SourceLocation Loc, StringRef Str) override;
165e5dd7070Spatrick void PragmaExecCharsetPop(SourceLocation Loc) override;
166e5dd7070Spatrick void PragmaAssumeNonNullBegin(SourceLocation Loc) override;
167e5dd7070Spatrick void PragmaAssumeNonNullEnd(SourceLocation Loc) override;
168e5dd7070Spatrick
169*12c85518Srobert /// Insert whitespace before emitting the next token.
170*12c85518Srobert ///
171*12c85518Srobert /// @param Tok Next token to be emitted.
172*12c85518Srobert /// @param RequireSpace Ensure at least one whitespace is emitted. Useful
173*12c85518Srobert /// if non-tokens have been emitted to the stream.
174*12c85518Srobert /// @param RequireSameLine Never emit newlines. Useful when semantics depend
175*12c85518Srobert /// on being on the same line, such as directives.
176*12c85518Srobert void HandleWhitespaceBeforeTok(const Token &Tok, bool RequireSpace,
177*12c85518Srobert bool RequireSameLine);
178e5dd7070Spatrick
179e5dd7070Spatrick /// Move to the line of the provided source location. This will
180*12c85518Srobert /// return true if a newline was inserted or if
181*12c85518Srobert /// the requested location is the first token on the first line.
182*12c85518Srobert /// In these cases the next output will be the first column on the line and
183*12c85518Srobert /// make it possible to insert indention. The newline was inserted
184*12c85518Srobert /// implicitly when at the beginning of the file.
185*12c85518Srobert ///
186*12c85518Srobert /// @param Tok Token where to move to.
187*12c85518Srobert /// @param RequireStartOfLine Whether the next line depends on being in the
188*12c85518Srobert /// first column, such as a directive.
189*12c85518Srobert ///
190*12c85518Srobert /// @return Whether column adjustments are necessary.
MoveToLine(const Token & Tok,bool RequireStartOfLine)191*12c85518Srobert bool MoveToLine(const Token &Tok, bool RequireStartOfLine) {
192*12c85518Srobert PresumedLoc PLoc = SM.getPresumedLoc(Tok.getLocation());
193*12c85518Srobert unsigned TargetLine = PLoc.isValid() ? PLoc.getLine() : CurLine;
194*12c85518Srobert bool IsFirstInFile =
195*12c85518Srobert Tok.isAtStartOfLine() && PLoc.isValid() && PLoc.getLine() == 1;
196*12c85518Srobert return MoveToLine(TargetLine, RequireStartOfLine) || IsFirstInFile;
197e5dd7070Spatrick }
198*12c85518Srobert
199*12c85518Srobert /// Move to the line of the provided source location. Returns true if a new
200*12c85518Srobert /// line was inserted.
MoveToLine(SourceLocation Loc,bool RequireStartOfLine)201*12c85518Srobert bool MoveToLine(SourceLocation Loc, bool RequireStartOfLine) {
202*12c85518Srobert PresumedLoc PLoc = SM.getPresumedLoc(Loc);
203*12c85518Srobert unsigned TargetLine = PLoc.isValid() ? PLoc.getLine() : CurLine;
204*12c85518Srobert return MoveToLine(TargetLine, RequireStartOfLine);
205*12c85518Srobert }
206*12c85518Srobert bool MoveToLine(unsigned LineNo, bool RequireStartOfLine);
207e5dd7070Spatrick
AvoidConcat(const Token & PrevPrevTok,const Token & PrevTok,const Token & Tok)208e5dd7070Spatrick bool AvoidConcat(const Token &PrevPrevTok, const Token &PrevTok,
209e5dd7070Spatrick const Token &Tok) {
210e5dd7070Spatrick return ConcatInfo.AvoidConcat(PrevPrevTok, PrevTok, Tok);
211e5dd7070Spatrick }
212e5dd7070Spatrick void WriteLineInfo(unsigned LineNo, const char *Extra=nullptr,
213e5dd7070Spatrick unsigned ExtraLen=0);
LineMarkersAreDisabled() const214e5dd7070Spatrick bool LineMarkersAreDisabled() const { return DisableLineMarkers; }
215e5dd7070Spatrick void HandleNewlinesInToken(const char *TokStr, unsigned Len);
216e5dd7070Spatrick
217e5dd7070Spatrick /// MacroDefined - This hook is called whenever a macro definition is seen.
218e5dd7070Spatrick void MacroDefined(const Token &MacroNameTok,
219e5dd7070Spatrick const MacroDirective *MD) override;
220e5dd7070Spatrick
221e5dd7070Spatrick /// MacroUndefined - This hook is called whenever a macro #undef is seen.
222e5dd7070Spatrick void MacroUndefined(const Token &MacroNameTok,
223e5dd7070Spatrick const MacroDefinition &MD,
224e5dd7070Spatrick const MacroDirective *Undef) override;
225e5dd7070Spatrick
226e5dd7070Spatrick void BeginModule(const Module *M);
227e5dd7070Spatrick void EndModule(const Module *M);
228e5dd7070Spatrick };
229e5dd7070Spatrick } // end anonymous namespace
230e5dd7070Spatrick
WriteLineInfo(unsigned LineNo,const char * Extra,unsigned ExtraLen)231e5dd7070Spatrick void PrintPPOutputPPCallbacks::WriteLineInfo(unsigned LineNo,
232e5dd7070Spatrick const char *Extra,
233e5dd7070Spatrick unsigned ExtraLen) {
234*12c85518Srobert startNewLineIfNeeded();
235e5dd7070Spatrick
236e5dd7070Spatrick // Emit #line directives or GNU line markers depending on what mode we're in.
237e5dd7070Spatrick if (UseLineDirectives) {
238e5dd7070Spatrick OS << "#line" << ' ' << LineNo << ' ' << '"';
239e5dd7070Spatrick OS.write_escaped(CurFilename);
240e5dd7070Spatrick OS << '"';
241e5dd7070Spatrick } else {
242e5dd7070Spatrick OS << '#' << ' ' << LineNo << ' ' << '"';
243e5dd7070Spatrick OS.write_escaped(CurFilename);
244e5dd7070Spatrick OS << '"';
245e5dd7070Spatrick
246e5dd7070Spatrick if (ExtraLen)
247e5dd7070Spatrick OS.write(Extra, ExtraLen);
248e5dd7070Spatrick
249e5dd7070Spatrick if (FileType == SrcMgr::C_System)
250e5dd7070Spatrick OS.write(" 3", 2);
251e5dd7070Spatrick else if (FileType == SrcMgr::C_ExternCSystem)
252e5dd7070Spatrick OS.write(" 3 4", 4);
253e5dd7070Spatrick }
254e5dd7070Spatrick OS << '\n';
255e5dd7070Spatrick }
256e5dd7070Spatrick
257e5dd7070Spatrick /// MoveToLine - Move the output to the source line specified by the location
258e5dd7070Spatrick /// object. We can do this by emitting some number of \n's, or be emitting a
259e5dd7070Spatrick /// #line directive. This returns false if already at the specified line, true
260e5dd7070Spatrick /// if some newlines were emitted.
MoveToLine(unsigned LineNo,bool RequireStartOfLine)261*12c85518Srobert bool PrintPPOutputPPCallbacks::MoveToLine(unsigned LineNo,
262*12c85518Srobert bool RequireStartOfLine) {
263*12c85518Srobert // If it is required to start a new line or finish the current, insert
264*12c85518Srobert // vertical whitespace now and take it into account when moving to the
265*12c85518Srobert // expected line.
266*12c85518Srobert bool StartedNewLine = false;
267*12c85518Srobert if ((RequireStartOfLine && EmittedTokensOnThisLine) ||
268*12c85518Srobert EmittedDirectiveOnThisLine) {
269*12c85518Srobert OS << '\n';
270*12c85518Srobert StartedNewLine = true;
271*12c85518Srobert CurLine += 1;
272*12c85518Srobert EmittedTokensOnThisLine = false;
273*12c85518Srobert EmittedDirectiveOnThisLine = false;
274*12c85518Srobert }
275*12c85518Srobert
276e5dd7070Spatrick // If this line is "close enough" to the original line, just print newlines,
277e5dd7070Spatrick // otherwise print a #line directive.
278*12c85518Srobert if (CurLine == LineNo) {
279*12c85518Srobert // Nothing to do if we are already on the correct line.
280*12c85518Srobert } else if (MinimizeWhitespace && DisableLineMarkers) {
281*12c85518Srobert // With -E -P -fminimize-whitespace, don't emit anything if not necessary.
282*12c85518Srobert } else if (!StartedNewLine && LineNo - CurLine == 1) {
283*12c85518Srobert // Printing a single line has priority over printing a #line directive, even
284*12c85518Srobert // when minimizing whitespace which otherwise would print #line directives
285*12c85518Srobert // for every single line.
286e5dd7070Spatrick OS << '\n';
287*12c85518Srobert StartedNewLine = true;
288*12c85518Srobert } else if (!DisableLineMarkers) {
289*12c85518Srobert if (LineNo - CurLine <= 8) {
290e5dd7070Spatrick const char *NewLines = "\n\n\n\n\n\n\n\n";
291e5dd7070Spatrick OS.write(NewLines, LineNo - CurLine);
292*12c85518Srobert } else {
293e5dd7070Spatrick // Emit a #line or line marker.
294e5dd7070Spatrick WriteLineInfo(LineNo, nullptr, 0);
295*12c85518Srobert }
296*12c85518Srobert StartedNewLine = true;
297*12c85518Srobert } else if (EmittedTokensOnThisLine) {
298*12c85518Srobert // If we are not on the correct line and don't need to be line-correct,
299*12c85518Srobert // at least ensure we start on a new line.
300*12c85518Srobert OS << '\n';
301*12c85518Srobert StartedNewLine = true;
302*12c85518Srobert }
303*12c85518Srobert
304*12c85518Srobert if (StartedNewLine) {
305*12c85518Srobert EmittedTokensOnThisLine = false;
306*12c85518Srobert EmittedDirectiveOnThisLine = false;
307e5dd7070Spatrick }
308e5dd7070Spatrick
309e5dd7070Spatrick CurLine = LineNo;
310*12c85518Srobert return StartedNewLine;
311e5dd7070Spatrick }
312e5dd7070Spatrick
startNewLineIfNeeded()313*12c85518Srobert void PrintPPOutputPPCallbacks::startNewLineIfNeeded() {
314e5dd7070Spatrick if (EmittedTokensOnThisLine || EmittedDirectiveOnThisLine) {
315e5dd7070Spatrick OS << '\n';
316e5dd7070Spatrick EmittedTokensOnThisLine = false;
317e5dd7070Spatrick EmittedDirectiveOnThisLine = false;
318e5dd7070Spatrick }
319e5dd7070Spatrick }
320e5dd7070Spatrick
321e5dd7070Spatrick /// FileChanged - Whenever the preprocessor enters or exits a #include file
322e5dd7070Spatrick /// it invokes this handler. Update our conception of the current source
323e5dd7070Spatrick /// position.
FileChanged(SourceLocation Loc,FileChangeReason Reason,SrcMgr::CharacteristicKind NewFileType,FileID PrevFID)324e5dd7070Spatrick void PrintPPOutputPPCallbacks::FileChanged(SourceLocation Loc,
325e5dd7070Spatrick FileChangeReason Reason,
326e5dd7070Spatrick SrcMgr::CharacteristicKind NewFileType,
327e5dd7070Spatrick FileID PrevFID) {
328e5dd7070Spatrick // Unless we are exiting a #include, make sure to skip ahead to the line the
329e5dd7070Spatrick // #include directive was at.
330e5dd7070Spatrick SourceManager &SourceMgr = SM;
331e5dd7070Spatrick
332e5dd7070Spatrick PresumedLoc UserLoc = SourceMgr.getPresumedLoc(Loc);
333e5dd7070Spatrick if (UserLoc.isInvalid())
334e5dd7070Spatrick return;
335e5dd7070Spatrick
336e5dd7070Spatrick unsigned NewLine = UserLoc.getLine();
337e5dd7070Spatrick
338e5dd7070Spatrick if (Reason == PPCallbacks::EnterFile) {
339e5dd7070Spatrick SourceLocation IncludeLoc = UserLoc.getIncludeLoc();
340e5dd7070Spatrick if (IncludeLoc.isValid())
341*12c85518Srobert MoveToLine(IncludeLoc, /*RequireStartOfLine=*/false);
342e5dd7070Spatrick } else if (Reason == PPCallbacks::SystemHeaderPragma) {
343e5dd7070Spatrick // GCC emits the # directive for this directive on the line AFTER the
344e5dd7070Spatrick // directive and emits a bunch of spaces that aren't needed. This is because
345e5dd7070Spatrick // otherwise we will emit a line marker for THIS line, which requires an
346e5dd7070Spatrick // extra blank line after the directive to avoid making all following lines
347e5dd7070Spatrick // off by one. We can do better by simply incrementing NewLine here.
348e5dd7070Spatrick NewLine += 1;
349e5dd7070Spatrick }
350e5dd7070Spatrick
351e5dd7070Spatrick CurLine = NewLine;
352e5dd7070Spatrick
353e5dd7070Spatrick CurFilename.clear();
354e5dd7070Spatrick CurFilename += UserLoc.getFilename();
355e5dd7070Spatrick FileType = NewFileType;
356e5dd7070Spatrick
357e5dd7070Spatrick if (DisableLineMarkers) {
358*12c85518Srobert if (!MinimizeWhitespace)
359*12c85518Srobert startNewLineIfNeeded();
360e5dd7070Spatrick return;
361e5dd7070Spatrick }
362e5dd7070Spatrick
363e5dd7070Spatrick if (!Initialized) {
364e5dd7070Spatrick WriteLineInfo(CurLine);
365e5dd7070Spatrick Initialized = true;
366e5dd7070Spatrick }
367e5dd7070Spatrick
368e5dd7070Spatrick // Do not emit an enter marker for the main file (which we expect is the first
369e5dd7070Spatrick // entered file). This matches gcc, and improves compatibility with some tools
370e5dd7070Spatrick // which track the # line markers as a way to determine when the preprocessed
371e5dd7070Spatrick // output is in the context of the main file.
372e5dd7070Spatrick if (Reason == PPCallbacks::EnterFile && !IsFirstFileEntered) {
373e5dd7070Spatrick IsFirstFileEntered = true;
374e5dd7070Spatrick return;
375e5dd7070Spatrick }
376e5dd7070Spatrick
377e5dd7070Spatrick switch (Reason) {
378e5dd7070Spatrick case PPCallbacks::EnterFile:
379e5dd7070Spatrick WriteLineInfo(CurLine, " 1", 2);
380e5dd7070Spatrick break;
381e5dd7070Spatrick case PPCallbacks::ExitFile:
382e5dd7070Spatrick WriteLineInfo(CurLine, " 2", 2);
383e5dd7070Spatrick break;
384e5dd7070Spatrick case PPCallbacks::SystemHeaderPragma:
385e5dd7070Spatrick case PPCallbacks::RenameFile:
386e5dd7070Spatrick WriteLineInfo(CurLine);
387e5dd7070Spatrick break;
388e5dd7070Spatrick }
389e5dd7070Spatrick }
390e5dd7070Spatrick
InclusionDirective(SourceLocation HashLoc,const Token & IncludeTok,StringRef FileName,bool IsAngled,CharSourceRange FilenameRange,OptionalFileEntryRef File,StringRef SearchPath,StringRef RelativePath,const Module * Imported,SrcMgr::CharacteristicKind FileType)391e5dd7070Spatrick void PrintPPOutputPPCallbacks::InclusionDirective(
392*12c85518Srobert SourceLocation HashLoc, const Token &IncludeTok, StringRef FileName,
393*12c85518Srobert bool IsAngled, CharSourceRange FilenameRange, OptionalFileEntryRef File,
394*12c85518Srobert StringRef SearchPath, StringRef RelativePath, const Module *Imported,
395e5dd7070Spatrick SrcMgr::CharacteristicKind FileType) {
396e5dd7070Spatrick // In -dI mode, dump #include directives prior to dumping their content or
397e5dd7070Spatrick // interpretation.
398e5dd7070Spatrick if (DumpIncludeDirectives) {
399*12c85518Srobert MoveToLine(HashLoc, /*RequireStartOfLine=*/true);
400e5dd7070Spatrick const std::string TokenText = PP.getSpelling(IncludeTok);
401e5dd7070Spatrick assert(!TokenText.empty());
402e5dd7070Spatrick OS << "#" << TokenText << " "
403e5dd7070Spatrick << (IsAngled ? '<' : '"') << FileName << (IsAngled ? '>' : '"')
404e5dd7070Spatrick << " /* clang -E -dI */";
405e5dd7070Spatrick setEmittedDirectiveOnThisLine();
406e5dd7070Spatrick }
407e5dd7070Spatrick
408e5dd7070Spatrick // When preprocessing, turn implicit imports into module import pragmas.
409e5dd7070Spatrick if (Imported) {
410e5dd7070Spatrick switch (IncludeTok.getIdentifierInfo()->getPPKeywordID()) {
411e5dd7070Spatrick case tok::pp_include:
412e5dd7070Spatrick case tok::pp_import:
413e5dd7070Spatrick case tok::pp_include_next:
414*12c85518Srobert MoveToLine(HashLoc, /*RequireStartOfLine=*/true);
415e5dd7070Spatrick OS << "#pragma clang module import " << Imported->getFullModuleName(true)
416e5dd7070Spatrick << " /* clang -E: implicit import for "
417e5dd7070Spatrick << "#" << PP.getSpelling(IncludeTok) << " "
418e5dd7070Spatrick << (IsAngled ? '<' : '"') << FileName << (IsAngled ? '>' : '"')
419e5dd7070Spatrick << " */";
420*12c85518Srobert setEmittedDirectiveOnThisLine();
421e5dd7070Spatrick break;
422e5dd7070Spatrick
423e5dd7070Spatrick case tok::pp___include_macros:
424e5dd7070Spatrick // #__include_macros has no effect on a user of a preprocessed source
425e5dd7070Spatrick // file; the only effect is on preprocessing.
426e5dd7070Spatrick //
427e5dd7070Spatrick // FIXME: That's not *quite* true: it causes the module in question to
428e5dd7070Spatrick // be loaded, which can affect downstream diagnostics.
429e5dd7070Spatrick break;
430e5dd7070Spatrick
431e5dd7070Spatrick default:
432e5dd7070Spatrick llvm_unreachable("unknown include directive kind");
433e5dd7070Spatrick break;
434e5dd7070Spatrick }
435e5dd7070Spatrick }
436e5dd7070Spatrick }
437e5dd7070Spatrick
438e5dd7070Spatrick /// Handle entering the scope of a module during a module compilation.
BeginModule(const Module * M)439e5dd7070Spatrick void PrintPPOutputPPCallbacks::BeginModule(const Module *M) {
440e5dd7070Spatrick startNewLineIfNeeded();
441e5dd7070Spatrick OS << "#pragma clang module begin " << M->getFullModuleName(true);
442e5dd7070Spatrick setEmittedDirectiveOnThisLine();
443e5dd7070Spatrick }
444e5dd7070Spatrick
445e5dd7070Spatrick /// Handle leaving the scope of a module during a module compilation.
EndModule(const Module * M)446e5dd7070Spatrick void PrintPPOutputPPCallbacks::EndModule(const Module *M) {
447e5dd7070Spatrick startNewLineIfNeeded();
448e5dd7070Spatrick OS << "#pragma clang module end /*" << M->getFullModuleName(true) << "*/";
449e5dd7070Spatrick setEmittedDirectiveOnThisLine();
450e5dd7070Spatrick }
451e5dd7070Spatrick
452e5dd7070Spatrick /// Ident - Handle #ident directives when read by the preprocessor.
453e5dd7070Spatrick ///
Ident(SourceLocation Loc,StringRef S)454e5dd7070Spatrick void PrintPPOutputPPCallbacks::Ident(SourceLocation Loc, StringRef S) {
455*12c85518Srobert MoveToLine(Loc, /*RequireStartOfLine=*/true);
456e5dd7070Spatrick
457e5dd7070Spatrick OS.write("#ident ", strlen("#ident "));
458e5dd7070Spatrick OS.write(S.begin(), S.size());
459*12c85518Srobert setEmittedTokensOnThisLine();
460e5dd7070Spatrick }
461e5dd7070Spatrick
462e5dd7070Spatrick /// MacroDefined - This hook is called whenever a macro definition is seen.
MacroDefined(const Token & MacroNameTok,const MacroDirective * MD)463e5dd7070Spatrick void PrintPPOutputPPCallbacks::MacroDefined(const Token &MacroNameTok,
464e5dd7070Spatrick const MacroDirective *MD) {
465e5dd7070Spatrick const MacroInfo *MI = MD->getMacroInfo();
466*12c85518Srobert // Print out macro definitions in -dD mode and when we have -fdirectives-only
467*12c85518Srobert // for C++20 header units.
468*12c85518Srobert if ((!DumpDefines && !DirectivesOnly) ||
469e5dd7070Spatrick // Ignore __FILE__ etc.
470*12c85518Srobert MI->isBuiltinMacro())
471*12c85518Srobert return;
472e5dd7070Spatrick
473*12c85518Srobert SourceLocation DefLoc = MI->getDefinitionLoc();
474*12c85518Srobert if (DirectivesOnly && !MI->isUsed()) {
475*12c85518Srobert SourceManager &SM = PP.getSourceManager();
476*12c85518Srobert if (SM.isWrittenInBuiltinFile(DefLoc) ||
477*12c85518Srobert SM.isWrittenInCommandLineFile(DefLoc))
478*12c85518Srobert return;
479*12c85518Srobert }
480*12c85518Srobert MoveToLine(DefLoc, /*RequireStartOfLine=*/true);
481e5dd7070Spatrick PrintMacroDefinition(*MacroNameTok.getIdentifierInfo(), *MI, PP, OS);
482e5dd7070Spatrick setEmittedDirectiveOnThisLine();
483e5dd7070Spatrick }
484e5dd7070Spatrick
MacroUndefined(const Token & MacroNameTok,const MacroDefinition & MD,const MacroDirective * Undef)485e5dd7070Spatrick void PrintPPOutputPPCallbacks::MacroUndefined(const Token &MacroNameTok,
486e5dd7070Spatrick const MacroDefinition &MD,
487e5dd7070Spatrick const MacroDirective *Undef) {
488*12c85518Srobert // Print out macro definitions in -dD mode and when we have -fdirectives-only
489*12c85518Srobert // for C++20 header units.
490*12c85518Srobert if (!DumpDefines && !DirectivesOnly)
491*12c85518Srobert return;
492e5dd7070Spatrick
493*12c85518Srobert MoveToLine(MacroNameTok.getLocation(), /*RequireStartOfLine=*/true);
494e5dd7070Spatrick OS << "#undef " << MacroNameTok.getIdentifierInfo()->getName();
495e5dd7070Spatrick setEmittedDirectiveOnThisLine();
496e5dd7070Spatrick }
497e5dd7070Spatrick
outputPrintable(raw_ostream & OS,StringRef Str)498e5dd7070Spatrick static void outputPrintable(raw_ostream &OS, StringRef Str) {
499e5dd7070Spatrick for (unsigned char Char : Str) {
500e5dd7070Spatrick if (isPrintable(Char) && Char != '\\' && Char != '"')
501e5dd7070Spatrick OS << (char)Char;
502e5dd7070Spatrick else // Output anything hard as an octal escape.
503e5dd7070Spatrick OS << '\\'
504e5dd7070Spatrick << (char)('0' + ((Char >> 6) & 7))
505e5dd7070Spatrick << (char)('0' + ((Char >> 3) & 7))
506e5dd7070Spatrick << (char)('0' + ((Char >> 0) & 7));
507e5dd7070Spatrick }
508e5dd7070Spatrick }
509e5dd7070Spatrick
PragmaMessage(SourceLocation Loc,StringRef Namespace,PragmaMessageKind Kind,StringRef Str)510e5dd7070Spatrick void PrintPPOutputPPCallbacks::PragmaMessage(SourceLocation Loc,
511e5dd7070Spatrick StringRef Namespace,
512e5dd7070Spatrick PragmaMessageKind Kind,
513e5dd7070Spatrick StringRef Str) {
514*12c85518Srobert MoveToLine(Loc, /*RequireStartOfLine=*/true);
515e5dd7070Spatrick OS << "#pragma ";
516e5dd7070Spatrick if (!Namespace.empty())
517e5dd7070Spatrick OS << Namespace << ' ';
518e5dd7070Spatrick switch (Kind) {
519e5dd7070Spatrick case PMK_Message:
520e5dd7070Spatrick OS << "message(\"";
521e5dd7070Spatrick break;
522e5dd7070Spatrick case PMK_Warning:
523e5dd7070Spatrick OS << "warning \"";
524e5dd7070Spatrick break;
525e5dd7070Spatrick case PMK_Error:
526e5dd7070Spatrick OS << "error \"";
527e5dd7070Spatrick break;
528e5dd7070Spatrick }
529e5dd7070Spatrick
530e5dd7070Spatrick outputPrintable(OS, Str);
531e5dd7070Spatrick OS << '"';
532e5dd7070Spatrick if (Kind == PMK_Message)
533e5dd7070Spatrick OS << ')';
534e5dd7070Spatrick setEmittedDirectiveOnThisLine();
535e5dd7070Spatrick }
536e5dd7070Spatrick
PragmaDebug(SourceLocation Loc,StringRef DebugType)537e5dd7070Spatrick void PrintPPOutputPPCallbacks::PragmaDebug(SourceLocation Loc,
538e5dd7070Spatrick StringRef DebugType) {
539*12c85518Srobert MoveToLine(Loc, /*RequireStartOfLine=*/true);
540e5dd7070Spatrick
541e5dd7070Spatrick OS << "#pragma clang __debug ";
542e5dd7070Spatrick OS << DebugType;
543e5dd7070Spatrick
544e5dd7070Spatrick setEmittedDirectiveOnThisLine();
545e5dd7070Spatrick }
546e5dd7070Spatrick
547e5dd7070Spatrick void PrintPPOutputPPCallbacks::
PragmaDiagnosticPush(SourceLocation Loc,StringRef Namespace)548e5dd7070Spatrick PragmaDiagnosticPush(SourceLocation Loc, StringRef Namespace) {
549*12c85518Srobert MoveToLine(Loc, /*RequireStartOfLine=*/true);
550e5dd7070Spatrick OS << "#pragma " << Namespace << " diagnostic push";
551e5dd7070Spatrick setEmittedDirectiveOnThisLine();
552e5dd7070Spatrick }
553e5dd7070Spatrick
554e5dd7070Spatrick void PrintPPOutputPPCallbacks::
PragmaDiagnosticPop(SourceLocation Loc,StringRef Namespace)555e5dd7070Spatrick PragmaDiagnosticPop(SourceLocation Loc, StringRef Namespace) {
556*12c85518Srobert MoveToLine(Loc, /*RequireStartOfLine=*/true);
557e5dd7070Spatrick OS << "#pragma " << Namespace << " diagnostic pop";
558e5dd7070Spatrick setEmittedDirectiveOnThisLine();
559e5dd7070Spatrick }
560e5dd7070Spatrick
PragmaDiagnostic(SourceLocation Loc,StringRef Namespace,diag::Severity Map,StringRef Str)561e5dd7070Spatrick void PrintPPOutputPPCallbacks::PragmaDiagnostic(SourceLocation Loc,
562e5dd7070Spatrick StringRef Namespace,
563e5dd7070Spatrick diag::Severity Map,
564e5dd7070Spatrick StringRef Str) {
565*12c85518Srobert MoveToLine(Loc, /*RequireStartOfLine=*/true);
566e5dd7070Spatrick OS << "#pragma " << Namespace << " diagnostic ";
567e5dd7070Spatrick switch (Map) {
568e5dd7070Spatrick case diag::Severity::Remark:
569e5dd7070Spatrick OS << "remark";
570e5dd7070Spatrick break;
571e5dd7070Spatrick case diag::Severity::Warning:
572e5dd7070Spatrick OS << "warning";
573e5dd7070Spatrick break;
574e5dd7070Spatrick case diag::Severity::Error:
575e5dd7070Spatrick OS << "error";
576e5dd7070Spatrick break;
577e5dd7070Spatrick case diag::Severity::Ignored:
578e5dd7070Spatrick OS << "ignored";
579e5dd7070Spatrick break;
580e5dd7070Spatrick case diag::Severity::Fatal:
581e5dd7070Spatrick OS << "fatal";
582e5dd7070Spatrick break;
583e5dd7070Spatrick }
584e5dd7070Spatrick OS << " \"" << Str << '"';
585e5dd7070Spatrick setEmittedDirectiveOnThisLine();
586e5dd7070Spatrick }
587e5dd7070Spatrick
PragmaWarning(SourceLocation Loc,PragmaWarningSpecifier WarningSpec,ArrayRef<int> Ids)588e5dd7070Spatrick void PrintPPOutputPPCallbacks::PragmaWarning(SourceLocation Loc,
589*12c85518Srobert PragmaWarningSpecifier WarningSpec,
590e5dd7070Spatrick ArrayRef<int> Ids) {
591*12c85518Srobert MoveToLine(Loc, /*RequireStartOfLine=*/true);
592*12c85518Srobert
593*12c85518Srobert OS << "#pragma warning(";
594*12c85518Srobert switch(WarningSpec) {
595*12c85518Srobert case PWS_Default: OS << "default"; break;
596*12c85518Srobert case PWS_Disable: OS << "disable"; break;
597*12c85518Srobert case PWS_Error: OS << "error"; break;
598*12c85518Srobert case PWS_Once: OS << "once"; break;
599*12c85518Srobert case PWS_Suppress: OS << "suppress"; break;
600*12c85518Srobert case PWS_Level1: OS << '1'; break;
601*12c85518Srobert case PWS_Level2: OS << '2'; break;
602*12c85518Srobert case PWS_Level3: OS << '3'; break;
603*12c85518Srobert case PWS_Level4: OS << '4'; break;
604*12c85518Srobert }
605*12c85518Srobert OS << ':';
606*12c85518Srobert
607e5dd7070Spatrick for (ArrayRef<int>::iterator I = Ids.begin(), E = Ids.end(); I != E; ++I)
608e5dd7070Spatrick OS << ' ' << *I;
609e5dd7070Spatrick OS << ')';
610e5dd7070Spatrick setEmittedDirectiveOnThisLine();
611e5dd7070Spatrick }
612e5dd7070Spatrick
PragmaWarningPush(SourceLocation Loc,int Level)613e5dd7070Spatrick void PrintPPOutputPPCallbacks::PragmaWarningPush(SourceLocation Loc,
614e5dd7070Spatrick int Level) {
615*12c85518Srobert MoveToLine(Loc, /*RequireStartOfLine=*/true);
616e5dd7070Spatrick OS << "#pragma warning(push";
617e5dd7070Spatrick if (Level >= 0)
618e5dd7070Spatrick OS << ", " << Level;
619e5dd7070Spatrick OS << ')';
620e5dd7070Spatrick setEmittedDirectiveOnThisLine();
621e5dd7070Spatrick }
622e5dd7070Spatrick
PragmaWarningPop(SourceLocation Loc)623e5dd7070Spatrick void PrintPPOutputPPCallbacks::PragmaWarningPop(SourceLocation Loc) {
624*12c85518Srobert MoveToLine(Loc, /*RequireStartOfLine=*/true);
625e5dd7070Spatrick OS << "#pragma warning(pop)";
626e5dd7070Spatrick setEmittedDirectiveOnThisLine();
627e5dd7070Spatrick }
628e5dd7070Spatrick
PragmaExecCharsetPush(SourceLocation Loc,StringRef Str)629e5dd7070Spatrick void PrintPPOutputPPCallbacks::PragmaExecCharsetPush(SourceLocation Loc,
630e5dd7070Spatrick StringRef Str) {
631*12c85518Srobert MoveToLine(Loc, /*RequireStartOfLine=*/true);
632e5dd7070Spatrick OS << "#pragma character_execution_set(push";
633e5dd7070Spatrick if (!Str.empty())
634e5dd7070Spatrick OS << ", " << Str;
635e5dd7070Spatrick OS << ')';
636e5dd7070Spatrick setEmittedDirectiveOnThisLine();
637e5dd7070Spatrick }
638e5dd7070Spatrick
PragmaExecCharsetPop(SourceLocation Loc)639e5dd7070Spatrick void PrintPPOutputPPCallbacks::PragmaExecCharsetPop(SourceLocation Loc) {
640*12c85518Srobert MoveToLine(Loc, /*RequireStartOfLine=*/true);
641e5dd7070Spatrick OS << "#pragma character_execution_set(pop)";
642e5dd7070Spatrick setEmittedDirectiveOnThisLine();
643e5dd7070Spatrick }
644e5dd7070Spatrick
645e5dd7070Spatrick void PrintPPOutputPPCallbacks::
PragmaAssumeNonNullBegin(SourceLocation Loc)646e5dd7070Spatrick PragmaAssumeNonNullBegin(SourceLocation Loc) {
647*12c85518Srobert MoveToLine(Loc, /*RequireStartOfLine=*/true);
648e5dd7070Spatrick OS << "#pragma clang assume_nonnull begin";
649e5dd7070Spatrick setEmittedDirectiveOnThisLine();
650e5dd7070Spatrick }
651e5dd7070Spatrick
652e5dd7070Spatrick void PrintPPOutputPPCallbacks::
PragmaAssumeNonNullEnd(SourceLocation Loc)653e5dd7070Spatrick PragmaAssumeNonNullEnd(SourceLocation Loc) {
654*12c85518Srobert MoveToLine(Loc, /*RequireStartOfLine=*/true);
655e5dd7070Spatrick OS << "#pragma clang assume_nonnull end";
656e5dd7070Spatrick setEmittedDirectiveOnThisLine();
657e5dd7070Spatrick }
658e5dd7070Spatrick
HandleWhitespaceBeforeTok(const Token & Tok,bool RequireSpace,bool RequireSameLine)659*12c85518Srobert void PrintPPOutputPPCallbacks::HandleWhitespaceBeforeTok(const Token &Tok,
660*12c85518Srobert bool RequireSpace,
661*12c85518Srobert bool RequireSameLine) {
662*12c85518Srobert // These tokens are not expanded to anything and don't need whitespace before
663*12c85518Srobert // them.
664*12c85518Srobert if (Tok.is(tok::eof) ||
665*12c85518Srobert (Tok.isAnnotation() && !Tok.is(tok::annot_header_unit) &&
666*12c85518Srobert !Tok.is(tok::annot_module_begin) && !Tok.is(tok::annot_module_end)))
667*12c85518Srobert return;
668e5dd7070Spatrick
669*12c85518Srobert // EmittedDirectiveOnThisLine takes priority over RequireSameLine.
670*12c85518Srobert if ((!RequireSameLine || EmittedDirectiveOnThisLine) &&
671*12c85518Srobert MoveToLine(Tok, /*RequireStartOfLine=*/EmittedDirectiveOnThisLine)) {
672*12c85518Srobert if (MinimizeWhitespace) {
673*12c85518Srobert // Avoid interpreting hash as a directive under -fpreprocessed.
674*12c85518Srobert if (Tok.is(tok::hash))
675*12c85518Srobert OS << ' ';
676*12c85518Srobert } else {
677e5dd7070Spatrick // Print out space characters so that the first token on a line is
678e5dd7070Spatrick // indented for easy reading.
679e5dd7070Spatrick unsigned ColNo = SM.getExpansionColumnNumber(Tok.getLocation());
680e5dd7070Spatrick
681*12c85518Srobert // The first token on a line can have a column number of 1, yet still
682*12c85518Srobert // expect leading white space, if a macro expansion in column 1 starts
683*12c85518Srobert // with an empty macro argument, or an empty nested macro expansion. In
684*12c85518Srobert // this case, move the token to column 2.
685e5dd7070Spatrick if (ColNo == 1 && Tok.hasLeadingSpace())
686e5dd7070Spatrick ColNo = 2;
687e5dd7070Spatrick
688e5dd7070Spatrick // This hack prevents stuff like:
689e5dd7070Spatrick // #define HASH #
690e5dd7070Spatrick // HASH define foo bar
691e5dd7070Spatrick // From having the # character end up at column 1, which makes it so it
692e5dd7070Spatrick // is not handled as a #define next time through the preprocessor if in
693e5dd7070Spatrick // -fpreprocessed mode.
694e5dd7070Spatrick if (ColNo <= 1 && Tok.is(tok::hash))
695e5dd7070Spatrick OS << ' ';
696e5dd7070Spatrick
697e5dd7070Spatrick // Otherwise, indent the appropriate number of spaces.
698e5dd7070Spatrick for (; ColNo > 1; --ColNo)
699e5dd7070Spatrick OS << ' ';
700*12c85518Srobert }
701*12c85518Srobert } else {
702*12c85518Srobert // Insert whitespace between the previous and next token if either
703*12c85518Srobert // - The caller requires it
704*12c85518Srobert // - The input had whitespace between them and we are not in
705*12c85518Srobert // whitespace-minimization mode
706*12c85518Srobert // - The whitespace is necessary to keep the tokens apart and there is not
707*12c85518Srobert // already a newline between them
708*12c85518Srobert if (RequireSpace || (!MinimizeWhitespace && Tok.hasLeadingSpace()) ||
709*12c85518Srobert ((EmittedTokensOnThisLine || EmittedDirectiveOnThisLine) &&
710*12c85518Srobert AvoidConcat(PrevPrevTok, PrevTok, Tok)))
711*12c85518Srobert OS << ' ';
712*12c85518Srobert }
713e5dd7070Spatrick
714*12c85518Srobert PrevPrevTok = PrevTok;
715*12c85518Srobert PrevTok = Tok;
716e5dd7070Spatrick }
717e5dd7070Spatrick
HandleNewlinesInToken(const char * TokStr,unsigned Len)718e5dd7070Spatrick void PrintPPOutputPPCallbacks::HandleNewlinesInToken(const char *TokStr,
719e5dd7070Spatrick unsigned Len) {
720e5dd7070Spatrick unsigned NumNewlines = 0;
721e5dd7070Spatrick for (; Len; --Len, ++TokStr) {
722e5dd7070Spatrick if (*TokStr != '\n' &&
723e5dd7070Spatrick *TokStr != '\r')
724e5dd7070Spatrick continue;
725e5dd7070Spatrick
726e5dd7070Spatrick ++NumNewlines;
727e5dd7070Spatrick
728e5dd7070Spatrick // If we have \n\r or \r\n, skip both and count as one line.
729e5dd7070Spatrick if (Len != 1 &&
730e5dd7070Spatrick (TokStr[1] == '\n' || TokStr[1] == '\r') &&
731e5dd7070Spatrick TokStr[0] != TokStr[1]) {
732e5dd7070Spatrick ++TokStr;
733e5dd7070Spatrick --Len;
734e5dd7070Spatrick }
735e5dd7070Spatrick }
736e5dd7070Spatrick
737e5dd7070Spatrick if (NumNewlines == 0) return;
738e5dd7070Spatrick
739e5dd7070Spatrick CurLine += NumNewlines;
740e5dd7070Spatrick }
741e5dd7070Spatrick
742e5dd7070Spatrick
743e5dd7070Spatrick namespace {
744e5dd7070Spatrick struct UnknownPragmaHandler : public PragmaHandler {
745e5dd7070Spatrick const char *Prefix;
746e5dd7070Spatrick PrintPPOutputPPCallbacks *Callbacks;
747e5dd7070Spatrick
748e5dd7070Spatrick // Set to true if tokens should be expanded
749e5dd7070Spatrick bool ShouldExpandTokens;
750e5dd7070Spatrick
UnknownPragmaHandler__anon5785420d0211::UnknownPragmaHandler751e5dd7070Spatrick UnknownPragmaHandler(const char *prefix, PrintPPOutputPPCallbacks *callbacks,
752e5dd7070Spatrick bool RequireTokenExpansion)
753e5dd7070Spatrick : Prefix(prefix), Callbacks(callbacks),
754e5dd7070Spatrick ShouldExpandTokens(RequireTokenExpansion) {}
HandlePragma__anon5785420d0211::UnknownPragmaHandler755e5dd7070Spatrick void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
756e5dd7070Spatrick Token &PragmaTok) override {
757e5dd7070Spatrick // Figure out what line we went to and insert the appropriate number of
758e5dd7070Spatrick // newline characters.
759*12c85518Srobert Callbacks->MoveToLine(PragmaTok.getLocation(), /*RequireStartOfLine=*/true);
760e5dd7070Spatrick Callbacks->OS.write(Prefix, strlen(Prefix));
761*12c85518Srobert Callbacks->setEmittedTokensOnThisLine();
762e5dd7070Spatrick
763e5dd7070Spatrick if (ShouldExpandTokens) {
764e5dd7070Spatrick // The first token does not have expanded macros. Expand them, if
765e5dd7070Spatrick // required.
766e5dd7070Spatrick auto Toks = std::make_unique<Token[]>(1);
767e5dd7070Spatrick Toks[0] = PragmaTok;
768e5dd7070Spatrick PP.EnterTokenStream(std::move(Toks), /*NumToks=*/1,
769e5dd7070Spatrick /*DisableMacroExpansion=*/false,
770e5dd7070Spatrick /*IsReinject=*/false);
771e5dd7070Spatrick PP.Lex(PragmaTok);
772e5dd7070Spatrick }
773e5dd7070Spatrick
774e5dd7070Spatrick // Read and print all of the pragma tokens.
775*12c85518Srobert bool IsFirst = true;
776e5dd7070Spatrick while (PragmaTok.isNot(tok::eod)) {
777*12c85518Srobert Callbacks->HandleWhitespaceBeforeTok(PragmaTok, /*RequireSpace=*/IsFirst,
778*12c85518Srobert /*RequireSameLine=*/true);
779*12c85518Srobert IsFirst = false;
780e5dd7070Spatrick std::string TokSpell = PP.getSpelling(PragmaTok);
781e5dd7070Spatrick Callbacks->OS.write(&TokSpell[0], TokSpell.size());
782*12c85518Srobert Callbacks->setEmittedTokensOnThisLine();
783e5dd7070Spatrick
784e5dd7070Spatrick if (ShouldExpandTokens)
785e5dd7070Spatrick PP.Lex(PragmaTok);
786e5dd7070Spatrick else
787e5dd7070Spatrick PP.LexUnexpandedToken(PragmaTok);
788e5dd7070Spatrick }
789e5dd7070Spatrick Callbacks->setEmittedDirectiveOnThisLine();
790e5dd7070Spatrick }
791e5dd7070Spatrick };
792e5dd7070Spatrick } // end anonymous namespace
793e5dd7070Spatrick
794e5dd7070Spatrick
PrintPreprocessedTokens(Preprocessor & PP,Token & Tok,PrintPPOutputPPCallbacks * Callbacks,raw_ostream & OS)795e5dd7070Spatrick static void PrintPreprocessedTokens(Preprocessor &PP, Token &Tok,
796e5dd7070Spatrick PrintPPOutputPPCallbacks *Callbacks,
797e5dd7070Spatrick raw_ostream &OS) {
798e5dd7070Spatrick bool DropComments = PP.getLangOpts().TraditionalCPP &&
799e5dd7070Spatrick !PP.getCommentRetentionState();
800e5dd7070Spatrick
801*12c85518Srobert bool IsStartOfLine = false;
802e5dd7070Spatrick char Buffer[256];
803*12c85518Srobert while (true) {
804*12c85518Srobert // Two lines joined with line continuation ('\' as last character on the
805*12c85518Srobert // line) must be emitted as one line even though Tok.getLine() returns two
806*12c85518Srobert // different values. In this situation Tok.isAtStartOfLine() is false even
807*12c85518Srobert // though it may be the first token on the lexical line. When
808*12c85518Srobert // dropping/skipping a token that is at the start of a line, propagate the
809*12c85518Srobert // start-of-line-ness to the next token to not append it to the previous
810*12c85518Srobert // line.
811*12c85518Srobert IsStartOfLine = IsStartOfLine || Tok.isAtStartOfLine();
812e5dd7070Spatrick
813*12c85518Srobert Callbacks->HandleWhitespaceBeforeTok(Tok, /*RequireSpace=*/false,
814*12c85518Srobert /*RequireSameLine=*/!IsStartOfLine);
815e5dd7070Spatrick
816e5dd7070Spatrick if (DropComments && Tok.is(tok::comment)) {
817e5dd7070Spatrick // Skip comments. Normally the preprocessor does not generate
818e5dd7070Spatrick // tok::comment nodes at all when not keeping comments, but under
819e5dd7070Spatrick // -traditional-cpp the lexer keeps /all/ whitespace, including comments.
820*12c85518Srobert PP.Lex(Tok);
821*12c85518Srobert continue;
822e5dd7070Spatrick } else if (Tok.is(tok::eod)) {
823e5dd7070Spatrick // Don't print end of directive tokens, since they are typically newlines
824e5dd7070Spatrick // that mess up our line tracking. These come from unknown pre-processor
825e5dd7070Spatrick // directives or hash-prefixed comments in standalone assembly files.
826e5dd7070Spatrick PP.Lex(Tok);
827*12c85518Srobert // FIXME: The token on the next line after #include should have
828*12c85518Srobert // Tok.isAtStartOfLine() set.
829*12c85518Srobert IsStartOfLine = true;
830e5dd7070Spatrick continue;
831e5dd7070Spatrick } else if (Tok.is(tok::annot_module_include)) {
832e5dd7070Spatrick // PrintPPOutputPPCallbacks::InclusionDirective handles producing
833e5dd7070Spatrick // appropriate output here. Ignore this token entirely.
834e5dd7070Spatrick PP.Lex(Tok);
835*12c85518Srobert IsStartOfLine = true;
836e5dd7070Spatrick continue;
837e5dd7070Spatrick } else if (Tok.is(tok::annot_module_begin)) {
838e5dd7070Spatrick // FIXME: We retrieve this token after the FileChanged callback, and
839e5dd7070Spatrick // retrieve the module_end token before the FileChanged callback, so
840e5dd7070Spatrick // we render this within the file and render the module end outside the
841e5dd7070Spatrick // file, but this is backwards from the token locations: the module_begin
842e5dd7070Spatrick // token is at the include location (outside the file) and the module_end
843e5dd7070Spatrick // token is at the EOF location (within the file).
844e5dd7070Spatrick Callbacks->BeginModule(
845e5dd7070Spatrick reinterpret_cast<Module *>(Tok.getAnnotationValue()));
846e5dd7070Spatrick PP.Lex(Tok);
847*12c85518Srobert IsStartOfLine = true;
848e5dd7070Spatrick continue;
849e5dd7070Spatrick } else if (Tok.is(tok::annot_module_end)) {
850e5dd7070Spatrick Callbacks->EndModule(
851e5dd7070Spatrick reinterpret_cast<Module *>(Tok.getAnnotationValue()));
852e5dd7070Spatrick PP.Lex(Tok);
853*12c85518Srobert IsStartOfLine = true;
854e5dd7070Spatrick continue;
855e5dd7070Spatrick } else if (Tok.is(tok::annot_header_unit)) {
856e5dd7070Spatrick // This is a header-name that has been (effectively) converted into a
857e5dd7070Spatrick // module-name.
858e5dd7070Spatrick // FIXME: The module name could contain non-identifier module name
859e5dd7070Spatrick // components. We don't have a good way to round-trip those.
860e5dd7070Spatrick Module *M = reinterpret_cast<Module *>(Tok.getAnnotationValue());
861e5dd7070Spatrick std::string Name = M->getFullModuleName();
862e5dd7070Spatrick OS.write(Name.data(), Name.size());
863e5dd7070Spatrick Callbacks->HandleNewlinesInToken(Name.data(), Name.size());
864e5dd7070Spatrick } else if (Tok.isAnnotation()) {
865e5dd7070Spatrick // Ignore annotation tokens created by pragmas - the pragmas themselves
866e5dd7070Spatrick // will be reproduced in the preprocessed output.
867e5dd7070Spatrick PP.Lex(Tok);
868e5dd7070Spatrick continue;
869e5dd7070Spatrick } else if (IdentifierInfo *II = Tok.getIdentifierInfo()) {
870e5dd7070Spatrick OS << II->getName();
871e5dd7070Spatrick } else if (Tok.isLiteral() && !Tok.needsCleaning() &&
872e5dd7070Spatrick Tok.getLiteralData()) {
873e5dd7070Spatrick OS.write(Tok.getLiteralData(), Tok.getLength());
874*12c85518Srobert } else if (Tok.getLength() < std::size(Buffer)) {
875e5dd7070Spatrick const char *TokPtr = Buffer;
876e5dd7070Spatrick unsigned Len = PP.getSpelling(Tok, TokPtr);
877e5dd7070Spatrick OS.write(TokPtr, Len);
878e5dd7070Spatrick
879e5dd7070Spatrick // Tokens that can contain embedded newlines need to adjust our current
880e5dd7070Spatrick // line number.
881*12c85518Srobert // FIXME: The token may end with a newline in which case
882*12c85518Srobert // setEmittedDirectiveOnThisLine/setEmittedTokensOnThisLine afterwards is
883*12c85518Srobert // wrong.
884e5dd7070Spatrick if (Tok.getKind() == tok::comment || Tok.getKind() == tok::unknown)
885e5dd7070Spatrick Callbacks->HandleNewlinesInToken(TokPtr, Len);
886*12c85518Srobert if (Tok.is(tok::comment) && Len >= 2 && TokPtr[0] == '/' &&
887*12c85518Srobert TokPtr[1] == '/') {
888*12c85518Srobert // It's a line comment;
889*12c85518Srobert // Ensure that we don't concatenate anything behind it.
890*12c85518Srobert Callbacks->setEmittedDirectiveOnThisLine();
891*12c85518Srobert }
892e5dd7070Spatrick } else {
893e5dd7070Spatrick std::string S = PP.getSpelling(Tok);
894e5dd7070Spatrick OS.write(S.data(), S.size());
895e5dd7070Spatrick
896e5dd7070Spatrick // Tokens that can contain embedded newlines need to adjust our current
897e5dd7070Spatrick // line number.
898e5dd7070Spatrick if (Tok.getKind() == tok::comment || Tok.getKind() == tok::unknown)
899e5dd7070Spatrick Callbacks->HandleNewlinesInToken(S.data(), S.size());
900*12c85518Srobert if (Tok.is(tok::comment) && S.size() >= 2 && S[0] == '/' && S[1] == '/') {
901*12c85518Srobert // It's a line comment;
902*12c85518Srobert // Ensure that we don't concatenate anything behind it.
903*12c85518Srobert Callbacks->setEmittedDirectiveOnThisLine();
904*12c85518Srobert }
905e5dd7070Spatrick }
906e5dd7070Spatrick Callbacks->setEmittedTokensOnThisLine();
907*12c85518Srobert IsStartOfLine = false;
908e5dd7070Spatrick
909e5dd7070Spatrick if (Tok.is(tok::eof)) break;
910e5dd7070Spatrick
911e5dd7070Spatrick PP.Lex(Tok);
912e5dd7070Spatrick }
913e5dd7070Spatrick }
914e5dd7070Spatrick
915e5dd7070Spatrick typedef std::pair<const IdentifierInfo *, MacroInfo *> id_macro_pair;
MacroIDCompare(const id_macro_pair * LHS,const id_macro_pair * RHS)916e5dd7070Spatrick static int MacroIDCompare(const id_macro_pair *LHS, const id_macro_pair *RHS) {
917e5dd7070Spatrick return LHS->first->getName().compare(RHS->first->getName());
918e5dd7070Spatrick }
919e5dd7070Spatrick
DoPrintMacros(Preprocessor & PP,raw_ostream * OS)920e5dd7070Spatrick static void DoPrintMacros(Preprocessor &PP, raw_ostream *OS) {
921e5dd7070Spatrick // Ignore unknown pragmas.
922e5dd7070Spatrick PP.IgnorePragmas();
923e5dd7070Spatrick
924e5dd7070Spatrick // -dM mode just scans and ignores all tokens in the files, then dumps out
925e5dd7070Spatrick // the macro table at the end.
926e5dd7070Spatrick PP.EnterMainSourceFile();
927e5dd7070Spatrick
928e5dd7070Spatrick Token Tok;
929e5dd7070Spatrick do PP.Lex(Tok);
930e5dd7070Spatrick while (Tok.isNot(tok::eof));
931e5dd7070Spatrick
932e5dd7070Spatrick SmallVector<id_macro_pair, 128> MacrosByID;
933e5dd7070Spatrick for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end();
934e5dd7070Spatrick I != E; ++I) {
935e5dd7070Spatrick auto *MD = I->second.getLatest();
936e5dd7070Spatrick if (MD && MD->isDefined())
937e5dd7070Spatrick MacrosByID.push_back(id_macro_pair(I->first, MD->getMacroInfo()));
938e5dd7070Spatrick }
939e5dd7070Spatrick llvm::array_pod_sort(MacrosByID.begin(), MacrosByID.end(), MacroIDCompare);
940e5dd7070Spatrick
941e5dd7070Spatrick for (unsigned i = 0, e = MacrosByID.size(); i != e; ++i) {
942e5dd7070Spatrick MacroInfo &MI = *MacrosByID[i].second;
943e5dd7070Spatrick // Ignore computed macros like __LINE__ and friends.
944e5dd7070Spatrick if (MI.isBuiltinMacro()) continue;
945e5dd7070Spatrick
946e5dd7070Spatrick PrintMacroDefinition(*MacrosByID[i].first, MI, PP, *OS);
947e5dd7070Spatrick *OS << '\n';
948e5dd7070Spatrick }
949e5dd7070Spatrick }
950e5dd7070Spatrick
951e5dd7070Spatrick /// DoPrintPreprocessedInput - This implements -E mode.
952e5dd7070Spatrick ///
DoPrintPreprocessedInput(Preprocessor & PP,raw_ostream * OS,const PreprocessorOutputOptions & Opts)953e5dd7070Spatrick void clang::DoPrintPreprocessedInput(Preprocessor &PP, raw_ostream *OS,
954e5dd7070Spatrick const PreprocessorOutputOptions &Opts) {
955e5dd7070Spatrick // Show macros with no output is handled specially.
956e5dd7070Spatrick if (!Opts.ShowCPP) {
957e5dd7070Spatrick assert(Opts.ShowMacros && "Not yet implemented!");
958e5dd7070Spatrick DoPrintMacros(PP, OS);
959e5dd7070Spatrick return;
960e5dd7070Spatrick }
961e5dd7070Spatrick
962e5dd7070Spatrick // Inform the preprocessor whether we want it to retain comments or not, due
963e5dd7070Spatrick // to -C or -CC.
964e5dd7070Spatrick PP.SetCommentRetentionState(Opts.ShowComments, Opts.ShowMacroComments);
965e5dd7070Spatrick
966e5dd7070Spatrick PrintPPOutputPPCallbacks *Callbacks = new PrintPPOutputPPCallbacks(
967e5dd7070Spatrick PP, *OS, !Opts.ShowLineMarkers, Opts.ShowMacros,
968*12c85518Srobert Opts.ShowIncludeDirectives, Opts.UseLineDirectives,
969*12c85518Srobert Opts.MinimizeWhitespace, Opts.DirectivesOnly);
970e5dd7070Spatrick
971e5dd7070Spatrick // Expand macros in pragmas with -fms-extensions. The assumption is that
972e5dd7070Spatrick // the majority of pragmas in such a file will be Microsoft pragmas.
973e5dd7070Spatrick // Remember the handlers we will add so that we can remove them later.
974e5dd7070Spatrick std::unique_ptr<UnknownPragmaHandler> MicrosoftExtHandler(
975e5dd7070Spatrick new UnknownPragmaHandler(
976e5dd7070Spatrick "#pragma", Callbacks,
977e5dd7070Spatrick /*RequireTokenExpansion=*/PP.getLangOpts().MicrosoftExt));
978e5dd7070Spatrick
979e5dd7070Spatrick std::unique_ptr<UnknownPragmaHandler> GCCHandler(new UnknownPragmaHandler(
980e5dd7070Spatrick "#pragma GCC", Callbacks,
981e5dd7070Spatrick /*RequireTokenExpansion=*/PP.getLangOpts().MicrosoftExt));
982e5dd7070Spatrick
983e5dd7070Spatrick std::unique_ptr<UnknownPragmaHandler> ClangHandler(new UnknownPragmaHandler(
984e5dd7070Spatrick "#pragma clang", Callbacks,
985e5dd7070Spatrick /*RequireTokenExpansion=*/PP.getLangOpts().MicrosoftExt));
986e5dd7070Spatrick
987e5dd7070Spatrick PP.AddPragmaHandler(MicrosoftExtHandler.get());
988e5dd7070Spatrick PP.AddPragmaHandler("GCC", GCCHandler.get());
989e5dd7070Spatrick PP.AddPragmaHandler("clang", ClangHandler.get());
990e5dd7070Spatrick
991e5dd7070Spatrick // The tokens after pragma omp need to be expanded.
992e5dd7070Spatrick //
993e5dd7070Spatrick // OpenMP [2.1, Directive format]
994e5dd7070Spatrick // Preprocessing tokens following the #pragma omp are subject to macro
995e5dd7070Spatrick // replacement.
996e5dd7070Spatrick std::unique_ptr<UnknownPragmaHandler> OpenMPHandler(
997e5dd7070Spatrick new UnknownPragmaHandler("#pragma omp", Callbacks,
998e5dd7070Spatrick /*RequireTokenExpansion=*/true));
999e5dd7070Spatrick PP.AddPragmaHandler("omp", OpenMPHandler.get());
1000e5dd7070Spatrick
1001e5dd7070Spatrick PP.addPPCallbacks(std::unique_ptr<PPCallbacks>(Callbacks));
1002e5dd7070Spatrick
1003e5dd7070Spatrick // After we have configured the preprocessor, enter the main file.
1004e5dd7070Spatrick PP.EnterMainSourceFile();
1005*12c85518Srobert if (Opts.DirectivesOnly)
1006*12c85518Srobert PP.SetMacroExpansionOnlyInDirectives();
1007e5dd7070Spatrick
1008e5dd7070Spatrick // Consume all of the tokens that come from the predefines buffer. Those
1009e5dd7070Spatrick // should not be emitted into the output and are guaranteed to be at the
1010e5dd7070Spatrick // start.
1011e5dd7070Spatrick const SourceManager &SourceMgr = PP.getSourceManager();
1012e5dd7070Spatrick Token Tok;
1013e5dd7070Spatrick do {
1014e5dd7070Spatrick PP.Lex(Tok);
1015e5dd7070Spatrick if (Tok.is(tok::eof) || !Tok.getLocation().isFileID())
1016e5dd7070Spatrick break;
1017e5dd7070Spatrick
1018e5dd7070Spatrick PresumedLoc PLoc = SourceMgr.getPresumedLoc(Tok.getLocation());
1019e5dd7070Spatrick if (PLoc.isInvalid())
1020e5dd7070Spatrick break;
1021e5dd7070Spatrick
1022e5dd7070Spatrick if (strcmp(PLoc.getFilename(), "<built-in>"))
1023e5dd7070Spatrick break;
1024e5dd7070Spatrick } while (true);
1025e5dd7070Spatrick
1026e5dd7070Spatrick // Read all the preprocessed tokens, printing them out to the stream.
1027e5dd7070Spatrick PrintPreprocessedTokens(PP, Tok, Callbacks, *OS);
1028e5dd7070Spatrick *OS << '\n';
1029e5dd7070Spatrick
1030e5dd7070Spatrick // Remove the handlers we just added to leave the preprocessor in a sane state
1031e5dd7070Spatrick // so that it can be reused (for example by a clang::Parser instance).
1032e5dd7070Spatrick PP.RemovePragmaHandler(MicrosoftExtHandler.get());
1033e5dd7070Spatrick PP.RemovePragmaHandler("GCC", GCCHandler.get());
1034e5dd7070Spatrick PP.RemovePragmaHandler("clang", ClangHandler.get());
1035e5dd7070Spatrick PP.RemovePragmaHandler("omp", OpenMPHandler.get());
1036e5dd7070Spatrick }
1037