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