blob: f54e52cf9b0513c31b03c500942c9e22457b36cf [file] [log] [blame]
Chris Lattner09e3cdf2006-07-04 19:04:05 +00001//===--- PrintPreprocessedOutput.cpp - Implement the -E mode --------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source 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.
12//
13//===----------------------------------------------------------------------===//
14
15#include "clang.h"
16#include "clang/Lex/Preprocessor.h"
17#include "clang/Lex/Pragma.h"
18#include "clang/Basic/SourceManager.h"
19#include "llvm/Support/CommandLine.h"
Chris Lattnerdeb37012006-07-04 19:24:06 +000020// NOTE: we use stdio because it is empirically much faster than iostreams.
21#include <cstdio>
Chris Lattner09e3cdf2006-07-04 19:04:05 +000022using namespace llvm;
23using namespace clang;
24
25static cl::opt<bool>
26DisableLineMarkers("P", cl::desc("Disable linemarker output in -E mode"));
27
28static unsigned EModeCurLine;
29static std::string EModeCurFilename;
30static Preprocessor *EModePP;
31static bool EmodeEmittedTokensOnThisLine;
32static DirectoryLookup::DirType EmodeFileType =DirectoryLookup::NormalHeaderDir;
33
Chris Lattner3338ba82006-07-04 21:19:39 +000034static void MoveToLine(SourceLocation Loc) {
35 if (DisableLineMarkers) return;
36
37 unsigned LineNo = EModePP->getSourceManager().getLineNumber(Loc);
38
Chris Lattner09e3cdf2006-07-04 19:04:05 +000039 // If this line is "close enough" to the original line, just print newlines,
40 // otherwise print a #line directive.
41 if (LineNo-EModeCurLine < 8) {
Chris Lattnerdeb37012006-07-04 19:24:06 +000042 unsigned CurLine = EModeCurLine;
Chris Lattnerdeb37012006-07-04 19:24:06 +000043 for (; CurLine != LineNo; ++CurLine)
44 putchar_unlocked('\n');
45 EModeCurLine = CurLine;
Chris Lattner09e3cdf2006-07-04 19:04:05 +000046 } else {
47 if (EmodeEmittedTokensOnThisLine) {
Chris Lattnerdeb37012006-07-04 19:24:06 +000048 putchar_unlocked('\n');
Chris Lattner09e3cdf2006-07-04 19:04:05 +000049 EmodeEmittedTokensOnThisLine = false;
50 }
51
52 EModeCurLine = LineNo;
53 if (DisableLineMarkers) return;
54
Chris Lattnerdeb37012006-07-04 19:24:06 +000055 printf("# %d %s", LineNo, EModeCurFilename.c_str());
Chris Lattner09e3cdf2006-07-04 19:04:05 +000056
57 if (EmodeFileType == DirectoryLookup::SystemHeaderDir)
Chris Lattnerdeb37012006-07-04 19:24:06 +000058 printf(" 3");
Chris Lattner09e3cdf2006-07-04 19:04:05 +000059 else if (EmodeFileType == DirectoryLookup::ExternCSystemHeaderDir)
Chris Lattnerdeb37012006-07-04 19:24:06 +000060 printf(" 3 4");
61 putchar_unlocked('\n');
Chris Lattner09e3cdf2006-07-04 19:04:05 +000062 }
63}
64
65/// HandleFileChange - Whenever the preprocessor enters or exits a #include file
66/// it invokes this handler. Update our conception of the current
67static void HandleFileChange(SourceLocation Loc,
68 Preprocessor::FileChangeReason Reason,
69 DirectoryLookup::DirType FileType) {
Chris Lattner03cbe1f2006-07-04 21:24:33 +000070 if (DisableLineMarkers) return;
Chris Lattner73b6a2f2006-07-04 19:40:52 +000071
Chris Lattner09e3cdf2006-07-04 19:04:05 +000072 // Unless we are exiting a #include, make sure to skip ahead to the line the
73 // #include directive was at.
Chris Lattnerff3f5f42006-07-04 21:25:59 +000074 SourceManager &SourceMgr = EModePP->getSourceManager();
Chris Lattner09e3cdf2006-07-04 19:04:05 +000075 if (Reason == Preprocessor::EnterFile) {
Chris Lattner3338ba82006-07-04 21:19:39 +000076 MoveToLine(SourceMgr.getIncludeLoc(Loc.getFileID()));
Chris Lattner09e3cdf2006-07-04 19:04:05 +000077 } else if (Reason == Preprocessor::SystemHeaderPragma) {
Chris Lattner3338ba82006-07-04 21:19:39 +000078 MoveToLine(Loc);
Chris Lattner09e3cdf2006-07-04 19:04:05 +000079
80 // TODO GCC emits the # directive for this directive on the line AFTER the
81 // directive and emits a bunch of spaces that aren't needed. Emulate this
82 // strange behavior.
83 }
84
85 EModeCurLine = SourceMgr.getLineNumber(Loc);
86 EModeCurFilename = Lexer::Stringify(SourceMgr.getSourceName(Loc));
87 EmodeFileType = FileType;
88
89 if (EmodeEmittedTokensOnThisLine) {
Chris Lattnerdeb37012006-07-04 19:24:06 +000090 putchar_unlocked('\n');
Chris Lattner09e3cdf2006-07-04 19:04:05 +000091 EmodeEmittedTokensOnThisLine = false;
92 }
93
94 if (DisableLineMarkers) return;
95
Chris Lattnerdeb37012006-07-04 19:24:06 +000096 printf("# %d %s", EModeCurLine, EModeCurFilename.c_str());
Chris Lattner09e3cdf2006-07-04 19:04:05 +000097 switch (Reason) {
Chris Lattner3338ba82006-07-04 21:19:39 +000098 case Preprocessor::EnterFile:
99 printf(" 1");
100 break;
101 case Preprocessor::ExitFile:
102 printf(" 2");
103 break;
104 case Preprocessor::SystemHeaderPragma: break;
105 case Preprocessor::RenameFile: break;
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000106 }
107
108 if (FileType == DirectoryLookup::SystemHeaderDir)
Chris Lattnerdeb37012006-07-04 19:24:06 +0000109 printf(" 3");
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000110 else if (FileType == DirectoryLookup::ExternCSystemHeaderDir)
Chris Lattnerdeb37012006-07-04 19:24:06 +0000111 printf(" 3 4");
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000112
Chris Lattnerdeb37012006-07-04 19:24:06 +0000113 putchar_unlocked('\n');
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000114}
115
116static void HandleIdent(SourceLocation Loc, const std::string &Val) {
Chris Lattner3338ba82006-07-04 21:19:39 +0000117 MoveToLine(Loc);
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000118
Chris Lattnerdeb37012006-07-04 19:24:06 +0000119 printf("#ident %s", Val.c_str());
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000120 EmodeEmittedTokensOnThisLine = true;
121}
122
123/// HandleFirstTokOnLine - When emitting a preprocessed file in -E mode, this
124/// is called for the first token on each new line.
125static void HandleFirstTokOnLine(LexerToken &Tok, Preprocessor &PP) {
126 // Figure out what line we went to and insert the appropriate number of
127 // newline characters.
Chris Lattner3338ba82006-07-04 21:19:39 +0000128 MoveToLine(Tok.getLocation());
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000129
130 // Print out space characters so that the first token on a line is
131 // indented for easy reading.
132 unsigned ColNo =
133 PP.getSourceManager().getColumnNumber(Tok.getLocation());
134
135 // This hack prevents stuff like:
136 // #define HASH #
137 // HASH define foo bar
138 // From having the # character end up at column 1, which makes it so it
139 // is not handled as a #define next time through the preprocessor if in
140 // -fpreprocessed mode.
141 if (ColNo <= 1 && Tok.getKind() == tok::hash)
Chris Lattnerdeb37012006-07-04 19:24:06 +0000142 putchar_unlocked(' ');
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000143
144 // Otherwise, indent the appropriate number of spaces.
145 for (; ColNo > 1; --ColNo)
Chris Lattnerdeb37012006-07-04 19:24:06 +0000146 putchar_unlocked(' ');
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000147}
148
Chris Lattner5de858c2006-07-04 19:04:44 +0000149namespace {
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000150struct UnknownPragmaHandler : public PragmaHandler {
151 const char *Prefix;
152 UnknownPragmaHandler(const char *prefix) : PragmaHandler(0), Prefix(prefix) {}
153 virtual void HandlePragma(Preprocessor &PP, LexerToken &PragmaTok) {
154 // Figure out what line we went to and insert the appropriate number of
155 // newline characters.
Chris Lattner3338ba82006-07-04 21:19:39 +0000156 MoveToLine(PragmaTok.getLocation());
Chris Lattnerdeb37012006-07-04 19:24:06 +0000157 printf(Prefix);
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000158
159 // Read and print all of the pragma tokens.
160 while (PragmaTok.getKind() != tok::eom) {
161 if (PragmaTok.hasLeadingSpace())
Chris Lattnerdeb37012006-07-04 19:24:06 +0000162 putchar_unlocked(' ');
163 printf("%s", PP.getSpelling(PragmaTok).c_str());
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000164 PP.LexUnexpandedToken(PragmaTok);
165 }
Chris Lattnerdeb37012006-07-04 19:24:06 +0000166 putchar_unlocked('\n');
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000167 }
168};
Chris Lattner5de858c2006-07-04 19:04:44 +0000169} // end anonymous namespace
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000170
171/// DoPrintPreprocessedInput - This implements -E mode.
172void clang::DoPrintPreprocessedInput(Preprocessor &PP) {
173 LexerToken Tok;
174 char Buffer[256];
175 EModeCurLine = 0;
176 EModeCurFilename = "\"<uninit>\"";
177 PP.setFileChangeHandler(HandleFileChange);
178 PP.setIdentHandler(HandleIdent);
179 EModePP = &PP;
180 EmodeEmittedTokensOnThisLine = false;
181
182 PP.AddPragmaHandler(0, new UnknownPragmaHandler("#pragma"));
183 PP.AddPragmaHandler("GCC", new UnknownPragmaHandler("#pragma GCC"));
184 do {
185 PP.Lex(Tok);
186
187 // If this token is at the start of a line. Emit the \n and indentation.
188 // FIXME: this shouldn't use the isAtStartOfLine flag. This should use a
189 // "newline callback" from the lexer.
190 // FIXME: For some tests, this fails just because there is no col# info from
191 // macro expansions!
192 if (Tok.isAtStartOfLine()) {
193 HandleFirstTokOnLine(Tok, PP);
194 } else if (Tok.hasLeadingSpace()) {
Chris Lattnerdeb37012006-07-04 19:24:06 +0000195 putchar_unlocked(' ');
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000196 }
197
198 if (Tok.getLength() < 256) {
199 unsigned Len = PP.getSpelling(Tok, Buffer);
200 Buffer[Len] = 0;
Chris Lattnerdeb37012006-07-04 19:24:06 +0000201 fwrite(Buffer, Len, 1, stdout);
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000202 } else {
Chris Lattnerdeb37012006-07-04 19:24:06 +0000203 std::string S = PP.getSpelling(Tok);
204 fwrite(&S[0], S.size(), 1, stdout);
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000205 }
206 EmodeEmittedTokensOnThisLine = true;
207 } while (Tok.getKind() != tok::eof);
Chris Lattnerdeb37012006-07-04 19:24:06 +0000208 putchar_unlocked('\n');
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000209}
210