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