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