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