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