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