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