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