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 | 677757a | 2006-06-28 05:26:32 +0000 | [diff] [blame] | 481 | |
Chris Lattner | f373a4a | 2006-06-26 06:16:29 +0000 | [diff] [blame] | 482 | /// HandleMacroExpandedIdentifier - If an identifier token is read that is to be |
| 483 | /// expanded as a macro, handle it and return the next token as 'Identifier'. |
Chris Lattner | 7818605 | 2006-07-09 00:45:31 +0000 | [diff] [blame^] | 484 | bool Preprocessor::HandleMacroExpandedIdentifier(LexerToken &Identifier, |
Chris Lattner | f373a4a | 2006-06-26 06:16:29 +0000 | [diff] [blame] | 485 | MacroInfo *MI) { |
Chris Lattner | 7818605 | 2006-07-09 00:45:31 +0000 | [diff] [blame^] | 486 | |
| 487 | // If this is a builtin macro, like __LINE__ or _Pragma, handle it specially. |
| 488 | if (MI->isBuiltinMacro()) { |
| 489 | ExpandBuiltinMacro(Identifier); |
| 490 | return false; |
| 491 | } |
| 492 | |
| 493 | /// FormalArgs - If this is a function-like macro expansion, this contains, |
| 494 | /// for each macro argument, the list of tokens that were provided to the |
| 495 | /// invocation. |
| 496 | MacroFormalArgs *FormalArgs = 0; |
| 497 | |
| 498 | // If this is a function-like macro, read the arguments. |
| 499 | if (MI->isFunctionLike()) { |
| 500 | // FIXME: We need to query to see if the ( exists without reading it. |
| 501 | |
| 502 | // C99 6.10.3p10: If the preprocessing token immediately after the the macro |
| 503 | // name isn't a '(', this macro should not be expanded. |
| 504 | bool isFunctionInvocation = true; |
| 505 | if (!isFunctionInvocation) |
| 506 | return true; |
| 507 | |
| 508 | LexerToken Tok; |
| 509 | LexUnexpandedToken(Tok); |
| 510 | assert(Tok.getKind() == tok::l_paren && |
| 511 | "not a function-like macro invocation!"); |
| 512 | |
| 513 | // Remember that we are now parsing the arguments to a macro invocation. |
| 514 | // Preprocessor directives used inside macro arguments are not portable, and |
| 515 | // this enables the warning. |
| 516 | InMacroFormalArgs = true; |
| 517 | FormalArgs = ReadFunctionLikeMacroFormalArgs(Identifier, MI); |
| 518 | |
| 519 | // Finished parsing args. |
| 520 | InMacroFormalArgs = false; |
| 521 | |
| 522 | // If there was an error parsing the arguments, bail out. |
| 523 | if (FormalArgs == 0) return false; |
| 524 | |
| 525 | ++NumFnMacroExpanded; |
| 526 | } else { |
| 527 | ++NumMacroExpanded; |
| 528 | } |
Chris Lattner | 13044d9 | 2006-07-03 05:16:44 +0000 | [diff] [blame] | 529 | |
| 530 | // Notice that this macro has been used. |
| 531 | MI->setIsUsed(true); |
Chris Lattner | 69772b0 | 2006-07-02 20:34:39 +0000 | [diff] [blame] | 532 | |
| 533 | // If we started lexing a macro, enter the macro expansion body. |
Chris Lattner | f373a4a | 2006-06-26 06:16:29 +0000 | [diff] [blame] | 534 | |
| 535 | // If this macro expands to no tokens, don't bother to push it onto the |
| 536 | // expansion stack, only to take it right back off. |
| 537 | if (MI->getNumTokens() == 0) { |
Chris Lattner | 7818605 | 2006-07-09 00:45:31 +0000 | [diff] [blame^] | 538 | // No need for formal arg info. |
| 539 | delete FormalArgs; |
| 540 | |
Chris Lattner | f373a4a | 2006-06-26 06:16:29 +0000 | [diff] [blame] | 541 | // Ignore this macro use, just return the next token in the current |
| 542 | // buffer. |
| 543 | bool HadLeadingSpace = Identifier.hasLeadingSpace(); |
| 544 | bool IsAtStartOfLine = Identifier.isAtStartOfLine(); |
| 545 | |
| 546 | Lex(Identifier); |
| 547 | |
| 548 | // If the identifier isn't on some OTHER line, inherit the leading |
| 549 | // whitespace/first-on-a-line property of this token. This handles |
| 550 | // stuff like "! XX," -> "! ," and " XX," -> " ,", when XX is |
| 551 | // empty. |
| 552 | if (!Identifier.isAtStartOfLine()) { |
| 553 | if (IsAtStartOfLine) Identifier.SetFlag(LexerToken::StartOfLine); |
| 554 | if (HadLeadingSpace) Identifier.SetFlag(LexerToken::LeadingSpace); |
| 555 | } |
| 556 | ++NumFastMacroExpanded; |
Chris Lattner | 7818605 | 2006-07-09 00:45:31 +0000 | [diff] [blame^] | 557 | return false; |
Chris Lattner | f373a4a | 2006-06-26 06:16:29 +0000 | [diff] [blame] | 558 | |
| 559 | } else if (MI->getNumTokens() == 1 && |
Chris Lattner | 7818605 | 2006-07-09 00:45:31 +0000 | [diff] [blame^] | 560 | // FIXME: Fn-Like Macros: Fast if arg not used. |
| 561 | FormalArgs == 0 && |
Chris Lattner | f373a4a | 2006-06-26 06:16:29 +0000 | [diff] [blame] | 562 | // Don't handle identifiers if they need recursive expansion. |
| 563 | (MI->getReplacementToken(0).getIdentifierInfo() == 0 || |
| 564 | !MI->getReplacementToken(0).getIdentifierInfo()->getMacroInfo())){ |
Chris Lattner | f373a4a | 2006-06-26 06:16:29 +0000 | [diff] [blame] | 565 | |
| 566 | // Otherwise, if this macro expands into a single trivially-expanded |
| 567 | // token: expand it now. This handles common cases like |
| 568 | // "#define VAL 42". |
| 569 | |
| 570 | // Propagate the isAtStartOfLine/hasLeadingSpace markers of the macro |
| 571 | // identifier to the expanded token. |
| 572 | bool isAtStartOfLine = Identifier.isAtStartOfLine(); |
| 573 | bool hasLeadingSpace = Identifier.hasLeadingSpace(); |
| 574 | |
| 575 | // Remember where the token is instantiated. |
| 576 | SourceLocation InstantiateLoc = Identifier.getLocation(); |
| 577 | |
| 578 | // Replace the result token. |
| 579 | Identifier = MI->getReplacementToken(0); |
| 580 | |
| 581 | // Restore the StartOfLine/LeadingSpace markers. |
| 582 | Identifier.SetFlagValue(LexerToken::StartOfLine , isAtStartOfLine); |
| 583 | Identifier.SetFlagValue(LexerToken::LeadingSpace, hasLeadingSpace); |
| 584 | |
| 585 | // Update the tokens location to include both its logical and physical |
| 586 | // locations. |
| 587 | SourceLocation Loc = |
Chris Lattner | c673f90 | 2006-06-30 06:10:41 +0000 | [diff] [blame] | 588 | SourceMgr.getInstantiationLoc(Identifier.getLocation(), InstantiateLoc); |
Chris Lattner | f373a4a | 2006-06-26 06:16:29 +0000 | [diff] [blame] | 589 | Identifier.SetLocation(Loc); |
| 590 | |
| 591 | // Since this is not an identifier token, it can't be macro expanded, so |
| 592 | // we're done. |
| 593 | ++NumFastMacroExpanded; |
Chris Lattner | 7818605 | 2006-07-09 00:45:31 +0000 | [diff] [blame^] | 594 | return false; |
Chris Lattner | f373a4a | 2006-06-26 06:16:29 +0000 | [diff] [blame] | 595 | } |
| 596 | |
Chris Lattner | 7818605 | 2006-07-09 00:45:31 +0000 | [diff] [blame^] | 597 | // Start expanding the macro. |
| 598 | EnterMacro(Identifier, FormalArgs); |
Chris Lattner | f373a4a | 2006-06-26 06:16:29 +0000 | [diff] [blame] | 599 | |
| 600 | // Now that the macro is at the top of the include stack, ask the |
| 601 | // preprocessor to read the next token from it. |
Chris Lattner | 7818605 | 2006-07-09 00:45:31 +0000 | [diff] [blame^] | 602 | Lex(Identifier); |
| 603 | return false; |
| 604 | } |
| 605 | |
| 606 | /// ReadFunctionLikeMacroFormalArgs - After reading "MACRO(", this method is |
| 607 | /// invoked to read all of the formal arguments specified for the macro |
| 608 | /// invocation. This returns null on error. |
| 609 | MacroFormalArgs *Preprocessor:: |
| 610 | ReadFunctionLikeMacroFormalArgs(LexerToken &MacroName, MacroInfo *MI) { |
| 611 | // Use an auto_ptr here so that the MacroFormalArgs object is deleted on |
| 612 | // all error paths. |
| 613 | std::auto_ptr<MacroFormalArgs> Args(new MacroFormalArgs(MI)); |
| 614 | |
| 615 | // The number of fixed arguments to parse. |
| 616 | unsigned NumFixedArgsLeft = MI->getNumArgs(); |
| 617 | bool isVariadic = MI->isVariadic(); |
| 618 | |
| 619 | // If this is a C99-style varargs macro invocation, add an extra expected |
| 620 | // argument, which will catch all of the varargs formals in one argument. |
| 621 | if (MI->isC99Varargs()) |
| 622 | ++NumFixedArgsLeft; |
| 623 | |
| 624 | // Outer loop, while there are more arguments, keep reading them. |
| 625 | LexerToken Tok; |
| 626 | Tok.SetKind(tok::comma); |
| 627 | --NumFixedArgsLeft; // Start reading the first arg. |
| 628 | |
| 629 | while (Tok.getKind() == tok::comma) { |
| 630 | // ArgTokens - Build up a list of tokens that make up this argument. |
| 631 | std::vector<LexerToken> ArgTokens; |
| 632 | // C99 6.10.3p11: Keep track of the number of l_parens we have seen. |
| 633 | unsigned NumParens = 0; |
| 634 | |
| 635 | while (1) { |
| 636 | LexUnexpandedToken(Tok); |
| 637 | |
| 638 | if (Tok.getKind() == tok::eof) { |
| 639 | Diag(MacroName, diag::err_unterm_macro_invoc); |
| 640 | // Do not lose the EOF. Return it to the client. |
| 641 | MacroName = Tok; |
| 642 | return 0; |
| 643 | } else if (Tok.getKind() == tok::r_paren) { |
| 644 | // If we found the ) token, the macro arg list is done. |
| 645 | if (NumParens-- == 0) |
| 646 | break; |
| 647 | } else if (Tok.getKind() == tok::l_paren) { |
| 648 | ++NumParens; |
| 649 | } else if (Tok.getKind() == tok::comma && NumParens == 0) { |
| 650 | // Comma ends this argument if there are more fixed arguments expected. |
| 651 | if (NumFixedArgsLeft) |
| 652 | break; |
| 653 | |
| 654 | // If this is not a variadic macro, too many formals were specified. |
| 655 | if (!isVariadic) { |
| 656 | // Emit the diagnostic at the macro name in case there is a missing ). |
| 657 | // Emitting it at the , could be far away from the macro name. |
| 658 | Diag(MacroName, diag::err_too_many_formals_in_macro_invoc); |
| 659 | return 0; |
| 660 | } |
| 661 | // Otherwise, continue to add the tokens to this variable argument. |
| 662 | } |
| 663 | |
| 664 | ArgTokens.push_back(Tok); |
| 665 | } |
| 666 | |
| 667 | // Remember the tokens that make up this argument. This destroys ArgTokens. |
| 668 | Args->addArgument(ArgTokens); |
| 669 | --NumFixedArgsLeft; |
| 670 | }; |
| 671 | |
| 672 | // Okay, we either found the r_paren. Check to see if we parsed too few |
| 673 | // arguments. |
| 674 | unsigned NumFormals = Args->getNumArguments(); |
| 675 | unsigned MinArgsExpected = MI->getNumArgs(); |
| 676 | |
| 677 | // C99 expects us to pass at least one vararg arg (but as an extension, we |
| 678 | // don't require this). |
| 679 | if (MI->isC99Varargs()) |
| 680 | ++MinArgsExpected; |
| 681 | |
| 682 | if (NumFormals < MinArgsExpected) { |
| 683 | // There are several cases where too few arguments is ok, handle them now. |
| 684 | if (NumFormals+1 == MinArgsExpected && MI->isVariadic()) { |
| 685 | // Varargs where the named vararg parameter is missing: ok as extension. |
| 686 | // #define A(x, ...) |
| 687 | // A("blah") |
| 688 | Diag(Tok, diag::ext_missing_varargs_arg); |
| 689 | } else if (MI->getNumArgs() == 1) { |
| 690 | // #define A(x) |
| 691 | // A() |
| 692 | // is ok. Add an empty argument. |
| 693 | std::vector<LexerToken> ArgTokens; |
| 694 | Args->addArgument(ArgTokens); |
| 695 | } else { |
| 696 | // Otherwise, emit the error. |
| 697 | Diag(Tok, diag::err_too_few_formals_in_macro_invoc); |
| 698 | return 0; |
| 699 | } |
| 700 | } |
| 701 | |
| 702 | return Args.release(); |
Chris Lattner | f373a4a | 2006-06-26 06:16:29 +0000 | [diff] [blame] | 703 | } |
| 704 | |
Chris Lattner | c673f90 | 2006-06-30 06:10:41 +0000 | [diff] [blame] | 705 | /// ComputeDATE_TIME - Compute the current time, enter it into the specified |
| 706 | /// scratch buffer, then return DATELoc/TIMELoc locations with the position of |
| 707 | /// the identifier tokens inserted. |
| 708 | static void ComputeDATE_TIME(SourceLocation &DATELoc, SourceLocation &TIMELoc, |
| 709 | ScratchBuffer *ScratchBuf) { |
| 710 | time_t TT = time(0); |
| 711 | struct tm *TM = localtime(&TT); |
| 712 | |
| 713 | static const char * const Months[] = { |
| 714 | "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec" |
| 715 | }; |
| 716 | |
| 717 | char TmpBuffer[100]; |
| 718 | sprintf(TmpBuffer, "\"%s %2d %4d\"", Months[TM->tm_mon], TM->tm_mday, |
| 719 | TM->tm_year+1900); |
| 720 | DATELoc = ScratchBuf->getToken(TmpBuffer, strlen(TmpBuffer)); |
| 721 | |
| 722 | sprintf(TmpBuffer, "\"%02d:%02d:%02d\"", TM->tm_hour, TM->tm_min, TM->tm_sec); |
| 723 | TIMELoc = ScratchBuf->getToken(TmpBuffer, strlen(TmpBuffer)); |
| 724 | } |
| 725 | |
Chris Lattner | 0b8cfc2 | 2006-06-28 06:49:17 +0000 | [diff] [blame] | 726 | /// ExpandBuiltinMacro - If an identifier token is read that is to be expanded |
| 727 | /// 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] | 728 | void Preprocessor::ExpandBuiltinMacro(LexerToken &Tok) { |
Chris Lattner | 0b8cfc2 | 2006-06-28 06:49:17 +0000 | [diff] [blame] | 729 | // Figure out which token this is. |
Chris Lattner | c79f6fb | 2006-07-04 17:53:21 +0000 | [diff] [blame] | 730 | IdentifierInfo *II = Tok.getIdentifierInfo(); |
| 731 | assert(II && "Can't be a macro without id info!"); |
Chris Lattner | 69772b0 | 2006-07-02 20:34:39 +0000 | [diff] [blame] | 732 | |
| 733 | // If this is an _Pragma directive, expand it, invoke the pragma handler, then |
| 734 | // lex the token after it. |
Chris Lattner | c79f6fb | 2006-07-04 17:53:21 +0000 | [diff] [blame] | 735 | if (II == Ident_Pragma) |
Chris Lattner | 69772b0 | 2006-07-02 20:34:39 +0000 | [diff] [blame] | 736 | return Handle_Pragma(Tok); |
| 737 | |
Chris Lattner | 7818605 | 2006-07-09 00:45:31 +0000 | [diff] [blame^] | 738 | ++NumBuiltinMacroExpanded; |
| 739 | |
Chris Lattner | 0b8cfc2 | 2006-06-28 06:49:17 +0000 | [diff] [blame] | 740 | char TmpBuffer[100]; |
Chris Lattner | 69772b0 | 2006-07-02 20:34:39 +0000 | [diff] [blame] | 741 | |
| 742 | // Set up the return result. |
Chris Lattner | 630b33c | 2006-07-01 22:46:53 +0000 | [diff] [blame] | 743 | Tok.SetIdentifierInfo(0); |
| 744 | Tok.ClearFlag(LexerToken::NeedsCleaning); |
| 745 | |
Chris Lattner | c79f6fb | 2006-07-04 17:53:21 +0000 | [diff] [blame] | 746 | if (II == Ident__LINE__) { |
Chris Lattner | 0b8cfc2 | 2006-06-28 06:49:17 +0000 | [diff] [blame] | 747 | // __LINE__ expands to a simple numeric value. |
| 748 | sprintf(TmpBuffer, "%u", SourceMgr.getLineNumber(Tok.getLocation())); |
| 749 | unsigned Length = strlen(TmpBuffer); |
| 750 | Tok.SetKind(tok::numeric_constant); |
| 751 | Tok.SetLength(Length); |
| 752 | Tok.SetLocation(ScratchBuf->getToken(TmpBuffer, Length, Tok.getLocation())); |
Chris Lattner | c79f6fb | 2006-07-04 17:53:21 +0000 | [diff] [blame] | 753 | } else if (II == Ident__FILE__ || II == Ident__BASE_FILE__) { |
Chris Lattner | c1283b9 | 2006-07-01 23:16:30 +0000 | [diff] [blame] | 754 | SourceLocation Loc = Tok.getLocation(); |
Chris Lattner | c79f6fb | 2006-07-04 17:53:21 +0000 | [diff] [blame] | 755 | if (II == Ident__BASE_FILE__) { |
Chris Lattner | c1283b9 | 2006-07-01 23:16:30 +0000 | [diff] [blame] | 756 | Diag(Tok, diag::ext_pp_base_file); |
| 757 | SourceLocation NextLoc = SourceMgr.getIncludeLoc(Loc.getFileID()); |
| 758 | while (NextLoc.getFileID() != 0) { |
| 759 | Loc = NextLoc; |
| 760 | NextLoc = SourceMgr.getIncludeLoc(Loc.getFileID()); |
| 761 | } |
| 762 | } |
| 763 | |
Chris Lattner | 0766e59 | 2006-07-03 01:07:01 +0000 | [diff] [blame] | 764 | // Escape this filename. Turn '\' -> '\\' '"' -> '\"' |
| 765 | std::string FN = SourceMgr.getSourceName(Loc); |
Chris Lattner | e3e81ea | 2006-07-03 01:13:26 +0000 | [diff] [blame] | 766 | FN = Lexer::Stringify(FN); |
Chris Lattner | 630b33c | 2006-07-01 22:46:53 +0000 | [diff] [blame] | 767 | Tok.SetKind(tok::string_literal); |
| 768 | Tok.SetLength(FN.size()); |
| 769 | Tok.SetLocation(ScratchBuf->getToken(&FN[0], FN.size(), Tok.getLocation())); |
Chris Lattner | c79f6fb | 2006-07-04 17:53:21 +0000 | [diff] [blame] | 770 | } else if (II == Ident__DATE__) { |
Chris Lattner | c673f90 | 2006-06-30 06:10:41 +0000 | [diff] [blame] | 771 | if (!DATELoc.isValid()) |
| 772 | ComputeDATE_TIME(DATELoc, TIMELoc, ScratchBuf); |
| 773 | Tok.SetKind(tok::string_literal); |
| 774 | Tok.SetLength(strlen("\"Mmm dd yyyy\"")); |
| 775 | Tok.SetLocation(SourceMgr.getInstantiationLoc(DATELoc, Tok.getLocation())); |
Chris Lattner | c79f6fb | 2006-07-04 17:53:21 +0000 | [diff] [blame] | 776 | } else if (II == Ident__TIME__) { |
Chris Lattner | c673f90 | 2006-06-30 06:10:41 +0000 | [diff] [blame] | 777 | if (!TIMELoc.isValid()) |
| 778 | ComputeDATE_TIME(DATELoc, TIMELoc, ScratchBuf); |
| 779 | Tok.SetKind(tok::string_literal); |
| 780 | Tok.SetLength(strlen("\"hh:mm:ss\"")); |
| 781 | Tok.SetLocation(SourceMgr.getInstantiationLoc(TIMELoc, Tok.getLocation())); |
Chris Lattner | c79f6fb | 2006-07-04 17:53:21 +0000 | [diff] [blame] | 782 | } else if (II == Ident__INCLUDE_LEVEL__) { |
Chris Lattner | c1283b9 | 2006-07-01 23:16:30 +0000 | [diff] [blame] | 783 | Diag(Tok, diag::ext_pp_include_level); |
| 784 | |
| 785 | // Compute the include depth of this token. |
| 786 | unsigned Depth = 0; |
| 787 | SourceLocation Loc = SourceMgr.getIncludeLoc(Tok.getLocation().getFileID()); |
| 788 | for (; Loc.getFileID() != 0; ++Depth) |
| 789 | Loc = SourceMgr.getIncludeLoc(Loc.getFileID()); |
| 790 | |
| 791 | // __INCLUDE_LEVEL__ expands to a simple numeric value. |
| 792 | sprintf(TmpBuffer, "%u", Depth); |
| 793 | unsigned Length = strlen(TmpBuffer); |
| 794 | Tok.SetKind(tok::numeric_constant); |
| 795 | Tok.SetLength(Length); |
| 796 | Tok.SetLocation(ScratchBuf->getToken(TmpBuffer, Length, Tok.getLocation())); |
Chris Lattner | c79f6fb | 2006-07-04 17:53:21 +0000 | [diff] [blame] | 797 | } else if (II == Ident__TIMESTAMP__) { |
Chris Lattner | 847e0e4 | 2006-07-01 23:49:16 +0000 | [diff] [blame] | 798 | // MSVC, ICC, GCC, VisualAge C++ extension. The generated string should be |
| 799 | // of the form "Ddd Mmm dd hh::mm::ss yyyy", which is returned by asctime. |
| 800 | Diag(Tok, diag::ext_pp_timestamp); |
| 801 | |
| 802 | // Get the file that we are lexing out of. If we're currently lexing from |
| 803 | // a macro, dig into the include stack. |
| 804 | const FileEntry *CurFile = 0; |
Chris Lattner | ecfeafe | 2006-07-02 21:26:45 +0000 | [diff] [blame] | 805 | Lexer *TheLexer = getCurrentFileLexer(); |
Chris Lattner | 847e0e4 | 2006-07-01 23:49:16 +0000 | [diff] [blame] | 806 | |
| 807 | if (TheLexer) |
| 808 | CurFile = SourceMgr.getFileEntryForFileID(TheLexer->getCurFileID()); |
| 809 | |
| 810 | // If this file is older than the file it depends on, emit a diagnostic. |
| 811 | const char *Result; |
| 812 | if (CurFile) { |
| 813 | time_t TT = CurFile->getModificationTime(); |
| 814 | struct tm *TM = localtime(&TT); |
| 815 | Result = asctime(TM); |
| 816 | } else { |
| 817 | Result = "??? ??? ?? ??:??:?? ????\n"; |
| 818 | } |
| 819 | TmpBuffer[0] = '"'; |
| 820 | strcpy(TmpBuffer+1, Result); |
| 821 | unsigned Len = strlen(TmpBuffer); |
| 822 | TmpBuffer[Len-1] = '"'; // Replace the newline with a quote. |
| 823 | Tok.SetKind(tok::string_literal); |
| 824 | Tok.SetLength(Len); |
| 825 | Tok.SetLocation(ScratchBuf->getToken(TmpBuffer, Len, Tok.getLocation())); |
Chris Lattner | 0b8cfc2 | 2006-06-28 06:49:17 +0000 | [diff] [blame] | 826 | } else { |
| 827 | assert(0 && "Unknown identifier!"); |
| 828 | } |
| 829 | } |
Chris Lattner | 677757a | 2006-06-28 05:26:32 +0000 | [diff] [blame] | 830 | |
Chris Lattner | 13044d9 | 2006-07-03 05:16:44 +0000 | [diff] [blame] | 831 | namespace { |
| 832 | struct UnusedIdentifierReporter : public IdentifierVisitor { |
| 833 | Preprocessor &PP; |
| 834 | UnusedIdentifierReporter(Preprocessor &pp) : PP(pp) {} |
| 835 | |
Chris Lattner | c79f6fb | 2006-07-04 17:53:21 +0000 | [diff] [blame] | 836 | void VisitIdentifier(IdentifierInfo &II) const { |
| 837 | if (II.getMacroInfo() && !II.getMacroInfo()->isUsed()) |
| 838 | PP.Diag(II.getMacroInfo()->getDefinitionLoc(), diag::pp_macro_not_used); |
Chris Lattner | 13044d9 | 2006-07-03 05:16:44 +0000 | [diff] [blame] | 839 | } |
| 840 | }; |
| 841 | } |
| 842 | |
Chris Lattner | 677757a | 2006-06-28 05:26:32 +0000 | [diff] [blame] | 843 | //===----------------------------------------------------------------------===// |
| 844 | // Lexer Event Handling. |
| 845 | //===----------------------------------------------------------------------===// |
| 846 | |
Chris Lattner | cefc768 | 2006-07-08 08:28:12 +0000 | [diff] [blame] | 847 | /// LookUpIdentifierInfo - Given a tok::identifier token, look up the |
| 848 | /// identifier information for the token and install it into the token. |
| 849 | IdentifierInfo *Preprocessor::LookUpIdentifierInfo(LexerToken &Identifier, |
| 850 | const char *BufPtr) { |
| 851 | assert(Identifier.getKind() == tok::identifier && "Not an identifier!"); |
| 852 | assert(Identifier.getIdentifierInfo() == 0 && "Identinfo already exists!"); |
| 853 | |
| 854 | // Look up this token, see if it is a macro, or if it is a language keyword. |
| 855 | IdentifierInfo *II; |
| 856 | if (BufPtr && !Identifier.needsCleaning()) { |
| 857 | // No cleaning needed, just use the characters from the lexed buffer. |
| 858 | II = getIdentifierInfo(BufPtr, BufPtr+Identifier.getLength()); |
| 859 | } else { |
| 860 | // Cleaning needed, alloca a buffer, clean into it, then use the buffer. |
| 861 | const char *TmpBuf = (char*)alloca(Identifier.getLength()); |
| 862 | unsigned Size = getSpelling(Identifier, TmpBuf); |
| 863 | II = getIdentifierInfo(TmpBuf, TmpBuf+Size); |
| 864 | } |
| 865 | Identifier.SetIdentifierInfo(II); |
| 866 | return II; |
| 867 | } |
| 868 | |
| 869 | |
Chris Lattner | 677757a | 2006-06-28 05:26:32 +0000 | [diff] [blame] | 870 | /// HandleIdentifier - This callback is invoked when the lexer reads an |
| 871 | /// identifier. This callback looks up the identifier in the map and/or |
| 872 | /// potentially macro expands it or turns it into a named token (like 'for'). |
| 873 | void Preprocessor::HandleIdentifier(LexerToken &Identifier) { |
| 874 | if (Identifier.getIdentifierInfo() == 0) { |
| 875 | // If we are skipping tokens (because we are in a #if 0 block), there will |
| 876 | // be no identifier info, just return the token. |
| 877 | assert(isSkipping() && "Token isn't an identifier?"); |
| 878 | return; |
| 879 | } |
Chris Lattner | c79f6fb | 2006-07-04 17:53:21 +0000 | [diff] [blame] | 880 | IdentifierInfo &II = *Identifier.getIdentifierInfo(); |
Chris Lattner | 677757a | 2006-06-28 05:26:32 +0000 | [diff] [blame] | 881 | |
| 882 | // If this identifier was poisoned, and if it was not produced from a macro |
| 883 | // expansion, emit an error. |
Chris Lattner | 8ff7199 | 2006-07-06 05:17:39 +0000 | [diff] [blame] | 884 | if (II.isPoisoned() && CurLexer) { |
| 885 | if (&II != Ident__VA_ARGS__) // We warn about __VA_ARGS__ with poisoning. |
| 886 | Diag(Identifier, diag::err_pp_used_poisoned_id); |
| 887 | else |
| 888 | Diag(Identifier, diag::ext_pp_bad_vaargs_use); |
| 889 | } |
Chris Lattner | 677757a | 2006-06-28 05:26:32 +0000 | [diff] [blame] | 890 | |
Chris Lattner | 7818605 | 2006-07-09 00:45:31 +0000 | [diff] [blame^] | 891 | // If this is a macro to be expanded, do it. |
Chris Lattner | c79f6fb | 2006-07-04 17:53:21 +0000 | [diff] [blame] | 892 | if (MacroInfo *MI = II.getMacroInfo()) |
Chris Lattner | 677757a | 2006-06-28 05:26:32 +0000 | [diff] [blame] | 893 | if (MI->isEnabled() && !DisableMacroExpansion) |
Chris Lattner | 7818605 | 2006-07-09 00:45:31 +0000 | [diff] [blame^] | 894 | if (!HandleMacroExpandedIdentifier(Identifier, MI)) |
| 895 | return; |
Chris Lattner | 677757a | 2006-06-28 05:26:32 +0000 | [diff] [blame] | 896 | |
| 897 | // Change the kind of this identifier to the appropriate token kind, e.g. |
| 898 | // turning "for" into a keyword. |
Chris Lattner | c79f6fb | 2006-07-04 17:53:21 +0000 | [diff] [blame] | 899 | Identifier.SetKind(II.getTokenID()); |
Chris Lattner | 677757a | 2006-06-28 05:26:32 +0000 | [diff] [blame] | 900 | |
| 901 | // If this is an extension token, diagnose its use. |
Chris Lattner | c79f6fb | 2006-07-04 17:53:21 +0000 | [diff] [blame] | 902 | if (II.isExtensionToken()) Diag(Identifier, diag::ext_token_used); |
Chris Lattner | 677757a | 2006-06-28 05:26:32 +0000 | [diff] [blame] | 903 | } |
| 904 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 905 | /// HandleEndOfFile - This callback is invoked when the lexer hits the end of |
| 906 | /// the current file. This either returns the EOF token or pops a level off |
| 907 | /// the include stack and keeps going. |
Chris Lattner | 0c885f5 | 2006-06-21 06:50:18 +0000 | [diff] [blame] | 908 | void Preprocessor::HandleEndOfFile(LexerToken &Result, bool isEndOfMacro) { |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 909 | assert(!CurMacroExpander && |
| 910 | "Ending a file when currently in a macro!"); |
| 911 | |
| 912 | // If we are in a #if 0 block skipping tokens, and we see the end of the file, |
| 913 | // this is an error condition. Just return the EOF token up to |
| 914 | // SkipExcludedConditionalBlock. The Lexer will have already have issued |
| 915 | // errors for the unterminated #if's on the conditional stack. |
| 916 | if (isSkipping()) { |
Chris Lattner | d01e291 | 2006-06-18 16:22:51 +0000 | [diff] [blame] | 917 | Result.StartToken(); |
| 918 | CurLexer->BufferPtr = CurLexer->BufferEnd; |
| 919 | CurLexer->FormTokenWithChars(Result, CurLexer->BufferEnd); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 920 | Result.SetKind(tok::eof); |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 921 | return; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 922 | } |
| 923 | |
Chris Lattner | 371ac8a | 2006-07-04 07:11:10 +0000 | [diff] [blame] | 924 | // See if this file had a controlling macro. |
Chris Lattner | 3665f16 | 2006-07-04 07:26:10 +0000 | [diff] [blame] | 925 | if (CurLexer) { // Not ending a macro, ignore it. |
Chris Lattner | c79f6fb | 2006-07-04 17:53:21 +0000 | [diff] [blame] | 926 | if (const IdentifierInfo *ControllingMacro = |
Chris Lattner | 371ac8a | 2006-07-04 07:11:10 +0000 | [diff] [blame] | 927 | CurLexer->MIOpt.GetControllingMacroAtEndOfFile()) { |
Chris Lattner | 3665f16 | 2006-07-04 07:26:10 +0000 | [diff] [blame] | 928 | // Okay, this has a controlling macro, remember in PerFileInfo. |
| 929 | if (const FileEntry *FE = |
| 930 | SourceMgr.getFileEntryForFileID(CurLexer->getCurFileID())) |
| 931 | getFileInfo(FE).ControllingMacro = ControllingMacro; |
Chris Lattner | 371ac8a | 2006-07-04 07:11:10 +0000 | [diff] [blame] | 932 | } |
| 933 | } |
| 934 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 935 | // If this is a #include'd file, pop it off the include stack and continue |
| 936 | // lexing the #includer file. |
Chris Lattner | 69772b0 | 2006-07-02 20:34:39 +0000 | [diff] [blame] | 937 | if (!IncludeMacroStack.empty()) { |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 938 | // We're done with the #included file. |
| 939 | delete CurLexer; |
Chris Lattner | 69772b0 | 2006-07-02 20:34:39 +0000 | [diff] [blame] | 940 | CurLexer = IncludeMacroStack.back().TheLexer; |
| 941 | CurDirLookup = IncludeMacroStack.back().TheDirLookup; |
| 942 | CurMacroExpander = IncludeMacroStack.back().TheMacroExpander; |
| 943 | IncludeMacroStack.pop_back(); |
Chris Lattner | 0c885f5 | 2006-06-21 06:50:18 +0000 | [diff] [blame] | 944 | |
| 945 | // 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] | 946 | if (FileChangeHandler && !isEndOfMacro && CurLexer) { |
Chris Lattner | c899718 | 2006-06-22 05:52:16 +0000 | [diff] [blame] | 947 | DirectoryLookup::DirType FileType = DirectoryLookup::NormalHeaderDir; |
| 948 | |
| 949 | // Get the file entry for the current file. |
| 950 | if (const FileEntry *FE = |
| 951 | SourceMgr.getFileEntryForFileID(CurLexer->getCurFileID())) |
| 952 | FileType = getFileInfo(FE).DirInfo; |
| 953 | |
Chris Lattner | 0c885f5 | 2006-06-21 06:50:18 +0000 | [diff] [blame] | 954 | FileChangeHandler(CurLexer->getSourceLocation(CurLexer->BufferPtr), |
Chris Lattner | 55a6095 | 2006-06-25 04:20:34 +0000 | [diff] [blame] | 955 | ExitFile, FileType); |
Chris Lattner | c899718 | 2006-06-22 05:52:16 +0000 | [diff] [blame] | 956 | } |
Chris Lattner | 0c885f5 | 2006-06-21 06:50:18 +0000 | [diff] [blame] | 957 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 958 | return Lex(Result); |
| 959 | } |
| 960 | |
Chris Lattner | d01e291 | 2006-06-18 16:22:51 +0000 | [diff] [blame] | 961 | Result.StartToken(); |
| 962 | CurLexer->BufferPtr = CurLexer->BufferEnd; |
| 963 | CurLexer->FormTokenWithChars(Result, CurLexer->BufferEnd); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 964 | Result.SetKind(tok::eof); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 965 | |
| 966 | // We're done with the #included file. |
| 967 | delete CurLexer; |
| 968 | CurLexer = 0; |
Chris Lattner | 13044d9 | 2006-07-03 05:16:44 +0000 | [diff] [blame] | 969 | |
| 970 | // This is the end of the top-level file. |
Chris Lattner | c79f6fb | 2006-07-04 17:53:21 +0000 | [diff] [blame] | 971 | Identifiers.VisitIdentifiers(UnusedIdentifierReporter(*this)); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 972 | } |
| 973 | |
| 974 | /// HandleEndOfMacro - This callback is invoked when the lexer hits the end of |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 975 | /// the current macro line. |
| 976 | void Preprocessor::HandleEndOfMacro(LexerToken &Result) { |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 977 | assert(CurMacroExpander && !CurLexer && |
| 978 | "Ending a macro when currently in a #include file!"); |
| 979 | |
| 980 | // Mark macro not ignored now that it is no longer being expanded. |
| 981 | CurMacroExpander->getMacro().EnableMacro(); |
| 982 | delete CurMacroExpander; |
| 983 | |
Chris Lattner | 69772b0 | 2006-07-02 20:34:39 +0000 | [diff] [blame] | 984 | // Handle this like a #include file being popped off the stack. |
| 985 | CurMacroExpander = 0; |
| 986 | return HandleEndOfFile(Result, true); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 987 | } |
| 988 | |
| 989 | |
| 990 | //===----------------------------------------------------------------------===// |
| 991 | // Utility Methods for Preprocessor Directive Handling. |
| 992 | //===----------------------------------------------------------------------===// |
| 993 | |
| 994 | /// DiscardUntilEndOfDirective - Read and discard all tokens remaining on the |
| 995 | /// current line until the tok::eom token is found. |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 996 | void Preprocessor::DiscardUntilEndOfDirective() { |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 997 | LexerToken Tmp; |
| 998 | do { |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 999 | LexUnexpandedToken(Tmp); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1000 | } while (Tmp.getKind() != tok::eom); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1001 | } |
| 1002 | |
| 1003 | /// ReadMacroName - Lex and validate a macro name, which occurs after a |
| 1004 | /// #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] | 1005 | /// of the macro line if the macro name is invalid. isDefineUndef is 1 if |
| 1006 | /// 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] | 1007 | /// else (e.g. #ifdef). |
Chris Lattner | e8eef32 | 2006-07-08 07:01:00 +0000 | [diff] [blame] | 1008 | void Preprocessor::ReadMacroName(LexerToken &MacroNameTok, char isDefineUndef) { |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1009 | // Read the token, don't allow macro expansion on it. |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1010 | LexUnexpandedToken(MacroNameTok); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1011 | |
| 1012 | // Missing macro name? |
| 1013 | if (MacroNameTok.getKind() == tok::eom) |
| 1014 | return Diag(MacroNameTok, diag::err_pp_missing_macro_name); |
| 1015 | |
Chris Lattner | c79f6fb | 2006-07-04 17:53:21 +0000 | [diff] [blame] | 1016 | IdentifierInfo *II = MacroNameTok.getIdentifierInfo(); |
| 1017 | if (II == 0) { |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1018 | Diag(MacroNameTok, diag::err_pp_macro_not_identifier); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1019 | // Fall through on error. |
| 1020 | } else if (0) { |
Chris Lattner | 4d5e1a7 | 2006-07-03 01:01:29 +0000 | [diff] [blame] | 1021 | // FIXME: C++. Error if defining a C++ named operator. |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1022 | |
Chris Lattner | c79f6fb | 2006-07-04 17:53:21 +0000 | [diff] [blame] | 1023 | } else if (isDefineUndef && II->getName()[0] == 'd' && // defined |
| 1024 | !strcmp(II->getName()+1, "efined")) { |
Chris Lattner | 44f8a66 | 2006-07-03 01:27:27 +0000 | [diff] [blame] | 1025 | // Error if defining "defined": C99 6.10.8.4. |
Chris Lattner | aaf0911 | 2006-07-03 01:17:59 +0000 | [diff] [blame] | 1026 | Diag(MacroNameTok, diag::err_defined_macro_name); |
Chris Lattner | c79f6fb | 2006-07-04 17:53:21 +0000 | [diff] [blame] | 1027 | } else if (isDefineUndef && II->getMacroInfo() && |
| 1028 | II->getMacroInfo()->isBuiltinMacro()) { |
Chris Lattner | 44f8a66 | 2006-07-03 01:27:27 +0000 | [diff] [blame] | 1029 | // Error if defining "__LINE__" and other builtins: C99 6.10.8.4. |
Chris Lattner | e8eef32 | 2006-07-08 07:01:00 +0000 | [diff] [blame] | 1030 | if (isDefineUndef == 1) |
| 1031 | Diag(MacroNameTok, diag::pp_redef_builtin_macro); |
| 1032 | else |
| 1033 | Diag(MacroNameTok, diag::pp_undef_builtin_macro); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1034 | } else { |
| 1035 | // Okay, we got a good identifier node. Return it. |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1036 | return; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1037 | } |
| 1038 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1039 | // Invalid macro name, read and discard the rest of the line. Then set the |
| 1040 | // token kind to tok::eom. |
| 1041 | MacroNameTok.SetKind(tok::eom); |
| 1042 | return DiscardUntilEndOfDirective(); |
| 1043 | } |
| 1044 | |
| 1045 | /// CheckEndOfDirective - Ensure that the next token is a tok::eom token. If |
| 1046 | /// not, emit a diagnostic and consume up until the eom. |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1047 | void Preprocessor::CheckEndOfDirective(const char *DirType) { |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1048 | LexerToken Tmp; |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1049 | Lex(Tmp); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1050 | // There should be no tokens after the directive, but we allow them as an |
| 1051 | // extension. |
| 1052 | if (Tmp.getKind() != tok::eom) { |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1053 | Diag(Tmp, diag::ext_pp_extra_tokens_at_eol, DirType); |
| 1054 | DiscardUntilEndOfDirective(); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1055 | } |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1056 | } |
| 1057 | |
| 1058 | |
| 1059 | |
| 1060 | /// SkipExcludedConditionalBlock - We just read a #if or related directive and |
| 1061 | /// decided that the subsequent tokens are in the #if'd out portion of the |
| 1062 | /// file. Lex the rest of the file, until we see an #endif. If |
| 1063 | /// FoundNonSkipPortion is true, then we have already emitted code for part of |
| 1064 | /// this #if directive, so #else/#elif blocks should never be entered. If ElseOk |
| 1065 | /// is true, then #else directives are ok, if not, then we have already seen one |
| 1066 | /// so a #else directive is a duplicate. When this returns, the caller can lex |
| 1067 | /// the first valid token. |
Chris Lattner | d01e291 | 2006-06-18 16:22:51 +0000 | [diff] [blame] | 1068 | void Preprocessor::SkipExcludedConditionalBlock(SourceLocation IfTokenLoc, |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1069 | bool FoundNonSkipPortion, |
| 1070 | bool FoundElse) { |
| 1071 | ++NumSkipped; |
Chris Lattner | 69772b0 | 2006-07-02 20:34:39 +0000 | [diff] [blame] | 1072 | assert(CurMacroExpander == 0 && CurLexer && |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1073 | "Lexing a macro, not a file?"); |
| 1074 | |
| 1075 | CurLexer->pushConditionalLevel(IfTokenLoc, /*isSkipping*/false, |
| 1076 | FoundNonSkipPortion, FoundElse); |
| 1077 | |
| 1078 | // Know that we are going to be skipping tokens. Set this flag to indicate |
| 1079 | // this, which has a couple of effects: |
| 1080 | // 1. If EOF of the current lexer is found, the include stack isn't popped. |
| 1081 | // 2. Identifier information is not looked up for identifier tokens. As an |
| 1082 | // effect of this, implicit macro expansion is naturally disabled. |
| 1083 | // 3. "#" tokens at the start of a line are treated as normal tokens, not |
| 1084 | // implicitly transformed by the lexer. |
| 1085 | // 4. All notes, warnings, and extension messages are disabled. |
| 1086 | // |
| 1087 | SkippingContents = true; |
| 1088 | LexerToken Tok; |
| 1089 | while (1) { |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1090 | CurLexer->Lex(Tok); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1091 | |
| 1092 | // If this is the end of the buffer, we have an error. The lexer will have |
| 1093 | // already handled this error condition, so just return and let the caller |
| 1094 | // lex after this #include. |
| 1095 | if (Tok.getKind() == tok::eof) break; |
| 1096 | |
| 1097 | // If this token is not a preprocessor directive, just skip it. |
| 1098 | if (Tok.getKind() != tok::hash || !Tok.isAtStartOfLine()) |
| 1099 | continue; |
| 1100 | |
| 1101 | // We just parsed a # character at the start of a line, so we're in |
| 1102 | // directive mode. Tell the lexer this so any newlines we see will be |
| 1103 | // converted into an EOM token (this terminates the macro). |
| 1104 | CurLexer->ParsingPreprocessorDirective = true; |
| 1105 | |
| 1106 | // Read the next token, the directive flavor. |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1107 | LexUnexpandedToken(Tok); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1108 | |
| 1109 | // If this isn't an identifier directive (e.g. is "# 1\n" or "#\n", or |
| 1110 | // something bogus), skip it. |
| 1111 | if (Tok.getKind() != tok::identifier) { |
| 1112 | CurLexer->ParsingPreprocessorDirective = false; |
| 1113 | continue; |
| 1114 | } |
Chris Lattner | e60165f | 2006-06-22 06:36:29 +0000 | [diff] [blame] | 1115 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1116 | // If the first letter isn't i or e, it isn't intesting to us. We know that |
| 1117 | // this is safe in the face of spelling differences, because there is no way |
| 1118 | // 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] | 1119 | // allows us to avoid looking up the identifier info for #define/#undef and |
| 1120 | // other common directives. |
| 1121 | const char *RawCharData = SourceMgr.getCharacterData(Tok.getLocation()); |
| 1122 | char FirstChar = RawCharData[0]; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1123 | if (FirstChar >= 'a' && FirstChar <= 'z' && |
| 1124 | FirstChar != 'i' && FirstChar != 'e') { |
| 1125 | CurLexer->ParsingPreprocessorDirective = false; |
| 1126 | continue; |
| 1127 | } |
| 1128 | |
Chris Lattner | e60165f | 2006-06-22 06:36:29 +0000 | [diff] [blame] | 1129 | // Get the identifier name without trigraphs or embedded newlines. Note |
| 1130 | // that we can't use Tok.getIdentifierInfo() because its lookup is disabled |
| 1131 | // when skipping. |
| 1132 | // TODO: could do this with zero copies in the no-clean case by using |
| 1133 | // strncmp below. |
| 1134 | char Directive[20]; |
| 1135 | unsigned IdLen; |
| 1136 | if (!Tok.needsCleaning() && Tok.getLength() < 20) { |
| 1137 | IdLen = Tok.getLength(); |
| 1138 | memcpy(Directive, RawCharData, IdLen); |
| 1139 | Directive[IdLen] = 0; |
| 1140 | } else { |
| 1141 | std::string DirectiveStr = getSpelling(Tok); |
| 1142 | IdLen = DirectiveStr.size(); |
| 1143 | if (IdLen >= 20) { |
| 1144 | CurLexer->ParsingPreprocessorDirective = false; |
| 1145 | continue; |
| 1146 | } |
| 1147 | memcpy(Directive, &DirectiveStr[0], IdLen); |
| 1148 | Directive[IdLen] = 0; |
| 1149 | } |
| 1150 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1151 | if (FirstChar == 'i' && Directive[1] == 'f') { |
Chris Lattner | e60165f | 2006-06-22 06:36:29 +0000 | [diff] [blame] | 1152 | if ((IdLen == 2) || // "if" |
| 1153 | (IdLen == 5 && !strcmp(Directive+2, "def")) || // "ifdef" |
| 1154 | (IdLen == 6 && !strcmp(Directive+2, "ndef"))) { // "ifndef" |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1155 | // We know the entire #if/#ifdef/#ifndef block will be skipped, don't |
| 1156 | // bother parsing the condition. |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1157 | DiscardUntilEndOfDirective(); |
Chris Lattner | 50b497e | 2006-06-18 16:32:35 +0000 | [diff] [blame] | 1158 | CurLexer->pushConditionalLevel(Tok.getLocation(), /*wasskipping*/true, |
Chris Lattner | d01e291 | 2006-06-18 16:22:51 +0000 | [diff] [blame] | 1159 | /*foundnonskip*/false, |
| 1160 | /*fnddelse*/false); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1161 | } |
| 1162 | } else if (FirstChar == 'e') { |
Chris Lattner | e60165f | 2006-06-22 06:36:29 +0000 | [diff] [blame] | 1163 | if (IdLen == 5 && !strcmp(Directive+1, "ndif")) { // "endif" |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1164 | CheckEndOfDirective("#endif"); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1165 | PPConditionalInfo CondInfo; |
| 1166 | CondInfo.WasSkipping = true; // Silence bogus warning. |
| 1167 | bool InCond = CurLexer->popConditionalLevel(CondInfo); |
| 1168 | assert(!InCond && "Can't be skipping if not in a conditional!"); |
| 1169 | |
| 1170 | // If we popped the outermost skipping block, we're done skipping! |
| 1171 | if (!CondInfo.WasSkipping) |
| 1172 | break; |
Chris Lattner | e60165f | 2006-06-22 06:36:29 +0000 | [diff] [blame] | 1173 | } else if (IdLen == 4 && !strcmp(Directive+1, "lse")) { // "else". |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1174 | // #else directive in a skipping conditional. If not in some other |
| 1175 | // skipping conditional, and if #else hasn't already been seen, enter it |
| 1176 | // as a non-skipping conditional. |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1177 | CheckEndOfDirective("#else"); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1178 | PPConditionalInfo &CondInfo = CurLexer->peekConditionalLevel(); |
| 1179 | |
| 1180 | // 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] | 1181 | if (CondInfo.FoundElse) Diag(Tok, diag::pp_err_else_after_else); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1182 | |
| 1183 | // Note that we've seen a #else in this conditional. |
| 1184 | CondInfo.FoundElse = true; |
| 1185 | |
| 1186 | // If the conditional is at the top level, and the #if block wasn't |
| 1187 | // entered, enter the #else block now. |
| 1188 | if (!CondInfo.WasSkipping && !CondInfo.FoundNonSkip) { |
| 1189 | CondInfo.FoundNonSkip = true; |
| 1190 | break; |
| 1191 | } |
Chris Lattner | e60165f | 2006-06-22 06:36:29 +0000 | [diff] [blame] | 1192 | } else if (IdLen == 4 && !strcmp(Directive+1, "lif")) { // "elif". |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1193 | PPConditionalInfo &CondInfo = CurLexer->peekConditionalLevel(); |
| 1194 | |
| 1195 | bool ShouldEnter; |
| 1196 | // If this is in a skipping block or if we're already handled this #if |
| 1197 | // block, don't bother parsing the condition. |
| 1198 | if (CondInfo.WasSkipping || CondInfo.FoundNonSkip) { |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1199 | DiscardUntilEndOfDirective(); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1200 | ShouldEnter = false; |
| 1201 | } else { |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1202 | // Restore the value of SkippingContents so that identifiers are |
| 1203 | // looked up, etc, inside the #elif expression. |
| 1204 | assert(SkippingContents && "We have to be skipping here!"); |
| 1205 | SkippingContents = false; |
Chris Lattner | c79f6fb | 2006-07-04 17:53:21 +0000 | [diff] [blame] | 1206 | IdentifierInfo *IfNDefMacro = 0; |
Chris Lattner | a8654ca | 2006-07-04 17:42:08 +0000 | [diff] [blame] | 1207 | ShouldEnter = EvaluateDirectiveExpression(IfNDefMacro); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1208 | SkippingContents = true; |
| 1209 | } |
| 1210 | |
| 1211 | // 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] | 1212 | if (CondInfo.FoundElse) Diag(Tok, diag::pp_err_elif_after_else); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1213 | |
| 1214 | // If this condition is true, enter it! |
| 1215 | if (ShouldEnter) { |
| 1216 | CondInfo.FoundNonSkip = true; |
| 1217 | break; |
| 1218 | } |
| 1219 | } |
| 1220 | } |
| 1221 | |
| 1222 | CurLexer->ParsingPreprocessorDirective = false; |
| 1223 | } |
| 1224 | |
| 1225 | // Finally, if we are out of the conditional (saw an #endif or ran off the end |
| 1226 | // of the file, just stop skipping and return to lexing whatever came after |
| 1227 | // the #if block. |
| 1228 | SkippingContents = false; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1229 | } |
| 1230 | |
| 1231 | //===----------------------------------------------------------------------===// |
| 1232 | // Preprocessor Directive Handling. |
| 1233 | //===----------------------------------------------------------------------===// |
| 1234 | |
| 1235 | /// HandleDirective - This callback is invoked when the lexer sees a # token |
| 1236 | /// at the start of a line. This consumes the directive, modifies the |
| 1237 | /// lexer/preprocessor state, and advances the lexer(s) so that the next token |
| 1238 | /// read is the correct one. |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1239 | void Preprocessor::HandleDirective(LexerToken &Result) { |
Chris Lattner | 4d5e1a7 | 2006-07-03 01:01:29 +0000 | [diff] [blame] | 1240 | // FIXME: Traditional: # with whitespace before it not recognized by K&R? |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1241 | |
| 1242 | // We just parsed a # character at the start of a line, so we're in directive |
| 1243 | // 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^] | 1244 | // EOM token (which terminates the directive). |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1245 | CurLexer->ParsingPreprocessorDirective = true; |
| 1246 | |
| 1247 | ++NumDirectives; |
| 1248 | |
Chris Lattner | 371ac8a | 2006-07-04 07:11:10 +0000 | [diff] [blame] | 1249 | // We are about to read a token. For the multiple-include optimization FA to |
| 1250 | // work, we have to remember if we had read any tokens *before* this |
| 1251 | // pp-directive. |
| 1252 | bool ReadAnyTokensBeforeDirective = CurLexer->MIOpt.getHasReadAnyTokensVal(); |
| 1253 | |
Chris Lattner | 7818605 | 2006-07-09 00:45:31 +0000 | [diff] [blame^] | 1254 | // Read the next token, the directive flavor. This isn't expanded due to |
| 1255 | // C99 6.10.3p8. |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1256 | LexUnexpandedToken(Result); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1257 | |
Chris Lattner | 7818605 | 2006-07-09 00:45:31 +0000 | [diff] [blame^] | 1258 | // C99 6.10.3p11: Is this preprocessor directive in macro invocation? e.g.: |
| 1259 | // #define A(x) #x |
| 1260 | // A(abc |
| 1261 | // #warning blah |
| 1262 | // def) |
| 1263 | // If so, the user is relying on non-portable behavior, emit a diagnostic. |
| 1264 | if (InMacroFormalArgs) |
| 1265 | Diag(Result, diag::ext_embedded_directive); |
| 1266 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1267 | switch (Result.getKind()) { |
| 1268 | default: break; |
| 1269 | case tok::eom: |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1270 | return; // null directive. |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1271 | |
| 1272 | #if 0 |
| 1273 | case tok::numeric_constant: |
| 1274 | // FIXME: implement # 7 line numbers! |
| 1275 | break; |
| 1276 | #endif |
| 1277 | case tok::kw_else: |
| 1278 | return HandleElseDirective(Result); |
| 1279 | case tok::kw_if: |
Chris Lattner | a8654ca | 2006-07-04 17:42:08 +0000 | [diff] [blame] | 1280 | return HandleIfDirective(Result, ReadAnyTokensBeforeDirective); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1281 | case tok::identifier: |
Chris Lattner | 4093192 | 2006-06-22 06:14:04 +0000 | [diff] [blame] | 1282 | // Get the identifier name without trigraphs or embedded newlines. |
| 1283 | const char *Directive = Result.getIdentifierInfo()->getName(); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1284 | bool isExtension = false; |
Chris Lattner | 4093192 | 2006-06-22 06:14:04 +0000 | [diff] [blame] | 1285 | switch (Result.getIdentifierInfo()->getNameLength()) { |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1286 | case 4: |
Chris Lattner | 4093192 | 2006-06-22 06:14:04 +0000 | [diff] [blame] | 1287 | if (Directive[0] == 'l' && !strcmp(Directive, "line")) |
Chris Lattner | a8654ca | 2006-07-04 17:42:08 +0000 | [diff] [blame] | 1288 | ; // FIXME: implement #line |
Chris Lattner | 4093192 | 2006-06-22 06:14:04 +0000 | [diff] [blame] | 1289 | if (Directive[0] == 'e' && !strcmp(Directive, "elif")) |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1290 | return HandleElifDirective(Result); |
Chris Lattner | 01d66cc | 2006-07-03 22:16:27 +0000 | [diff] [blame] | 1291 | if (Directive[0] == 's' && !strcmp(Directive, "sccs")) |
| 1292 | return HandleIdentSCCSDirective(Result); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1293 | break; |
| 1294 | case 5: |
Chris Lattner | 4093192 | 2006-06-22 06:14:04 +0000 | [diff] [blame] | 1295 | if (Directive[0] == 'e' && !strcmp(Directive, "endif")) |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1296 | return HandleEndifDirective(Result); |
Chris Lattner | 4093192 | 2006-06-22 06:14:04 +0000 | [diff] [blame] | 1297 | if (Directive[0] == 'i' && !strcmp(Directive, "ifdef")) |
Chris Lattner | 371ac8a | 2006-07-04 07:11:10 +0000 | [diff] [blame] | 1298 | return HandleIfdefDirective(Result, false, true/*not valid for miopt*/); |
Chris Lattner | 4093192 | 2006-06-22 06:14:04 +0000 | [diff] [blame] | 1299 | if (Directive[0] == 'u' && !strcmp(Directive, "undef")) |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1300 | return HandleUndefDirective(Result); |
Chris Lattner | 4093192 | 2006-06-22 06:14:04 +0000 | [diff] [blame] | 1301 | if (Directive[0] == 'e' && !strcmp(Directive, "error")) |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1302 | return HandleUserDiagnosticDirective(Result, false); |
Chris Lattner | 4093192 | 2006-06-22 06:14:04 +0000 | [diff] [blame] | 1303 | if (Directive[0] == 'i' && !strcmp(Directive, "ident")) |
Chris Lattner | 01d66cc | 2006-07-03 22:16:27 +0000 | [diff] [blame] | 1304 | return HandleIdentSCCSDirective(Result); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1305 | break; |
| 1306 | case 6: |
Chris Lattner | 4093192 | 2006-06-22 06:14:04 +0000 | [diff] [blame] | 1307 | if (Directive[0] == 'd' && !strcmp(Directive, "define")) |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1308 | return HandleDefineDirective(Result); |
Chris Lattner | 4093192 | 2006-06-22 06:14:04 +0000 | [diff] [blame] | 1309 | if (Directive[0] == 'i' && !strcmp(Directive, "ifndef")) |
Chris Lattner | 371ac8a | 2006-07-04 07:11:10 +0000 | [diff] [blame] | 1310 | return HandleIfdefDirective(Result, true, ReadAnyTokensBeforeDirective); |
Chris Lattner | 4093192 | 2006-06-22 06:14:04 +0000 | [diff] [blame] | 1311 | if (Directive[0] == 'i' && !strcmp(Directive, "import")) |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1312 | return HandleImportDirective(Result); |
Chris Lattner | b876183 | 2006-06-24 21:31:03 +0000 | [diff] [blame] | 1313 | if (Directive[0] == 'p' && !strcmp(Directive, "pragma")) |
Chris Lattner | 69772b0 | 2006-07-02 20:34:39 +0000 | [diff] [blame] | 1314 | return HandlePragmaDirective(); |
Chris Lattner | b876183 | 2006-06-24 21:31:03 +0000 | [diff] [blame] | 1315 | if (Directive[0] == 'a' && !strcmp(Directive, "assert")) |
| 1316 | isExtension = true; // FIXME: implement #assert |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1317 | break; |
| 1318 | case 7: |
Chris Lattner | 4093192 | 2006-06-22 06:14:04 +0000 | [diff] [blame] | 1319 | if (Directive[0] == 'i' && !strcmp(Directive, "include")) |
| 1320 | return HandleIncludeDirective(Result); // Handle #include. |
| 1321 | if (Directive[0] == 'w' && !strcmp(Directive, "warning")) { |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1322 | Diag(Result, diag::ext_pp_warning_directive); |
Chris Lattner | 504f2eb | 2006-06-18 07:19:54 +0000 | [diff] [blame] | 1323 | return HandleUserDiagnosticDirective(Result, true); |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1324 | } |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1325 | break; |
| 1326 | case 8: |
Chris Lattner | 4093192 | 2006-06-22 06:14:04 +0000 | [diff] [blame] | 1327 | if (Directive[0] == 'u' && !strcmp(Directive, "unassert")) { |
Chris Lattner | b876183 | 2006-06-24 21:31:03 +0000 | [diff] [blame] | 1328 | isExtension = true; // FIXME: implement #unassert |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1329 | } |
| 1330 | break; |
| 1331 | case 12: |
Chris Lattner | 4093192 | 2006-06-22 06:14:04 +0000 | [diff] [blame] | 1332 | if (Directive[0] == 'i' && !strcmp(Directive, "include_next")) |
| 1333 | return HandleIncludeNextDirective(Result); // Handle #include_next. |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1334 | break; |
| 1335 | } |
| 1336 | break; |
| 1337 | } |
| 1338 | |
| 1339 | // If we reached here, the preprocessing token is not valid! |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1340 | Diag(Result, diag::err_pp_invalid_directive); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1341 | |
| 1342 | // Read the rest of the PP line. |
Chris Lattner | 371ac8a | 2006-07-04 07:11:10 +0000 | [diff] [blame] | 1343 | DiscardUntilEndOfDirective(); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1344 | |
| 1345 | // Okay, we're done parsing the directive. |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1346 | } |
| 1347 | |
Chris Lattner | 01d66cc | 2006-07-03 22:16:27 +0000 | [diff] [blame] | 1348 | void Preprocessor::HandleUserDiagnosticDirective(LexerToken &Tok, |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1349 | bool isWarning) { |
| 1350 | // Read the rest of the line raw. We do this because we don't want macros |
| 1351 | // to be expanded and we don't require that the tokens be valid preprocessing |
| 1352 | // tokens. For example, this is allowed: "#warning ` 'foo". GCC does |
| 1353 | // collapse multiple consequtive white space between tokens, but this isn't |
| 1354 | // specified by the standard. |
| 1355 | std::string Message = CurLexer->ReadToEndOfLine(); |
| 1356 | |
| 1357 | unsigned DiagID = isWarning ? diag::pp_hash_warning : diag::err_pp_hash_error; |
Chris Lattner | 01d66cc | 2006-07-03 22:16:27 +0000 | [diff] [blame] | 1358 | return Diag(Tok, DiagID, Message); |
| 1359 | } |
| 1360 | |
| 1361 | /// HandleIdentSCCSDirective - Handle a #ident/#sccs directive. |
| 1362 | /// |
| 1363 | void Preprocessor::HandleIdentSCCSDirective(LexerToken &Tok) { |
Chris Lattner | 371ac8a | 2006-07-04 07:11:10 +0000 | [diff] [blame] | 1364 | // Yes, this directive is an extension. |
Chris Lattner | 01d66cc | 2006-07-03 22:16:27 +0000 | [diff] [blame] | 1365 | Diag(Tok, diag::ext_pp_ident_directive); |
| 1366 | |
Chris Lattner | 371ac8a | 2006-07-04 07:11:10 +0000 | [diff] [blame] | 1367 | // Read the string argument. |
Chris Lattner | 01d66cc | 2006-07-03 22:16:27 +0000 | [diff] [blame] | 1368 | LexerToken StrTok; |
| 1369 | Lex(StrTok); |
| 1370 | |
| 1371 | // If the token kind isn't a string, it's a malformed directive. |
| 1372 | if (StrTok.getKind() != tok::string_literal) |
| 1373 | return Diag(StrTok, diag::err_pp_malformed_ident); |
| 1374 | |
| 1375 | // Verify that there is nothing after the string, other than EOM. |
| 1376 | CheckEndOfDirective("#ident"); |
| 1377 | |
| 1378 | if (IdentHandler) |
| 1379 | IdentHandler(Tok.getLocation(), getSpelling(StrTok)); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1380 | } |
| 1381 | |
Chris Lattner | b876183 | 2006-06-24 21:31:03 +0000 | [diff] [blame] | 1382 | //===----------------------------------------------------------------------===// |
| 1383 | // Preprocessor Include Directive Handling. |
| 1384 | //===----------------------------------------------------------------------===// |
| 1385 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1386 | /// HandleIncludeDirective - The "#include" tokens have just been read, read the |
| 1387 | /// file to be included from the lexer, then include it! This is a common |
| 1388 | /// routine with functionality shared between #include, #include_next and |
| 1389 | /// #import. |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1390 | void Preprocessor::HandleIncludeDirective(LexerToken &IncludeTok, |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1391 | const DirectoryLookup *LookupFrom, |
| 1392 | bool isImport) { |
| 1393 | ++NumIncluded; |
Chris Lattner | 371ac8a | 2006-07-04 07:11:10 +0000 | [diff] [blame] | 1394 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1395 | LexerToken FilenameTok; |
Chris Lattner | 269c232 | 2006-06-25 06:23:00 +0000 | [diff] [blame] | 1396 | std::string Filename = CurLexer->LexIncludeFilename(FilenameTok); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1397 | |
| 1398 | // If the token kind is EOM, the error has already been diagnosed. |
| 1399 | if (FilenameTok.getKind() == tok::eom) |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1400 | return; |
Chris Lattner | 269c232 | 2006-06-25 06:23:00 +0000 | [diff] [blame] | 1401 | |
| 1402 | // Verify that there is nothing after the filename, other than EOM. Use the |
| 1403 | // preprocessor to lex this in case lexing the filename entered a macro. |
| 1404 | CheckEndOfDirective("#include"); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1405 | |
| 1406 | // Check that we don't have infinite #include recursion. |
Chris Lattner | 69772b0 | 2006-07-02 20:34:39 +0000 | [diff] [blame] | 1407 | if (IncludeMacroStack.size() == MaxAllowedIncludeStackDepth-1) |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1408 | return Diag(FilenameTok, diag::err_pp_include_too_deep); |
| 1409 | |
Chris Lattner | 269c232 | 2006-06-25 06:23:00 +0000 | [diff] [blame] | 1410 | // Find out whether the filename is <x> or "x". |
| 1411 | bool isAngled = Filename[0] == '<'; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1412 | |
| 1413 | // Remove the quotes. |
| 1414 | Filename = std::string(Filename.begin()+1, Filename.end()-1); |
| 1415 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1416 | // Search include directories. |
Chris Lattner | c899718 | 2006-06-22 05:52:16 +0000 | [diff] [blame] | 1417 | const DirectoryLookup *CurDir; |
| 1418 | const FileEntry *File = LookupFile(Filename, isAngled, LookupFrom, CurDir); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1419 | if (File == 0) |
| 1420 | return Diag(FilenameTok, diag::err_pp_file_not_found); |
| 1421 | |
| 1422 | // Get information about this file. |
| 1423 | PerFileInfo &FileInfo = getFileInfo(File); |
| 1424 | |
| 1425 | // If this is a #import directive, check that we have not already imported |
| 1426 | // this header. |
| 1427 | if (isImport) { |
| 1428 | // If this has already been imported, don't import it again. |
| 1429 | FileInfo.isImport = true; |
| 1430 | |
| 1431 | // Has this already been #import'ed or #include'd? |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1432 | if (FileInfo.NumIncludes) return; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1433 | } else { |
| 1434 | // Otherwise, if this is a #include of a file that was previously #import'd |
| 1435 | // or if this is the second #include of a #pragma once file, ignore it. |
| 1436 | if (FileInfo.isImport) |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1437 | return; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1438 | } |
Chris Lattner | 3665f16 | 2006-07-04 07:26:10 +0000 | [diff] [blame] | 1439 | |
| 1440 | // Next, check to see if the file is wrapped with #ifndef guards. If so, and |
| 1441 | // if the macro that guards it is defined, we know the #include has no effect. |
| 1442 | if (FileInfo.ControllingMacro && FileInfo.ControllingMacro->getMacroInfo()) { |
| 1443 | ++NumMultiIncludeFileOptzn; |
| 1444 | return; |
| 1445 | } |
| 1446 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1447 | |
| 1448 | // Look up the file, create a File ID for it. |
Chris Lattner | 371ac8a | 2006-07-04 07:11:10 +0000 | [diff] [blame] | 1449 | unsigned FileID = SourceMgr.createFileID(File, FilenameTok.getLocation()); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1450 | if (FileID == 0) |
| 1451 | return Diag(FilenameTok, diag::err_pp_file_not_found); |
| 1452 | |
| 1453 | // Finally, if all is good, enter the new file! |
Chris Lattner | c899718 | 2006-06-22 05:52:16 +0000 | [diff] [blame] | 1454 | EnterSourceFile(FileID, CurDir); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1455 | |
| 1456 | // Increment the number of times this file has been included. |
| 1457 | ++FileInfo.NumIncludes; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1458 | } |
| 1459 | |
| 1460 | /// HandleIncludeNextDirective - Implements #include_next. |
| 1461 | /// |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1462 | void Preprocessor::HandleIncludeNextDirective(LexerToken &IncludeNextTok) { |
| 1463 | Diag(IncludeNextTok, diag::ext_pp_include_next_directive); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1464 | |
| 1465 | // #include_next is like #include, except that we start searching after |
| 1466 | // the current found directory. If we can't do this, issue a |
| 1467 | // diagnostic. |
Chris Lattner | c899718 | 2006-06-22 05:52:16 +0000 | [diff] [blame] | 1468 | const DirectoryLookup *Lookup = CurDirLookup; |
Chris Lattner | 69772b0 | 2006-07-02 20:34:39 +0000 | [diff] [blame] | 1469 | if (isInPrimaryFile()) { |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1470 | Lookup = 0; |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1471 | Diag(IncludeNextTok, diag::pp_include_next_in_primary); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1472 | } else if (Lookup == 0) { |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1473 | Diag(IncludeNextTok, diag::pp_include_next_absolute_path); |
Chris Lattner | c899718 | 2006-06-22 05:52:16 +0000 | [diff] [blame] | 1474 | } else { |
| 1475 | // Start looking up in the next directory. |
| 1476 | ++Lookup; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1477 | } |
| 1478 | |
| 1479 | return HandleIncludeDirective(IncludeNextTok, Lookup); |
| 1480 | } |
| 1481 | |
| 1482 | /// HandleImportDirective - Implements #import. |
| 1483 | /// |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1484 | void Preprocessor::HandleImportDirective(LexerToken &ImportTok) { |
| 1485 | Diag(ImportTok, diag::ext_pp_import_directive); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1486 | |
| 1487 | return HandleIncludeDirective(ImportTok, 0, true); |
| 1488 | } |
| 1489 | |
Chris Lattner | b876183 | 2006-06-24 21:31:03 +0000 | [diff] [blame] | 1490 | //===----------------------------------------------------------------------===// |
| 1491 | // Preprocessor Macro Directive Handling. |
| 1492 | //===----------------------------------------------------------------------===// |
| 1493 | |
Chris Lattner | cefc768 | 2006-07-08 08:28:12 +0000 | [diff] [blame] | 1494 | /// ReadMacroDefinitionArgList - The ( starting an argument list of a macro |
| 1495 | /// definition has just been read. Lex the rest of the arguments and the |
| 1496 | /// closing ), updating MI with what we learn. Return true if an error occurs |
| 1497 | /// parsing the arg list. |
| 1498 | bool Preprocessor::ReadMacroDefinitionArgList(MacroInfo *MI) { |
| 1499 | LexerToken Tok; |
Chris Lattner | cefc768 | 2006-07-08 08:28:12 +0000 | [diff] [blame] | 1500 | while (1) { |
| 1501 | LexUnexpandedToken(Tok); |
| 1502 | switch (Tok.getKind()) { |
| 1503 | case tok::r_paren: |
| 1504 | // Found the end of the argument list. |
Chris Lattner | 6e0d42c | 2006-07-08 20:32:52 +0000 | [diff] [blame] | 1505 | if (MI->arg_begin() == MI->arg_end()) return false; // #define FOO() |
Chris Lattner | cefc768 | 2006-07-08 08:28:12 +0000 | [diff] [blame] | 1506 | // Otherwise we have #define FOO(A,) |
| 1507 | Diag(Tok, diag::err_pp_expected_ident_in_arg_list); |
| 1508 | return true; |
| 1509 | case tok::ellipsis: // #define X(... -> C99 varargs |
| 1510 | // Warn if use of C99 feature in non-C99 mode. |
| 1511 | if (!Features.C99) Diag(Tok, diag::ext_variadic_macro); |
| 1512 | |
| 1513 | // Lex the token after the identifier. |
| 1514 | LexUnexpandedToken(Tok); |
| 1515 | if (Tok.getKind() != tok::r_paren) { |
| 1516 | Diag(Tok, diag::err_pp_missing_rparen_in_macro_def); |
| 1517 | return true; |
| 1518 | } |
| 1519 | MI->setIsC99Varargs(); |
| 1520 | return false; |
| 1521 | case tok::eom: // #define X( |
| 1522 | Diag(Tok, diag::err_pp_missing_rparen_in_macro_def); |
| 1523 | return true; |
| 1524 | default: // #define X(1 |
| 1525 | Diag(Tok, diag::err_pp_invalid_tok_in_arg_list); |
| 1526 | return true; |
| 1527 | case tok::identifier: |
Chris Lattner | 6e0d42c | 2006-07-08 20:32:52 +0000 | [diff] [blame] | 1528 | IdentifierInfo *II = Tok.getIdentifierInfo(); |
| 1529 | |
| 1530 | // If this is already used as an argument, it is used multiple times (e.g. |
| 1531 | // #define X(A,A. |
| 1532 | if (II->isMacroArg()) { // C99 6.10.3p6 |
| 1533 | Diag(Tok, diag::err_pp_duplicate_name_in_arg_list, II->getName()); |
| 1534 | return true; |
| 1535 | } |
| 1536 | |
| 1537 | // Add the argument to the macro info. |
| 1538 | MI->addArgument(II); |
| 1539 | // Remember it is an argument now. |
| 1540 | II->setIsMacroArg(true); |
Chris Lattner | cefc768 | 2006-07-08 08:28:12 +0000 | [diff] [blame] | 1541 | |
| 1542 | // Lex the token after the identifier. |
| 1543 | LexUnexpandedToken(Tok); |
| 1544 | |
| 1545 | switch (Tok.getKind()) { |
| 1546 | default: // #define X(A B |
| 1547 | Diag(Tok, diag::err_pp_expected_comma_in_arg_list); |
| 1548 | return true; |
| 1549 | case tok::r_paren: // #define X(A) |
| 1550 | return false; |
| 1551 | case tok::comma: // #define X(A, |
| 1552 | break; |
| 1553 | case tok::ellipsis: // #define X(A... -> GCC extension |
| 1554 | // Diagnose extension. |
| 1555 | Diag(Tok, diag::ext_named_variadic_macro); |
| 1556 | |
| 1557 | // Lex the token after the identifier. |
| 1558 | LexUnexpandedToken(Tok); |
| 1559 | if (Tok.getKind() != tok::r_paren) { |
| 1560 | Diag(Tok, diag::err_pp_missing_rparen_in_macro_def); |
| 1561 | return true; |
| 1562 | } |
| 1563 | |
| 1564 | MI->setIsGNUVarargs(); |
| 1565 | return false; |
| 1566 | } |
| 1567 | } |
| 1568 | } |
| 1569 | } |
| 1570 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1571 | /// HandleDefineDirective - Implements #define. This consumes the entire macro |
| 1572 | /// line then lets the caller lex the next real token. |
| 1573 | /// |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1574 | void Preprocessor::HandleDefineDirective(LexerToken &DefineTok) { |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1575 | ++NumDefined; |
Chris Lattner | 371ac8a | 2006-07-04 07:11:10 +0000 | [diff] [blame] | 1576 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1577 | LexerToken MacroNameTok; |
Chris Lattner | e8eef32 | 2006-07-08 07:01:00 +0000 | [diff] [blame] | 1578 | ReadMacroName(MacroNameTok, 1); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1579 | |
| 1580 | // Error reading macro name? If so, diagnostic already issued. |
| 1581 | if (MacroNameTok.getKind() == tok::eom) |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1582 | return; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1583 | |
Chris Lattner | 50b497e | 2006-06-18 16:32:35 +0000 | [diff] [blame] | 1584 | MacroInfo *MI = new MacroInfo(MacroNameTok.getLocation()); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1585 | |
| 1586 | LexerToken Tok; |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1587 | LexUnexpandedToken(Tok); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1588 | |
Chris Lattner | 7818605 | 2006-07-09 00:45:31 +0000 | [diff] [blame^] | 1589 | // FIXME: Enable __VA_ARGS__. |
| 1590 | |
Chris Lattner | 6e0d42c | 2006-07-08 20:32:52 +0000 | [diff] [blame] | 1591 | // If this is a function-like macro definition, parse the argument list, |
| 1592 | // marking each of the identifiers as being used as macro arguments. Also, |
| 1593 | // check other constraints on the first token of the macro body. |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1594 | if (Tok.getKind() == tok::eom) { |
| 1595 | // If there is no body to this macro, we have no special handling here. |
| 1596 | } else if (Tok.getKind() == tok::l_paren && !Tok.hasLeadingSpace()) { |
Chris Lattner | cefc768 | 2006-07-08 08:28:12 +0000 | [diff] [blame] | 1597 | // This is a function-like macro definition. Read the argument list. |
| 1598 | MI->setIsFunctionLike(); |
| 1599 | if (ReadMacroDefinitionArgList(MI)) { |
Chris Lattner | 6e0d42c | 2006-07-08 20:32:52 +0000 | [diff] [blame] | 1600 | // Clear the "isMacroArg" flags from all the macro arguments parsed. |
| 1601 | MI->SetIdentifierIsMacroArgFlags(false); |
| 1602 | // Forget about MI. |
Chris Lattner | cefc768 | 2006-07-08 08:28:12 +0000 | [diff] [blame] | 1603 | delete MI; |
Chris Lattner | 6e0d42c | 2006-07-08 20:32:52 +0000 | [diff] [blame] | 1604 | // Throw away the rest of the line. |
Chris Lattner | cefc768 | 2006-07-08 08:28:12 +0000 | [diff] [blame] | 1605 | if (CurLexer->ParsingPreprocessorDirective) |
| 1606 | DiscardUntilEndOfDirective(); |
| 1607 | return; |
| 1608 | } |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1609 | |
Chris Lattner | 815a1f9 | 2006-07-08 20:48:04 +0000 | [diff] [blame] | 1610 | // Read the first token after the arg list for down below. |
| 1611 | LexUnexpandedToken(Tok); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1612 | } else if (!Tok.hasLeadingSpace()) { |
| 1613 | // C99 requires whitespace between the macro definition and the body. Emit |
| 1614 | // a diagnostic for something like "#define X+". |
| 1615 | if (Features.C99) { |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1616 | Diag(Tok, diag::ext_c99_whitespace_required_after_macro_name); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1617 | } else { |
| 1618 | // FIXME: C90/C++ do not get this diagnostic, but it does get a similar |
| 1619 | // one in some cases! |
| 1620 | } |
| 1621 | } else { |
| 1622 | // This is a normal token with leading space. Clear the leading space |
| 1623 | // marker on the first token to get proper expansion. |
| 1624 | Tok.ClearFlag(LexerToken::LeadingSpace); |
| 1625 | } |
| 1626 | |
| 1627 | // Read the rest of the macro body. |
| 1628 | while (Tok.getKind() != tok::eom) { |
| 1629 | MI->AddTokenToBody(Tok); |
Chris Lattner | 815a1f9 | 2006-07-08 20:48:04 +0000 | [diff] [blame] | 1630 | |
| 1631 | // Check C99 6.10.3.2p1: ensure that # operators are followed by macro |
| 1632 | // parameters. |
| 1633 | if (Tok.getKind() != tok::hash) { |
| 1634 | // Get the next token of the macro. |
| 1635 | LexUnexpandedToken(Tok); |
| 1636 | continue; |
| 1637 | } |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1638 | |
Chris Lattner | 815a1f9 | 2006-07-08 20:48:04 +0000 | [diff] [blame] | 1639 | // Get the next token of the macro. |
| 1640 | LexUnexpandedToken(Tok); |
| 1641 | |
| 1642 | // Not a macro arg identifier? |
| 1643 | if (!Tok.getIdentifierInfo() || !Tok.getIdentifierInfo()->isMacroArg()) { |
| 1644 | Diag(Tok, diag::err_pp_stringize_not_parameter); |
| 1645 | // Clear the "isMacroArg" flags from all the macro arguments. |
| 1646 | MI->SetIdentifierIsMacroArgFlags(false); |
| 1647 | delete MI; |
| 1648 | return; |
| 1649 | } |
| 1650 | |
| 1651 | // Things look ok, add the param name token to the macro. |
| 1652 | MI->AddTokenToBody(Tok); |
Chris Lattner | bff18d5 | 2006-07-06 04:49:18 +0000 | [diff] [blame] | 1653 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1654 | // Get the next token of the macro. |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1655 | LexUnexpandedToken(Tok); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1656 | } |
Chris Lattner | bff18d5 | 2006-07-06 04:49:18 +0000 | [diff] [blame] | 1657 | |
Chris Lattner | 7818605 | 2006-07-09 00:45:31 +0000 | [diff] [blame^] | 1658 | // Clear the "isMacroArg" flags from all the macro arguments. |
| 1659 | MI->SetIdentifierIsMacroArgFlags(false); |
| 1660 | |
Chris Lattner | bff18d5 | 2006-07-06 04:49:18 +0000 | [diff] [blame] | 1661 | // Check that there is no paste (##) operator at the begining or end of the |
| 1662 | // replacement list. |
Chris Lattner | 7818605 | 2006-07-09 00:45:31 +0000 | [diff] [blame^] | 1663 | unsigned NumTokens = MI->getNumTokens(); |
Chris Lattner | bff18d5 | 2006-07-06 04:49:18 +0000 | [diff] [blame] | 1664 | if (NumTokens != 0) { |
| 1665 | if (MI->getReplacementToken(0).getKind() == tok::hashhash) { |
Chris Lattner | 815a1f9 | 2006-07-08 20:48:04 +0000 | [diff] [blame] | 1666 | Diag(MI->getReplacementToken(0), diag::err_paste_at_start); |
Chris Lattner | bff18d5 | 2006-07-06 04:49:18 +0000 | [diff] [blame] | 1667 | delete MI; |
Chris Lattner | 815a1f9 | 2006-07-08 20:48:04 +0000 | [diff] [blame] | 1668 | return; |
Chris Lattner | bff18d5 | 2006-07-06 04:49:18 +0000 | [diff] [blame] | 1669 | } |
| 1670 | if (MI->getReplacementToken(NumTokens-1).getKind() == tok::hashhash) { |
Chris Lattner | 815a1f9 | 2006-07-08 20:48:04 +0000 | [diff] [blame] | 1671 | Diag(MI->getReplacementToken(NumTokens-1), diag::err_paste_at_end); |
Chris Lattner | bff18d5 | 2006-07-06 04:49:18 +0000 | [diff] [blame] | 1672 | delete MI; |
Chris Lattner | 815a1f9 | 2006-07-08 20:48:04 +0000 | [diff] [blame] | 1673 | return; |
Chris Lattner | bff18d5 | 2006-07-06 04:49:18 +0000 | [diff] [blame] | 1674 | } |
| 1675 | } |
| 1676 | |
Chris Lattner | 13044d9 | 2006-07-03 05:16:44 +0000 | [diff] [blame] | 1677 | // If this is the primary source file, remember that this macro hasn't been |
| 1678 | // used yet. |
| 1679 | if (isInPrimaryFile()) |
| 1680 | MI->setIsUsed(false); |
| 1681 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1682 | // Finally, if this identifier already had a macro defined for it, verify that |
| 1683 | // the macro bodies are identical and free the old definition. |
| 1684 | if (MacroInfo *OtherMI = MacroNameTok.getIdentifierInfo()->getMacroInfo()) { |
Chris Lattner | 13044d9 | 2006-07-03 05:16:44 +0000 | [diff] [blame] | 1685 | if (!OtherMI->isUsed()) |
| 1686 | Diag(OtherMI->getDefinitionLoc(), diag::pp_macro_not_used); |
| 1687 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1688 | // Macros must be identical. This means all tokes and whitespace separation |
Chris Lattner | 21284df | 2006-07-08 07:16:08 +0000 | [diff] [blame] | 1689 | // must be the same. C99 6.10.3.2. |
| 1690 | if (!MI->isIdenticalTo(*OtherMI, *this)) { |
Chris Lattner | e8eef32 | 2006-07-08 07:01:00 +0000 | [diff] [blame] | 1691 | Diag(MI->getDefinitionLoc(), diag::ext_pp_macro_redef, |
| 1692 | MacroNameTok.getIdentifierInfo()->getName()); |
| 1693 | Diag(OtherMI->getDefinitionLoc(), diag::ext_pp_macro_redef2); |
| 1694 | } |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1695 | delete OtherMI; |
| 1696 | } |
| 1697 | |
| 1698 | MacroNameTok.getIdentifierInfo()->setMacroInfo(MI); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1699 | } |
| 1700 | |
| 1701 | |
| 1702 | /// HandleUndefDirective - Implements #undef. |
| 1703 | /// |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1704 | void Preprocessor::HandleUndefDirective(LexerToken &UndefTok) { |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1705 | ++NumUndefined; |
Chris Lattner | 371ac8a | 2006-07-04 07:11:10 +0000 | [diff] [blame] | 1706 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1707 | LexerToken MacroNameTok; |
Chris Lattner | e8eef32 | 2006-07-08 07:01:00 +0000 | [diff] [blame] | 1708 | ReadMacroName(MacroNameTok, 2); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1709 | |
| 1710 | // Error reading macro name? If so, diagnostic already issued. |
| 1711 | if (MacroNameTok.getKind() == tok::eom) |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1712 | return; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1713 | |
| 1714 | // 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] | 1715 | CheckEndOfDirective("#undef"); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1716 | |
| 1717 | // Okay, we finally have a valid identifier to undef. |
| 1718 | MacroInfo *MI = MacroNameTok.getIdentifierInfo()->getMacroInfo(); |
| 1719 | |
| 1720 | // 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] | 1721 | if (MI == 0) return; |
Chris Lattner | 677757a | 2006-06-28 05:26:32 +0000 | [diff] [blame] | 1722 | |
Chris Lattner | 13044d9 | 2006-07-03 05:16:44 +0000 | [diff] [blame] | 1723 | if (!MI->isUsed()) |
| 1724 | Diag(MI->getDefinitionLoc(), diag::pp_macro_not_used); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1725 | |
| 1726 | // Free macro definition. |
| 1727 | delete MI; |
| 1728 | MacroNameTok.getIdentifierInfo()->setMacroInfo(0); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1729 | } |
| 1730 | |
| 1731 | |
Chris Lattner | b876183 | 2006-06-24 21:31:03 +0000 | [diff] [blame] | 1732 | //===----------------------------------------------------------------------===// |
| 1733 | // Preprocessor Conditional Directive Handling. |
| 1734 | //===----------------------------------------------------------------------===// |
| 1735 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1736 | /// HandleIfdefDirective - Implements the #ifdef/#ifndef directive. isIfndef is |
Chris Lattner | 371ac8a | 2006-07-04 07:11:10 +0000 | [diff] [blame] | 1737 | /// true when this is a #ifndef directive. ReadAnyTokensBeforeDirective is true |
| 1738 | /// if any tokens have been returned or pp-directives activated before this |
| 1739 | /// #ifndef has been lexed. |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1740 | /// |
Chris Lattner | 371ac8a | 2006-07-04 07:11:10 +0000 | [diff] [blame] | 1741 | void Preprocessor::HandleIfdefDirective(LexerToken &Result, bool isIfndef, |
| 1742 | bool ReadAnyTokensBeforeDirective) { |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1743 | ++NumIf; |
| 1744 | LexerToken DirectiveTok = Result; |
Chris Lattner | 371ac8a | 2006-07-04 07:11:10 +0000 | [diff] [blame] | 1745 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1746 | LexerToken MacroNameTok; |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1747 | ReadMacroName(MacroNameTok); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1748 | |
| 1749 | // Error reading macro name? If so, diagnostic already issued. |
| 1750 | if (MacroNameTok.getKind() == tok::eom) |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1751 | return; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1752 | |
| 1753 | // 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] | 1754 | CheckEndOfDirective(isIfndef ? "#ifndef" : "#ifdef"); |
| 1755 | |
| 1756 | // If the start of a top-level #ifdef, inform MIOpt. |
| 1757 | if (!ReadAnyTokensBeforeDirective && |
| 1758 | CurLexer->getConditionalStackDepth() == 0) { |
| 1759 | assert(isIfndef && "#ifdef shouldn't reach here"); |
| 1760 | CurLexer->MIOpt.EnterTopLevelIFNDEF(MacroNameTok.getIdentifierInfo()); |
| 1761 | } |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1762 | |
Chris Lattner | a78a97e | 2006-07-03 05:42:18 +0000 | [diff] [blame] | 1763 | MacroInfo *MI = MacroNameTok.getIdentifierInfo()->getMacroInfo(); |
| 1764 | |
| 1765 | // If there is a macro, mark it used. |
| 1766 | if (MI) MI->setIsUsed(true); |
| 1767 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1768 | // Should we include the stuff contained by this directive? |
Chris Lattner | a78a97e | 2006-07-03 05:42:18 +0000 | [diff] [blame] | 1769 | if (!MI == isIfndef) { |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1770 | // 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] | 1771 | CurLexer->pushConditionalLevel(DirectiveTok.getLocation(), /*wasskip*/false, |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1772 | /*foundnonskip*/true, /*foundelse*/false); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1773 | } else { |
| 1774 | // 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] | 1775 | SkipExcludedConditionalBlock(DirectiveTok.getLocation(), |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1776 | /*Foundnonskip*/false, |
| 1777 | /*FoundElse*/false); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1778 | } |
| 1779 | } |
| 1780 | |
| 1781 | /// HandleIfDirective - Implements the #if directive. |
| 1782 | /// |
Chris Lattner | a8654ca | 2006-07-04 17:42:08 +0000 | [diff] [blame] | 1783 | void Preprocessor::HandleIfDirective(LexerToken &IfToken, |
| 1784 | bool ReadAnyTokensBeforeDirective) { |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1785 | ++NumIf; |
Chris Lattner | 371ac8a | 2006-07-04 07:11:10 +0000 | [diff] [blame] | 1786 | |
Chris Lattner | 371ac8a | 2006-07-04 07:11:10 +0000 | [diff] [blame] | 1787 | // Parse and evaluation the conditional expression. |
Chris Lattner | c79f6fb | 2006-07-04 17:53:21 +0000 | [diff] [blame] | 1788 | IdentifierInfo *IfNDefMacro = 0; |
Chris Lattner | a8654ca | 2006-07-04 17:42:08 +0000 | [diff] [blame] | 1789 | bool ConditionalTrue = EvaluateDirectiveExpression(IfNDefMacro); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1790 | |
| 1791 | // Should we include the stuff contained by this directive? |
| 1792 | if (ConditionalTrue) { |
Chris Lattner | a8654ca | 2006-07-04 17:42:08 +0000 | [diff] [blame] | 1793 | // If this condition is equivalent to #ifndef X, and if this is the first |
| 1794 | // directive seen, handle it for the multiple-include optimization. |
| 1795 | if (!ReadAnyTokensBeforeDirective && |
| 1796 | CurLexer->getConditionalStackDepth() == 0 && IfNDefMacro) |
| 1797 | CurLexer->MIOpt.EnterTopLevelIFNDEF(IfNDefMacro); |
| 1798 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1799 | // 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] | 1800 | CurLexer->pushConditionalLevel(IfToken.getLocation(), /*wasskip*/false, |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1801 | /*foundnonskip*/true, /*foundelse*/false); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1802 | } else { |
| 1803 | // 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] | 1804 | SkipExcludedConditionalBlock(IfToken.getLocation(), /*Foundnonskip*/false, |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1805 | /*FoundElse*/false); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1806 | } |
| 1807 | } |
| 1808 | |
| 1809 | /// HandleEndifDirective - Implements the #endif directive. |
| 1810 | /// |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1811 | void Preprocessor::HandleEndifDirective(LexerToken &EndifToken) { |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1812 | ++NumEndif; |
Chris Lattner | 371ac8a | 2006-07-04 07:11:10 +0000 | [diff] [blame] | 1813 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1814 | // Check that this is the whole directive. |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1815 | CheckEndOfDirective("#endif"); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1816 | |
| 1817 | PPConditionalInfo CondInfo; |
| 1818 | if (CurLexer->popConditionalLevel(CondInfo)) { |
| 1819 | // No conditionals on the stack: this is an #endif without an #if. |
| 1820 | return Diag(EndifToken, diag::err_pp_endif_without_if); |
| 1821 | } |
| 1822 | |
Chris Lattner | 371ac8a | 2006-07-04 07:11:10 +0000 | [diff] [blame] | 1823 | // If this the end of a top-level #endif, inform MIOpt. |
| 1824 | if (CurLexer->getConditionalStackDepth() == 0) |
| 1825 | CurLexer->MIOpt.ExitTopLevelConditional(); |
| 1826 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1827 | assert(!CondInfo.WasSkipping && !isSkipping() && |
| 1828 | "This code should only be reachable in the non-skipping case!"); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1829 | } |
| 1830 | |
| 1831 | |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1832 | void Preprocessor::HandleElseDirective(LexerToken &Result) { |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1833 | ++NumElse; |
Chris Lattner | 371ac8a | 2006-07-04 07:11:10 +0000 | [diff] [blame] | 1834 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1835 | // #else directive in a non-skipping conditional... start skipping. |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1836 | CheckEndOfDirective("#else"); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1837 | |
| 1838 | PPConditionalInfo CI; |
| 1839 | if (CurLexer->popConditionalLevel(CI)) |
| 1840 | return Diag(Result, diag::pp_err_else_without_if); |
Chris Lattner | 371ac8a | 2006-07-04 07:11:10 +0000 | [diff] [blame] | 1841 | |
| 1842 | // If this is a top-level #else, inform the MIOpt. |
| 1843 | if (CurLexer->getConditionalStackDepth() == 0) |
| 1844 | CurLexer->MIOpt.FoundTopLevelElse(); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1845 | |
| 1846 | // 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] | 1847 | if (CI.FoundElse) Diag(Result, diag::pp_err_else_after_else); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1848 | |
| 1849 | // Finally, skip the rest of the contents of this block and return the first |
| 1850 | // token after it. |
| 1851 | return SkipExcludedConditionalBlock(CI.IfLoc, /*Foundnonskip*/true, |
| 1852 | /*FoundElse*/true); |
| 1853 | } |
| 1854 | |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1855 | void Preprocessor::HandleElifDirective(LexerToken &ElifToken) { |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1856 | ++NumElse; |
Chris Lattner | 371ac8a | 2006-07-04 07:11:10 +0000 | [diff] [blame] | 1857 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1858 | // #elif directive in a non-skipping conditional... start skipping. |
| 1859 | // We don't care what the condition is, because we will always skip it (since |
| 1860 | // the block immediately before it was included). |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1861 | DiscardUntilEndOfDirective(); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1862 | |
| 1863 | PPConditionalInfo CI; |
| 1864 | if (CurLexer->popConditionalLevel(CI)) |
| 1865 | return Diag(ElifToken, diag::pp_err_elif_without_if); |
| 1866 | |
Chris Lattner | 371ac8a | 2006-07-04 07:11:10 +0000 | [diff] [blame] | 1867 | // If this is a top-level #elif, inform the MIOpt. |
| 1868 | if (CurLexer->getConditionalStackDepth() == 0) |
| 1869 | CurLexer->MIOpt.FoundTopLevelElse(); |
| 1870 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1871 | // 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] | 1872 | if (CI.FoundElse) Diag(ElifToken, diag::pp_err_elif_after_else); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1873 | |
| 1874 | // Finally, skip the rest of the contents of this block and return the first |
| 1875 | // token after it. |
| 1876 | return SkipExcludedConditionalBlock(CI.IfLoc, /*Foundnonskip*/true, |
| 1877 | /*FoundElse*/CI.FoundElse); |
| 1878 | } |
Chris Lattner | b876183 | 2006-06-24 21:31:03 +0000 | [diff] [blame] | 1879 | |