1 //===--- PrintPreprocessedOutput.cpp - Implement the -E mode --------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This code simply runs the preprocessor on the input file and prints out the
11 // result. This is the traditional behavior of the -E option.
13 //===----------------------------------------------------------------------===//
15 #include "clang/Frontend/Utils.h"
16 #include "clang/Basic/Diagnostic.h"
17 #include "clang/Basic/SourceManager.h"
18 #include "clang/Frontend/PreprocessorOutputOptions.h"
19 #include "clang/Lex/MacroInfo.h"
20 #include "clang/Lex/PPCallbacks.h"
21 #include "clang/Lex/Pragma.h"
22 #include "clang/Lex/Preprocessor.h"
23 #include "clang/Lex/TokenConcatenation.h"
24 #include "llvm/ADT/SmallString.h"
25 #include "llvm/ADT/STLExtras.h"
26 #include "llvm/ADT/StringRef.h"
27 #include "llvm/Config/config.h"
28 #include "llvm/Support/raw_ostream.h"
30 using namespace clang
;
32 /// PrintMacroDefinition - Print a macro definition in a form that will be
33 /// properly accepted back as a definition.
34 static void PrintMacroDefinition(const IdentifierInfo
&II
, const MacroInfo
&MI
,
35 Preprocessor
&PP
, llvm::raw_ostream
&OS
) {
36 OS
<< "#define " << II
.getName();
38 if (MI
.isFunctionLike()) {
40 if (!MI
.arg_empty()) {
41 MacroInfo::arg_iterator AI
= MI
.arg_begin(), E
= MI
.arg_end();
42 for (; AI
+1 != E
; ++AI
) {
43 OS
<< (*AI
)->getName();
48 if ((*AI
)->getName() == "__VA_ARGS__")
51 OS
<< (*AI
)->getName();
54 if (MI
.isGNUVarargs())
55 OS
<< "..."; // #define foo(x...)
60 // GCC always emits a space, even if the macro body is empty. However, do not
61 // want to emit two spaces if the first token has a leading space.
62 if (MI
.tokens_empty() || !MI
.tokens_begin()->hasLeadingSpace())
65 llvm::SmallString
<128> SpellingBuffer
;
66 for (MacroInfo::tokens_iterator I
= MI
.tokens_begin(), E
= MI
.tokens_end();
68 if (I
->hasLeadingSpace())
71 OS
<< PP
.getSpelling(*I
, SpellingBuffer
);
75 //===----------------------------------------------------------------------===//
76 // Preprocessed token printer
77 //===----------------------------------------------------------------------===//
80 class PrintPPOutputPPCallbacks
: public PPCallbacks
{
83 TokenConcatenation ConcatInfo
;
85 llvm::raw_ostream
&OS
;
89 bool EmittedTokensOnThisLine
;
90 bool EmittedMacroOnThisLine
;
91 SrcMgr::CharacteristicKind FileType
;
92 llvm::SmallString
<512> CurFilename
;
94 bool DisableLineMarkers
;
96 bool UseLineDirective
;
98 PrintPPOutputPPCallbacks(Preprocessor
&pp
, llvm::raw_ostream
&os
,
99 bool lineMarkers
, bool defines
)
100 : PP(pp
), SM(PP
.getSourceManager()),
101 ConcatInfo(PP
), OS(os
), DisableLineMarkers(lineMarkers
),
102 DumpDefines(defines
) {
104 CurFilename
+= "<uninit>";
105 EmittedTokensOnThisLine
= false;
106 EmittedMacroOnThisLine
= false;
107 FileType
= SrcMgr::C_User
;
110 // If we're in microsoft mode, use normal #line instead of line markers.
111 UseLineDirective
= PP
.getLangOptions().Microsoft
;
114 void SetEmittedTokensOnThisLine() { EmittedTokensOnThisLine
= true; }
115 bool hasEmittedTokensOnThisLine() const { return EmittedTokensOnThisLine
; }
117 bool StartNewLineIfNeeded();
119 virtual void FileChanged(SourceLocation Loc
, FileChangeReason Reason
,
120 SrcMgr::CharacteristicKind FileType
);
121 virtual void Ident(SourceLocation Loc
, const std::string
&str
);
122 virtual void PragmaComment(SourceLocation Loc
, const IdentifierInfo
*Kind
,
123 const std::string
&Str
);
124 virtual void PragmaMessage(SourceLocation Loc
, llvm::StringRef Str
);
126 bool HandleFirstTokOnLine(Token
&Tok
);
127 bool MoveToLine(SourceLocation Loc
) {
128 PresumedLoc PLoc
= SM
.getPresumedLoc(Loc
);
129 if (PLoc
.isInvalid())
131 return MoveToLine(PLoc
.getLine());
133 bool MoveToLine(unsigned LineNo
);
135 bool AvoidConcat(const Token
&PrevPrevTok
, const Token
&PrevTok
,
137 return ConcatInfo
.AvoidConcat(PrevPrevTok
, PrevTok
, Tok
);
139 void WriteLineInfo(unsigned LineNo
, const char *Extra
=0, unsigned ExtraLen
=0);
140 bool LineMarkersAreDisabled() const { return DisableLineMarkers
; }
141 void HandleNewlinesInToken(const char *TokStr
, unsigned Len
);
143 /// MacroDefined - This hook is called whenever a macro definition is seen.
144 void MacroDefined(const Token
&MacroNameTok
, const MacroInfo
*MI
);
146 /// MacroUndefined - This hook is called whenever a macro #undef is seen.
147 void MacroUndefined(const Token
&MacroNameTok
, const MacroInfo
*MI
);
149 } // end anonymous namespace
151 void PrintPPOutputPPCallbacks::WriteLineInfo(unsigned LineNo
,
154 if (EmittedTokensOnThisLine
|| EmittedMacroOnThisLine
) {
156 EmittedTokensOnThisLine
= false;
157 EmittedMacroOnThisLine
= false;
160 // Emit #line directives or GNU line markers depending on what mode we're in.
161 if (UseLineDirective
) {
162 OS
<< "#line" << ' ' << LineNo
<< ' ' << '"';
163 OS
.write(CurFilename
.data(), CurFilename
.size());
166 OS
<< '#' << ' ' << LineNo
<< ' ' << '"';
167 OS
.write(CurFilename
.data(), CurFilename
.size());
171 OS
.write(Extra
, ExtraLen
);
173 if (FileType
== SrcMgr::C_System
)
175 else if (FileType
== SrcMgr::C_ExternCSystem
)
181 /// MoveToLine - Move the output to the source line specified by the location
182 /// object. We can do this by emitting some number of \n's, or be emitting a
183 /// #line directive. This returns false if already at the specified line, true
184 /// if some newlines were emitted.
185 bool PrintPPOutputPPCallbacks::MoveToLine(unsigned LineNo
) {
186 // If this line is "close enough" to the original line, just print newlines,
187 // otherwise print a #line directive.
188 if (LineNo
-CurLine
<= 8) {
189 if (LineNo
-CurLine
== 1)
191 else if (LineNo
== CurLine
)
192 return false; // Spelling line moved, but instantiation line didn't.
194 const char *NewLines
= "\n\n\n\n\n\n\n\n";
195 OS
.write(NewLines
, LineNo
-CurLine
);
197 } else if (!DisableLineMarkers
) {
198 // Emit a #line or line marker.
199 WriteLineInfo(LineNo
, 0, 0);
201 // Okay, we're in -P mode, which turns off line markers. However, we still
202 // need to emit a newline between tokens on different lines.
203 if (EmittedTokensOnThisLine
|| EmittedMacroOnThisLine
) {
205 EmittedTokensOnThisLine
= false;
206 EmittedMacroOnThisLine
= false;
214 bool PrintPPOutputPPCallbacks::StartNewLineIfNeeded() {
215 if (EmittedTokensOnThisLine
|| EmittedMacroOnThisLine
) {
217 EmittedTokensOnThisLine
= false;
218 EmittedMacroOnThisLine
= false;
226 /// FileChanged - Whenever the preprocessor enters or exits a #include file
227 /// it invokes this handler. Update our conception of the current source
229 void PrintPPOutputPPCallbacks::FileChanged(SourceLocation Loc
,
230 FileChangeReason Reason
,
231 SrcMgr::CharacteristicKind NewFileType
) {
232 // Unless we are exiting a #include, make sure to skip ahead to the line the
233 // #include directive was at.
234 SourceManager
&SourceMgr
= SM
;
236 PresumedLoc UserLoc
= SourceMgr
.getPresumedLoc(Loc
);
237 if (UserLoc
.isInvalid())
240 unsigned NewLine
= UserLoc
.getLine();
242 if (Reason
== PPCallbacks::EnterFile
) {
243 SourceLocation IncludeLoc
= UserLoc
.getIncludeLoc();
244 if (IncludeLoc
.isValid())
245 MoveToLine(IncludeLoc
);
246 } else if (Reason
== PPCallbacks::SystemHeaderPragma
) {
249 // TODO GCC emits the # directive for this directive on the line AFTER the
250 // directive and emits a bunch of spaces that aren't needed. Emulate this
257 CurFilename
+= UserLoc
.getFilename();
258 Lexer::Stringify(CurFilename
);
259 FileType
= NewFileType
;
261 if (DisableLineMarkers
) return;
264 WriteLineInfo(CurLine
);
269 case PPCallbacks::EnterFile
:
270 WriteLineInfo(CurLine
, " 1", 2);
272 case PPCallbacks::ExitFile
:
273 WriteLineInfo(CurLine
, " 2", 2);
275 case PPCallbacks::SystemHeaderPragma
:
276 case PPCallbacks::RenameFile
:
277 WriteLineInfo(CurLine
);
282 /// Ident - Handle #ident directives when read by the preprocessor.
284 void PrintPPOutputPPCallbacks::Ident(SourceLocation Loc
, const std::string
&S
) {
287 OS
.write("#ident ", strlen("#ident "));
288 OS
.write(&S
[0], S
.size());
289 EmittedTokensOnThisLine
= true;
292 /// MacroDefined - This hook is called whenever a macro definition is seen.
293 void PrintPPOutputPPCallbacks::MacroDefined(const Token
&MacroNameTok
,
294 const MacroInfo
*MI
) {
295 // Only print out macro definitions in -dD mode.
297 // Ignore __FILE__ etc.
298 MI
->isBuiltinMacro()) return;
300 MoveToLine(MI
->getDefinitionLoc());
301 PrintMacroDefinition(*MacroNameTok
.getIdentifierInfo(), *MI
, PP
, OS
);
302 EmittedMacroOnThisLine
= true;
305 void PrintPPOutputPPCallbacks::MacroUndefined(const Token
&MacroNameTok
,
306 const MacroInfo
*MI
) {
307 // Only print out macro definitions in -dD mode.
308 if (!DumpDefines
) return;
310 MoveToLine(MacroNameTok
.getLocation());
311 OS
<< "#undef " << MacroNameTok
.getIdentifierInfo()->getName();
312 EmittedMacroOnThisLine
= true;
315 void PrintPPOutputPPCallbacks::PragmaComment(SourceLocation Loc
,
316 const IdentifierInfo
*Kind
,
317 const std::string
&Str
) {
319 OS
<< "#pragma comment(" << Kind
->getName();
324 for (unsigned i
= 0, e
= Str
.size(); i
!= e
; ++i
) {
325 unsigned char Char
= Str
[i
];
326 if (isprint(Char
) && Char
!= '\\' && Char
!= '"')
328 else // Output anything hard as an octal escape.
330 << (char)('0'+ ((Char
>> 6) & 7))
331 << (char)('0'+ ((Char
>> 3) & 7))
332 << (char)('0'+ ((Char
>> 0) & 7));
338 EmittedTokensOnThisLine
= true;
341 void PrintPPOutputPPCallbacks::PragmaMessage(SourceLocation Loc
,
342 llvm::StringRef Str
) {
344 OS
<< "#pragma message(";
348 for (unsigned i
= 0, e
= Str
.size(); i
!= e
; ++i
) {
349 unsigned char Char
= Str
[i
];
350 if (isprint(Char
) && Char
!= '\\' && Char
!= '"')
352 else // Output anything hard as an octal escape.
354 << (char)('0'+ ((Char
>> 6) & 7))
355 << (char)('0'+ ((Char
>> 3) & 7))
356 << (char)('0'+ ((Char
>> 0) & 7));
361 EmittedTokensOnThisLine
= true;
365 /// HandleFirstTokOnLine - When emitting a preprocessed file in -E mode, this
366 /// is called for the first token on each new line. If this really is the start
367 /// of a new logical line, handle it and return true, otherwise return false.
368 /// This may not be the start of a logical line because the "start of line"
369 /// marker is set for spelling lines, not instantiation ones.
370 bool PrintPPOutputPPCallbacks::HandleFirstTokOnLine(Token
&Tok
) {
371 // Figure out what line we went to and insert the appropriate number of
372 // newline characters.
373 if (!MoveToLine(Tok
.getLocation()))
376 // Print out space characters so that the first token on a line is
377 // indented for easy reading.
378 unsigned ColNo
= SM
.getInstantiationColumnNumber(Tok
.getLocation());
380 // This hack prevents stuff like:
382 // HASH define foo bar
383 // From having the # character end up at column 1, which makes it so it
384 // is not handled as a #define next time through the preprocessor if in
385 // -fpreprocessed mode.
386 if (ColNo
<= 1 && Tok
.is(tok::hash
))
389 // Otherwise, indent the appropriate number of spaces.
390 for (; ColNo
> 1; --ColNo
)
396 void PrintPPOutputPPCallbacks::HandleNewlinesInToken(const char *TokStr
,
398 unsigned NumNewlines
= 0;
399 for (; Len
; --Len
, ++TokStr
) {
400 if (*TokStr
!= '\n' &&
406 // If we have \n\r or \r\n, skip both and count as one line.
408 (TokStr
[1] == '\n' || TokStr
[1] == '\r') &&
409 TokStr
[0] != TokStr
[1])
413 if (NumNewlines
== 0) return;
415 CurLine
+= NumNewlines
;
420 struct UnknownPragmaHandler
: public PragmaHandler
{
422 PrintPPOutputPPCallbacks
*Callbacks
;
424 UnknownPragmaHandler(const char *prefix
, PrintPPOutputPPCallbacks
*callbacks
)
425 : Prefix(prefix
), Callbacks(callbacks
) {}
426 virtual void HandlePragma(Preprocessor
&PP
, PragmaIntroducerKind Introducer
,
428 // Figure out what line we went to and insert the appropriate number of
429 // newline characters.
430 Callbacks
->StartNewLineIfNeeded();
431 Callbacks
->MoveToLine(PragmaTok
.getLocation());
432 Callbacks
->OS
.write(Prefix
, strlen(Prefix
));
433 Callbacks
->SetEmittedTokensOnThisLine();
434 // Read and print all of the pragma tokens.
435 while (PragmaTok
.isNot(tok::eom
)) {
436 if (PragmaTok
.hasLeadingSpace())
437 Callbacks
->OS
<< ' ';
438 std::string TokSpell
= PP
.getSpelling(PragmaTok
);
439 Callbacks
->OS
.write(&TokSpell
[0], TokSpell
.size());
440 PP
.LexUnexpandedToken(PragmaTok
);
442 Callbacks
->StartNewLineIfNeeded();
445 } // end anonymous namespace
448 static void PrintPreprocessedTokens(Preprocessor
&PP
, Token
&Tok
,
449 PrintPPOutputPPCallbacks
*Callbacks
,
450 llvm::raw_ostream
&OS
) {
452 Token PrevPrevTok
, PrevTok
;
453 PrevPrevTok
.startToken();
454 PrevTok
.startToken();
457 // If this token is at the start of a line, emit newlines if needed.
458 if (Tok
.isAtStartOfLine() && Callbacks
->HandleFirstTokOnLine(Tok
)) {
460 } else if (Tok
.hasLeadingSpace() ||
461 // If we haven't emitted a token on this line yet, PrevTok isn't
462 // useful to look at and no concatenation could happen anyway.
463 (Callbacks
->hasEmittedTokensOnThisLine() &&
464 // Don't print "-" next to "-", it would form "--".
465 Callbacks
->AvoidConcat(PrevPrevTok
, PrevTok
, Tok
))) {
469 if (IdentifierInfo
*II
= Tok
.getIdentifierInfo()) {
471 } else if (Tok
.isLiteral() && !Tok
.needsCleaning() &&
472 Tok
.getLiteralData()) {
473 OS
.write(Tok
.getLiteralData(), Tok
.getLength());
474 } else if (Tok
.getLength() < 256) {
475 const char *TokPtr
= Buffer
;
476 unsigned Len
= PP
.getSpelling(Tok
, TokPtr
);
477 OS
.write(TokPtr
, Len
);
479 // Tokens that can contain embedded newlines need to adjust our current
481 if (Tok
.getKind() == tok::comment
)
482 Callbacks
->HandleNewlinesInToken(TokPtr
, Len
);
484 std::string S
= PP
.getSpelling(Tok
);
485 OS
.write(&S
[0], S
.size());
487 // Tokens that can contain embedded newlines need to adjust our current
489 if (Tok
.getKind() == tok::comment
)
490 Callbacks
->HandleNewlinesInToken(&S
[0], S
.size());
492 Callbacks
->SetEmittedTokensOnThisLine();
494 if (Tok
.is(tok::eof
)) break;
496 PrevPrevTok
= PrevTok
;
502 typedef std::pair
<IdentifierInfo
*, MacroInfo
*> id_macro_pair
;
503 static int MacroIDCompare(const void* a
, const void* b
) {
504 const id_macro_pair
*LHS
= static_cast<const id_macro_pair
*>(a
);
505 const id_macro_pair
*RHS
= static_cast<const id_macro_pair
*>(b
);
506 return LHS
->first
->getName().compare(RHS
->first
->getName());
509 static void DoPrintMacros(Preprocessor
&PP
, llvm::raw_ostream
*OS
) {
510 // Ignore unknown pragmas.
511 PP
.AddPragmaHandler(new EmptyPragmaHandler());
513 // -dM mode just scans and ignores all tokens in the files, then dumps out
514 // the macro table at the end.
515 PP
.EnterMainSourceFile();
519 while (Tok
.isNot(tok::eof
));
521 llvm::SmallVector
<id_macro_pair
, 128>
522 MacrosByID(PP
.macro_begin(), PP
.macro_end());
523 llvm::array_pod_sort(MacrosByID
.begin(), MacrosByID
.end(), MacroIDCompare
);
525 for (unsigned i
= 0, e
= MacrosByID
.size(); i
!= e
; ++i
) {
526 MacroInfo
&MI
= *MacrosByID
[i
].second
;
527 // Ignore computed macros like __LINE__ and friends.
528 if (MI
.isBuiltinMacro()) continue;
530 PrintMacroDefinition(*MacrosByID
[i
].first
, MI
, PP
, *OS
);
535 /// DoPrintPreprocessedInput - This implements -E mode.
537 void clang::DoPrintPreprocessedInput(Preprocessor
&PP
, llvm::raw_ostream
*OS
,
538 const PreprocessorOutputOptions
&Opts
) {
539 // Show macros with no output is handled specially.
541 assert(Opts
.ShowMacros
&& "Not yet implemented!");
542 DoPrintMacros(PP
, OS
);
546 // Inform the preprocessor whether we want it to retain comments or not, due
548 PP
.SetCommentRetentionState(Opts
.ShowComments
, Opts
.ShowMacroComments
);
550 PrintPPOutputPPCallbacks
*Callbacks
=
551 new PrintPPOutputPPCallbacks(PP
, *OS
, !Opts
.ShowLineMarkers
,
553 PP
.AddPragmaHandler(new UnknownPragmaHandler("#pragma", Callbacks
));
554 PP
.AddPragmaHandler("GCC", new UnknownPragmaHandler("#pragma GCC",Callbacks
));
555 PP
.AddPragmaHandler("clang",
556 new UnknownPragmaHandler("#pragma clang", Callbacks
));
558 PP
.addPPCallbacks(Callbacks
);
560 // After we have configured the preprocessor, enter the main file.
561 PP
.EnterMainSourceFile();
563 // Consume all of the tokens that come from the predefines buffer. Those
564 // should not be emitted into the output and are guaranteed to be at the
566 const SourceManager
&SourceMgr
= PP
.getSourceManager();
570 if (Tok
.is(tok::eof
) || !Tok
.getLocation().isFileID())
573 PresumedLoc PLoc
= SourceMgr
.getPresumedLoc(Tok
.getLocation());
574 if (PLoc
.isInvalid())
577 if (strcmp(PLoc
.getFilename(), "<built-in>"))
581 // Read all the preprocessed tokens, printing them out to the stream.
582 PrintPreprocessedTokens(PP
, Tok
, Callbacks
, *OS
);