Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1 | //===--- PrintPreprocessedOutput.cpp - Implement the -E mode --------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 959e5be | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 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/PPCallbacks.h" |
| 17 | #include "clang/Lex/Preprocessor.h" |
| 18 | #include "clang/Lex/Pragma.h" |
| 19 | #include "clang/Basic/SourceManager.h" |
Chris Lattner | 6619f66 | 2008-04-08 04:16:20 +0000 | [diff] [blame] | 20 | #include "clang/Basic/Diagnostic.h" |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/SmallString.h" |
| 22 | #include "llvm/ADT/StringExtras.h" |
Chris Lattner | 6619f66 | 2008-04-08 04:16:20 +0000 | [diff] [blame] | 23 | #include "llvm/System/Path.h" |
| 24 | #include "llvm/Support/CommandLine.h" |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 25 | #include "llvm/Config/config.h" |
Chris Lattner | 93b4f30 | 2008-08-17 01:47:12 +0000 | [diff] [blame] | 26 | #include "llvm/Support/raw_ostream.h" |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 27 | #include <cstdio> |
| 28 | using namespace clang; |
| 29 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 30 | //===----------------------------------------------------------------------===// |
| 31 | // Preprocessed token printer |
| 32 | //===----------------------------------------------------------------------===// |
| 33 | |
| 34 | static llvm::cl::opt<bool> |
| 35 | DisableLineMarkers("P", llvm::cl::desc("Disable linemarker output in -E mode")); |
| 36 | static llvm::cl::opt<bool> |
| 37 | EnableCommentOutput("C", llvm::cl::desc("Enable comment output in -E mode")); |
| 38 | static llvm::cl::opt<bool> |
| 39 | EnableMacroCommentOutput("CC", |
| 40 | llvm::cl::desc("Enable comment output in -E mode, " |
| 41 | "even from macro expansions")); |
| 42 | |
| 43 | namespace { |
| 44 | class PrintPPOutputPPCallbacks : public PPCallbacks { |
| 45 | Preprocessor &PP; |
Chris Lattner | 2149422 | 2008-08-17 03:12:02 +0000 | [diff] [blame] | 46 | public: |
| 47 | llvm::raw_ostream &OS; |
| 48 | private: |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 49 | unsigned CurLine; |
| 50 | bool EmittedTokensOnThisLine; |
| 51 | DirectoryLookup::DirType FileType; |
| 52 | llvm::SmallString<512> CurFilename; |
| 53 | public: |
Chris Lattner | 2149422 | 2008-08-17 03:12:02 +0000 | [diff] [blame] | 54 | PrintPPOutputPPCallbacks(Preprocessor &pp, llvm::raw_ostream &os) |
| 55 | : PP(pp), OS(os) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 56 | CurLine = 0; |
| 57 | CurFilename += "<uninit>"; |
| 58 | EmittedTokensOnThisLine = false; |
| 59 | FileType = DirectoryLookup::NormalHeaderDir; |
| 60 | } |
| 61 | |
| 62 | void SetEmittedTokensOnThisLine() { EmittedTokensOnThisLine = true; } |
| 63 | bool hasEmittedTokensOnThisLine() const { return EmittedTokensOnThisLine; } |
| 64 | |
| 65 | virtual void FileChanged(SourceLocation Loc, FileChangeReason Reason, |
| 66 | DirectoryLookup::DirType FileType); |
| 67 | virtual void Ident(SourceLocation Loc, const std::string &str); |
| 68 | |
| 69 | |
Chris Lattner | 6c45129 | 2007-12-09 21:11:08 +0000 | [diff] [blame] | 70 | bool HandleFirstTokOnLine(Token &Tok); |
| 71 | bool MoveToLine(SourceLocation Loc); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 72 | bool AvoidConcat(const Token &PrevTok, const Token &Tok); |
| 73 | }; |
Chris Lattner | 6619f66 | 2008-04-08 04:16:20 +0000 | [diff] [blame] | 74 | } // end anonymous namespace |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 75 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 76 | /// MoveToLine - Move the output to the source line specified by the location |
| 77 | /// object. We can do this by emitting some number of \n's, or be emitting a |
Chris Lattner | 6c45129 | 2007-12-09 21:11:08 +0000 | [diff] [blame] | 78 | /// #line directive. This returns false if already at the specified line, true |
| 79 | /// if some newlines were emitted. |
| 80 | bool PrintPPOutputPPCallbacks::MoveToLine(SourceLocation Loc) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 81 | if (DisableLineMarkers) { |
Chris Lattner | 6c45129 | 2007-12-09 21:11:08 +0000 | [diff] [blame] | 82 | unsigned LineNo = PP.getSourceManager().getLogicalLineNumber(Loc); |
| 83 | if (LineNo == CurLine) return false; |
| 84 | |
| 85 | CurLine = LineNo; |
| 86 | |
| 87 | if (!EmittedTokensOnThisLine) |
| 88 | return true; |
| 89 | |
Chris Lattner | 2149422 | 2008-08-17 03:12:02 +0000 | [diff] [blame] | 90 | OS << '\n'; |
Chris Lattner | 6c45129 | 2007-12-09 21:11:08 +0000 | [diff] [blame] | 91 | EmittedTokensOnThisLine = false; |
| 92 | return true; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | unsigned LineNo = PP.getSourceManager().getLogicalLineNumber(Loc); |
| 96 | |
| 97 | // If this line is "close enough" to the original line, just print newlines, |
| 98 | // otherwise print a #line directive. |
| 99 | if (LineNo-CurLine < 8) { |
| 100 | if (LineNo-CurLine == 1) |
Chris Lattner | 2149422 | 2008-08-17 03:12:02 +0000 | [diff] [blame] | 101 | OS << '\n'; |
Chris Lattner | 6c45129 | 2007-12-09 21:11:08 +0000 | [diff] [blame] | 102 | else if (LineNo == CurLine) |
| 103 | return false; // Phys line moved, but logical line didn't. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 104 | else { |
| 105 | const char *NewLines = "\n\n\n\n\n\n\n\n"; |
Chris Lattner | 2149422 | 2008-08-17 03:12:02 +0000 | [diff] [blame] | 106 | OS.write(NewLines, LineNo-CurLine); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 107 | } |
Chris Lattner | 45ac817 | 2007-12-09 20:45:43 +0000 | [diff] [blame] | 108 | CurLine = LineNo; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 109 | } else { |
| 110 | if (EmittedTokensOnThisLine) { |
Chris Lattner | 2149422 | 2008-08-17 03:12:02 +0000 | [diff] [blame] | 111 | OS << '\n'; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 112 | EmittedTokensOnThisLine = false; |
| 113 | } |
| 114 | |
| 115 | CurLine = LineNo; |
| 116 | |
Chris Lattner | 3688251 | 2008-08-19 04:23:15 +0000 | [diff] [blame] | 117 | OS << '#' << ' ' << LineNo << ' ' << '"'; |
Chris Lattner | 2149422 | 2008-08-17 03:12:02 +0000 | [diff] [blame] | 118 | OS.write(&CurFilename[0], CurFilename.size()); |
| 119 | OS << '"'; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 120 | |
| 121 | if (FileType == DirectoryLookup::SystemHeaderDir) |
Chris Lattner | 2149422 | 2008-08-17 03:12:02 +0000 | [diff] [blame] | 122 | OS.write(" 3", 2); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 123 | else if (FileType == DirectoryLookup::ExternCSystemHeaderDir) |
Chris Lattner | 2149422 | 2008-08-17 03:12:02 +0000 | [diff] [blame] | 124 | OS.write(" 3 4", 4); |
| 125 | OS << '\n'; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 126 | } |
Chris Lattner | 6c45129 | 2007-12-09 21:11:08 +0000 | [diff] [blame] | 127 | return true; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | |
| 131 | /// FileChanged - Whenever the preprocessor enters or exits a #include file |
| 132 | /// it invokes this handler. Update our conception of the current source |
| 133 | /// position. |
| 134 | void PrintPPOutputPPCallbacks::FileChanged(SourceLocation Loc, |
| 135 | FileChangeReason Reason, |
| 136 | DirectoryLookup::DirType FileType) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 137 | // Unless we are exiting a #include, make sure to skip ahead to the line the |
| 138 | // #include directive was at. |
| 139 | SourceManager &SourceMgr = PP.getSourceManager(); |
| 140 | if (Reason == PPCallbacks::EnterFile) { |
| 141 | MoveToLine(SourceMgr.getIncludeLoc(Loc)); |
| 142 | } else if (Reason == PPCallbacks::SystemHeaderPragma) { |
| 143 | MoveToLine(Loc); |
| 144 | |
| 145 | // TODO GCC emits the # directive for this directive on the line AFTER the |
| 146 | // directive and emits a bunch of spaces that aren't needed. Emulate this |
| 147 | // strange behavior. |
| 148 | } |
| 149 | |
| 150 | Loc = SourceMgr.getLogicalLoc(Loc); |
| 151 | CurLine = SourceMgr.getLineNumber(Loc); |
Chris Lattner | 6c45129 | 2007-12-09 21:11:08 +0000 | [diff] [blame] | 152 | |
| 153 | if (DisableLineMarkers) return; |
| 154 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 155 | CurFilename.clear(); |
| 156 | CurFilename += SourceMgr.getSourceName(Loc); |
| 157 | Lexer::Stringify(CurFilename); |
| 158 | FileType = FileType; |
| 159 | |
| 160 | if (EmittedTokensOnThisLine) { |
Chris Lattner | 2149422 | 2008-08-17 03:12:02 +0000 | [diff] [blame] | 161 | OS << '\n'; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 162 | EmittedTokensOnThisLine = false; |
| 163 | } |
| 164 | |
Chris Lattner | 3688251 | 2008-08-19 04:23:15 +0000 | [diff] [blame] | 165 | OS << '#' << ' ' << CurLine << ' ' << '"'; |
Chris Lattner | 2149422 | 2008-08-17 03:12:02 +0000 | [diff] [blame] | 166 | OS.write(&CurFilename[0], CurFilename.size()); |
| 167 | OS << '"'; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 168 | |
| 169 | switch (Reason) { |
| 170 | case PPCallbacks::EnterFile: |
Chris Lattner | 2149422 | 2008-08-17 03:12:02 +0000 | [diff] [blame] | 171 | OS.write(" 1", 2); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 172 | break; |
| 173 | case PPCallbacks::ExitFile: |
Chris Lattner | 2149422 | 2008-08-17 03:12:02 +0000 | [diff] [blame] | 174 | OS.write(" 2", 2); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 175 | break; |
| 176 | case PPCallbacks::SystemHeaderPragma: break; |
| 177 | case PPCallbacks::RenameFile: break; |
| 178 | } |
| 179 | |
| 180 | if (FileType == DirectoryLookup::SystemHeaderDir) |
Chris Lattner | 2149422 | 2008-08-17 03:12:02 +0000 | [diff] [blame] | 181 | OS.write(" 3", 2); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 182 | else if (FileType == DirectoryLookup::ExternCSystemHeaderDir) |
Chris Lattner | 2149422 | 2008-08-17 03:12:02 +0000 | [diff] [blame] | 183 | OS.write(" 3 4", 4); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 184 | |
Chris Lattner | 2149422 | 2008-08-17 03:12:02 +0000 | [diff] [blame] | 185 | OS << '\n'; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | /// HandleIdent - Handle #ident directives when read by the preprocessor. |
| 189 | /// |
| 190 | void PrintPPOutputPPCallbacks::Ident(SourceLocation Loc, const std::string &S) { |
| 191 | MoveToLine(Loc); |
| 192 | |
Chris Lattner | 2149422 | 2008-08-17 03:12:02 +0000 | [diff] [blame] | 193 | OS.write("#ident ", strlen("#ident ")); |
| 194 | OS.write(&S[0], S.size()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 195 | EmittedTokensOnThisLine = true; |
| 196 | } |
| 197 | |
| 198 | /// HandleFirstTokOnLine - When emitting a preprocessed file in -E mode, this |
Chris Lattner | 6c45129 | 2007-12-09 21:11:08 +0000 | [diff] [blame] | 199 | /// is called for the first token on each new line. If this really is the start |
| 200 | /// of a new logical line, handle it and return true, otherwise return false. |
| 201 | /// This may not be the start of a logical line because the "start of line" |
| 202 | /// marker is set for physical lines, not logical ones. |
| 203 | bool PrintPPOutputPPCallbacks::HandleFirstTokOnLine(Token &Tok) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 204 | // Figure out what line we went to and insert the appropriate number of |
| 205 | // newline characters. |
Chris Lattner | 6c45129 | 2007-12-09 21:11:08 +0000 | [diff] [blame] | 206 | if (!MoveToLine(Tok.getLocation())) |
| 207 | return false; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 208 | |
| 209 | // Print out space characters so that the first token on a line is |
| 210 | // indented for easy reading. |
| 211 | const SourceManager &SourceMgr = PP.getSourceManager(); |
| 212 | unsigned ColNo = SourceMgr.getLogicalColumnNumber(Tok.getLocation()); |
| 213 | |
| 214 | // This hack prevents stuff like: |
| 215 | // #define HASH # |
| 216 | // HASH define foo bar |
| 217 | // From having the # character end up at column 1, which makes it so it |
| 218 | // is not handled as a #define next time through the preprocessor if in |
| 219 | // -fpreprocessed mode. |
Chris Lattner | 3b49415 | 2007-10-09 18:03:42 +0000 | [diff] [blame] | 220 | if (ColNo <= 1 && Tok.is(tok::hash)) |
Chris Lattner | 2149422 | 2008-08-17 03:12:02 +0000 | [diff] [blame] | 221 | OS << ' '; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 222 | |
| 223 | // Otherwise, indent the appropriate number of spaces. |
| 224 | for (; ColNo > 1; --ColNo) |
Chris Lattner | 2149422 | 2008-08-17 03:12:02 +0000 | [diff] [blame] | 225 | OS << ' '; |
Chris Lattner | 6c45129 | 2007-12-09 21:11:08 +0000 | [diff] [blame] | 226 | |
| 227 | return true; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | namespace { |
| 231 | struct UnknownPragmaHandler : public PragmaHandler { |
| 232 | const char *Prefix; |
| 233 | PrintPPOutputPPCallbacks *Callbacks; |
| 234 | |
| 235 | UnknownPragmaHandler(const char *prefix, PrintPPOutputPPCallbacks *callbacks) |
| 236 | : PragmaHandler(0), Prefix(prefix), Callbacks(callbacks) {} |
| 237 | virtual void HandlePragma(Preprocessor &PP, Token &PragmaTok) { |
| 238 | // Figure out what line we went to and insert the appropriate number of |
| 239 | // newline characters. |
| 240 | Callbacks->MoveToLine(PragmaTok.getLocation()); |
Chris Lattner | 2149422 | 2008-08-17 03:12:02 +0000 | [diff] [blame] | 241 | Callbacks->OS.write(Prefix, strlen(Prefix)); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 242 | |
| 243 | // Read and print all of the pragma tokens. |
Chris Lattner | 3b49415 | 2007-10-09 18:03:42 +0000 | [diff] [blame] | 244 | while (PragmaTok.isNot(tok::eom)) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 245 | if (PragmaTok.hasLeadingSpace()) |
Chris Lattner | 2149422 | 2008-08-17 03:12:02 +0000 | [diff] [blame] | 246 | Callbacks->OS << ' '; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 247 | std::string TokSpell = PP.getSpelling(PragmaTok); |
Chris Lattner | 2149422 | 2008-08-17 03:12:02 +0000 | [diff] [blame] | 248 | Callbacks->OS.write(&TokSpell[0], TokSpell.size()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 249 | PP.LexUnexpandedToken(PragmaTok); |
| 250 | } |
Chris Lattner | 2149422 | 2008-08-17 03:12:02 +0000 | [diff] [blame] | 251 | Callbacks->OS << '\n'; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 252 | } |
| 253 | }; |
| 254 | } // end anonymous namespace |
| 255 | |
| 256 | |
| 257 | enum AvoidConcatInfo { |
| 258 | /// By default, a token never needs to avoid concatenation. Most tokens (e.g. |
| 259 | /// ',', ')', etc) don't cause a problem when concatenated. |
| 260 | aci_never_avoid_concat = 0, |
| 261 | |
| 262 | /// aci_custom_firstchar - AvoidConcat contains custom code to handle this |
| 263 | /// token's requirements, and it needs to know the first character of the |
| 264 | /// token. |
| 265 | aci_custom_firstchar = 1, |
| 266 | |
| 267 | /// aci_custom - AvoidConcat contains custom code to handle this token's |
| 268 | /// requirements, but it doesn't need to know the first character of the |
| 269 | /// token. |
| 270 | aci_custom = 2, |
| 271 | |
| 272 | /// aci_avoid_equal - Many tokens cannot be safely followed by an '=' |
| 273 | /// character. For example, "<<" turns into "<<=" when followed by an =. |
| 274 | aci_avoid_equal = 4 |
| 275 | }; |
| 276 | |
| 277 | /// This array contains information for each token on what action to take when |
| 278 | /// avoiding concatenation of tokens in the AvoidConcat method. |
| 279 | static char TokenInfo[tok::NUM_TOKENS]; |
| 280 | |
| 281 | /// InitAvoidConcatTokenInfo - Tokens that must avoid concatenation should be |
| 282 | /// marked by this function. |
| 283 | static void InitAvoidConcatTokenInfo() { |
| 284 | // These tokens have custom code in AvoidConcat. |
| 285 | TokenInfo[tok::identifier ] |= aci_custom; |
| 286 | TokenInfo[tok::numeric_constant] |= aci_custom_firstchar; |
| 287 | TokenInfo[tok::period ] |= aci_custom_firstchar; |
| 288 | TokenInfo[tok::amp ] |= aci_custom_firstchar; |
| 289 | TokenInfo[tok::plus ] |= aci_custom_firstchar; |
| 290 | TokenInfo[tok::minus ] |= aci_custom_firstchar; |
| 291 | TokenInfo[tok::slash ] |= aci_custom_firstchar; |
| 292 | TokenInfo[tok::less ] |= aci_custom_firstchar; |
| 293 | TokenInfo[tok::greater ] |= aci_custom_firstchar; |
| 294 | TokenInfo[tok::pipe ] |= aci_custom_firstchar; |
| 295 | TokenInfo[tok::percent ] |= aci_custom_firstchar; |
| 296 | TokenInfo[tok::colon ] |= aci_custom_firstchar; |
| 297 | TokenInfo[tok::hash ] |= aci_custom_firstchar; |
| 298 | TokenInfo[tok::arrow ] |= aci_custom_firstchar; |
| 299 | |
| 300 | // These tokens change behavior if followed by an '='. |
| 301 | TokenInfo[tok::amp ] |= aci_avoid_equal; // &= |
| 302 | TokenInfo[tok::plus ] |= aci_avoid_equal; // += |
| 303 | TokenInfo[tok::minus ] |= aci_avoid_equal; // -= |
| 304 | TokenInfo[tok::slash ] |= aci_avoid_equal; // /= |
| 305 | TokenInfo[tok::less ] |= aci_avoid_equal; // <= |
| 306 | TokenInfo[tok::greater ] |= aci_avoid_equal; // >= |
| 307 | TokenInfo[tok::pipe ] |= aci_avoid_equal; // |= |
| 308 | TokenInfo[tok::percent ] |= aci_avoid_equal; // %= |
| 309 | TokenInfo[tok::star ] |= aci_avoid_equal; // *= |
| 310 | TokenInfo[tok::exclaim ] |= aci_avoid_equal; // != |
| 311 | TokenInfo[tok::lessless ] |= aci_avoid_equal; // <<= |
| 312 | TokenInfo[tok::greaterequal] |= aci_avoid_equal; // >>= |
| 313 | TokenInfo[tok::caret ] |= aci_avoid_equal; // ^= |
| 314 | TokenInfo[tok::equal ] |= aci_avoid_equal; // == |
| 315 | } |
| 316 | |
Chris Lattner | afa4012 | 2008-01-15 05:22:14 +0000 | [diff] [blame] | 317 | /// StartsWithL - Return true if the spelling of this token starts with 'L'. |
Chris Lattner | 400f024 | 2008-01-15 05:14:19 +0000 | [diff] [blame] | 318 | static bool StartsWithL(const Token &Tok, Preprocessor &PP) { |
Chris Lattner | 400f024 | 2008-01-15 05:14:19 +0000 | [diff] [blame] | 319 | if (!Tok.needsCleaning()) { |
| 320 | SourceManager &SrcMgr = PP.getSourceManager(); |
| 321 | return *SrcMgr.getCharacterData(SrcMgr.getPhysicalLoc(Tok.getLocation())) |
| 322 | == 'L'; |
| 323 | } |
| 324 | |
| 325 | if (Tok.getLength() < 256) { |
Chris Lattner | afa4012 | 2008-01-15 05:22:14 +0000 | [diff] [blame] | 326 | char Buffer[256]; |
Chris Lattner | 400f024 | 2008-01-15 05:14:19 +0000 | [diff] [blame] | 327 | const char *TokPtr = Buffer; |
| 328 | PP.getSpelling(Tok, TokPtr); |
| 329 | return TokPtr[0] == 'L'; |
| 330 | } |
| 331 | |
| 332 | return PP.getSpelling(Tok)[0] == 'L'; |
| 333 | } |
| 334 | |
Chris Lattner | afa4012 | 2008-01-15 05:22:14 +0000 | [diff] [blame] | 335 | /// IsIdentifierL - Return true if the spelling of this token is literally 'L'. |
| 336 | static bool IsIdentifierL(const Token &Tok, Preprocessor &PP) { |
| 337 | if (!Tok.needsCleaning()) { |
| 338 | if (Tok.getLength() != 1) |
| 339 | return false; |
| 340 | SourceManager &SrcMgr = PP.getSourceManager(); |
| 341 | return *SrcMgr.getCharacterData(SrcMgr.getPhysicalLoc(Tok.getLocation())) |
| 342 | == 'L'; |
| 343 | } |
| 344 | |
| 345 | if (Tok.getLength() < 256) { |
| 346 | char Buffer[256]; |
| 347 | const char *TokPtr = Buffer; |
| 348 | if (PP.getSpelling(Tok, TokPtr) != 1) |
| 349 | return false; |
| 350 | return TokPtr[0] == 'L'; |
| 351 | } |
| 352 | |
| 353 | return PP.getSpelling(Tok) == "L"; |
| 354 | } |
| 355 | |
| 356 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 357 | /// AvoidConcat - If printing PrevTok immediately followed by Tok would cause |
| 358 | /// the two individual tokens to be lexed as a single token, return true (which |
| 359 | /// causes a space to be printed between them). This allows the output of -E |
| 360 | /// mode to be lexed to the same token stream as lexing the input directly |
| 361 | /// would. |
| 362 | /// |
| 363 | /// This code must conservatively return true if it doesn't want to be 100% |
| 364 | /// accurate. This will cause the output to include extra space characters, but |
| 365 | /// the resulting output won't have incorrect concatenations going on. Examples |
| 366 | /// include "..", which we print with a space between, because we don't want to |
| 367 | /// track enough to tell "x.." from "...". |
| 368 | bool PrintPPOutputPPCallbacks::AvoidConcat(const Token &PrevTok, |
| 369 | const Token &Tok) { |
| 370 | char Buffer[256]; |
| 371 | |
| 372 | tok::TokenKind PrevKind = PrevTok.getKind(); |
| 373 | if (PrevTok.getIdentifierInfo()) // Language keyword or named operator. |
| 374 | PrevKind = tok::identifier; |
| 375 | |
| 376 | // Look up information on when we should avoid concatenation with prevtok. |
| 377 | unsigned ConcatInfo = TokenInfo[PrevKind]; |
| 378 | |
| 379 | // If prevtok never causes a problem for anything after it, return quickly. |
| 380 | if (ConcatInfo == 0) return false; |
| 381 | |
| 382 | if (ConcatInfo & aci_avoid_equal) { |
| 383 | // If the next token is '=' or '==', avoid concatenation. |
Chris Lattner | 3b49415 | 2007-10-09 18:03:42 +0000 | [diff] [blame] | 384 | if (Tok.is(tok::equal) || Tok.is(tok::equalequal)) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 385 | return true; |
| 386 | ConcatInfo &= ~aci_avoid_equal; |
| 387 | } |
| 388 | |
| 389 | if (ConcatInfo == 0) return false; |
| 390 | |
| 391 | |
| 392 | |
| 393 | // Basic algorithm: we look at the first character of the second token, and |
| 394 | // determine whether it, if appended to the first token, would form (or would |
| 395 | // contribute) to a larger token if concatenated. |
| 396 | char FirstChar = 0; |
| 397 | if (ConcatInfo & aci_custom) { |
| 398 | // If the token does not need to know the first character, don't get it. |
| 399 | } else if (IdentifierInfo *II = Tok.getIdentifierInfo()) { |
| 400 | // Avoid spelling identifiers, the most common form of token. |
| 401 | FirstChar = II->getName()[0]; |
| 402 | } else if (!Tok.needsCleaning()) { |
| 403 | SourceManager &SrcMgr = PP.getSourceManager(); |
| 404 | FirstChar = |
| 405 | *SrcMgr.getCharacterData(SrcMgr.getPhysicalLoc(Tok.getLocation())); |
| 406 | } else if (Tok.getLength() < 256) { |
| 407 | const char *TokPtr = Buffer; |
| 408 | PP.getSpelling(Tok, TokPtr); |
| 409 | FirstChar = TokPtr[0]; |
| 410 | } else { |
| 411 | FirstChar = PP.getSpelling(Tok)[0]; |
| 412 | } |
| 413 | |
| 414 | switch (PrevKind) { |
| 415 | default: assert(0 && "InitAvoidConcatTokenInfo built wrong"); |
| 416 | case tok::identifier: // id+id or id+number or id+L"foo". |
Chris Lattner | 3b49415 | 2007-10-09 18:03:42 +0000 | [diff] [blame] | 417 | if (Tok.is(tok::numeric_constant) || Tok.getIdentifierInfo() || |
| 418 | Tok.is(tok::wide_string_literal) /* || |
| 419 | Tok.is(tok::wide_char_literal)*/) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 420 | return true; |
Chris Lattner | 400f024 | 2008-01-15 05:14:19 +0000 | [diff] [blame] | 421 | |
| 422 | // If this isn't identifier + string, we're done. |
| 423 | if (Tok.isNot(tok::char_constant) && Tok.isNot(tok::string_literal)) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 424 | return false; |
| 425 | |
| 426 | // FIXME: need a wide_char_constant! |
Chris Lattner | 400f024 | 2008-01-15 05:14:19 +0000 | [diff] [blame] | 427 | |
| 428 | // If the string was a wide string L"foo" or wide char L'f', it would concat |
| 429 | // with the previous identifier into fooL"bar". Avoid this. |
| 430 | if (StartsWithL(Tok, PP)) |
| 431 | return true; |
| 432 | |
Chris Lattner | afa4012 | 2008-01-15 05:22:14 +0000 | [diff] [blame] | 433 | // Otherwise, this is a narrow character or string. If the *identifier* is |
| 434 | // a literal 'L', avoid pasting L "foo" -> L"foo". |
| 435 | return IsIdentifierL(PrevTok, PP); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 436 | case tok::numeric_constant: |
Chris Lattner | 3b49415 | 2007-10-09 18:03:42 +0000 | [diff] [blame] | 437 | return isalnum(FirstChar) || Tok.is(tok::numeric_constant) || |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 438 | FirstChar == '+' || FirstChar == '-' || FirstChar == '.'; |
| 439 | case tok::period: // ..., .*, .1234 |
| 440 | return FirstChar == '.' || FirstChar == '*' || isdigit(FirstChar); |
| 441 | case tok::amp: // && |
| 442 | return FirstChar == '&'; |
| 443 | case tok::plus: // ++ |
| 444 | return FirstChar == '+'; |
| 445 | case tok::minus: // --, ->, ->* |
| 446 | return FirstChar == '-' || FirstChar == '>'; |
| 447 | case tok::slash: //, /*, // |
| 448 | return FirstChar == '*' || FirstChar == '/'; |
| 449 | case tok::less: // <<, <<=, <:, <% |
| 450 | return FirstChar == '<' || FirstChar == ':' || FirstChar == '%'; |
| 451 | case tok::greater: // >>, >>= |
| 452 | return FirstChar == '>'; |
| 453 | case tok::pipe: // || |
| 454 | return FirstChar == '|'; |
| 455 | case tok::percent: // %>, %: |
| 456 | return FirstChar == '>' || FirstChar == ':'; |
| 457 | case tok::colon: // ::, :> |
| 458 | return FirstChar == ':' || FirstChar == '>'; |
| 459 | case tok::hash: // ##, #@, %:%: |
| 460 | return FirstChar == '#' || FirstChar == '@' || FirstChar == '%'; |
| 461 | case tok::arrow: // ->* |
| 462 | return FirstChar == '*'; |
| 463 | } |
| 464 | } |
| 465 | |
| 466 | /// DoPrintPreprocessedInput - This implements -E mode. |
| 467 | /// |
Chris Lattner | 6619f66 | 2008-04-08 04:16:20 +0000 | [diff] [blame] | 468 | void clang::DoPrintPreprocessedInput(Preprocessor &PP, |
| 469 | const std::string &OutFile) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 470 | // Inform the preprocessor whether we want it to retain comments or not, due |
| 471 | // to -C or -CC. |
| 472 | PP.SetCommentRetentionState(EnableCommentOutput, EnableMacroCommentOutput); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 473 | InitAvoidConcatTokenInfo(); |
Chris Lattner | 2149422 | 2008-08-17 03:12:02 +0000 | [diff] [blame] | 474 | |
| 475 | |
| 476 | // Open the output buffer. |
Chris Lattner | 1be9690 | 2008-08-17 03:54:39 +0000 | [diff] [blame] | 477 | std::string Err; |
Chris Lattner | 03074ed | 2008-08-17 07:09:08 +0000 | [diff] [blame] | 478 | llvm::raw_fd_ostream OS(OutFile.empty() ? "-" : OutFile.c_str(), Err); |
Chris Lattner | 1be9690 | 2008-08-17 03:54:39 +0000 | [diff] [blame] | 479 | if (!Err.empty()) { |
| 480 | fprintf(stderr, "%s\n", Err.c_str()); |
| 481 | exit(1); |
Chris Lattner | 2149422 | 2008-08-17 03:12:02 +0000 | [diff] [blame] | 482 | } |
Chris Lattner | 2149422 | 2008-08-17 03:12:02 +0000 | [diff] [blame] | 483 | |
Chris Lattner | 1be9690 | 2008-08-17 03:54:39 +0000 | [diff] [blame] | 484 | OS.SetBufferSize(64*1024); |
| 485 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 486 | |
| 487 | Token Tok, PrevTok; |
| 488 | char Buffer[256]; |
Chris Lattner | 2149422 | 2008-08-17 03:12:02 +0000 | [diff] [blame] | 489 | PrintPPOutputPPCallbacks *Callbacks = new PrintPPOutputPPCallbacks(PP, OS); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 490 | PP.setPPCallbacks(Callbacks); |
| 491 | |
| 492 | PP.AddPragmaHandler(0, new UnknownPragmaHandler("#pragma", Callbacks)); |
| 493 | PP.AddPragmaHandler("GCC", new UnknownPragmaHandler("#pragma GCC",Callbacks)); |
| 494 | |
| 495 | // After we have configured the preprocessor, enter the main file. |
| 496 | |
| 497 | // Start parsing the specified input file. |
Ted Kremenek | 17861c5 | 2007-12-19 22:51:13 +0000 | [diff] [blame] | 498 | PP.EnterMainSourceFile(); |
Chris Lattner | 3eddc86 | 2007-10-10 20:45:16 +0000 | [diff] [blame] | 499 | |
| 500 | // Consume all of the tokens that come from the predefines buffer. Those |
| 501 | // should not be emitted into the output and are guaranteed to be at the |
| 502 | // start. |
| 503 | const SourceManager &SourceMgr = PP.getSourceManager(); |
| 504 | do PP.Lex(Tok); |
Chris Lattner | 890c593 | 2007-10-10 23:31:03 +0000 | [diff] [blame] | 505 | while (Tok.isNot(tok::eof) && Tok.getLocation().isFileID() && |
Chris Lattner | 3eddc86 | 2007-10-10 20:45:16 +0000 | [diff] [blame] | 506 | !strcmp(SourceMgr.getSourceName(Tok.getLocation()), "<predefines>")); |
| 507 | |
| 508 | while (1) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 509 | |
| 510 | // If this token is at the start of a line, emit newlines if needed. |
Chris Lattner | 6c45129 | 2007-12-09 21:11:08 +0000 | [diff] [blame] | 511 | if (Tok.isAtStartOfLine() && Callbacks->HandleFirstTokOnLine(Tok)) { |
| 512 | // done. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 513 | } else if (Tok.hasLeadingSpace() || |
| 514 | // If we haven't emitted a token on this line yet, PrevTok isn't |
| 515 | // useful to look at and no concatenation could happen anyway. |
| 516 | (Callbacks->hasEmittedTokensOnThisLine() && |
| 517 | // Don't print "-" next to "-", it would form "--". |
| 518 | Callbacks->AvoidConcat(PrevTok, Tok))) { |
Chris Lattner | 2149422 | 2008-08-17 03:12:02 +0000 | [diff] [blame] | 519 | OS << ' '; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 520 | } |
| 521 | |
| 522 | if (IdentifierInfo *II = Tok.getIdentifierInfo()) { |
| 523 | const char *Str = II->getName(); |
| 524 | unsigned Len = Tok.needsCleaning() ? strlen(Str) : Tok.getLength(); |
Chris Lattner | 2149422 | 2008-08-17 03:12:02 +0000 | [diff] [blame] | 525 | OS.write(Str, Len); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 526 | } else if (Tok.getLength() < 256) { |
| 527 | const char *TokPtr = Buffer; |
| 528 | unsigned Len = PP.getSpelling(Tok, TokPtr); |
Chris Lattner | 2149422 | 2008-08-17 03:12:02 +0000 | [diff] [blame] | 529 | OS.write(TokPtr, Len); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 530 | } else { |
| 531 | std::string S = PP.getSpelling(Tok); |
Chris Lattner | 2149422 | 2008-08-17 03:12:02 +0000 | [diff] [blame] | 532 | OS.write(&S[0], S.size()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 533 | } |
| 534 | Callbacks->SetEmittedTokensOnThisLine(); |
Chris Lattner | 3eddc86 | 2007-10-10 20:45:16 +0000 | [diff] [blame] | 535 | |
| 536 | if (Tok.is(tok::eof)) break; |
| 537 | |
| 538 | PrevTok = Tok; |
| 539 | PP.Lex(Tok); |
| 540 | } |
Chris Lattner | 2149422 | 2008-08-17 03:12:02 +0000 | [diff] [blame] | 541 | OS << '\n'; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 542 | |
Chris Lattner | cb7f180 | 2008-08-17 07:07:01 +0000 | [diff] [blame] | 543 | // Flush the ostream. |
| 544 | OS.flush(); |
Chris Lattner | 2149422 | 2008-08-17 03:12:02 +0000 | [diff] [blame] | 545 | |
| 546 | // If an error occurred, remove the output file. |
| 547 | if (PP.getDiagnostics().hasErrorOccurred() && !OutFile.empty()) |
| 548 | llvm::sys::Path(OutFile).eraseFromDisk(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 549 | } |
| 550 | |