Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1 | //===--- Preprocess.cpp - C Language Family Preprocessor Implementation ---===// |
| 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 file implements the Preprocessor interface. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | // |
| 14 | // TODO: GCC Diagnostics emitted by the lexer: |
| 15 | // |
| 16 | // ERROR : __VA_ARGS__ can only appear in the expansion of a C99 variadic macro |
| 17 | // |
| 18 | // Options to support: |
| 19 | // -H - Print the name of each header file used. |
| 20 | // -C -CC - Do not discard comments for cpp. |
| 21 | // -P - Do not emit #line directives. |
| 22 | // -d[MDNI] - Dump various things. |
| 23 | // -fworking-directory - #line's with preprocessor's working dir. |
| 24 | // -fpreprocessed |
| 25 | // -dependency-file,-M,-MM,-MF,-MG,-MP,-MT,-MQ,-MD,-MMD |
| 26 | // -W* |
| 27 | // -w |
| 28 | // |
| 29 | // Messages to emit: |
| 30 | // "Multiple include guards may be useful for:\n" |
| 31 | // |
| 32 | // TODO: Implement the include guard optimization. |
| 33 | // |
| 34 | //===----------------------------------------------------------------------===// |
| 35 | |
| 36 | #include "clang/Lex/Preprocessor.h" |
| 37 | #include "clang/Lex/MacroInfo.h" |
Chris Lattner | b876183 | 2006-06-24 21:31:03 +0000 | [diff] [blame] | 38 | #include "clang/Lex/Pragma.h" |
Chris Lattner | 0b8cfc2 | 2006-06-28 06:49:17 +0000 | [diff] [blame] | 39 | #include "clang/Lex/ScratchBuffer.h" |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 40 | #include "clang/Basic/Diagnostic.h" |
| 41 | #include "clang/Basic/FileManager.h" |
| 42 | #include "clang/Basic/SourceManager.h" |
| 43 | #include <iostream> |
| 44 | using namespace llvm; |
| 45 | using namespace clang; |
| 46 | |
| 47 | //===----------------------------------------------------------------------===// |
| 48 | |
| 49 | Preprocessor::Preprocessor(Diagnostic &diags, const LangOptions &opts, |
| 50 | FileManager &FM, SourceManager &SM) |
| 51 | : Diags(diags), Features(opts), FileMgr(FM), SourceMgr(SM), |
| 52 | SystemDirIdx(0), NoCurDirSearch(false), |
Chris Lattner | c899718 | 2006-06-22 05:52:16 +0000 | [diff] [blame] | 53 | CurLexer(0), CurDirLookup(0), CurMacroExpander(0) { |
Chris Lattner | 0b8cfc2 | 2006-06-28 06:49:17 +0000 | [diff] [blame] | 54 | ScratchBuf = new ScratchBuffer(SourceMgr); |
| 55 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 56 | // Clear stats. |
| 57 | NumDirectives = NumIncluded = NumDefined = NumUndefined = NumPragma = 0; |
| 58 | NumIf = NumElse = NumEndif = 0; |
| 59 | NumEnteredSourceFiles = NumMacroExpanded = NumFastMacroExpanded = 0; |
Chris Lattner | 69772b0 | 2006-07-02 20:34:39 +0000 | [diff] [blame^] | 60 | MaxIncludeStackDepth = 0; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 61 | NumSkipped = 0; |
Chris Lattner | 0c885f5 | 2006-06-21 06:50:18 +0000 | [diff] [blame] | 62 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 63 | // Macro expansion is enabled. |
| 64 | DisableMacroExpansion = false; |
| 65 | SkippingContents = false; |
Chris Lattner | 0c885f5 | 2006-06-21 06:50:18 +0000 | [diff] [blame] | 66 | |
| 67 | // There is no file-change handler yet. |
| 68 | FileChangeHandler = 0; |
Chris Lattner | b876183 | 2006-06-24 21:31:03 +0000 | [diff] [blame] | 69 | |
| 70 | // Initialize the pragma handlers. |
| 71 | PragmaHandlers = new PragmaNamespace(0); |
| 72 | RegisterBuiltinPragmas(); |
Chris Lattner | 677757a | 2006-06-28 05:26:32 +0000 | [diff] [blame] | 73 | |
| 74 | // Initialize builtin macros like __LINE__ and friends. |
| 75 | RegisterBuiltinMacros(); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | Preprocessor::~Preprocessor() { |
| 79 | // Free any active lexers. |
| 80 | delete CurLexer; |
| 81 | |
Chris Lattner | 69772b0 | 2006-07-02 20:34:39 +0000 | [diff] [blame^] | 82 | while (!IncludeMacroStack.empty()) { |
| 83 | delete IncludeMacroStack.back().TheLexer; |
| 84 | delete IncludeMacroStack.back().TheMacroExpander; |
| 85 | IncludeMacroStack.pop_back(); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 86 | } |
Chris Lattner | b876183 | 2006-06-24 21:31:03 +0000 | [diff] [blame] | 87 | |
| 88 | // Release pragma information. |
| 89 | delete PragmaHandlers; |
Chris Lattner | 0b8cfc2 | 2006-06-28 06:49:17 +0000 | [diff] [blame] | 90 | |
| 91 | // Delete the scratch buffer info. |
| 92 | delete ScratchBuf; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | /// getFileInfo - Return the PerFileInfo structure for the specified |
| 96 | /// FileEntry. |
| 97 | Preprocessor::PerFileInfo &Preprocessor::getFileInfo(const FileEntry *FE) { |
| 98 | if (FE->getUID() >= FileInfo.size()) |
| 99 | FileInfo.resize(FE->getUID()+1); |
| 100 | return FileInfo[FE->getUID()]; |
| 101 | } |
| 102 | |
| 103 | |
| 104 | /// AddKeywords - Add all keywords to the symbol table. |
| 105 | /// |
| 106 | void Preprocessor::AddKeywords() { |
| 107 | enum { |
| 108 | C90Shift = 0, |
| 109 | EXTC90 = 1 << C90Shift, |
| 110 | NOTC90 = 2 << C90Shift, |
| 111 | C99Shift = 2, |
| 112 | EXTC99 = 1 << C99Shift, |
| 113 | NOTC99 = 2 << C99Shift, |
| 114 | CPPShift = 4, |
| 115 | EXTCPP = 1 << CPPShift, |
| 116 | NOTCPP = 2 << CPPShift, |
| 117 | Mask = 3 |
| 118 | }; |
| 119 | |
| 120 | // Add keywords and tokens for the current language. |
| 121 | #define KEYWORD(NAME, FLAGS) \ |
| 122 | AddKeyword(#NAME+1, tok::kw##NAME, \ |
| 123 | (FLAGS >> C90Shift) & Mask, \ |
| 124 | (FLAGS >> C99Shift) & Mask, \ |
| 125 | (FLAGS >> CPPShift) & Mask); |
| 126 | #define ALIAS(NAME, TOK) \ |
| 127 | AddKeyword(NAME, tok::kw_ ## TOK, 0, 0, 0); |
| 128 | #include "clang/Basic/TokenKinds.def" |
| 129 | } |
| 130 | |
| 131 | /// Diag - Forwarding function for diagnostics. This emits a diagnostic at |
| 132 | /// the specified LexerToken's location, translating the token's start |
| 133 | /// position in the current buffer into a SourcePosition object for rendering. |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 134 | void Preprocessor::Diag(SourceLocation Loc, unsigned DiagID, |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 135 | const std::string &Msg) { |
| 136 | // If we are in a '#if 0' block, don't emit any diagnostics for notes, |
| 137 | // warnings or extensions. |
| 138 | if (isSkipping() && Diagnostic::isNoteWarningOrExtension(DiagID)) |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 139 | return; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 140 | |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 141 | Diags.Report(Loc, DiagID, Msg); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 142 | } |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 143 | void Preprocessor::Diag(const LexerToken &Tok, unsigned DiagID, |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 144 | const std::string &Msg) { |
| 145 | // If we are in a '#if 0' block, don't emit any diagnostics for notes, |
| 146 | // warnings or extensions. |
| 147 | if (isSkipping() && Diagnostic::isNoteWarningOrExtension(DiagID)) |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 148 | return; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 149 | |
Chris Lattner | 50b497e | 2006-06-18 16:32:35 +0000 | [diff] [blame] | 150 | Diag(Tok.getLocation(), DiagID, Msg); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 151 | } |
| 152 | |
Chris Lattner | d01e291 | 2006-06-18 16:22:51 +0000 | [diff] [blame] | 153 | |
| 154 | void Preprocessor::DumpToken(const LexerToken &Tok, bool DumpFlags) const { |
| 155 | std::cerr << tok::getTokenName(Tok.getKind()) << " '" |
| 156 | << getSpelling(Tok) << "'"; |
| 157 | |
| 158 | if (!DumpFlags) return; |
| 159 | std::cerr << "\t"; |
| 160 | if (Tok.isAtStartOfLine()) |
| 161 | std::cerr << " [StartOfLine]"; |
| 162 | if (Tok.hasLeadingSpace()) |
| 163 | std::cerr << " [LeadingSpace]"; |
| 164 | if (Tok.needsCleaning()) { |
Chris Lattner | 50b497e | 2006-06-18 16:32:35 +0000 | [diff] [blame] | 165 | const char *Start = SourceMgr.getCharacterData(Tok.getLocation()); |
Chris Lattner | d01e291 | 2006-06-18 16:22:51 +0000 | [diff] [blame] | 166 | std::cerr << " [UnClean='" << std::string(Start, Start+Tok.getLength()) |
| 167 | << "']"; |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | void Preprocessor::DumpMacro(const MacroInfo &MI) const { |
| 172 | std::cerr << "MACRO: "; |
| 173 | for (unsigned i = 0, e = MI.getNumTokens(); i != e; ++i) { |
| 174 | DumpToken(MI.getReplacementToken(i)); |
| 175 | std::cerr << " "; |
| 176 | } |
| 177 | std::cerr << "\n"; |
| 178 | } |
| 179 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 180 | void Preprocessor::PrintStats() { |
| 181 | std::cerr << "\n*** Preprocessor Stats:\n"; |
| 182 | std::cerr << FileInfo.size() << " files tracked.\n"; |
| 183 | unsigned NumOnceOnlyFiles = 0, MaxNumIncludes = 0, NumSingleIncludedFiles = 0; |
| 184 | for (unsigned i = 0, e = FileInfo.size(); i != e; ++i) { |
| 185 | NumOnceOnlyFiles += FileInfo[i].isImport; |
| 186 | if (MaxNumIncludes < FileInfo[i].NumIncludes) |
| 187 | MaxNumIncludes = FileInfo[i].NumIncludes; |
| 188 | NumSingleIncludedFiles += FileInfo[i].NumIncludes == 1; |
| 189 | } |
| 190 | std::cerr << " " << NumOnceOnlyFiles << " #import/#pragma once files.\n"; |
| 191 | std::cerr << " " << NumSingleIncludedFiles << " included exactly once.\n"; |
| 192 | std::cerr << " " << MaxNumIncludes << " max times a file is included.\n"; |
| 193 | |
| 194 | std::cerr << NumDirectives << " directives found:\n"; |
| 195 | std::cerr << " " << NumDefined << " #define.\n"; |
| 196 | std::cerr << " " << NumUndefined << " #undef.\n"; |
| 197 | std::cerr << " " << NumIncluded << " #include/#include_next/#import.\n"; |
| 198 | std::cerr << " " << NumEnteredSourceFiles << " source files entered.\n"; |
| 199 | std::cerr << " " << MaxIncludeStackDepth << " max include stack depth\n"; |
| 200 | std::cerr << " " << NumIf << " #if/#ifndef/#ifdef.\n"; |
| 201 | std::cerr << " " << NumElse << " #else/#elif.\n"; |
| 202 | std::cerr << " " << NumEndif << " #endif.\n"; |
| 203 | std::cerr << " " << NumPragma << " #pragma.\n"; |
| 204 | std::cerr << NumSkipped << " #if/#ifndef#ifdef regions skipped\n"; |
| 205 | |
| 206 | std::cerr << NumMacroExpanded << " macros expanded, " |
| 207 | << NumFastMacroExpanded << " on the fast path.\n"; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | //===----------------------------------------------------------------------===// |
Chris Lattner | d01e291 | 2006-06-18 16:22:51 +0000 | [diff] [blame] | 211 | // Token Spelling |
| 212 | //===----------------------------------------------------------------------===// |
| 213 | |
| 214 | |
| 215 | /// getSpelling() - Return the 'spelling' of this token. The spelling of a |
| 216 | /// token are the characters used to represent the token in the source file |
| 217 | /// after trigraph expansion and escaped-newline folding. In particular, this |
| 218 | /// wants to get the true, uncanonicalized, spelling of things like digraphs |
| 219 | /// UCNs, etc. |
| 220 | std::string Preprocessor::getSpelling(const LexerToken &Tok) const { |
| 221 | assert((int)Tok.getLength() >= 0 && "Token character range is bogus!"); |
| 222 | |
| 223 | // If this token contains nothing interesting, return it directly. |
Chris Lattner | 50b497e | 2006-06-18 16:32:35 +0000 | [diff] [blame] | 224 | const char *TokStart = SourceMgr.getCharacterData(Tok.getLocation()); |
Chris Lattner | d01e291 | 2006-06-18 16:22:51 +0000 | [diff] [blame] | 225 | assert(TokStart && "Token has invalid location!"); |
| 226 | if (!Tok.needsCleaning()) |
| 227 | return std::string(TokStart, TokStart+Tok.getLength()); |
| 228 | |
| 229 | // Otherwise, hard case, relex the characters into the string. |
| 230 | std::string Result; |
| 231 | Result.reserve(Tok.getLength()); |
| 232 | |
| 233 | for (const char *Ptr = TokStart, *End = TokStart+Tok.getLength(); |
| 234 | Ptr != End; ) { |
| 235 | unsigned CharSize; |
| 236 | Result.push_back(Lexer::getCharAndSizeNoWarn(Ptr, CharSize, Features)); |
| 237 | Ptr += CharSize; |
| 238 | } |
| 239 | assert(Result.size() != unsigned(Tok.getLength()) && |
| 240 | "NeedsCleaning flag set on something that didn't need cleaning!"); |
| 241 | return Result; |
| 242 | } |
| 243 | |
| 244 | /// getSpelling - This method is used to get the spelling of a token into a |
| 245 | /// preallocated buffer, instead of as an std::string. The caller is required |
| 246 | /// to allocate enough space for the token, which is guaranteed to be at least |
| 247 | /// Tok.getLength() bytes long. The actual length of the token is returned. |
| 248 | unsigned Preprocessor::getSpelling(const LexerToken &Tok, char *Buffer) const { |
| 249 | assert((int)Tok.getLength() >= 0 && "Token character range is bogus!"); |
| 250 | |
Chris Lattner | 50b497e | 2006-06-18 16:32:35 +0000 | [diff] [blame] | 251 | const char *TokStart = SourceMgr.getCharacterData(Tok.getLocation()); |
Chris Lattner | d01e291 | 2006-06-18 16:22:51 +0000 | [diff] [blame] | 252 | assert(TokStart && "Token has invalid location!"); |
| 253 | |
| 254 | // If this token contains nothing interesting, return it directly. |
| 255 | if (!Tok.needsCleaning()) { |
| 256 | unsigned Size = Tok.getLength(); |
| 257 | memcpy(Buffer, TokStart, Size); |
| 258 | return Size; |
| 259 | } |
| 260 | // Otherwise, hard case, relex the characters into the string. |
| 261 | std::string Result; |
| 262 | Result.reserve(Tok.getLength()); |
| 263 | |
| 264 | char *OutBuf = Buffer; |
| 265 | for (const char *Ptr = TokStart, *End = TokStart+Tok.getLength(); |
| 266 | Ptr != End; ) { |
| 267 | unsigned CharSize; |
| 268 | *OutBuf++ = Lexer::getCharAndSizeNoWarn(Ptr, CharSize, Features); |
| 269 | Ptr += CharSize; |
| 270 | } |
| 271 | assert(unsigned(OutBuf-Buffer) != Tok.getLength() && |
| 272 | "NeedsCleaning flag set on something that didn't need cleaning!"); |
| 273 | |
| 274 | return OutBuf-Buffer; |
| 275 | } |
| 276 | |
| 277 | //===----------------------------------------------------------------------===// |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 278 | // Source File Location Methods. |
| 279 | //===----------------------------------------------------------------------===// |
| 280 | |
| 281 | |
| 282 | /// LookupFile - Given a "foo" or <foo> reference, look up the indicated file, |
| 283 | /// return null on failure. isAngled indicates whether the file reference is |
| 284 | /// for system #include's or not (i.e. using <> instead of ""). |
| 285 | const FileEntry *Preprocessor::LookupFile(const std::string &Filename, |
Chris Lattner | c899718 | 2006-06-22 05:52:16 +0000 | [diff] [blame] | 286 | bool isAngled, |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 287 | const DirectoryLookup *FromDir, |
Chris Lattner | c899718 | 2006-06-22 05:52:16 +0000 | [diff] [blame] | 288 | const DirectoryLookup *&CurDir) { |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 289 | assert(CurLexer && "Cannot enter a #include inside a macro expansion!"); |
Chris Lattner | c899718 | 2006-06-22 05:52:16 +0000 | [diff] [blame] | 290 | CurDir = 0; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 291 | |
| 292 | // If 'Filename' is absolute, check to see if it exists and no searching. |
| 293 | // FIXME: this should be a sys::Path interface, this doesn't handle things |
| 294 | // like C:\foo.txt right, nor win32 \\network\device\blah. |
| 295 | if (Filename[0] == '/') { |
| 296 | // If this was an #include_next "/absolute/file", fail. |
| 297 | if (FromDir) return 0; |
| 298 | |
| 299 | // Otherwise, just return the file. |
| 300 | return FileMgr.getFile(Filename); |
| 301 | } |
| 302 | |
| 303 | // Step #0, unless disabled, check to see if the file is in the #includer's |
| 304 | // directory. This search is not done for <> headers. |
Chris Lattner | c899718 | 2006-06-22 05:52:16 +0000 | [diff] [blame] | 305 | if (!isAngled && !FromDir && !NoCurDirSearch) { |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 306 | const FileEntry *CurFE = |
| 307 | SourceMgr.getFileEntryForFileID(CurLexer->getCurFileID()); |
| 308 | if (CurFE) { |
Chris Lattner | c899718 | 2006-06-22 05:52:16 +0000 | [diff] [blame] | 309 | // Concatenate the requested file onto the directory. |
| 310 | // FIXME: should be in sys::Path. |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 311 | if (const FileEntry *FE = |
| 312 | FileMgr.getFile(CurFE->getDir()->getName()+"/"+Filename)) { |
Chris Lattner | c899718 | 2006-06-22 05:52:16 +0000 | [diff] [blame] | 313 | if (CurDirLookup) |
| 314 | CurDir = CurDirLookup; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 315 | else |
Chris Lattner | c899718 | 2006-06-22 05:52:16 +0000 | [diff] [blame] | 316 | CurDir = 0; |
| 317 | |
| 318 | // This file is a system header or C++ unfriendly if the old file is. |
| 319 | getFileInfo(FE).DirInfo = getFileInfo(CurFE).DirInfo; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 320 | return FE; |
| 321 | } |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | // If this is a system #include, ignore the user #include locs. |
Chris Lattner | c899718 | 2006-06-22 05:52:16 +0000 | [diff] [blame] | 326 | unsigned i = isAngled ? SystemDirIdx : 0; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 327 | |
| 328 | // If this is a #include_next request, start searching after the directory the |
| 329 | // file was found in. |
| 330 | if (FromDir) |
| 331 | i = FromDir-&SearchDirs[0]; |
| 332 | |
| 333 | // Check each directory in sequence to see if it contains this file. |
| 334 | for (; i != SearchDirs.size(); ++i) { |
| 335 | // Concatenate the requested file onto the directory. |
| 336 | // FIXME: should be in sys::Path. |
| 337 | if (const FileEntry *FE = |
| 338 | FileMgr.getFile(SearchDirs[i].getDir()->getName()+"/"+Filename)) { |
Chris Lattner | c899718 | 2006-06-22 05:52:16 +0000 | [diff] [blame] | 339 | CurDir = &SearchDirs[i]; |
| 340 | |
| 341 | // This file is a system header or C++ unfriendly if the dir is. |
| 342 | getFileInfo(FE).DirInfo = CurDir->getDirCharacteristic(); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 343 | return FE; |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | // Otherwise, didn't find it. |
| 348 | return 0; |
| 349 | } |
| 350 | |
| 351 | /// EnterSourceFile - Add a source file to the top of the include stack and |
| 352 | /// start lexing tokens from it instead of the current buffer. Return true |
| 353 | /// on failure. |
| 354 | void Preprocessor::EnterSourceFile(unsigned FileID, |
Chris Lattner | c899718 | 2006-06-22 05:52:16 +0000 | [diff] [blame] | 355 | const DirectoryLookup *CurDir) { |
Chris Lattner | 69772b0 | 2006-07-02 20:34:39 +0000 | [diff] [blame^] | 356 | assert(CurMacroExpander == 0 && "Cannot #include a file inside a macro!"); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 357 | ++NumEnteredSourceFiles; |
| 358 | |
Chris Lattner | 69772b0 | 2006-07-02 20:34:39 +0000 | [diff] [blame^] | 359 | if (MaxIncludeStackDepth < IncludeMacroStack.size()) |
| 360 | MaxIncludeStackDepth = IncludeMacroStack.size(); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 361 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 362 | const SourceBuffer *Buffer = SourceMgr.getBuffer(FileID); |
Chris Lattner | 69772b0 | 2006-07-02 20:34:39 +0000 | [diff] [blame^] | 363 | Lexer *TheLexer = new Lexer(Buffer, FileID, *this); |
| 364 | EnterSourceFileWithLexer(TheLexer, CurDir); |
| 365 | } |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 366 | |
Chris Lattner | 69772b0 | 2006-07-02 20:34:39 +0000 | [diff] [blame^] | 367 | /// EnterSourceFile - Add a source file to the top of the include stack and |
| 368 | /// start lexing tokens from it instead of the current buffer. |
| 369 | void Preprocessor::EnterSourceFileWithLexer(Lexer *TheLexer, |
| 370 | const DirectoryLookup *CurDir) { |
| 371 | |
| 372 | // Add the current lexer to the include stack. |
| 373 | if (CurLexer || CurMacroExpander) |
| 374 | IncludeMacroStack.push_back(IncludeStackInfo(CurLexer, CurDirLookup, |
| 375 | CurMacroExpander)); |
| 376 | |
| 377 | CurLexer = TheLexer; |
Chris Lattner | c899718 | 2006-06-22 05:52:16 +0000 | [diff] [blame] | 378 | CurDirLookup = CurDir; |
Chris Lattner | 69772b0 | 2006-07-02 20:34:39 +0000 | [diff] [blame^] | 379 | CurMacroExpander = 0; |
Chris Lattner | 0c885f5 | 2006-06-21 06:50:18 +0000 | [diff] [blame] | 380 | |
| 381 | // Notify the client, if desired, that we are in a new source file. |
Chris Lattner | c899718 | 2006-06-22 05:52:16 +0000 | [diff] [blame] | 382 | if (FileChangeHandler) { |
| 383 | DirectoryLookup::DirType FileType = DirectoryLookup::NormalHeaderDir; |
| 384 | |
| 385 | // Get the file entry for the current file. |
| 386 | if (const FileEntry *FE = |
| 387 | SourceMgr.getFileEntryForFileID(CurLexer->getCurFileID())) |
| 388 | FileType = getFileInfo(FE).DirInfo; |
| 389 | |
Chris Lattner | 55a6095 | 2006-06-25 04:20:34 +0000 | [diff] [blame] | 390 | FileChangeHandler(CurLexer->getSourceLocation(CurLexer->BufferStart), |
| 391 | EnterFile, FileType); |
Chris Lattner | c899718 | 2006-06-22 05:52:16 +0000 | [diff] [blame] | 392 | } |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 393 | } |
| 394 | |
Chris Lattner | 69772b0 | 2006-07-02 20:34:39 +0000 | [diff] [blame^] | 395 | |
| 396 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 397 | /// EnterMacro - Add a Macro to the top of the include stack and start lexing |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 398 | /// tokens from it instead of the current buffer. |
| 399 | void Preprocessor::EnterMacro(LexerToken &Tok) { |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 400 | IdentifierTokenInfo *Identifier = Tok.getIdentifierInfo(); |
| 401 | MacroInfo &MI = *Identifier->getMacroInfo(); |
Chris Lattner | 69772b0 | 2006-07-02 20:34:39 +0000 | [diff] [blame^] | 402 | IncludeMacroStack.push_back(IncludeStackInfo(CurLexer, CurDirLookup, |
| 403 | CurMacroExpander)); |
| 404 | CurLexer = 0; |
| 405 | CurDirLookup = 0; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 406 | |
| 407 | // TODO: Figure out arguments. |
| 408 | |
| 409 | // Mark the macro as currently disabled, so that it is not recursively |
| 410 | // expanded. |
| 411 | MI.DisableMacro(); |
Chris Lattner | d01e291 | 2006-06-18 16:22:51 +0000 | [diff] [blame] | 412 | CurMacroExpander = new MacroExpander(Tok, *this); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 413 | } |
| 414 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 415 | //===----------------------------------------------------------------------===// |
Chris Lattner | 677757a | 2006-06-28 05:26:32 +0000 | [diff] [blame] | 416 | // Macro Expansion Handling. |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 417 | //===----------------------------------------------------------------------===// |
| 418 | |
Chris Lattner | 0b8cfc2 | 2006-06-28 06:49:17 +0000 | [diff] [blame] | 419 | /// RegisterBuiltinMacro - Register the specified identifier in the identifier |
| 420 | /// table and mark it as a builtin macro to be expanded. |
| 421 | IdentifierTokenInfo *Preprocessor::RegisterBuiltinMacro(const char *Name) { |
| 422 | // Get the identifier. |
| 423 | IdentifierTokenInfo *Id = getIdentifierInfo(Name); |
| 424 | |
| 425 | // Mark it as being a macro that is builtin. |
| 426 | MacroInfo *MI = new MacroInfo(SourceLocation()); |
| 427 | MI->setIsBuiltinMacro(); |
| 428 | Id->setMacroInfo(MI); |
| 429 | return Id; |
| 430 | } |
| 431 | |
| 432 | |
Chris Lattner | 677757a | 2006-06-28 05:26:32 +0000 | [diff] [blame] | 433 | /// RegisterBuiltinMacros - Register builtin macros, such as __LINE__ with the |
| 434 | /// identifier table. |
| 435 | void Preprocessor::RegisterBuiltinMacros() { |
Chris Lattner | 0b8cfc2 | 2006-06-28 06:49:17 +0000 | [diff] [blame] | 436 | Ident__LINE__ = RegisterBuiltinMacro("__LINE__"); |
Chris Lattner | 630b33c | 2006-07-01 22:46:53 +0000 | [diff] [blame] | 437 | Ident__FILE__ = RegisterBuiltinMacro("__FILE__"); |
Chris Lattner | c673f90 | 2006-06-30 06:10:41 +0000 | [diff] [blame] | 438 | Ident__DATE__ = RegisterBuiltinMacro("__DATE__"); |
| 439 | Ident__TIME__ = RegisterBuiltinMacro("__TIME__"); |
Chris Lattner | 69772b0 | 2006-07-02 20:34:39 +0000 | [diff] [blame^] | 440 | Ident_Pragma = RegisterBuiltinMacro("_Pragma"); |
Chris Lattner | c1283b9 | 2006-07-01 23:16:30 +0000 | [diff] [blame] | 441 | |
| 442 | // GCC Extensions. |
| 443 | Ident__BASE_FILE__ = RegisterBuiltinMacro("__BASE_FILE__"); |
| 444 | Ident__INCLUDE_LEVEL__ = RegisterBuiltinMacro("__INCLUDE_LEVEL__"); |
Chris Lattner | 847e0e4 | 2006-07-01 23:49:16 +0000 | [diff] [blame] | 445 | Ident__TIMESTAMP__ = RegisterBuiltinMacro("__TIMESTAMP__"); |
Chris Lattner | c1283b9 | 2006-07-01 23:16:30 +0000 | [diff] [blame] | 446 | |
Chris Lattner | 69772b0 | 2006-07-02 20:34:39 +0000 | [diff] [blame^] | 447 | // FIXME: implement them all: |
Chris Lattner | c1283b9 | 2006-07-01 23:16:30 +0000 | [diff] [blame] | 448 | //Pseudo #defines. |
| 449 | // __STDC__ 1 if !stdc_0_in_system_headers and "std" |
| 450 | // __STDC_VERSION__ |
| 451 | // __STDC_HOSTED__ |
| 452 | // __OBJC__ |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 453 | } |
| 454 | |
Chris Lattner | 677757a | 2006-06-28 05:26:32 +0000 | [diff] [blame] | 455 | |
Chris Lattner | f373a4a | 2006-06-26 06:16:29 +0000 | [diff] [blame] | 456 | /// HandleMacroExpandedIdentifier - If an identifier token is read that is to be |
| 457 | /// expanded as a macro, handle it and return the next token as 'Identifier'. |
| 458 | void Preprocessor::HandleMacroExpandedIdentifier(LexerToken &Identifier, |
| 459 | MacroInfo *MI) { |
| 460 | ++NumMacroExpanded; |
Chris Lattner | 0b8cfc2 | 2006-06-28 06:49:17 +0000 | [diff] [blame] | 461 | |
| 462 | // If this is a builtin macro, like __LINE__ or _Pragma, handle it specially. |
| 463 | if (MI->isBuiltinMacro()) |
Chris Lattner | 69772b0 | 2006-07-02 20:34:39 +0000 | [diff] [blame^] | 464 | return ExpandBuiltinMacro(Identifier); |
| 465 | |
| 466 | // If we started lexing a macro, enter the macro expansion body. |
| 467 | // FIXME: Read/Validate the argument list here! |
| 468 | |
Chris Lattner | f373a4a | 2006-06-26 06:16:29 +0000 | [diff] [blame] | 469 | |
| 470 | // If this macro expands to no tokens, don't bother to push it onto the |
| 471 | // expansion stack, only to take it right back off. |
| 472 | if (MI->getNumTokens() == 0) { |
| 473 | // Ignore this macro use, just return the next token in the current |
| 474 | // buffer. |
| 475 | bool HadLeadingSpace = Identifier.hasLeadingSpace(); |
| 476 | bool IsAtStartOfLine = Identifier.isAtStartOfLine(); |
| 477 | |
| 478 | Lex(Identifier); |
| 479 | |
| 480 | // If the identifier isn't on some OTHER line, inherit the leading |
| 481 | // whitespace/first-on-a-line property of this token. This handles |
| 482 | // stuff like "! XX," -> "! ," and " XX," -> " ,", when XX is |
| 483 | // empty. |
| 484 | if (!Identifier.isAtStartOfLine()) { |
| 485 | if (IsAtStartOfLine) Identifier.SetFlag(LexerToken::StartOfLine); |
| 486 | if (HadLeadingSpace) Identifier.SetFlag(LexerToken::LeadingSpace); |
| 487 | } |
| 488 | ++NumFastMacroExpanded; |
| 489 | return; |
| 490 | |
| 491 | } else if (MI->getNumTokens() == 1 && |
| 492 | // Don't handle identifiers if they need recursive expansion. |
| 493 | (MI->getReplacementToken(0).getIdentifierInfo() == 0 || |
| 494 | !MI->getReplacementToken(0).getIdentifierInfo()->getMacroInfo())){ |
| 495 | // FIXME: Function-style macros only if no arguments? |
| 496 | |
| 497 | // Otherwise, if this macro expands into a single trivially-expanded |
| 498 | // token: expand it now. This handles common cases like |
| 499 | // "#define VAL 42". |
| 500 | |
| 501 | // Propagate the isAtStartOfLine/hasLeadingSpace markers of the macro |
| 502 | // identifier to the expanded token. |
| 503 | bool isAtStartOfLine = Identifier.isAtStartOfLine(); |
| 504 | bool hasLeadingSpace = Identifier.hasLeadingSpace(); |
| 505 | |
| 506 | // Remember where the token is instantiated. |
| 507 | SourceLocation InstantiateLoc = Identifier.getLocation(); |
| 508 | |
| 509 | // Replace the result token. |
| 510 | Identifier = MI->getReplacementToken(0); |
| 511 | |
| 512 | // Restore the StartOfLine/LeadingSpace markers. |
| 513 | Identifier.SetFlagValue(LexerToken::StartOfLine , isAtStartOfLine); |
| 514 | Identifier.SetFlagValue(LexerToken::LeadingSpace, hasLeadingSpace); |
| 515 | |
| 516 | // Update the tokens location to include both its logical and physical |
| 517 | // locations. |
| 518 | SourceLocation Loc = |
Chris Lattner | c673f90 | 2006-06-30 06:10:41 +0000 | [diff] [blame] | 519 | SourceMgr.getInstantiationLoc(Identifier.getLocation(), InstantiateLoc); |
Chris Lattner | f373a4a | 2006-06-26 06:16:29 +0000 | [diff] [blame] | 520 | Identifier.SetLocation(Loc); |
| 521 | |
| 522 | // Since this is not an identifier token, it can't be macro expanded, so |
| 523 | // we're done. |
| 524 | ++NumFastMacroExpanded; |
| 525 | return; |
| 526 | } |
| 527 | |
| 528 | // Start expanding the macro (FIXME, pass arguments). |
| 529 | EnterMacro(Identifier); |
| 530 | |
| 531 | // Now that the macro is at the top of the include stack, ask the |
| 532 | // preprocessor to read the next token from it. |
| 533 | return Lex(Identifier); |
| 534 | } |
| 535 | |
Chris Lattner | c673f90 | 2006-06-30 06:10:41 +0000 | [diff] [blame] | 536 | /// ComputeDATE_TIME - Compute the current time, enter it into the specified |
| 537 | /// scratch buffer, then return DATELoc/TIMELoc locations with the position of |
| 538 | /// the identifier tokens inserted. |
| 539 | static void ComputeDATE_TIME(SourceLocation &DATELoc, SourceLocation &TIMELoc, |
| 540 | ScratchBuffer *ScratchBuf) { |
| 541 | time_t TT = time(0); |
| 542 | struct tm *TM = localtime(&TT); |
| 543 | |
| 544 | static const char * const Months[] = { |
| 545 | "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec" |
| 546 | }; |
| 547 | |
| 548 | char TmpBuffer[100]; |
| 549 | sprintf(TmpBuffer, "\"%s %2d %4d\"", Months[TM->tm_mon], TM->tm_mday, |
| 550 | TM->tm_year+1900); |
| 551 | DATELoc = ScratchBuf->getToken(TmpBuffer, strlen(TmpBuffer)); |
| 552 | |
| 553 | sprintf(TmpBuffer, "\"%02d:%02d:%02d\"", TM->tm_hour, TM->tm_min, TM->tm_sec); |
| 554 | TIMELoc = ScratchBuf->getToken(TmpBuffer, strlen(TmpBuffer)); |
| 555 | } |
| 556 | |
Chris Lattner | 0b8cfc2 | 2006-06-28 06:49:17 +0000 | [diff] [blame] | 557 | /// ExpandBuiltinMacro - If an identifier token is read that is to be expanded |
| 558 | /// as a builtin macro, handle it and return the next token as 'Tok'. |
Chris Lattner | 69772b0 | 2006-07-02 20:34:39 +0000 | [diff] [blame^] | 559 | void Preprocessor::ExpandBuiltinMacro(LexerToken &Tok) { |
Chris Lattner | 0b8cfc2 | 2006-06-28 06:49:17 +0000 | [diff] [blame] | 560 | // Figure out which token this is. |
| 561 | IdentifierTokenInfo *ITI = Tok.getIdentifierInfo(); |
| 562 | assert(ITI && "Can't be a macro without id info!"); |
Chris Lattner | 69772b0 | 2006-07-02 20:34:39 +0000 | [diff] [blame^] | 563 | |
| 564 | // If this is an _Pragma directive, expand it, invoke the pragma handler, then |
| 565 | // lex the token after it. |
| 566 | if (ITI == Ident_Pragma) |
| 567 | return Handle_Pragma(Tok); |
| 568 | |
Chris Lattner | 0b8cfc2 | 2006-06-28 06:49:17 +0000 | [diff] [blame] | 569 | char TmpBuffer[100]; |
Chris Lattner | 69772b0 | 2006-07-02 20:34:39 +0000 | [diff] [blame^] | 570 | |
| 571 | // Set up the return result. |
Chris Lattner | 630b33c | 2006-07-01 22:46:53 +0000 | [diff] [blame] | 572 | Tok.SetIdentifierInfo(0); |
| 573 | Tok.ClearFlag(LexerToken::NeedsCleaning); |
| 574 | |
Chris Lattner | 0b8cfc2 | 2006-06-28 06:49:17 +0000 | [diff] [blame] | 575 | if (ITI == Ident__LINE__) { |
| 576 | // __LINE__ expands to a simple numeric value. |
| 577 | sprintf(TmpBuffer, "%u", SourceMgr.getLineNumber(Tok.getLocation())); |
| 578 | unsigned Length = strlen(TmpBuffer); |
| 579 | Tok.SetKind(tok::numeric_constant); |
| 580 | Tok.SetLength(Length); |
| 581 | Tok.SetLocation(ScratchBuf->getToken(TmpBuffer, Length, Tok.getLocation())); |
Chris Lattner | c1283b9 | 2006-07-01 23:16:30 +0000 | [diff] [blame] | 582 | } else if (ITI == Ident__FILE__ || ITI == Ident__BASE_FILE__) { |
| 583 | SourceLocation Loc = Tok.getLocation(); |
| 584 | if (ITI == Ident__BASE_FILE__) { |
| 585 | Diag(Tok, diag::ext_pp_base_file); |
| 586 | SourceLocation NextLoc = SourceMgr.getIncludeLoc(Loc.getFileID()); |
| 587 | while (NextLoc.getFileID() != 0) { |
| 588 | Loc = NextLoc; |
| 589 | NextLoc = SourceMgr.getIncludeLoc(Loc.getFileID()); |
| 590 | } |
| 591 | } |
| 592 | |
| 593 | // FIXME: Escape this filename correctly. |
| 594 | std::string FN = '"' + SourceMgr.getSourceName(Loc) + '"'; |
Chris Lattner | 630b33c | 2006-07-01 22:46:53 +0000 | [diff] [blame] | 595 | Tok.SetKind(tok::string_literal); |
| 596 | Tok.SetLength(FN.size()); |
| 597 | Tok.SetLocation(ScratchBuf->getToken(&FN[0], FN.size(), Tok.getLocation())); |
Chris Lattner | c673f90 | 2006-06-30 06:10:41 +0000 | [diff] [blame] | 598 | } else if (ITI == Ident__DATE__) { |
| 599 | if (!DATELoc.isValid()) |
| 600 | ComputeDATE_TIME(DATELoc, TIMELoc, ScratchBuf); |
| 601 | Tok.SetKind(tok::string_literal); |
| 602 | Tok.SetLength(strlen("\"Mmm dd yyyy\"")); |
| 603 | Tok.SetLocation(SourceMgr.getInstantiationLoc(DATELoc, Tok.getLocation())); |
Chris Lattner | c673f90 | 2006-06-30 06:10:41 +0000 | [diff] [blame] | 604 | } else if (ITI == Ident__TIME__) { |
| 605 | if (!TIMELoc.isValid()) |
| 606 | ComputeDATE_TIME(DATELoc, TIMELoc, ScratchBuf); |
| 607 | Tok.SetKind(tok::string_literal); |
| 608 | Tok.SetLength(strlen("\"hh:mm:ss\"")); |
| 609 | Tok.SetLocation(SourceMgr.getInstantiationLoc(TIMELoc, Tok.getLocation())); |
Chris Lattner | c1283b9 | 2006-07-01 23:16:30 +0000 | [diff] [blame] | 610 | } else if (ITI == Ident__INCLUDE_LEVEL__) { |
| 611 | Diag(Tok, diag::ext_pp_include_level); |
| 612 | |
| 613 | // Compute the include depth of this token. |
| 614 | unsigned Depth = 0; |
| 615 | SourceLocation Loc = SourceMgr.getIncludeLoc(Tok.getLocation().getFileID()); |
| 616 | for (; Loc.getFileID() != 0; ++Depth) |
| 617 | Loc = SourceMgr.getIncludeLoc(Loc.getFileID()); |
| 618 | |
| 619 | // __INCLUDE_LEVEL__ expands to a simple numeric value. |
| 620 | sprintf(TmpBuffer, "%u", Depth); |
| 621 | unsigned Length = strlen(TmpBuffer); |
| 622 | Tok.SetKind(tok::numeric_constant); |
| 623 | Tok.SetLength(Length); |
| 624 | Tok.SetLocation(ScratchBuf->getToken(TmpBuffer, Length, Tok.getLocation())); |
Chris Lattner | 847e0e4 | 2006-07-01 23:49:16 +0000 | [diff] [blame] | 625 | } else if (ITI == Ident__TIMESTAMP__) { |
| 626 | // MSVC, ICC, GCC, VisualAge C++ extension. The generated string should be |
| 627 | // of the form "Ddd Mmm dd hh::mm::ss yyyy", which is returned by asctime. |
| 628 | Diag(Tok, diag::ext_pp_timestamp); |
| 629 | |
| 630 | // Get the file that we are lexing out of. If we're currently lexing from |
| 631 | // a macro, dig into the include stack. |
| 632 | const FileEntry *CurFile = 0; |
Chris Lattner | 69772b0 | 2006-07-02 20:34:39 +0000 | [diff] [blame^] | 633 | Lexer *TheLexer = getCurrentLexer(); |
Chris Lattner | 847e0e4 | 2006-07-01 23:49:16 +0000 | [diff] [blame] | 634 | |
| 635 | if (TheLexer) |
| 636 | CurFile = SourceMgr.getFileEntryForFileID(TheLexer->getCurFileID()); |
| 637 | |
| 638 | // If this file is older than the file it depends on, emit a diagnostic. |
| 639 | const char *Result; |
| 640 | if (CurFile) { |
| 641 | time_t TT = CurFile->getModificationTime(); |
| 642 | struct tm *TM = localtime(&TT); |
| 643 | Result = asctime(TM); |
| 644 | } else { |
| 645 | Result = "??? ??? ?? ??:??:?? ????\n"; |
| 646 | } |
| 647 | TmpBuffer[0] = '"'; |
| 648 | strcpy(TmpBuffer+1, Result); |
| 649 | unsigned Len = strlen(TmpBuffer); |
| 650 | TmpBuffer[Len-1] = '"'; // Replace the newline with a quote. |
| 651 | Tok.SetKind(tok::string_literal); |
| 652 | Tok.SetLength(Len); |
| 653 | Tok.SetLocation(ScratchBuf->getToken(TmpBuffer, Len, Tok.getLocation())); |
Chris Lattner | 0b8cfc2 | 2006-06-28 06:49:17 +0000 | [diff] [blame] | 654 | } else { |
| 655 | assert(0 && "Unknown identifier!"); |
| 656 | } |
| 657 | } |
Chris Lattner | 677757a | 2006-06-28 05:26:32 +0000 | [diff] [blame] | 658 | |
| 659 | //===----------------------------------------------------------------------===// |
| 660 | // Lexer Event Handling. |
| 661 | //===----------------------------------------------------------------------===// |
| 662 | |
| 663 | /// HandleIdentifier - This callback is invoked when the lexer reads an |
| 664 | /// identifier. This callback looks up the identifier in the map and/or |
| 665 | /// potentially macro expands it or turns it into a named token (like 'for'). |
| 666 | void Preprocessor::HandleIdentifier(LexerToken &Identifier) { |
| 667 | if (Identifier.getIdentifierInfo() == 0) { |
| 668 | // If we are skipping tokens (because we are in a #if 0 block), there will |
| 669 | // be no identifier info, just return the token. |
| 670 | assert(isSkipping() && "Token isn't an identifier?"); |
| 671 | return; |
| 672 | } |
| 673 | IdentifierTokenInfo &ITI = *Identifier.getIdentifierInfo(); |
| 674 | |
| 675 | // If this identifier was poisoned, and if it was not produced from a macro |
| 676 | // expansion, emit an error. |
| 677 | if (ITI.isPoisoned() && CurLexer) |
| 678 | Diag(Identifier, diag::err_pp_used_poisoned_id); |
| 679 | |
| 680 | if (MacroInfo *MI = ITI.getMacroInfo()) |
| 681 | if (MI->isEnabled() && !DisableMacroExpansion) |
| 682 | return HandleMacroExpandedIdentifier(Identifier, MI); |
| 683 | |
| 684 | // Change the kind of this identifier to the appropriate token kind, e.g. |
| 685 | // turning "for" into a keyword. |
| 686 | Identifier.SetKind(ITI.getTokenID()); |
| 687 | |
| 688 | // If this is an extension token, diagnose its use. |
| 689 | if (ITI.isExtensionToken()) Diag(Identifier, diag::ext_token_used); |
| 690 | } |
| 691 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 692 | /// HandleEndOfFile - This callback is invoked when the lexer hits the end of |
| 693 | /// the current file. This either returns the EOF token or pops a level off |
| 694 | /// the include stack and keeps going. |
Chris Lattner | 0c885f5 | 2006-06-21 06:50:18 +0000 | [diff] [blame] | 695 | void Preprocessor::HandleEndOfFile(LexerToken &Result, bool isEndOfMacro) { |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 696 | assert(!CurMacroExpander && |
| 697 | "Ending a file when currently in a macro!"); |
| 698 | |
| 699 | // If we are in a #if 0 block skipping tokens, and we see the end of the file, |
| 700 | // this is an error condition. Just return the EOF token up to |
| 701 | // SkipExcludedConditionalBlock. The Lexer will have already have issued |
| 702 | // errors for the unterminated #if's on the conditional stack. |
| 703 | if (isSkipping()) { |
Chris Lattner | d01e291 | 2006-06-18 16:22:51 +0000 | [diff] [blame] | 704 | Result.StartToken(); |
| 705 | CurLexer->BufferPtr = CurLexer->BufferEnd; |
| 706 | CurLexer->FormTokenWithChars(Result, CurLexer->BufferEnd); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 707 | Result.SetKind(tok::eof); |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 708 | return; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 709 | } |
| 710 | |
| 711 | // If this is a #include'd file, pop it off the include stack and continue |
| 712 | // lexing the #includer file. |
Chris Lattner | 69772b0 | 2006-07-02 20:34:39 +0000 | [diff] [blame^] | 713 | if (!IncludeMacroStack.empty()) { |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 714 | // We're done with the #included file. |
| 715 | delete CurLexer; |
Chris Lattner | 69772b0 | 2006-07-02 20:34:39 +0000 | [diff] [blame^] | 716 | CurLexer = IncludeMacroStack.back().TheLexer; |
| 717 | CurDirLookup = IncludeMacroStack.back().TheDirLookup; |
| 718 | CurMacroExpander = IncludeMacroStack.back().TheMacroExpander; |
| 719 | IncludeMacroStack.pop_back(); |
Chris Lattner | 0c885f5 | 2006-06-21 06:50:18 +0000 | [diff] [blame] | 720 | |
| 721 | // Notify the client, if desired, that we are in a new source file. |
Chris Lattner | 69772b0 | 2006-07-02 20:34:39 +0000 | [diff] [blame^] | 722 | if (FileChangeHandler && !isEndOfMacro && CurLexer) { |
Chris Lattner | c899718 | 2006-06-22 05:52:16 +0000 | [diff] [blame] | 723 | DirectoryLookup::DirType FileType = DirectoryLookup::NormalHeaderDir; |
| 724 | |
| 725 | // Get the file entry for the current file. |
| 726 | if (const FileEntry *FE = |
| 727 | SourceMgr.getFileEntryForFileID(CurLexer->getCurFileID())) |
| 728 | FileType = getFileInfo(FE).DirInfo; |
| 729 | |
Chris Lattner | 0c885f5 | 2006-06-21 06:50:18 +0000 | [diff] [blame] | 730 | FileChangeHandler(CurLexer->getSourceLocation(CurLexer->BufferPtr), |
Chris Lattner | 55a6095 | 2006-06-25 04:20:34 +0000 | [diff] [blame] | 731 | ExitFile, FileType); |
Chris Lattner | c899718 | 2006-06-22 05:52:16 +0000 | [diff] [blame] | 732 | } |
Chris Lattner | 0c885f5 | 2006-06-21 06:50:18 +0000 | [diff] [blame] | 733 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 734 | return Lex(Result); |
| 735 | } |
| 736 | |
Chris Lattner | d01e291 | 2006-06-18 16:22:51 +0000 | [diff] [blame] | 737 | Result.StartToken(); |
| 738 | CurLexer->BufferPtr = CurLexer->BufferEnd; |
| 739 | CurLexer->FormTokenWithChars(Result, CurLexer->BufferEnd); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 740 | Result.SetKind(tok::eof); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 741 | |
| 742 | // We're done with the #included file. |
| 743 | delete CurLexer; |
| 744 | CurLexer = 0; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 745 | } |
| 746 | |
| 747 | /// HandleEndOfMacro - This callback is invoked when the lexer hits the end of |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 748 | /// the current macro line. |
| 749 | void Preprocessor::HandleEndOfMacro(LexerToken &Result) { |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 750 | assert(CurMacroExpander && !CurLexer && |
| 751 | "Ending a macro when currently in a #include file!"); |
| 752 | |
| 753 | // Mark macro not ignored now that it is no longer being expanded. |
| 754 | CurMacroExpander->getMacro().EnableMacro(); |
| 755 | delete CurMacroExpander; |
| 756 | |
Chris Lattner | 69772b0 | 2006-07-02 20:34:39 +0000 | [diff] [blame^] | 757 | // Handle this like a #include file being popped off the stack. |
| 758 | CurMacroExpander = 0; |
| 759 | return HandleEndOfFile(Result, true); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 760 | } |
| 761 | |
| 762 | |
| 763 | //===----------------------------------------------------------------------===// |
| 764 | // Utility Methods for Preprocessor Directive Handling. |
| 765 | //===----------------------------------------------------------------------===// |
| 766 | |
| 767 | /// DiscardUntilEndOfDirective - Read and discard all tokens remaining on the |
| 768 | /// current line until the tok::eom token is found. |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 769 | void Preprocessor::DiscardUntilEndOfDirective() { |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 770 | LexerToken Tmp; |
| 771 | do { |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 772 | LexUnexpandedToken(Tmp); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 773 | } while (Tmp.getKind() != tok::eom); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 774 | } |
| 775 | |
| 776 | /// ReadMacroName - Lex and validate a macro name, which occurs after a |
| 777 | /// #define or #undef. This sets the token kind to eom and discards the rest |
| 778 | /// of the macro line if the macro name is invalid. |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 779 | void Preprocessor::ReadMacroName(LexerToken &MacroNameTok) { |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 780 | // Read the token, don't allow macro expansion on it. |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 781 | LexUnexpandedToken(MacroNameTok); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 782 | |
| 783 | // Missing macro name? |
| 784 | if (MacroNameTok.getKind() == tok::eom) |
| 785 | return Diag(MacroNameTok, diag::err_pp_missing_macro_name); |
| 786 | |
| 787 | if (MacroNameTok.getIdentifierInfo() == 0) { |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 788 | Diag(MacroNameTok, diag::err_pp_macro_not_identifier); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 789 | // Fall through on error. |
| 790 | } else if (0) { |
| 791 | // FIXME: Error if defining a C++ named operator. |
| 792 | |
| 793 | } else if (0) { |
| 794 | // FIXME: Error if defining "defined", "__DATE__", and other predef macros |
| 795 | // in C99 6.10.8.4. |
| 796 | } else { |
| 797 | // Okay, we got a good identifier node. Return it. |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 798 | return; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 799 | } |
| 800 | |
| 801 | |
| 802 | // Invalid macro name, read and discard the rest of the line. Then set the |
| 803 | // token kind to tok::eom. |
| 804 | MacroNameTok.SetKind(tok::eom); |
| 805 | return DiscardUntilEndOfDirective(); |
| 806 | } |
| 807 | |
| 808 | /// CheckEndOfDirective - Ensure that the next token is a tok::eom token. If |
| 809 | /// not, emit a diagnostic and consume up until the eom. |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 810 | void Preprocessor::CheckEndOfDirective(const char *DirType) { |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 811 | LexerToken Tmp; |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 812 | Lex(Tmp); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 813 | // There should be no tokens after the directive, but we allow them as an |
| 814 | // extension. |
| 815 | if (Tmp.getKind() != tok::eom) { |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 816 | Diag(Tmp, diag::ext_pp_extra_tokens_at_eol, DirType); |
| 817 | DiscardUntilEndOfDirective(); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 818 | } |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 819 | } |
| 820 | |
| 821 | |
| 822 | |
| 823 | /// SkipExcludedConditionalBlock - We just read a #if or related directive and |
| 824 | /// decided that the subsequent tokens are in the #if'd out portion of the |
| 825 | /// file. Lex the rest of the file, until we see an #endif. If |
| 826 | /// FoundNonSkipPortion is true, then we have already emitted code for part of |
| 827 | /// this #if directive, so #else/#elif blocks should never be entered. If ElseOk |
| 828 | /// is true, then #else directives are ok, if not, then we have already seen one |
| 829 | /// so a #else directive is a duplicate. When this returns, the caller can lex |
| 830 | /// the first valid token. |
Chris Lattner | d01e291 | 2006-06-18 16:22:51 +0000 | [diff] [blame] | 831 | void Preprocessor::SkipExcludedConditionalBlock(SourceLocation IfTokenLoc, |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 832 | bool FoundNonSkipPortion, |
| 833 | bool FoundElse) { |
| 834 | ++NumSkipped; |
Chris Lattner | 69772b0 | 2006-07-02 20:34:39 +0000 | [diff] [blame^] | 835 | assert(CurMacroExpander == 0 && CurLexer && |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 836 | "Lexing a macro, not a file?"); |
| 837 | |
| 838 | CurLexer->pushConditionalLevel(IfTokenLoc, /*isSkipping*/false, |
| 839 | FoundNonSkipPortion, FoundElse); |
| 840 | |
| 841 | // Know that we are going to be skipping tokens. Set this flag to indicate |
| 842 | // this, which has a couple of effects: |
| 843 | // 1. If EOF of the current lexer is found, the include stack isn't popped. |
| 844 | // 2. Identifier information is not looked up for identifier tokens. As an |
| 845 | // effect of this, implicit macro expansion is naturally disabled. |
| 846 | // 3. "#" tokens at the start of a line are treated as normal tokens, not |
| 847 | // implicitly transformed by the lexer. |
| 848 | // 4. All notes, warnings, and extension messages are disabled. |
| 849 | // |
| 850 | SkippingContents = true; |
| 851 | LexerToken Tok; |
| 852 | while (1) { |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 853 | CurLexer->Lex(Tok); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 854 | |
| 855 | // If this is the end of the buffer, we have an error. The lexer will have |
| 856 | // already handled this error condition, so just return and let the caller |
| 857 | // lex after this #include. |
| 858 | if (Tok.getKind() == tok::eof) break; |
| 859 | |
| 860 | // If this token is not a preprocessor directive, just skip it. |
| 861 | if (Tok.getKind() != tok::hash || !Tok.isAtStartOfLine()) |
| 862 | continue; |
| 863 | |
| 864 | // We just parsed a # character at the start of a line, so we're in |
| 865 | // directive mode. Tell the lexer this so any newlines we see will be |
| 866 | // converted into an EOM token (this terminates the macro). |
| 867 | CurLexer->ParsingPreprocessorDirective = true; |
| 868 | |
| 869 | // Read the next token, the directive flavor. |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 870 | LexUnexpandedToken(Tok); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 871 | |
| 872 | // If this isn't an identifier directive (e.g. is "# 1\n" or "#\n", or |
| 873 | // something bogus), skip it. |
| 874 | if (Tok.getKind() != tok::identifier) { |
| 875 | CurLexer->ParsingPreprocessorDirective = false; |
| 876 | continue; |
| 877 | } |
Chris Lattner | e60165f | 2006-06-22 06:36:29 +0000 | [diff] [blame] | 878 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 879 | // If the first letter isn't i or e, it isn't intesting to us. We know that |
| 880 | // this is safe in the face of spelling differences, because there is no way |
| 881 | // to spell an i/e in a strange way that is another letter. Skipping this |
Chris Lattner | e60165f | 2006-06-22 06:36:29 +0000 | [diff] [blame] | 882 | // allows us to avoid looking up the identifier info for #define/#undef and |
| 883 | // other common directives. |
| 884 | const char *RawCharData = SourceMgr.getCharacterData(Tok.getLocation()); |
| 885 | char FirstChar = RawCharData[0]; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 886 | if (FirstChar >= 'a' && FirstChar <= 'z' && |
| 887 | FirstChar != 'i' && FirstChar != 'e') { |
| 888 | CurLexer->ParsingPreprocessorDirective = false; |
| 889 | continue; |
| 890 | } |
| 891 | |
Chris Lattner | e60165f | 2006-06-22 06:36:29 +0000 | [diff] [blame] | 892 | // Get the identifier name without trigraphs or embedded newlines. Note |
| 893 | // that we can't use Tok.getIdentifierInfo() because its lookup is disabled |
| 894 | // when skipping. |
| 895 | // TODO: could do this with zero copies in the no-clean case by using |
| 896 | // strncmp below. |
| 897 | char Directive[20]; |
| 898 | unsigned IdLen; |
| 899 | if (!Tok.needsCleaning() && Tok.getLength() < 20) { |
| 900 | IdLen = Tok.getLength(); |
| 901 | memcpy(Directive, RawCharData, IdLen); |
| 902 | Directive[IdLen] = 0; |
| 903 | } else { |
| 904 | std::string DirectiveStr = getSpelling(Tok); |
| 905 | IdLen = DirectiveStr.size(); |
| 906 | if (IdLen >= 20) { |
| 907 | CurLexer->ParsingPreprocessorDirective = false; |
| 908 | continue; |
| 909 | } |
| 910 | memcpy(Directive, &DirectiveStr[0], IdLen); |
| 911 | Directive[IdLen] = 0; |
| 912 | } |
| 913 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 914 | if (FirstChar == 'i' && Directive[1] == 'f') { |
Chris Lattner | e60165f | 2006-06-22 06:36:29 +0000 | [diff] [blame] | 915 | if ((IdLen == 2) || // "if" |
| 916 | (IdLen == 5 && !strcmp(Directive+2, "def")) || // "ifdef" |
| 917 | (IdLen == 6 && !strcmp(Directive+2, "ndef"))) { // "ifndef" |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 918 | // We know the entire #if/#ifdef/#ifndef block will be skipped, don't |
| 919 | // bother parsing the condition. |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 920 | DiscardUntilEndOfDirective(); |
Chris Lattner | 50b497e | 2006-06-18 16:32:35 +0000 | [diff] [blame] | 921 | CurLexer->pushConditionalLevel(Tok.getLocation(), /*wasskipping*/true, |
Chris Lattner | d01e291 | 2006-06-18 16:22:51 +0000 | [diff] [blame] | 922 | /*foundnonskip*/false, |
| 923 | /*fnddelse*/false); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 924 | } |
| 925 | } else if (FirstChar == 'e') { |
Chris Lattner | e60165f | 2006-06-22 06:36:29 +0000 | [diff] [blame] | 926 | if (IdLen == 5 && !strcmp(Directive+1, "ndif")) { // "endif" |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 927 | CheckEndOfDirective("#endif"); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 928 | PPConditionalInfo CondInfo; |
| 929 | CondInfo.WasSkipping = true; // Silence bogus warning. |
| 930 | bool InCond = CurLexer->popConditionalLevel(CondInfo); |
| 931 | assert(!InCond && "Can't be skipping if not in a conditional!"); |
| 932 | |
| 933 | // If we popped the outermost skipping block, we're done skipping! |
| 934 | if (!CondInfo.WasSkipping) |
| 935 | break; |
Chris Lattner | e60165f | 2006-06-22 06:36:29 +0000 | [diff] [blame] | 936 | } else if (IdLen == 4 && !strcmp(Directive+1, "lse")) { // "else". |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 937 | // #else directive in a skipping conditional. If not in some other |
| 938 | // skipping conditional, and if #else hasn't already been seen, enter it |
| 939 | // as a non-skipping conditional. |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 940 | CheckEndOfDirective("#else"); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 941 | PPConditionalInfo &CondInfo = CurLexer->peekConditionalLevel(); |
| 942 | |
| 943 | // If this is a #else with a #else before it, report the error. |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 944 | if (CondInfo.FoundElse) Diag(Tok, diag::pp_err_else_after_else); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 945 | |
| 946 | // Note that we've seen a #else in this conditional. |
| 947 | CondInfo.FoundElse = true; |
| 948 | |
| 949 | // If the conditional is at the top level, and the #if block wasn't |
| 950 | // entered, enter the #else block now. |
| 951 | if (!CondInfo.WasSkipping && !CondInfo.FoundNonSkip) { |
| 952 | CondInfo.FoundNonSkip = true; |
| 953 | break; |
| 954 | } |
Chris Lattner | e60165f | 2006-06-22 06:36:29 +0000 | [diff] [blame] | 955 | } else if (IdLen == 4 && !strcmp(Directive+1, "lif")) { // "elif". |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 956 | PPConditionalInfo &CondInfo = CurLexer->peekConditionalLevel(); |
| 957 | |
| 958 | bool ShouldEnter; |
| 959 | // If this is in a skipping block or if we're already handled this #if |
| 960 | // block, don't bother parsing the condition. |
| 961 | if (CondInfo.WasSkipping || CondInfo.FoundNonSkip) { |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 962 | DiscardUntilEndOfDirective(); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 963 | ShouldEnter = false; |
| 964 | } else { |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 965 | // Restore the value of SkippingContents so that identifiers are |
| 966 | // looked up, etc, inside the #elif expression. |
| 967 | assert(SkippingContents && "We have to be skipping here!"); |
| 968 | SkippingContents = false; |
Chris Lattner | 7966aaf | 2006-06-18 06:50:36 +0000 | [diff] [blame] | 969 | ShouldEnter = EvaluateDirectiveExpression(); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 970 | SkippingContents = true; |
| 971 | } |
| 972 | |
| 973 | // If this is a #elif with a #else before it, report the error. |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 974 | if (CondInfo.FoundElse) Diag(Tok, diag::pp_err_elif_after_else); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 975 | |
| 976 | // If this condition is true, enter it! |
| 977 | if (ShouldEnter) { |
| 978 | CondInfo.FoundNonSkip = true; |
| 979 | break; |
| 980 | } |
| 981 | } |
| 982 | } |
| 983 | |
| 984 | CurLexer->ParsingPreprocessorDirective = false; |
| 985 | } |
| 986 | |
| 987 | // Finally, if we are out of the conditional (saw an #endif or ran off the end |
| 988 | // of the file, just stop skipping and return to lexing whatever came after |
| 989 | // the #if block. |
| 990 | SkippingContents = false; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 991 | } |
| 992 | |
| 993 | //===----------------------------------------------------------------------===// |
| 994 | // Preprocessor Directive Handling. |
| 995 | //===----------------------------------------------------------------------===// |
| 996 | |
| 997 | /// HandleDirective - This callback is invoked when the lexer sees a # token |
| 998 | /// at the start of a line. This consumes the directive, modifies the |
| 999 | /// lexer/preprocessor state, and advances the lexer(s) so that the next token |
| 1000 | /// read is the correct one. |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1001 | void Preprocessor::HandleDirective(LexerToken &Result) { |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1002 | // FIXME: TRADITIONAL: # with whitespace before it not recognized by K&R? |
| 1003 | |
| 1004 | // We just parsed a # character at the start of a line, so we're in directive |
| 1005 | // mode. Tell the lexer this so any newlines we see will be converted into an |
| 1006 | // EOM token (this terminates the macro). |
| 1007 | CurLexer->ParsingPreprocessorDirective = true; |
| 1008 | |
| 1009 | ++NumDirectives; |
| 1010 | |
| 1011 | // Read the next token, the directive flavor. |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1012 | LexUnexpandedToken(Result); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1013 | |
| 1014 | switch (Result.getKind()) { |
| 1015 | default: break; |
| 1016 | case tok::eom: |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1017 | return; // null directive. |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1018 | |
| 1019 | #if 0 |
| 1020 | case tok::numeric_constant: |
| 1021 | // FIXME: implement # 7 line numbers! |
| 1022 | break; |
| 1023 | #endif |
| 1024 | case tok::kw_else: |
| 1025 | return HandleElseDirective(Result); |
| 1026 | case tok::kw_if: |
| 1027 | return HandleIfDirective(Result); |
| 1028 | case tok::identifier: |
Chris Lattner | 4093192 | 2006-06-22 06:14:04 +0000 | [diff] [blame] | 1029 | // Get the identifier name without trigraphs or embedded newlines. |
| 1030 | const char *Directive = Result.getIdentifierInfo()->getName(); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1031 | bool isExtension = false; |
Chris Lattner | 4093192 | 2006-06-22 06:14:04 +0000 | [diff] [blame] | 1032 | switch (Result.getIdentifierInfo()->getNameLength()) { |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1033 | case 4: |
Chris Lattner | 4093192 | 2006-06-22 06:14:04 +0000 | [diff] [blame] | 1034 | if (Directive[0] == 'l' && !strcmp(Directive, "line")) |
Chris Lattner | b876183 | 2006-06-24 21:31:03 +0000 | [diff] [blame] | 1035 | ; // FIXME: implement #line |
Chris Lattner | 4093192 | 2006-06-22 06:14:04 +0000 | [diff] [blame] | 1036 | if (Directive[0] == 'e' && !strcmp(Directive, "elif")) |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1037 | return HandleElifDirective(Result); |
Chris Lattner | 4093192 | 2006-06-22 06:14:04 +0000 | [diff] [blame] | 1038 | if (Directive[0] == 's' && !strcmp(Directive, "sccs")) { |
Chris Lattner | b876183 | 2006-06-24 21:31:03 +0000 | [diff] [blame] | 1039 | isExtension = true; // FIXME: implement #sccs |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1040 | // SCCS is the same as #ident. |
| 1041 | } |
| 1042 | break; |
| 1043 | case 5: |
Chris Lattner | 4093192 | 2006-06-22 06:14:04 +0000 | [diff] [blame] | 1044 | if (Directive[0] == 'e' && !strcmp(Directive, "endif")) |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1045 | return HandleEndifDirective(Result); |
Chris Lattner | 4093192 | 2006-06-22 06:14:04 +0000 | [diff] [blame] | 1046 | if (Directive[0] == 'i' && !strcmp(Directive, "ifdef")) |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1047 | return HandleIfdefDirective(Result, false); |
Chris Lattner | 4093192 | 2006-06-22 06:14:04 +0000 | [diff] [blame] | 1048 | if (Directive[0] == 'u' && !strcmp(Directive, "undef")) |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1049 | return HandleUndefDirective(Result); |
Chris Lattner | 4093192 | 2006-06-22 06:14:04 +0000 | [diff] [blame] | 1050 | if (Directive[0] == 'e' && !strcmp(Directive, "error")) |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1051 | return HandleUserDiagnosticDirective(Result, false); |
Chris Lattner | 4093192 | 2006-06-22 06:14:04 +0000 | [diff] [blame] | 1052 | if (Directive[0] == 'i' && !strcmp(Directive, "ident")) |
Chris Lattner | b876183 | 2006-06-24 21:31:03 +0000 | [diff] [blame] | 1053 | isExtension = true; // FIXME: implement #ident |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1054 | break; |
| 1055 | case 6: |
Chris Lattner | 4093192 | 2006-06-22 06:14:04 +0000 | [diff] [blame] | 1056 | if (Directive[0] == 'd' && !strcmp(Directive, "define")) |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1057 | return HandleDefineDirective(Result); |
Chris Lattner | 4093192 | 2006-06-22 06:14:04 +0000 | [diff] [blame] | 1058 | if (Directive[0] == 'i' && !strcmp(Directive, "ifndef")) |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1059 | return HandleIfdefDirective(Result, true); |
Chris Lattner | 4093192 | 2006-06-22 06:14:04 +0000 | [diff] [blame] | 1060 | if (Directive[0] == 'i' && !strcmp(Directive, "import")) |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1061 | return HandleImportDirective(Result); |
Chris Lattner | b876183 | 2006-06-24 21:31:03 +0000 | [diff] [blame] | 1062 | if (Directive[0] == 'p' && !strcmp(Directive, "pragma")) |
Chris Lattner | 69772b0 | 2006-07-02 20:34:39 +0000 | [diff] [blame^] | 1063 | return HandlePragmaDirective(); |
Chris Lattner | b876183 | 2006-06-24 21:31:03 +0000 | [diff] [blame] | 1064 | if (Directive[0] == 'a' && !strcmp(Directive, "assert")) |
| 1065 | isExtension = true; // FIXME: implement #assert |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1066 | break; |
| 1067 | case 7: |
Chris Lattner | 4093192 | 2006-06-22 06:14:04 +0000 | [diff] [blame] | 1068 | if (Directive[0] == 'i' && !strcmp(Directive, "include")) |
| 1069 | return HandleIncludeDirective(Result); // Handle #include. |
| 1070 | if (Directive[0] == 'w' && !strcmp(Directive, "warning")) { |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1071 | Diag(Result, diag::ext_pp_warning_directive); |
Chris Lattner | 504f2eb | 2006-06-18 07:19:54 +0000 | [diff] [blame] | 1072 | return HandleUserDiagnosticDirective(Result, true); |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1073 | } |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1074 | break; |
| 1075 | case 8: |
Chris Lattner | 4093192 | 2006-06-22 06:14:04 +0000 | [diff] [blame] | 1076 | if (Directive[0] == 'u' && !strcmp(Directive, "unassert")) { |
Chris Lattner | b876183 | 2006-06-24 21:31:03 +0000 | [diff] [blame] | 1077 | isExtension = true; // FIXME: implement #unassert |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1078 | } |
| 1079 | break; |
| 1080 | case 12: |
Chris Lattner | 4093192 | 2006-06-22 06:14:04 +0000 | [diff] [blame] | 1081 | if (Directive[0] == 'i' && !strcmp(Directive, "include_next")) |
| 1082 | return HandleIncludeNextDirective(Result); // Handle #include_next. |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1083 | break; |
| 1084 | } |
| 1085 | break; |
| 1086 | } |
| 1087 | |
| 1088 | // If we reached here, the preprocessing token is not valid! |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1089 | Diag(Result, diag::err_pp_invalid_directive); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1090 | |
| 1091 | // Read the rest of the PP line. |
| 1092 | do { |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1093 | Lex(Result); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1094 | } while (Result.getKind() != tok::eom); |
| 1095 | |
| 1096 | // Okay, we're done parsing the directive. |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1097 | } |
| 1098 | |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1099 | void Preprocessor::HandleUserDiagnosticDirective(LexerToken &Result, |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1100 | bool isWarning) { |
| 1101 | // Read the rest of the line raw. We do this because we don't want macros |
| 1102 | // to be expanded and we don't require that the tokens be valid preprocessing |
| 1103 | // tokens. For example, this is allowed: "#warning ` 'foo". GCC does |
| 1104 | // collapse multiple consequtive white space between tokens, but this isn't |
| 1105 | // specified by the standard. |
| 1106 | std::string Message = CurLexer->ReadToEndOfLine(); |
| 1107 | |
| 1108 | unsigned DiagID = isWarning ? diag::pp_hash_warning : diag::err_pp_hash_error; |
| 1109 | return Diag(Result, DiagID, Message); |
| 1110 | } |
| 1111 | |
Chris Lattner | b876183 | 2006-06-24 21:31:03 +0000 | [diff] [blame] | 1112 | //===----------------------------------------------------------------------===// |
| 1113 | // Preprocessor Include Directive Handling. |
| 1114 | //===----------------------------------------------------------------------===// |
| 1115 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1116 | /// HandleIncludeDirective - The "#include" tokens have just been read, read the |
| 1117 | /// file to be included from the lexer, then include it! This is a common |
| 1118 | /// routine with functionality shared between #include, #include_next and |
| 1119 | /// #import. |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1120 | void Preprocessor::HandleIncludeDirective(LexerToken &IncludeTok, |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1121 | const DirectoryLookup *LookupFrom, |
| 1122 | bool isImport) { |
| 1123 | ++NumIncluded; |
| 1124 | LexerToken FilenameTok; |
Chris Lattner | 269c232 | 2006-06-25 06:23:00 +0000 | [diff] [blame] | 1125 | std::string Filename = CurLexer->LexIncludeFilename(FilenameTok); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1126 | |
| 1127 | // If the token kind is EOM, the error has already been diagnosed. |
| 1128 | if (FilenameTok.getKind() == tok::eom) |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1129 | return; |
Chris Lattner | 269c232 | 2006-06-25 06:23:00 +0000 | [diff] [blame] | 1130 | |
| 1131 | // Verify that there is nothing after the filename, other than EOM. Use the |
| 1132 | // preprocessor to lex this in case lexing the filename entered a macro. |
| 1133 | CheckEndOfDirective("#include"); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1134 | |
| 1135 | // Check that we don't have infinite #include recursion. |
Chris Lattner | 69772b0 | 2006-07-02 20:34:39 +0000 | [diff] [blame^] | 1136 | if (IncludeMacroStack.size() == MaxAllowedIncludeStackDepth-1) |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1137 | return Diag(FilenameTok, diag::err_pp_include_too_deep); |
| 1138 | |
Chris Lattner | 269c232 | 2006-06-25 06:23:00 +0000 | [diff] [blame] | 1139 | // Find out whether the filename is <x> or "x". |
| 1140 | bool isAngled = Filename[0] == '<'; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1141 | |
| 1142 | // Remove the quotes. |
| 1143 | Filename = std::string(Filename.begin()+1, Filename.end()-1); |
| 1144 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1145 | // Search include directories. |
Chris Lattner | c899718 | 2006-06-22 05:52:16 +0000 | [diff] [blame] | 1146 | const DirectoryLookup *CurDir; |
| 1147 | const FileEntry *File = LookupFile(Filename, isAngled, LookupFrom, CurDir); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1148 | if (File == 0) |
| 1149 | return Diag(FilenameTok, diag::err_pp_file_not_found); |
| 1150 | |
| 1151 | // Get information about this file. |
| 1152 | PerFileInfo &FileInfo = getFileInfo(File); |
| 1153 | |
| 1154 | // If this is a #import directive, check that we have not already imported |
| 1155 | // this header. |
| 1156 | if (isImport) { |
| 1157 | // If this has already been imported, don't import it again. |
| 1158 | FileInfo.isImport = true; |
| 1159 | |
| 1160 | // Has this already been #import'ed or #include'd? |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1161 | if (FileInfo.NumIncludes) return; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1162 | } else { |
| 1163 | // Otherwise, if this is a #include of a file that was previously #import'd |
| 1164 | // or if this is the second #include of a #pragma once file, ignore it. |
| 1165 | if (FileInfo.isImport) |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1166 | return; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1167 | } |
| 1168 | |
| 1169 | // Look up the file, create a File ID for it. |
| 1170 | unsigned FileID = |
Chris Lattner | 50b497e | 2006-06-18 16:32:35 +0000 | [diff] [blame] | 1171 | SourceMgr.createFileID(File, FilenameTok.getLocation()); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1172 | if (FileID == 0) |
| 1173 | return Diag(FilenameTok, diag::err_pp_file_not_found); |
| 1174 | |
| 1175 | // Finally, if all is good, enter the new file! |
Chris Lattner | c899718 | 2006-06-22 05:52:16 +0000 | [diff] [blame] | 1176 | EnterSourceFile(FileID, CurDir); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1177 | |
| 1178 | // Increment the number of times this file has been included. |
| 1179 | ++FileInfo.NumIncludes; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1180 | } |
| 1181 | |
| 1182 | /// HandleIncludeNextDirective - Implements #include_next. |
| 1183 | /// |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1184 | void Preprocessor::HandleIncludeNextDirective(LexerToken &IncludeNextTok) { |
| 1185 | Diag(IncludeNextTok, diag::ext_pp_include_next_directive); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1186 | |
| 1187 | // #include_next is like #include, except that we start searching after |
| 1188 | // the current found directory. If we can't do this, issue a |
| 1189 | // diagnostic. |
Chris Lattner | c899718 | 2006-06-22 05:52:16 +0000 | [diff] [blame] | 1190 | const DirectoryLookup *Lookup = CurDirLookup; |
Chris Lattner | 69772b0 | 2006-07-02 20:34:39 +0000 | [diff] [blame^] | 1191 | if (isInPrimaryFile()) { |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1192 | Lookup = 0; |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1193 | Diag(IncludeNextTok, diag::pp_include_next_in_primary); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1194 | } else if (Lookup == 0) { |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1195 | Diag(IncludeNextTok, diag::pp_include_next_absolute_path); |
Chris Lattner | c899718 | 2006-06-22 05:52:16 +0000 | [diff] [blame] | 1196 | } else { |
| 1197 | // Start looking up in the next directory. |
| 1198 | ++Lookup; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1199 | } |
| 1200 | |
| 1201 | return HandleIncludeDirective(IncludeNextTok, Lookup); |
| 1202 | } |
| 1203 | |
| 1204 | /// HandleImportDirective - Implements #import. |
| 1205 | /// |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1206 | void Preprocessor::HandleImportDirective(LexerToken &ImportTok) { |
| 1207 | Diag(ImportTok, diag::ext_pp_import_directive); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1208 | |
| 1209 | return HandleIncludeDirective(ImportTok, 0, true); |
| 1210 | } |
| 1211 | |
Chris Lattner | b876183 | 2006-06-24 21:31:03 +0000 | [diff] [blame] | 1212 | //===----------------------------------------------------------------------===// |
| 1213 | // Preprocessor Macro Directive Handling. |
| 1214 | //===----------------------------------------------------------------------===// |
| 1215 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1216 | /// HandleDefineDirective - Implements #define. This consumes the entire macro |
| 1217 | /// line then lets the caller lex the next real token. |
| 1218 | /// |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1219 | void Preprocessor::HandleDefineDirective(LexerToken &DefineTok) { |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1220 | ++NumDefined; |
| 1221 | LexerToken MacroNameTok; |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1222 | ReadMacroName(MacroNameTok); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1223 | |
| 1224 | // Error reading macro name? If so, diagnostic already issued. |
| 1225 | if (MacroNameTok.getKind() == tok::eom) |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1226 | return; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1227 | |
Chris Lattner | 50b497e | 2006-06-18 16:32:35 +0000 | [diff] [blame] | 1228 | MacroInfo *MI = new MacroInfo(MacroNameTok.getLocation()); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1229 | |
| 1230 | LexerToken Tok; |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1231 | LexUnexpandedToken(Tok); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1232 | |
| 1233 | if (Tok.getKind() == tok::eom) { |
| 1234 | // If there is no body to this macro, we have no special handling here. |
| 1235 | } else if (Tok.getKind() == tok::l_paren && !Tok.hasLeadingSpace()) { |
| 1236 | // This is a function-like macro definition. |
| 1237 | //assert(0 && "Function-like macros not implemented!"); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1238 | return DiscardUntilEndOfDirective(); |
| 1239 | |
| 1240 | } else if (!Tok.hasLeadingSpace()) { |
| 1241 | // C99 requires whitespace between the macro definition and the body. Emit |
| 1242 | // a diagnostic for something like "#define X+". |
| 1243 | if (Features.C99) { |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1244 | Diag(Tok, diag::ext_c99_whitespace_required_after_macro_name); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1245 | } else { |
| 1246 | // FIXME: C90/C++ do not get this diagnostic, but it does get a similar |
| 1247 | // one in some cases! |
| 1248 | } |
| 1249 | } else { |
| 1250 | // This is a normal token with leading space. Clear the leading space |
| 1251 | // marker on the first token to get proper expansion. |
| 1252 | Tok.ClearFlag(LexerToken::LeadingSpace); |
| 1253 | } |
| 1254 | |
| 1255 | // Read the rest of the macro body. |
| 1256 | while (Tok.getKind() != tok::eom) { |
| 1257 | MI->AddTokenToBody(Tok); |
| 1258 | |
| 1259 | // FIXME: See create_iso_definition. |
| 1260 | |
| 1261 | // Get the next token of the macro. |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1262 | LexUnexpandedToken(Tok); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1263 | } |
| 1264 | |
| 1265 | // Finally, if this identifier already had a macro defined for it, verify that |
| 1266 | // the macro bodies are identical and free the old definition. |
| 1267 | if (MacroInfo *OtherMI = MacroNameTok.getIdentifierInfo()->getMacroInfo()) { |
Chris Lattner | 677757a | 2006-06-28 05:26:32 +0000 | [diff] [blame] | 1268 | if (OtherMI->isBuiltinMacro()) |
| 1269 | Diag(MacroNameTok, diag::pp_redef_builtin_macro); |
| 1270 | |
| 1271 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1272 | // FIXME: Verify the definition is the same. |
| 1273 | // Macros must be identical. This means all tokes and whitespace separation |
| 1274 | // must be the same. |
| 1275 | delete OtherMI; |
| 1276 | } |
| 1277 | |
| 1278 | MacroNameTok.getIdentifierInfo()->setMacroInfo(MI); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1279 | } |
| 1280 | |
| 1281 | |
| 1282 | /// HandleUndefDirective - Implements #undef. |
| 1283 | /// |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1284 | void Preprocessor::HandleUndefDirective(LexerToken &UndefTok) { |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1285 | ++NumUndefined; |
| 1286 | LexerToken MacroNameTok; |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1287 | ReadMacroName(MacroNameTok); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1288 | |
| 1289 | // Error reading macro name? If so, diagnostic already issued. |
| 1290 | if (MacroNameTok.getKind() == tok::eom) |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1291 | return; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1292 | |
| 1293 | // Check to see if this is the last token on the #undef line. |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1294 | CheckEndOfDirective("#undef"); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1295 | |
| 1296 | // Okay, we finally have a valid identifier to undef. |
| 1297 | MacroInfo *MI = MacroNameTok.getIdentifierInfo()->getMacroInfo(); |
| 1298 | |
| 1299 | // If the macro is not defined, this is a noop undef, just return. |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1300 | if (MI == 0) return; |
Chris Lattner | 677757a | 2006-06-28 05:26:32 +0000 | [diff] [blame] | 1301 | |
| 1302 | if (MI->isBuiltinMacro()) |
| 1303 | Diag(MacroNameTok, diag::pp_undef_builtin_macro); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1304 | |
| 1305 | #if 0 // FIXME: implement warn_unused_macros. |
| 1306 | if (CPP_OPTION (pfile, warn_unused_macros)) |
| 1307 | _cpp_warn_if_unused_macro (pfile, node, NULL); |
| 1308 | #endif |
| 1309 | |
| 1310 | // Free macro definition. |
| 1311 | delete MI; |
| 1312 | MacroNameTok.getIdentifierInfo()->setMacroInfo(0); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1313 | } |
| 1314 | |
| 1315 | |
Chris Lattner | b876183 | 2006-06-24 21:31:03 +0000 | [diff] [blame] | 1316 | //===----------------------------------------------------------------------===// |
| 1317 | // Preprocessor Conditional Directive Handling. |
| 1318 | //===----------------------------------------------------------------------===// |
| 1319 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1320 | /// HandleIfdefDirective - Implements the #ifdef/#ifndef directive. isIfndef is |
| 1321 | /// true when this is a #ifndef directive. |
| 1322 | /// |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1323 | void Preprocessor::HandleIfdefDirective(LexerToken &Result, bool isIfndef) { |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1324 | ++NumIf; |
| 1325 | LexerToken DirectiveTok = Result; |
| 1326 | |
| 1327 | LexerToken MacroNameTok; |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1328 | ReadMacroName(MacroNameTok); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1329 | |
| 1330 | // Error reading macro name? If so, diagnostic already issued. |
| 1331 | if (MacroNameTok.getKind() == tok::eom) |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1332 | return; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1333 | |
| 1334 | // Check to see if this is the last token on the #if[n]def line. |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1335 | CheckEndOfDirective("#ifdef"); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1336 | |
| 1337 | // Should we include the stuff contained by this directive? |
| 1338 | if (!MacroNameTok.getIdentifierInfo()->getMacroInfo() == isIfndef) { |
| 1339 | // Yes, remember that we are inside a conditional, then lex the next token. |
Chris Lattner | 50b497e | 2006-06-18 16:32:35 +0000 | [diff] [blame] | 1340 | CurLexer->pushConditionalLevel(DirectiveTok.getLocation(), /*wasskip*/false, |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1341 | /*foundnonskip*/true, /*foundelse*/false); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1342 | } else { |
| 1343 | // No, skip the contents of this block and return the first token after it. |
Chris Lattner | 50b497e | 2006-06-18 16:32:35 +0000 | [diff] [blame] | 1344 | SkipExcludedConditionalBlock(DirectiveTok.getLocation(), |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1345 | /*Foundnonskip*/false, |
| 1346 | /*FoundElse*/false); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1347 | } |
| 1348 | } |
| 1349 | |
| 1350 | /// HandleIfDirective - Implements the #if directive. |
| 1351 | /// |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1352 | void Preprocessor::HandleIfDirective(LexerToken &IfToken) { |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1353 | ++NumIf; |
Chris Lattner | 7966aaf | 2006-06-18 06:50:36 +0000 | [diff] [blame] | 1354 | bool ConditionalTrue = EvaluateDirectiveExpression(); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1355 | |
| 1356 | // Should we include the stuff contained by this directive? |
| 1357 | if (ConditionalTrue) { |
| 1358 | // Yes, remember that we are inside a conditional, then lex the next token. |
Chris Lattner | 50b497e | 2006-06-18 16:32:35 +0000 | [diff] [blame] | 1359 | CurLexer->pushConditionalLevel(IfToken.getLocation(), /*wasskip*/false, |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1360 | /*foundnonskip*/true, /*foundelse*/false); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1361 | } else { |
| 1362 | // No, skip the contents of this block and return the first token after it. |
Chris Lattner | 50b497e | 2006-06-18 16:32:35 +0000 | [diff] [blame] | 1363 | SkipExcludedConditionalBlock(IfToken.getLocation(), /*Foundnonskip*/false, |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1364 | /*FoundElse*/false); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1365 | } |
| 1366 | } |
| 1367 | |
| 1368 | /// HandleEndifDirective - Implements the #endif directive. |
| 1369 | /// |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1370 | void Preprocessor::HandleEndifDirective(LexerToken &EndifToken) { |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1371 | ++NumEndif; |
| 1372 | // Check that this is the whole directive. |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1373 | CheckEndOfDirective("#endif"); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1374 | |
| 1375 | PPConditionalInfo CondInfo; |
| 1376 | if (CurLexer->popConditionalLevel(CondInfo)) { |
| 1377 | // No conditionals on the stack: this is an #endif without an #if. |
| 1378 | return Diag(EndifToken, diag::err_pp_endif_without_if); |
| 1379 | } |
| 1380 | |
| 1381 | assert(!CondInfo.WasSkipping && !isSkipping() && |
| 1382 | "This code should only be reachable in the non-skipping case!"); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1383 | } |
| 1384 | |
| 1385 | |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1386 | void Preprocessor::HandleElseDirective(LexerToken &Result) { |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1387 | ++NumElse; |
| 1388 | // #else directive in a non-skipping conditional... start skipping. |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1389 | CheckEndOfDirective("#else"); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1390 | |
| 1391 | PPConditionalInfo CI; |
| 1392 | if (CurLexer->popConditionalLevel(CI)) |
| 1393 | return Diag(Result, diag::pp_err_else_without_if); |
| 1394 | |
| 1395 | // If this is a #else with a #else before it, report the error. |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1396 | if (CI.FoundElse) Diag(Result, diag::pp_err_else_after_else); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1397 | |
| 1398 | // Finally, skip the rest of the contents of this block and return the first |
| 1399 | // token after it. |
| 1400 | return SkipExcludedConditionalBlock(CI.IfLoc, /*Foundnonskip*/true, |
| 1401 | /*FoundElse*/true); |
| 1402 | } |
| 1403 | |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1404 | void Preprocessor::HandleElifDirective(LexerToken &ElifToken) { |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1405 | ++NumElse; |
| 1406 | // #elif directive in a non-skipping conditional... start skipping. |
| 1407 | // We don't care what the condition is, because we will always skip it (since |
| 1408 | // the block immediately before it was included). |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1409 | DiscardUntilEndOfDirective(); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1410 | |
| 1411 | PPConditionalInfo CI; |
| 1412 | if (CurLexer->popConditionalLevel(CI)) |
| 1413 | return Diag(ElifToken, diag::pp_err_elif_without_if); |
| 1414 | |
| 1415 | // If this is a #elif with a #else before it, report the error. |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1416 | if (CI.FoundElse) Diag(ElifToken, diag::pp_err_elif_after_else); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1417 | |
| 1418 | // Finally, skip the rest of the contents of this block and return the first |
| 1419 | // token after it. |
| 1420 | return SkipExcludedConditionalBlock(CI.IfLoc, /*Foundnonskip*/true, |
| 1421 | /*FoundElse*/CI.FoundElse); |
| 1422 | } |
Chris Lattner | b876183 | 2006-06-24 21:31:03 +0000 | [diff] [blame] | 1423 | |
| 1424 | |
| 1425 | //===----------------------------------------------------------------------===// |
| 1426 | // Preprocessor Pragma Directive Handling. |
| 1427 | //===----------------------------------------------------------------------===// |
| 1428 | |
Chris Lattner | 69772b0 | 2006-07-02 20:34:39 +0000 | [diff] [blame^] | 1429 | /// HandlePragmaDirective - The "#pragma" directive has been parsed. Lex the |
| 1430 | /// rest of the pragma, passing it to the registered pragma handlers. |
| 1431 | void Preprocessor::HandlePragmaDirective() { |
Chris Lattner | b876183 | 2006-06-24 21:31:03 +0000 | [diff] [blame] | 1432 | ++NumPragma; |
| 1433 | |
| 1434 | // Invoke the first level of pragma handlers which reads the namespace id. |
| 1435 | LexerToken Tok; |
| 1436 | PragmaHandlers->HandlePragma(*this, Tok); |
| 1437 | |
| 1438 | // If the pragma handler didn't read the rest of the line, consume it now. |
Chris Lattner | 1786217 | 2006-06-24 22:12:56 +0000 | [diff] [blame] | 1439 | if (CurLexer->ParsingPreprocessorDirective) |
| 1440 | DiscardUntilEndOfDirective(); |
Chris Lattner | b876183 | 2006-06-24 21:31:03 +0000 | [diff] [blame] | 1441 | } |
| 1442 | |
Chris Lattner | 69772b0 | 2006-07-02 20:34:39 +0000 | [diff] [blame^] | 1443 | /// Handle_Pragma - Read a _Pragma directive, slice it up, process it, then |
| 1444 | /// return the first token after the directive. The _Pragma token has just |
| 1445 | /// been read into 'Tok'. |
| 1446 | void Preprocessor::Handle_Pragma(LexerToken &Tok) { |
| 1447 | // Remember the pragma token location. |
| 1448 | SourceLocation PragmaLoc = Tok.getLocation(); |
| 1449 | |
| 1450 | // Read the '('. |
| 1451 | Lex(Tok); |
| 1452 | if (Tok.getKind() != tok::l_paren) |
| 1453 | return Diag(PragmaLoc, diag::err__Pragma_malformed); |
| 1454 | |
| 1455 | // Read the '"..."'. |
| 1456 | Lex(Tok); |
| 1457 | if (Tok.getKind() != tok::string_literal) |
| 1458 | return Diag(PragmaLoc, diag::err__Pragma_malformed); |
| 1459 | |
| 1460 | // Remember the string. |
| 1461 | std::string StrVal = getSpelling(Tok); |
| 1462 | SourceLocation StrLoc = Tok.getLocation(); |
| 1463 | |
| 1464 | // Read the ')'. |
| 1465 | Lex(Tok); |
| 1466 | if (Tok.getKind() != tok::r_paren) |
| 1467 | return Diag(PragmaLoc, diag::err__Pragma_malformed); |
| 1468 | |
| 1469 | // The _Pragma is lexically sound. Destringize according to C99 6.10.9.1. |
| 1470 | if (StrVal[0] == 'L') // Remove L prefix. |
| 1471 | StrVal.erase(StrVal.begin()); |
| 1472 | assert(StrVal[0] == '"' && StrVal[StrVal.size()-1] == '"' && |
| 1473 | "Invalid string token!"); |
| 1474 | |
| 1475 | // Remove the front quote, replacing it with a space, so that the pragma |
| 1476 | // contents appear to have a space before them. |
| 1477 | StrVal[0] = ' '; |
| 1478 | |
| 1479 | // Replace the terminating quote with a \n\0. |
| 1480 | StrVal[StrVal.size()-1] = '\n'; |
| 1481 | StrVal += '\0'; |
| 1482 | |
| 1483 | // Remove escaped quotes and escapes. |
| 1484 | for (unsigned i = 0, e = StrVal.size(); i != e-1; ++i) { |
| 1485 | if (StrVal[i] == '\\' && |
| 1486 | (StrVal[i+1] == '\\' || StrVal[i+1] == '"')) { |
| 1487 | // \\ -> '\' and \" -> '"'. |
| 1488 | StrVal.erase(StrVal.begin()+i); |
| 1489 | --e; |
| 1490 | } |
| 1491 | } |
| 1492 | |
| 1493 | // Plop the string (including the trailing null) into a buffer where we can |
| 1494 | // lex it. |
| 1495 | SourceLocation TokLoc = ScratchBuf->getToken(&StrVal[0], StrVal.size()); |
| 1496 | const char *StrData = SourceMgr.getCharacterData(TokLoc); |
| 1497 | |
| 1498 | // FIXME: Create appropriate mapping info for this FileID, so that we know the |
| 1499 | // tokens are coming out of the input string (StrLoc). |
| 1500 | unsigned FileID = TokLoc.getFileID(); |
| 1501 | assert(FileID && "Could not create FileID for predefines?"); |
| 1502 | |
| 1503 | // Make and enter a lexer object so that we lex and expand the tokens just |
| 1504 | // like any others. |
| 1505 | Lexer *TL = new Lexer(SourceMgr.getBuffer(FileID), FileID, *this, |
| 1506 | StrData, StrData+StrVal.size()-1 /* no null */); |
| 1507 | EnterSourceFileWithLexer(TL, 0); |
| 1508 | |
| 1509 | // Ensure that the lexer thinks it is inside a directive, so that end \n will |
| 1510 | // return an EOM token. |
| 1511 | TL->ParsingPreprocessorDirective = true; |
| 1512 | |
| 1513 | // With everything set up, lex this as a #pragma directive. |
| 1514 | HandlePragmaDirective(); |
| 1515 | |
| 1516 | // Finally, return whatever came after the pragma directive. |
| 1517 | return Lex(Tok); |
| 1518 | } |
| 1519 | |
| 1520 | |
| 1521 | |
Chris Lattner | b876183 | 2006-06-24 21:31:03 +0000 | [diff] [blame] | 1522 | /// HandlePragmaOnce - Handle #pragma once. OnceTok is the 'once'. |
Chris Lattner | 1786217 | 2006-06-24 22:12:56 +0000 | [diff] [blame] | 1523 | /// |
Chris Lattner | b876183 | 2006-06-24 21:31:03 +0000 | [diff] [blame] | 1524 | void Preprocessor::HandlePragmaOnce(LexerToken &OnceTok) { |
Chris Lattner | 69772b0 | 2006-07-02 20:34:39 +0000 | [diff] [blame^] | 1525 | if (isInPrimaryFile()) { |
Chris Lattner | b876183 | 2006-06-24 21:31:03 +0000 | [diff] [blame] | 1526 | Diag(OnceTok, diag::pp_pragma_once_in_main_file); |
| 1527 | return; |
| 1528 | } |
Chris Lattner | 1786217 | 2006-06-24 22:12:56 +0000 | [diff] [blame] | 1529 | |
| 1530 | // FIXME: implement the _Pragma thing. |
| 1531 | assert(CurLexer && "Cannot have a pragma in a macro expansion yet!"); |
| 1532 | |
| 1533 | // Mark the file as a once-only file now. |
| 1534 | const FileEntry *File = |
| 1535 | SourceMgr.getFileEntryForFileID(CurLexer->getCurFileID()); |
| 1536 | getFileInfo(File).isImport = true; |
Chris Lattner | b876183 | 2006-06-24 21:31:03 +0000 | [diff] [blame] | 1537 | } |
| 1538 | |
Chris Lattner | 1786217 | 2006-06-24 22:12:56 +0000 | [diff] [blame] | 1539 | /// HandlePragmaPoison - Handle #pragma GCC poison. PoisonTok is the 'poison'. |
| 1540 | /// |
| 1541 | void Preprocessor::HandlePragmaPoison(LexerToken &PoisonTok) { |
| 1542 | LexerToken Tok; |
| 1543 | assert(!SkippingContents && "Why are we handling pragmas while skipping?"); |
| 1544 | while (1) { |
| 1545 | // Read the next token to poison. While doing this, pretend that we are |
| 1546 | // skipping while reading the identifier to poison. |
| 1547 | // This avoids errors on code like: |
| 1548 | // #pragma GCC poison X |
| 1549 | // #pragma GCC poison X |
| 1550 | SkippingContents = true; |
| 1551 | LexUnexpandedToken(Tok); |
| 1552 | SkippingContents = false; |
| 1553 | |
| 1554 | // If we reached the end of line, we're done. |
| 1555 | if (Tok.getKind() == tok::eom) return; |
| 1556 | |
| 1557 | // Can only poison identifiers. |
| 1558 | if (Tok.getKind() != tok::identifier) { |
| 1559 | Diag(Tok, diag::err_pp_invalid_poison); |
| 1560 | return; |
| 1561 | } |
| 1562 | |
| 1563 | // Look up the identifier info for the token. |
| 1564 | std::string TokStr = getSpelling(Tok); |
| 1565 | IdentifierTokenInfo *II = |
| 1566 | getIdentifierInfo(&TokStr[0], &TokStr[0]+TokStr.size()); |
| 1567 | |
| 1568 | // Already poisoned. |
| 1569 | if (II->isPoisoned()) continue; |
| 1570 | |
| 1571 | // If this is a macro identifier, emit a warning. |
| 1572 | if (II->getMacroInfo()) |
| 1573 | Diag(Tok, diag::pp_poisoning_existing_macro); |
| 1574 | |
| 1575 | // Finally, poison it! |
| 1576 | II->setIsPoisoned(); |
| 1577 | } |
| 1578 | } |
Chris Lattner | b876183 | 2006-06-24 21:31:03 +0000 | [diff] [blame] | 1579 | |
Chris Lattner | 269c232 | 2006-06-25 06:23:00 +0000 | [diff] [blame] | 1580 | /// HandlePragmaSystemHeader - Implement #pragma GCC system_header. We know |
| 1581 | /// that the whole directive has been parsed. |
Chris Lattner | 55a6095 | 2006-06-25 04:20:34 +0000 | [diff] [blame] | 1582 | void Preprocessor::HandlePragmaSystemHeader(LexerToken &SysHeaderTok) { |
Chris Lattner | 69772b0 | 2006-07-02 20:34:39 +0000 | [diff] [blame^] | 1583 | if (isInPrimaryFile()) { |
Chris Lattner | 55a6095 | 2006-06-25 04:20:34 +0000 | [diff] [blame] | 1584 | Diag(SysHeaderTok, diag::pp_pragma_sysheader_in_main_file); |
| 1585 | return; |
| 1586 | } |
| 1587 | |
| 1588 | // Mark the file as a system header. |
| 1589 | const FileEntry *File = |
| 1590 | SourceMgr.getFileEntryForFileID(CurLexer->getCurFileID()); |
| 1591 | getFileInfo(File).DirInfo = DirectoryLookup::SystemHeaderDir; |
| 1592 | |
| 1593 | |
| 1594 | // Notify the client, if desired, that we are in a new source file. |
| 1595 | if (FileChangeHandler) |
| 1596 | FileChangeHandler(CurLexer->getSourceLocation(CurLexer->BufferPtr), |
| 1597 | SystemHeaderPragma, DirectoryLookup::SystemHeaderDir); |
| 1598 | } |
| 1599 | |
Chris Lattner | 269c232 | 2006-06-25 06:23:00 +0000 | [diff] [blame] | 1600 | /// HandlePragmaDependency - Handle #pragma GCC dependency "foo" blah. |
| 1601 | /// |
| 1602 | void Preprocessor::HandlePragmaDependency(LexerToken &DependencyTok) { |
| 1603 | LexerToken FilenameTok; |
| 1604 | std::string Filename = CurLexer->LexIncludeFilename(FilenameTok); |
| 1605 | |
| 1606 | // If the token kind is EOM, the error has already been diagnosed. |
| 1607 | if (FilenameTok.getKind() == tok::eom) |
| 1608 | return; |
| 1609 | |
| 1610 | // Find out whether the filename is <x> or "x". |
| 1611 | bool isAngled = Filename[0] == '<'; |
| 1612 | |
| 1613 | // Remove the quotes. |
| 1614 | Filename = std::string(Filename.begin()+1, Filename.end()-1); |
| 1615 | |
| 1616 | // Search include directories. |
| 1617 | const DirectoryLookup *CurDir; |
| 1618 | const FileEntry *File = LookupFile(Filename, isAngled, 0, CurDir); |
| 1619 | if (File == 0) |
| 1620 | return Diag(FilenameTok, diag::err_pp_file_not_found); |
| 1621 | |
Chris Lattner | 69772b0 | 2006-07-02 20:34:39 +0000 | [diff] [blame^] | 1622 | Lexer *TheLexer = getCurrentLexer(); |
Chris Lattner | 269c232 | 2006-06-25 06:23:00 +0000 | [diff] [blame] | 1623 | const FileEntry *CurFile = |
| 1624 | SourceMgr.getFileEntryForFileID(TheLexer->getCurFileID()); |
| 1625 | |
| 1626 | // If this file is older than the file it depends on, emit a diagnostic. |
| 1627 | if (CurFile && CurFile->getModificationTime() < File->getModificationTime()) { |
| 1628 | // Lex tokens at the end of the message and include them in the message. |
| 1629 | std::string Message; |
| 1630 | Lex(DependencyTok); |
| 1631 | while (DependencyTok.getKind() != tok::eom) { |
| 1632 | Message += getSpelling(DependencyTok) + " "; |
| 1633 | Lex(DependencyTok); |
| 1634 | } |
| 1635 | |
| 1636 | Message.erase(Message.end()-1); |
| 1637 | Diag(FilenameTok, diag::pp_out_of_date_dependency, Message); |
| 1638 | } |
| 1639 | } |
| 1640 | |
| 1641 | |
Chris Lattner | b876183 | 2006-06-24 21:31:03 +0000 | [diff] [blame] | 1642 | /// AddPragmaHandler - Add the specified pragma handler to the preprocessor. |
| 1643 | /// If 'Namespace' is non-null, then it is a token required to exist on the |
| 1644 | /// pragma line before the pragma string starts, e.g. "STDC" or "GCC". |
| 1645 | void Preprocessor::AddPragmaHandler(const char *Namespace, |
| 1646 | PragmaHandler *Handler) { |
| 1647 | PragmaNamespace *InsertNS = PragmaHandlers; |
| 1648 | |
| 1649 | // If this is specified to be in a namespace, step down into it. |
| 1650 | if (Namespace) { |
| 1651 | IdentifierTokenInfo *NSID = getIdentifierInfo(Namespace); |
| 1652 | |
| 1653 | // If there is already a pragma handler with the name of this namespace, |
| 1654 | // we either have an error (directive with the same name as a namespace) or |
| 1655 | // we already have the namespace to insert into. |
| 1656 | if (PragmaHandler *Existing = PragmaHandlers->FindHandler(NSID)) { |
| 1657 | InsertNS = Existing->getIfNamespace(); |
| 1658 | assert(InsertNS != 0 && "Cannot have a pragma namespace and pragma" |
| 1659 | " handler with the same name!"); |
| 1660 | } else { |
| 1661 | // Otherwise, this namespace doesn't exist yet, create and insert the |
| 1662 | // handler for it. |
| 1663 | InsertNS = new PragmaNamespace(NSID); |
| 1664 | PragmaHandlers->AddPragma(InsertNS); |
| 1665 | } |
| 1666 | } |
| 1667 | |
| 1668 | // Check to make sure we don't already have a pragma for this identifier. |
| 1669 | assert(!InsertNS->FindHandler(Handler->getName()) && |
| 1670 | "Pragma handler already exists for this identifier!"); |
| 1671 | InsertNS->AddPragma(Handler); |
| 1672 | } |
| 1673 | |
Chris Lattner | 1786217 | 2006-06-24 22:12:56 +0000 | [diff] [blame] | 1674 | namespace { |
Chris Lattner | 55a6095 | 2006-06-25 04:20:34 +0000 | [diff] [blame] | 1675 | struct PragmaOnceHandler : public PragmaHandler { |
Chris Lattner | b876183 | 2006-06-24 21:31:03 +0000 | [diff] [blame] | 1676 | PragmaOnceHandler(const IdentifierTokenInfo *OnceID) : PragmaHandler(OnceID){} |
| 1677 | virtual void HandlePragma(Preprocessor &PP, LexerToken &OnceTok) { |
| 1678 | PP.CheckEndOfDirective("#pragma once"); |
| 1679 | PP.HandlePragmaOnce(OnceTok); |
| 1680 | } |
| 1681 | }; |
| 1682 | |
Chris Lattner | 55a6095 | 2006-06-25 04:20:34 +0000 | [diff] [blame] | 1683 | struct PragmaPoisonHandler : public PragmaHandler { |
Chris Lattner | 1786217 | 2006-06-24 22:12:56 +0000 | [diff] [blame] | 1684 | PragmaPoisonHandler(const IdentifierTokenInfo *ID) : PragmaHandler(ID) {} |
| 1685 | virtual void HandlePragma(Preprocessor &PP, LexerToken &PoisonTok) { |
| 1686 | PP.HandlePragmaPoison(PoisonTok); |
| 1687 | } |
| 1688 | }; |
Chris Lattner | 55a6095 | 2006-06-25 04:20:34 +0000 | [diff] [blame] | 1689 | |
| 1690 | struct PragmaSystemHeaderHandler : public PragmaHandler { |
| 1691 | PragmaSystemHeaderHandler(const IdentifierTokenInfo *ID) : PragmaHandler(ID){} |
| 1692 | virtual void HandlePragma(Preprocessor &PP, LexerToken &SHToken) { |
| 1693 | PP.HandlePragmaSystemHeader(SHToken); |
| 1694 | PP.CheckEndOfDirective("#pragma"); |
| 1695 | } |
| 1696 | }; |
Chris Lattner | 269c232 | 2006-06-25 06:23:00 +0000 | [diff] [blame] | 1697 | struct PragmaDependencyHandler : public PragmaHandler { |
| 1698 | PragmaDependencyHandler(const IdentifierTokenInfo *ID) : PragmaHandler(ID) {} |
| 1699 | virtual void HandlePragma(Preprocessor &PP, LexerToken &DepToken) { |
| 1700 | PP.HandlePragmaDependency(DepToken); |
| 1701 | } |
| 1702 | }; |
Chris Lattner | 1786217 | 2006-06-24 22:12:56 +0000 | [diff] [blame] | 1703 | } |
| 1704 | |
Chris Lattner | b876183 | 2006-06-24 21:31:03 +0000 | [diff] [blame] | 1705 | |
| 1706 | /// RegisterBuiltinPragmas - Install the standard preprocessor pragmas: |
| 1707 | /// #pragma GCC poison/system_header/dependency and #pragma once. |
| 1708 | void Preprocessor::RegisterBuiltinPragmas() { |
| 1709 | AddPragmaHandler(0, new PragmaOnceHandler(getIdentifierInfo("once"))); |
Chris Lattner | 1786217 | 2006-06-24 22:12:56 +0000 | [diff] [blame] | 1710 | AddPragmaHandler("GCC", new PragmaPoisonHandler(getIdentifierInfo("poison"))); |
Chris Lattner | 55a6095 | 2006-06-25 04:20:34 +0000 | [diff] [blame] | 1711 | AddPragmaHandler("GCC", new PragmaSystemHeaderHandler( |
| 1712 | getIdentifierInfo("system_header"))); |
Chris Lattner | 269c232 | 2006-06-25 06:23:00 +0000 | [diff] [blame] | 1713 | AddPragmaHandler("GCC", new PragmaDependencyHandler( |
| 1714 | getIdentifierInfo("dependency"))); |
Chris Lattner | b876183 | 2006-06-24 21:31:03 +0000 | [diff] [blame] | 1715 | } |