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