Allow resolving headers from a PCH even after headers+PCH were moved to another path.
[clang.git] / lib / Frontend / PrintPreprocessedOutput.cpp
blob922d743adf4e96d4729e561a5c161ca238e3e7fd
1 //===--- PrintPreprocessedOutput.cpp - Implement the -E mode --------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
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"
29 #include <cstdio>
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()) {
39 OS << '(';
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();
44 OS << ',';
47 // Last argument.
48 if ((*AI)->getName() == "__VA_ARGS__")
49 OS << "...";
50 else
51 OS << (*AI)->getName();
54 if (MI.isGNUVarargs())
55 OS << "..."; // #define foo(x...)
57 OS << ')';
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())
63 OS << ' ';
65 llvm::SmallString<128> SpellingBuffer;
66 for (MacroInfo::tokens_iterator I = MI.tokens_begin(), E = MI.tokens_end();
67 I != E; ++I) {
68 if (I->hasLeadingSpace())
69 OS << ' ';
71 OS << PP.getSpelling(*I, SpellingBuffer);
75 //===----------------------------------------------------------------------===//
76 // Preprocessed token printer
77 //===----------------------------------------------------------------------===//
79 namespace {
80 class PrintPPOutputPPCallbacks : public PPCallbacks {
81 Preprocessor &PP;
82 SourceManager &SM;
83 TokenConcatenation ConcatInfo;
84 public:
85 llvm::raw_ostream &OS;
86 private:
87 unsigned CurLine;
89 bool EmittedTokensOnThisLine;
90 bool EmittedMacroOnThisLine;
91 SrcMgr::CharacteristicKind FileType;
92 llvm::SmallString<512> CurFilename;
93 bool Initialized;
94 bool DisableLineMarkers;
95 bool DumpDefines;
96 bool UseLineDirective;
97 public:
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) {
103 CurLine = 0;
104 CurFilename += "<uninit>";
105 EmittedTokensOnThisLine = false;
106 EmittedMacroOnThisLine = false;
107 FileType = SrcMgr::C_User;
108 Initialized = false;
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())
130 return false;
131 return MoveToLine(PLoc.getLine());
133 bool MoveToLine(unsigned LineNo);
135 bool AvoidConcat(const Token &PrevPrevTok, const Token &PrevTok,
136 const Token &Tok) {
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,
152 const char *Extra,
153 unsigned ExtraLen) {
154 if (EmittedTokensOnThisLine || EmittedMacroOnThisLine) {
155 OS << '\n';
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());
164 OS << '"';
165 } else {
166 OS << '#' << ' ' << LineNo << ' ' << '"';
167 OS.write(CurFilename.data(), CurFilename.size());
168 OS << '"';
170 if (ExtraLen)
171 OS.write(Extra, ExtraLen);
173 if (FileType == SrcMgr::C_System)
174 OS.write(" 3", 2);
175 else if (FileType == SrcMgr::C_ExternCSystem)
176 OS.write(" 3 4", 4);
178 OS << '\n';
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)
190 OS << '\n';
191 else if (LineNo == CurLine)
192 return false; // Spelling line moved, but instantiation line didn't.
193 else {
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);
200 } else {
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) {
204 OS << '\n';
205 EmittedTokensOnThisLine = false;
206 EmittedMacroOnThisLine = false;
210 CurLine = LineNo;
211 return true;
214 bool PrintPPOutputPPCallbacks::StartNewLineIfNeeded() {
215 if (EmittedTokensOnThisLine || EmittedMacroOnThisLine) {
216 OS << '\n';
217 EmittedTokensOnThisLine = false;
218 EmittedMacroOnThisLine = false;
219 ++CurLine;
220 return true;
223 return false;
226 /// FileChanged - Whenever the preprocessor enters or exits a #include file
227 /// it invokes this handler. Update our conception of the current source
228 /// position.
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())
238 return;
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) {
247 MoveToLine(NewLine);
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
251 // strange behavior.
254 CurLine = NewLine;
256 CurFilename.clear();
257 CurFilename += UserLoc.getFilename();
258 Lexer::Stringify(CurFilename);
259 FileType = NewFileType;
261 if (DisableLineMarkers) return;
263 if (!Initialized) {
264 WriteLineInfo(CurLine);
265 Initialized = true;
268 switch (Reason) {
269 case PPCallbacks::EnterFile:
270 WriteLineInfo(CurLine, " 1", 2);
271 break;
272 case PPCallbacks::ExitFile:
273 WriteLineInfo(CurLine, " 2", 2);
274 break;
275 case PPCallbacks::SystemHeaderPragma:
276 case PPCallbacks::RenameFile:
277 WriteLineInfo(CurLine);
278 break;
282 /// Ident - Handle #ident directives when read by the preprocessor.
284 void PrintPPOutputPPCallbacks::Ident(SourceLocation Loc, const std::string &S) {
285 MoveToLine(Loc);
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.
296 if (!DumpDefines ||
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) {
318 MoveToLine(Loc);
319 OS << "#pragma comment(" << Kind->getName();
321 if (!Str.empty()) {
322 OS << ", \"";
324 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
325 unsigned char Char = Str[i];
326 if (isprint(Char) && Char != '\\' && Char != '"')
327 OS << (char)Char;
328 else // Output anything hard as an octal escape.
329 OS << '\\'
330 << (char)('0'+ ((Char >> 6) & 7))
331 << (char)('0'+ ((Char >> 3) & 7))
332 << (char)('0'+ ((Char >> 0) & 7));
334 OS << '"';
337 OS << ')';
338 EmittedTokensOnThisLine = true;
341 void PrintPPOutputPPCallbacks::PragmaMessage(SourceLocation Loc,
342 llvm::StringRef Str) {
343 MoveToLine(Loc);
344 OS << "#pragma message(";
346 OS << '"';
348 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
349 unsigned char Char = Str[i];
350 if (isprint(Char) && Char != '\\' && Char != '"')
351 OS << (char)Char;
352 else // Output anything hard as an octal escape.
353 OS << '\\'
354 << (char)('0'+ ((Char >> 6) & 7))
355 << (char)('0'+ ((Char >> 3) & 7))
356 << (char)('0'+ ((Char >> 0) & 7));
358 OS << '"';
360 OS << ')';
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()))
374 return false;
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:
381 // #define HASH #
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))
387 OS << ' ';
389 // Otherwise, indent the appropriate number of spaces.
390 for (; ColNo > 1; --ColNo)
391 OS << ' ';
393 return true;
396 void PrintPPOutputPPCallbacks::HandleNewlinesInToken(const char *TokStr,
397 unsigned Len) {
398 unsigned NumNewlines = 0;
399 for (; Len; --Len, ++TokStr) {
400 if (*TokStr != '\n' &&
401 *TokStr != '\r')
402 continue;
404 ++NumNewlines;
406 // If we have \n\r or \r\n, skip both and count as one line.
407 if (Len != 1 &&
408 (TokStr[1] == '\n' || TokStr[1] == '\r') &&
409 TokStr[0] != TokStr[1])
410 ++TokStr, --Len;
413 if (NumNewlines == 0) return;
415 CurLine += NumNewlines;
419 namespace {
420 struct UnknownPragmaHandler : public PragmaHandler {
421 const char *Prefix;
422 PrintPPOutputPPCallbacks *Callbacks;
424 UnknownPragmaHandler(const char *prefix, PrintPPOutputPPCallbacks *callbacks)
425 : Prefix(prefix), Callbacks(callbacks) {}
426 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
427 Token &PragmaTok) {
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) {
451 char Buffer[256];
452 Token PrevPrevTok, PrevTok;
453 PrevPrevTok.startToken();
454 PrevTok.startToken();
455 while (1) {
457 // If this token is at the start of a line, emit newlines if needed.
458 if (Tok.isAtStartOfLine() && Callbacks->HandleFirstTokOnLine(Tok)) {
459 // done.
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))) {
466 OS << ' ';
469 if (IdentifierInfo *II = Tok.getIdentifierInfo()) {
470 OS << II->getName();
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
480 // line number.
481 if (Tok.getKind() == tok::comment)
482 Callbacks->HandleNewlinesInToken(TokPtr, Len);
483 } else {
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
488 // line number.
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;
497 PrevTok = Tok;
498 PP.Lex(Tok);
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();
517 Token Tok;
518 do PP.Lex(Tok);
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);
531 *OS << '\n';
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.
540 if (!Opts.ShowCPP) {
541 assert(Opts.ShowMacros && "Not yet implemented!");
542 DoPrintMacros(PP, OS);
543 return;
546 // Inform the preprocessor whether we want it to retain comments or not, due
547 // to -C or -CC.
548 PP.SetCommentRetentionState(Opts.ShowComments, Opts.ShowMacroComments);
550 PrintPPOutputPPCallbacks *Callbacks =
551 new PrintPPOutputPPCallbacks(PP, *OS, !Opts.ShowLineMarkers,
552 Opts.ShowMacros);
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
565 // start.
566 const SourceManager &SourceMgr = PP.getSourceManager();
567 Token Tok;
568 do {
569 PP.Lex(Tok);
570 if (Tok.is(tok::eof) || !Tok.getLocation().isFileID())
571 break;
573 PresumedLoc PLoc = SourceMgr.getPresumedLoc(Tok.getLocation());
574 if (PLoc.isInvalid())
575 break;
577 if (strcmp(PLoc.getFilename(), "<built-in>"))
578 break;
579 } while (true);
581 // Read all the preprocessed tokens, printing them out to the stream.
582 PrintPreprocessedTokens(PP, Tok, Callbacks, *OS);
583 *OS << '\n';