Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 1 | //===--- PPDirectives.cpp - Directive Handling for Preprocessor -----------===// |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements # directive processing for the Preprocessor. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "clang/Lex/Preprocessor.h" |
Chris Lattner | 359cc44 | 2009-01-26 05:29:08 +0000 | [diff] [blame] | 15 | #include "clang/Lex/LiteralSupport.h" |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 16 | #include "clang/Lex/HeaderSearch.h" |
| 17 | #include "clang/Lex/MacroInfo.h" |
Chris Lattner | 500d329 | 2009-01-29 05:15:15 +0000 | [diff] [blame] | 18 | #include "clang/Lex/LexDiagnostic.h" |
Douglas Gregor | f44e854 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 19 | #include "clang/Lex/CodeCompletionHandler.h" |
Douglas Gregor | 80c60f7 | 2010-09-09 22:45:38 +0000 | [diff] [blame] | 20 | #include "clang/Lex/Pragma.h" |
Chris Lattner | 6e29014 | 2009-11-30 04:18:44 +0000 | [diff] [blame] | 21 | #include "clang/Basic/FileManager.h" |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 22 | #include "clang/Basic/SourceManager.h" |
Chris Lattner | 359cc44 | 2009-01-26 05:29:08 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/APInt.h" |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 24 | using namespace clang; |
| 25 | |
| 26 | //===----------------------------------------------------------------------===// |
| 27 | // Utility Methods for Preprocessor Directive Handling. |
| 28 | //===----------------------------------------------------------------------===// |
| 29 | |
Chris Lattner | f47724b | 2010-08-17 15:55:45 +0000 | [diff] [blame] | 30 | MacroInfo *Preprocessor::AllocateMacroInfo() { |
Ted Kremenek | 9714a23 | 2010-10-19 22:15:20 +0000 | [diff] [blame] | 31 | MacroInfoChain *MIChain; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 32 | |
Ted Kremenek | 9714a23 | 2010-10-19 22:15:20 +0000 | [diff] [blame] | 33 | if (MICache) { |
| 34 | MIChain = MICache; |
| 35 | MICache = MICache->Next; |
Ted Kremenek | af8fa25 | 2010-10-19 18:16:54 +0000 | [diff] [blame] | 36 | } |
Ted Kremenek | 9714a23 | 2010-10-19 22:15:20 +0000 | [diff] [blame] | 37 | else { |
| 38 | MIChain = BP.Allocate<MacroInfoChain>(); |
| 39 | } |
| 40 | |
| 41 | MIChain->Next = MIChainHead; |
| 42 | MIChain->Prev = 0; |
| 43 | if (MIChainHead) |
| 44 | MIChainHead->Prev = MIChain; |
| 45 | MIChainHead = MIChain; |
| 46 | |
| 47 | return &(MIChain->MI); |
Chris Lattner | f47724b | 2010-08-17 15:55:45 +0000 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | MacroInfo *Preprocessor::AllocateMacroInfo(SourceLocation L) { |
| 51 | MacroInfo *MI = AllocateMacroInfo(); |
Ted Kremenek | 0ea7672 | 2008-12-15 19:56:42 +0000 | [diff] [blame] | 52 | new (MI) MacroInfo(L); |
| 53 | return MI; |
| 54 | } |
| 55 | |
Chris Lattner | f47724b | 2010-08-17 15:55:45 +0000 | [diff] [blame] | 56 | MacroInfo *Preprocessor::CloneMacroInfo(const MacroInfo &MacroToClone) { |
| 57 | MacroInfo *MI = AllocateMacroInfo(); |
| 58 | new (MI) MacroInfo(MacroToClone, BP); |
| 59 | return MI; |
| 60 | } |
| 61 | |
Chris Lattner | 0301b3f | 2009-02-20 22:19:20 +0000 | [diff] [blame] | 62 | /// ReleaseMacroInfo - Release the specified MacroInfo. This memory will |
| 63 | /// be reused for allocating new MacroInfo objects. |
Chris Lattner | 2c1ab90 | 2010-08-18 16:08:51 +0000 | [diff] [blame] | 64 | void Preprocessor::ReleaseMacroInfo(MacroInfo *MI) { |
Ted Kremenek | 9714a23 | 2010-10-19 22:15:20 +0000 | [diff] [blame] | 65 | MacroInfoChain *MIChain = (MacroInfoChain*) MI; |
| 66 | if (MacroInfoChain *Prev = MIChain->Prev) { |
| 67 | MacroInfoChain *Next = MIChain->Next; |
| 68 | Prev->Next = Next; |
| 69 | if (Next) |
| 70 | Next->Prev = Prev; |
| 71 | } |
| 72 | else { |
| 73 | assert(MIChainHead == MIChain); |
| 74 | MIChainHead = MIChain->Next; |
| 75 | MIChainHead->Prev = 0; |
| 76 | } |
| 77 | MIChain->Next = MICache; |
| 78 | MICache = MIChain; |
Chris Lattner | 0301b3f | 2009-02-20 22:19:20 +0000 | [diff] [blame] | 79 | |
Ted Kremenek | 9714a23 | 2010-10-19 22:15:20 +0000 | [diff] [blame] | 80 | MI->Destroy(); |
| 81 | } |
Chris Lattner | 0301b3f | 2009-02-20 22:19:20 +0000 | [diff] [blame] | 82 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 83 | /// DiscardUntilEndOfDirective - Read and discard all tokens remaining on the |
| 84 | /// current line until the tok::eom token is found. |
| 85 | void Preprocessor::DiscardUntilEndOfDirective() { |
| 86 | Token Tmp; |
| 87 | do { |
| 88 | LexUnexpandedToken(Tmp); |
| 89 | } while (Tmp.isNot(tok::eom)); |
| 90 | } |
| 91 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 92 | /// ReadMacroName - Lex and validate a macro name, which occurs after a |
| 93 | /// #define or #undef. This sets the token kind to eom and discards the rest |
| 94 | /// of the macro line if the macro name is invalid. isDefineUndef is 1 if |
| 95 | /// this is due to a a #define, 2 if #undef directive, 0 if it is something |
| 96 | /// else (e.g. #ifdef). |
| 97 | void Preprocessor::ReadMacroName(Token &MacroNameTok, char isDefineUndef) { |
| 98 | // Read the token, don't allow macro expansion on it. |
| 99 | LexUnexpandedToken(MacroNameTok); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 100 | |
Douglas Gregor | 1fbb447 | 2010-08-24 20:21:13 +0000 | [diff] [blame] | 101 | if (MacroNameTok.is(tok::code_completion)) { |
| 102 | if (CodeComplete) |
| 103 | CodeComplete->CodeCompleteMacroName(isDefineUndef == 1); |
| 104 | LexUnexpandedToken(MacroNameTok); |
| 105 | return; |
| 106 | } |
| 107 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 108 | // Missing macro name? |
Chris Lattner | 3692b09 | 2008-11-18 07:59:24 +0000 | [diff] [blame] | 109 | if (MacroNameTok.is(tok::eom)) { |
| 110 | Diag(MacroNameTok, diag::err_pp_missing_macro_name); |
| 111 | return; |
| 112 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 113 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 114 | IdentifierInfo *II = MacroNameTok.getIdentifierInfo(); |
| 115 | if (II == 0) { |
Douglas Gregor | 453091c | 2010-03-16 22:30:13 +0000 | [diff] [blame] | 116 | bool Invalid = false; |
| 117 | std::string Spelling = getSpelling(MacroNameTok, &Invalid); |
| 118 | if (Invalid) |
| 119 | return; |
| 120 | |
Chris Lattner | 9485d23 | 2008-12-13 20:12:40 +0000 | [diff] [blame] | 121 | const IdentifierInfo &Info = Identifiers.get(Spelling); |
| 122 | if (Info.isCPlusPlusOperatorKeyword()) |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 123 | // C++ 2.5p2: Alternative tokens behave the same as its primary token |
| 124 | // except for their spellings. |
Chris Lattner | 56b05c8 | 2008-11-18 08:02:48 +0000 | [diff] [blame] | 125 | Diag(MacroNameTok, diag::err_pp_operator_used_as_macro_name) << Spelling; |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 126 | else |
| 127 | Diag(MacroNameTok, diag::err_pp_macro_not_identifier); |
| 128 | // Fall through on error. |
| 129 | } else if (isDefineUndef && II->getPPKeywordID() == tok::pp_defined) { |
| 130 | // Error if defining "defined": C99 6.10.8.4. |
| 131 | Diag(MacroNameTok, diag::err_defined_macro_name); |
| 132 | } else if (isDefineUndef && II->hasMacroDefinition() && |
| 133 | getMacroInfo(II)->isBuiltinMacro()) { |
| 134 | // Error if defining "__LINE__" and other builtins: C99 6.10.8.4. |
| 135 | if (isDefineUndef == 1) |
| 136 | Diag(MacroNameTok, diag::pp_redef_builtin_macro); |
| 137 | else |
| 138 | Diag(MacroNameTok, diag::pp_undef_builtin_macro); |
| 139 | } else { |
| 140 | // Okay, we got a good identifier node. Return it. |
| 141 | return; |
| 142 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 143 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 144 | // Invalid macro name, read and discard the rest of the line. Then set the |
| 145 | // token kind to tok::eom. |
| 146 | MacroNameTok.setKind(tok::eom); |
| 147 | return DiscardUntilEndOfDirective(); |
| 148 | } |
| 149 | |
| 150 | /// CheckEndOfDirective - Ensure that the next token is a tok::eom token. If |
Chris Lattner | ab82f41 | 2009-04-17 23:30:53 +0000 | [diff] [blame] | 151 | /// not, emit a diagnostic and consume up until the eom. If EnableMacros is |
| 152 | /// true, then we consider macros that expand to zero tokens as being ok. |
| 153 | void Preprocessor::CheckEndOfDirective(const char *DirType, bool EnableMacros) { |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 154 | Token Tmp; |
Chris Lattner | ab82f41 | 2009-04-17 23:30:53 +0000 | [diff] [blame] | 155 | // Lex unexpanded tokens for most directives: macros might expand to zero |
| 156 | // tokens, causing us to miss diagnosing invalid lines. Some directives (like |
| 157 | // #line) allow empty macros. |
| 158 | if (EnableMacros) |
| 159 | Lex(Tmp); |
| 160 | else |
| 161 | LexUnexpandedToken(Tmp); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 162 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 163 | // There should be no tokens after the directive, but we allow them as an |
| 164 | // extension. |
| 165 | while (Tmp.is(tok::comment)) // Skip comments in -C mode. |
| 166 | LexUnexpandedToken(Tmp); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 167 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 168 | if (Tmp.isNot(tok::eom)) { |
Chris Lattner | 959875a | 2009-04-14 05:15:20 +0000 | [diff] [blame] | 169 | // Add a fixit in GNU/C99/C++ mode. Don't offer a fixit for strict-C89, |
| 170 | // because it is more trouble than it is worth to insert /**/ and check that |
| 171 | // there is no /**/ in the range also. |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 172 | FixItHint Hint; |
Chris Lattner | 959875a | 2009-04-14 05:15:20 +0000 | [diff] [blame] | 173 | if (Features.GNUMode || Features.C99 || Features.CPlusPlus) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 174 | Hint = FixItHint::CreateInsertion(Tmp.getLocation(),"//"); |
| 175 | Diag(Tmp, diag::ext_pp_extra_tokens_at_eol) << DirType << Hint; |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 176 | DiscardUntilEndOfDirective(); |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | |
| 181 | |
| 182 | /// SkipExcludedConditionalBlock - We just read a #if or related directive and |
| 183 | /// decided that the subsequent tokens are in the #if'd out portion of the |
| 184 | /// file. Lex the rest of the file, until we see an #endif. If |
| 185 | /// FoundNonSkipPortion is true, then we have already emitted code for part of |
| 186 | /// this #if directive, so #else/#elif blocks should never be entered. If ElseOk |
| 187 | /// is true, then #else directives are ok, if not, then we have already seen one |
| 188 | /// so a #else directive is a duplicate. When this returns, the caller can lex |
| 189 | /// the first valid token. |
| 190 | void Preprocessor::SkipExcludedConditionalBlock(SourceLocation IfTokenLoc, |
| 191 | bool FoundNonSkipPortion, |
| 192 | bool FoundElse) { |
| 193 | ++NumSkipped; |
Ted Kremenek | f6452c5 | 2008-11-18 01:04:47 +0000 | [diff] [blame] | 194 | assert(CurTokenLexer == 0 && CurPPLexer && "Lexing a macro, not a file?"); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 195 | |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 196 | CurPPLexer->pushConditionalLevel(IfTokenLoc, /*isSkipping*/false, |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 197 | FoundNonSkipPortion, FoundElse); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 198 | |
Ted Kremenek | 268ee70 | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 199 | if (CurPTHLexer) { |
| 200 | PTHSkipExcludedConditionalBlock(); |
| 201 | return; |
| 202 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 203 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 204 | // Enter raw mode to disable identifier lookup (and thus macro expansion), |
| 205 | // disabling warnings, etc. |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 206 | CurPPLexer->LexingRawMode = true; |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 207 | Token Tok; |
| 208 | while (1) { |
Chris Lattner | 2c6b193 | 2010-01-18 22:33:01 +0000 | [diff] [blame] | 209 | CurLexer->Lex(Tok); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 210 | |
Douglas Gregor | f44e854 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 211 | if (Tok.is(tok::code_completion)) { |
| 212 | if (CodeComplete) |
| 213 | CodeComplete->CodeCompleteInConditionalExclusion(); |
| 214 | continue; |
| 215 | } |
| 216 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 217 | // If this is the end of the buffer, we have an error. |
| 218 | if (Tok.is(tok::eof)) { |
| 219 | // Emit errors for each unterminated conditional on the stack, including |
| 220 | // the current one. |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 221 | while (!CurPPLexer->ConditionalStack.empty()) { |
Douglas Gregor | 2d474ba | 2010-08-12 17:04:55 +0000 | [diff] [blame] | 222 | if (!isCodeCompletionFile(Tok.getLocation())) |
| 223 | Diag(CurPPLexer->ConditionalStack.back().IfLoc, |
| 224 | diag::err_pp_unterminated_conditional); |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 225 | CurPPLexer->ConditionalStack.pop_back(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 226 | } |
| 227 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 228 | // Just return and let the caller lex after this #include. |
| 229 | break; |
| 230 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 231 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 232 | // If this token is not a preprocessor directive, just skip it. |
| 233 | if (Tok.isNot(tok::hash) || !Tok.isAtStartOfLine()) |
| 234 | continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 235 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 236 | // We just parsed a # character at the start of a line, so we're in |
| 237 | // directive mode. Tell the lexer this so any newlines we see will be |
| 238 | // converted into an EOM token (this terminates the macro). |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 239 | CurPPLexer->ParsingPreprocessorDirective = true; |
Ted Kremenek | ac6b06d | 2008-11-18 00:43:07 +0000 | [diff] [blame] | 240 | if (CurLexer) CurLexer->SetCommentRetentionState(false); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 241 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 242 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 243 | // Read the next token, the directive flavor. |
| 244 | LexUnexpandedToken(Tok); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 245 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 246 | // If this isn't an identifier directive (e.g. is "# 1\n" or "#\n", or |
| 247 | // something bogus), skip it. |
| 248 | if (Tok.isNot(tok::identifier)) { |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 249 | CurPPLexer->ParsingPreprocessorDirective = false; |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 250 | // Restore comment saving mode. |
Ted Kremenek | ac6b06d | 2008-11-18 00:43:07 +0000 | [diff] [blame] | 251 | if (CurLexer) CurLexer->SetCommentRetentionState(KeepComments); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 252 | continue; |
| 253 | } |
| 254 | |
| 255 | // If the first letter isn't i or e, it isn't intesting to us. We know that |
| 256 | // this is safe in the face of spelling differences, because there is no way |
| 257 | // to spell an i/e in a strange way that is another letter. Skipping this |
| 258 | // allows us to avoid looking up the identifier info for #define/#undef and |
| 259 | // other common directives. |
Douglas Gregor | a543016 | 2010-03-16 20:46:42 +0000 | [diff] [blame] | 260 | bool Invalid = false; |
| 261 | const char *RawCharData = SourceMgr.getCharacterData(Tok.getLocation(), |
| 262 | &Invalid); |
| 263 | if (Invalid) |
| 264 | return; |
| 265 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 266 | char FirstChar = RawCharData[0]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 267 | if (FirstChar >= 'a' && FirstChar <= 'z' && |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 268 | FirstChar != 'i' && FirstChar != 'e') { |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 269 | CurPPLexer->ParsingPreprocessorDirective = false; |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 270 | // Restore comment saving mode. |
Ted Kremenek | ac6b06d | 2008-11-18 00:43:07 +0000 | [diff] [blame] | 271 | if (CurLexer) CurLexer->SetCommentRetentionState(KeepComments); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 272 | continue; |
| 273 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 274 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 275 | // Get the identifier name without trigraphs or embedded newlines. Note |
| 276 | // that we can't use Tok.getIdentifierInfo() because its lookup is disabled |
| 277 | // when skipping. |
Benjamin Kramer | b939a4e | 2009-12-31 13:32:38 +0000 | [diff] [blame] | 278 | char DirectiveBuf[20]; |
| 279 | llvm::StringRef Directive; |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 280 | if (!Tok.needsCleaning() && Tok.getLength() < 20) { |
Benjamin Kramer | b939a4e | 2009-12-31 13:32:38 +0000 | [diff] [blame] | 281 | Directive = llvm::StringRef(RawCharData, Tok.getLength()); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 282 | } else { |
| 283 | std::string DirectiveStr = getSpelling(Tok); |
Benjamin Kramer | b939a4e | 2009-12-31 13:32:38 +0000 | [diff] [blame] | 284 | unsigned IdLen = DirectiveStr.size(); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 285 | if (IdLen >= 20) { |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 286 | CurPPLexer->ParsingPreprocessorDirective = false; |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 287 | // Restore comment saving mode. |
Ted Kremenek | ac6b06d | 2008-11-18 00:43:07 +0000 | [diff] [blame] | 288 | if (CurLexer) CurLexer->SetCommentRetentionState(KeepComments); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 289 | continue; |
| 290 | } |
Benjamin Kramer | b939a4e | 2009-12-31 13:32:38 +0000 | [diff] [blame] | 291 | memcpy(DirectiveBuf, &DirectiveStr[0], IdLen); |
| 292 | Directive = llvm::StringRef(DirectiveBuf, IdLen); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 293 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 294 | |
Benjamin Kramer | b939a4e | 2009-12-31 13:32:38 +0000 | [diff] [blame] | 295 | if (Directive.startswith("if")) { |
| 296 | llvm::StringRef Sub = Directive.substr(2); |
| 297 | if (Sub.empty() || // "if" |
| 298 | Sub == "def" || // "ifdef" |
| 299 | Sub == "ndef") { // "ifndef" |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 300 | // We know the entire #if/#ifdef/#ifndef block will be skipped, don't |
| 301 | // bother parsing the condition. |
| 302 | DiscardUntilEndOfDirective(); |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 303 | CurPPLexer->pushConditionalLevel(Tok.getLocation(), /*wasskipping*/true, |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 304 | /*foundnonskip*/false, |
| 305 | /*fnddelse*/false); |
| 306 | } |
Benjamin Kramer | b939a4e | 2009-12-31 13:32:38 +0000 | [diff] [blame] | 307 | } else if (Directive[0] == 'e') { |
| 308 | llvm::StringRef Sub = Directive.substr(1); |
| 309 | if (Sub == "ndif") { // "endif" |
Chris Lattner | 35410d5 | 2009-04-14 05:07:49 +0000 | [diff] [blame] | 310 | CheckEndOfDirective("endif"); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 311 | PPConditionalInfo CondInfo; |
| 312 | CondInfo.WasSkipping = true; // Silence bogus warning. |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 313 | bool InCond = CurPPLexer->popConditionalLevel(CondInfo); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 314 | InCond = InCond; // Silence warning in no-asserts mode. |
| 315 | assert(!InCond && "Can't be skipping if not in a conditional!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 316 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 317 | // If we popped the outermost skipping block, we're done skipping! |
| 318 | if (!CondInfo.WasSkipping) |
| 319 | break; |
Benjamin Kramer | b939a4e | 2009-12-31 13:32:38 +0000 | [diff] [blame] | 320 | } else if (Sub == "lse") { // "else". |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 321 | // #else directive in a skipping conditional. If not in some other |
| 322 | // skipping conditional, and if #else hasn't already been seen, enter it |
| 323 | // as a non-skipping conditional. |
Chris Lattner | 8fe00e7 | 2009-04-18 01:34:22 +0000 | [diff] [blame] | 324 | DiscardUntilEndOfDirective(); // C99 6.10p4. |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 325 | PPConditionalInfo &CondInfo = CurPPLexer->peekConditionalLevel(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 326 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 327 | // If this is a #else with a #else before it, report the error. |
| 328 | if (CondInfo.FoundElse) Diag(Tok, diag::pp_err_else_after_else); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 329 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 330 | // Note that we've seen a #else in this conditional. |
| 331 | CondInfo.FoundElse = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 332 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 333 | // If the conditional is at the top level, and the #if block wasn't |
| 334 | // entered, enter the #else block now. |
| 335 | if (!CondInfo.WasSkipping && !CondInfo.FoundNonSkip) { |
| 336 | CondInfo.FoundNonSkip = true; |
| 337 | break; |
| 338 | } |
Benjamin Kramer | b939a4e | 2009-12-31 13:32:38 +0000 | [diff] [blame] | 339 | } else if (Sub == "lif") { // "elif". |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 340 | PPConditionalInfo &CondInfo = CurPPLexer->peekConditionalLevel(); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 341 | |
| 342 | bool ShouldEnter; |
| 343 | // If this is in a skipping block or if we're already handled this #if |
| 344 | // block, don't bother parsing the condition. |
| 345 | if (CondInfo.WasSkipping || CondInfo.FoundNonSkip) { |
| 346 | DiscardUntilEndOfDirective(); |
| 347 | ShouldEnter = false; |
| 348 | } else { |
| 349 | // Restore the value of LexingRawMode so that identifiers are |
| 350 | // looked up, etc, inside the #elif expression. |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 351 | assert(CurPPLexer->LexingRawMode && "We have to be skipping here!"); |
| 352 | CurPPLexer->LexingRawMode = false; |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 353 | IdentifierInfo *IfNDefMacro = 0; |
| 354 | ShouldEnter = EvaluateDirectiveExpression(IfNDefMacro); |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 355 | CurPPLexer->LexingRawMode = true; |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 356 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 357 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 358 | // If this is a #elif with a #else before it, report the error. |
| 359 | if (CondInfo.FoundElse) Diag(Tok, diag::pp_err_elif_after_else); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 360 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 361 | // If this condition is true, enter it! |
| 362 | if (ShouldEnter) { |
| 363 | CondInfo.FoundNonSkip = true; |
| 364 | break; |
| 365 | } |
| 366 | } |
| 367 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 368 | |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 369 | CurPPLexer->ParsingPreprocessorDirective = false; |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 370 | // Restore comment saving mode. |
Ted Kremenek | ac6b06d | 2008-11-18 00:43:07 +0000 | [diff] [blame] | 371 | if (CurLexer) CurLexer->SetCommentRetentionState(KeepComments); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | // Finally, if we are out of the conditional (saw an #endif or ran off the end |
| 375 | // of the file, just stop skipping and return to lexing whatever came after |
| 376 | // the #if block. |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 377 | CurPPLexer->LexingRawMode = false; |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 378 | } |
| 379 | |
Ted Kremenek | 268ee70 | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 380 | void Preprocessor::PTHSkipExcludedConditionalBlock() { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 381 | |
| 382 | while (1) { |
Ted Kremenek | 268ee70 | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 383 | assert(CurPTHLexer); |
| 384 | assert(CurPTHLexer->LexingRawMode == false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 385 | |
Ted Kremenek | 268ee70 | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 386 | // Skip to the next '#else', '#elif', or #endif. |
| 387 | if (CurPTHLexer->SkipBlock()) { |
| 388 | // We have reached an #endif. Both the '#' and 'endif' tokens |
| 389 | // have been consumed by the PTHLexer. Just pop off the condition level. |
| 390 | PPConditionalInfo CondInfo; |
| 391 | bool InCond = CurPTHLexer->popConditionalLevel(CondInfo); |
| 392 | InCond = InCond; // Silence warning in no-asserts mode. |
| 393 | assert(!InCond && "Can't be skipping if not in a conditional!"); |
| 394 | break; |
| 395 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 396 | |
Ted Kremenek | 268ee70 | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 397 | // We have reached a '#else' or '#elif'. Lex the next token to get |
| 398 | // the directive flavor. |
| 399 | Token Tok; |
| 400 | LexUnexpandedToken(Tok); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 401 | |
Ted Kremenek | 268ee70 | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 402 | // We can actually look up the IdentifierInfo here since we aren't in |
| 403 | // raw mode. |
| 404 | tok::PPKeywordKind K = Tok.getIdentifierInfo()->getPPKeywordID(); |
| 405 | |
| 406 | if (K == tok::pp_else) { |
| 407 | // #else: Enter the else condition. We aren't in a nested condition |
| 408 | // since we skip those. We're always in the one matching the last |
| 409 | // blocked we skipped. |
| 410 | PPConditionalInfo &CondInfo = CurPTHLexer->peekConditionalLevel(); |
| 411 | // Note that we've seen a #else in this conditional. |
| 412 | CondInfo.FoundElse = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 413 | |
Ted Kremenek | 268ee70 | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 414 | // If the #if block wasn't entered then enter the #else block now. |
| 415 | if (!CondInfo.FoundNonSkip) { |
| 416 | CondInfo.FoundNonSkip = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 417 | |
Daniel Dunbar | 8533bd5 | 2009-04-13 17:57:49 +0000 | [diff] [blame] | 418 | // Scan until the eom token. |
Ted Kremenek | e5680f3 | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 419 | CurPTHLexer->ParsingPreprocessorDirective = true; |
Daniel Dunbar | 8533bd5 | 2009-04-13 17:57:49 +0000 | [diff] [blame] | 420 | DiscardUntilEndOfDirective(); |
Ted Kremenek | e5680f3 | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 421 | CurPTHLexer->ParsingPreprocessorDirective = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 422 | |
Ted Kremenek | 268ee70 | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 423 | break; |
| 424 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 425 | |
Ted Kremenek | 268ee70 | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 426 | // Otherwise skip this block. |
| 427 | continue; |
| 428 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 429 | |
Ted Kremenek | 268ee70 | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 430 | assert(K == tok::pp_elif); |
| 431 | PPConditionalInfo &CondInfo = CurPTHLexer->peekConditionalLevel(); |
| 432 | |
| 433 | // If this is a #elif with a #else before it, report the error. |
| 434 | if (CondInfo.FoundElse) |
| 435 | Diag(Tok, diag::pp_err_elif_after_else); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 436 | |
Ted Kremenek | 268ee70 | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 437 | // If this is in a skipping block or if we're already handled this #if |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 438 | // block, don't bother parsing the condition. We just skip this block. |
Ted Kremenek | 268ee70 | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 439 | if (CondInfo.FoundNonSkip) |
| 440 | continue; |
| 441 | |
| 442 | // Evaluate the condition of the #elif. |
| 443 | IdentifierInfo *IfNDefMacro = 0; |
| 444 | CurPTHLexer->ParsingPreprocessorDirective = true; |
| 445 | bool ShouldEnter = EvaluateDirectiveExpression(IfNDefMacro); |
| 446 | CurPTHLexer->ParsingPreprocessorDirective = false; |
| 447 | |
| 448 | // If this condition is true, enter it! |
| 449 | if (ShouldEnter) { |
| 450 | CondInfo.FoundNonSkip = true; |
| 451 | break; |
| 452 | } |
| 453 | |
| 454 | // Otherwise, skip this block and go to the next one. |
| 455 | continue; |
| 456 | } |
| 457 | } |
| 458 | |
Chris Lattner | 1072509 | 2008-03-09 04:17:44 +0000 | [diff] [blame] | 459 | /// LookupFile - Given a "foo" or <foo> reference, look up the indicated file, |
| 460 | /// return null on failure. isAngled indicates whether the file reference is |
| 461 | /// for system #include's or not (i.e. using <> instead of ""). |
Chris Lattner | a139481 | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 462 | const FileEntry *Preprocessor::LookupFile(llvm::StringRef Filename, |
Chris Lattner | 1072509 | 2008-03-09 04:17:44 +0000 | [diff] [blame] | 463 | bool isAngled, |
| 464 | const DirectoryLookup *FromDir, |
| 465 | const DirectoryLookup *&CurDir) { |
| 466 | // If the header lookup mechanism may be relative to the current file, pass in |
| 467 | // info about where the current file is. |
Douglas Gregor | 10fe93d | 2010-08-08 07:49:23 +0000 | [diff] [blame] | 468 | const FileEntry *CurFileEnt = 0; |
Chris Lattner | 1072509 | 2008-03-09 04:17:44 +0000 | [diff] [blame] | 469 | if (!FromDir) { |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 470 | FileID FID = getCurrentFileLexer()->getFileID(); |
Douglas Gregor | 10fe93d | 2010-08-08 07:49:23 +0000 | [diff] [blame] | 471 | CurFileEnt = SourceMgr.getFileEntryForID(FID); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 472 | |
Chris Lattner | be5c64d | 2009-02-04 19:45:07 +0000 | [diff] [blame] | 473 | // If there is no file entry associated with this file, it must be the |
| 474 | // predefines buffer. Any other file is not lexed with a normal lexer, so |
Douglas Gregor | 10fe93d | 2010-08-08 07:49:23 +0000 | [diff] [blame] | 475 | // it won't be scanned for preprocessor directives. If we have the |
| 476 | // predefines buffer, resolve #include references (which come from the |
| 477 | // -include command line argument) as if they came from the main file, this |
| 478 | // affects file lookup etc. |
| 479 | if (CurFileEnt == 0) { |
Chris Lattner | be5c64d | 2009-02-04 19:45:07 +0000 | [diff] [blame] | 480 | FID = SourceMgr.getMainFileID(); |
| 481 | CurFileEnt = SourceMgr.getFileEntryForID(FID); |
| 482 | } |
Chris Lattner | 1072509 | 2008-03-09 04:17:44 +0000 | [diff] [blame] | 483 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 484 | |
Chris Lattner | 1072509 | 2008-03-09 04:17:44 +0000 | [diff] [blame] | 485 | // Do a standard file entry lookup. |
| 486 | CurDir = CurDirLookup; |
| 487 | const FileEntry *FE = |
Douglas Gregor | 10fe93d | 2010-08-08 07:49:23 +0000 | [diff] [blame] | 488 | HeaderInfo.LookupFile(Filename, isAngled, FromDir, CurDir, CurFileEnt); |
Chris Lattner | f45b646 | 2010-01-22 00:14:44 +0000 | [diff] [blame] | 489 | if (FE) return FE; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 490 | |
Chris Lattner | 1072509 | 2008-03-09 04:17:44 +0000 | [diff] [blame] | 491 | // Otherwise, see if this is a subframework header. If so, this is relative |
| 492 | // to one of the headers on the #include stack. Walk the list of the current |
| 493 | // headers on the #include stack and pass them to HeaderInfo. |
Ted Kremenek | 81d24e1 | 2008-11-20 16:19:53 +0000 | [diff] [blame] | 494 | if (IsFileLexer()) { |
Ted Kremenek | 41938c8 | 2008-11-19 21:57:25 +0000 | [diff] [blame] | 495 | if ((CurFileEnt = SourceMgr.getFileEntryForID(CurPPLexer->getFileID()))) |
Chris Lattner | a139481 | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 496 | if ((FE = HeaderInfo.LookupSubframeworkHeader(Filename, CurFileEnt))) |
Chris Lattner | 1072509 | 2008-03-09 04:17:44 +0000 | [diff] [blame] | 497 | return FE; |
| 498 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 499 | |
Chris Lattner | 1072509 | 2008-03-09 04:17:44 +0000 | [diff] [blame] | 500 | for (unsigned i = 0, e = IncludeMacroStack.size(); i != e; ++i) { |
| 501 | IncludeStackInfo &ISEntry = IncludeMacroStack[e-i-1]; |
Ted Kremenek | 81d24e1 | 2008-11-20 16:19:53 +0000 | [diff] [blame] | 502 | if (IsFileLexer(ISEntry)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 503 | if ((CurFileEnt = |
Ted Kremenek | 41938c8 | 2008-11-19 21:57:25 +0000 | [diff] [blame] | 504 | SourceMgr.getFileEntryForID(ISEntry.ThePPLexer->getFileID()))) |
Chris Lattner | a139481 | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 505 | if ((FE = HeaderInfo.LookupSubframeworkHeader(Filename, CurFileEnt))) |
Chris Lattner | 1072509 | 2008-03-09 04:17:44 +0000 | [diff] [blame] | 506 | return FE; |
| 507 | } |
| 508 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 509 | |
Chris Lattner | 1072509 | 2008-03-09 04:17:44 +0000 | [diff] [blame] | 510 | // Otherwise, we really couldn't find the file. |
| 511 | return 0; |
| 512 | } |
| 513 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 514 | |
| 515 | //===----------------------------------------------------------------------===// |
| 516 | // Preprocessor Directive Handling. |
| 517 | //===----------------------------------------------------------------------===// |
| 518 | |
| 519 | /// HandleDirective - This callback is invoked when the lexer sees a # token |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 520 | /// at the start of a line. This consumes the directive, modifies the |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 521 | /// lexer/preprocessor state, and advances the lexer(s) so that the next token |
| 522 | /// read is the correct one. |
| 523 | void Preprocessor::HandleDirective(Token &Result) { |
| 524 | // FIXME: Traditional: # with whitespace before it not recognized by K&R? |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 525 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 526 | // We just parsed a # character at the start of a line, so we're in directive |
| 527 | // mode. Tell the lexer this so any newlines we see will be converted into an |
| 528 | // EOM token (which terminates the directive). |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 529 | CurPPLexer->ParsingPreprocessorDirective = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 530 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 531 | ++NumDirectives; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 532 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 533 | // We are about to read a token. For the multiple-include optimization FA to |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 534 | // work, we have to remember if we had read any tokens *before* this |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 535 | // pp-directive. |
Chris Lattner | 1d9c54d | 2009-12-14 04:54:40 +0000 | [diff] [blame] | 536 | bool ReadAnyTokensBeforeDirective =CurPPLexer->MIOpt.getHasReadAnyTokensVal(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 537 | |
Chris Lattner | 42aa16c | 2009-03-18 21:00:25 +0000 | [diff] [blame] | 538 | // Save the '#' token in case we need to return it later. |
| 539 | Token SavedHash = Result; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 540 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 541 | // Read the next token, the directive flavor. This isn't expanded due to |
| 542 | // C99 6.10.3p8. |
| 543 | LexUnexpandedToken(Result); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 544 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 545 | // C99 6.10.3p11: Is this preprocessor directive in macro invocation? e.g.: |
| 546 | // #define A(x) #x |
| 547 | // A(abc |
| 548 | // #warning blah |
| 549 | // def) |
| 550 | // If so, the user is relying on non-portable behavior, emit a diagnostic. |
| 551 | if (InMacroArgs) |
| 552 | Diag(Result, diag::ext_embedded_directive); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 553 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 554 | TryAgain: |
| 555 | switch (Result.getKind()) { |
| 556 | case tok::eom: |
| 557 | return; // null directive. |
| 558 | case tok::comment: |
| 559 | // Handle stuff like "# /*foo*/ define X" in -E -C mode. |
| 560 | LexUnexpandedToken(Result); |
| 561 | goto TryAgain; |
Douglas Gregor | f44e854 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 562 | case tok::code_completion: |
| 563 | if (CodeComplete) |
| 564 | CodeComplete->CodeCompleteDirective( |
| 565 | CurPPLexer->getConditionalStackDepth() > 0); |
| 566 | return; |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 567 | case tok::numeric_constant: // # 7 GNU line marker directive. |
Chris Lattner | 5f607c4 | 2009-03-18 20:41:10 +0000 | [diff] [blame] | 568 | if (getLangOptions().AsmPreprocessor) |
| 569 | break; // # 4 is not a preprocessor directive in .S files. |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 570 | return HandleDigitDirective(Result); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 571 | default: |
| 572 | IdentifierInfo *II = Result.getIdentifierInfo(); |
| 573 | if (II == 0) break; // Not an identifier. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 574 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 575 | // Ask what the preprocessor keyword ID is. |
| 576 | switch (II->getPPKeywordID()) { |
| 577 | default: break; |
| 578 | // C99 6.10.1 - Conditional Inclusion. |
| 579 | case tok::pp_if: |
| 580 | return HandleIfDirective(Result, ReadAnyTokensBeforeDirective); |
| 581 | case tok::pp_ifdef: |
| 582 | return HandleIfdefDirective(Result, false, true/*not valid for miopt*/); |
| 583 | case tok::pp_ifndef: |
| 584 | return HandleIfdefDirective(Result, true, ReadAnyTokensBeforeDirective); |
| 585 | case tok::pp_elif: |
| 586 | return HandleElifDirective(Result); |
| 587 | case tok::pp_else: |
| 588 | return HandleElseDirective(Result); |
| 589 | case tok::pp_endif: |
| 590 | return HandleEndifDirective(Result); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 591 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 592 | // C99 6.10.2 - Source File Inclusion. |
| 593 | case tok::pp_include: |
Chris Lattner | b8e240e | 2009-04-08 18:24:34 +0000 | [diff] [blame] | 594 | return HandleIncludeDirective(Result); // Handle #include. |
| 595 | case tok::pp___include_macros: |
Chris Lattner | de07665 | 2009-04-08 18:46:40 +0000 | [diff] [blame] | 596 | return HandleIncludeMacrosDirective(Result); // Handle -imacros. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 597 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 598 | // C99 6.10.3 - Macro Replacement. |
| 599 | case tok::pp_define: |
| 600 | return HandleDefineDirective(Result); |
| 601 | case tok::pp_undef: |
| 602 | return HandleUndefDirective(Result); |
| 603 | |
| 604 | // C99 6.10.4 - Line Control. |
| 605 | case tok::pp_line: |
Chris Lattner | 359cc44 | 2009-01-26 05:29:08 +0000 | [diff] [blame] | 606 | return HandleLineDirective(Result); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 607 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 608 | // C99 6.10.5 - Error Directive. |
| 609 | case tok::pp_error: |
| 610 | return HandleUserDiagnosticDirective(Result, false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 611 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 612 | // C99 6.10.6 - Pragma Directive. |
| 613 | case tok::pp_pragma: |
Douglas Gregor | 80c60f7 | 2010-09-09 22:45:38 +0000 | [diff] [blame] | 614 | return HandlePragmaDirective(PIK_HashPragma); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 615 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 616 | // GNU Extensions. |
| 617 | case tok::pp_import: |
| 618 | return HandleImportDirective(Result); |
| 619 | case tok::pp_include_next: |
| 620 | return HandleIncludeNextDirective(Result); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 621 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 622 | case tok::pp_warning: |
| 623 | Diag(Result, diag::ext_pp_warning_directive); |
| 624 | return HandleUserDiagnosticDirective(Result, true); |
| 625 | case tok::pp_ident: |
| 626 | return HandleIdentSCCSDirective(Result); |
| 627 | case tok::pp_sccs: |
| 628 | return HandleIdentSCCSDirective(Result); |
| 629 | case tok::pp_assert: |
| 630 | //isExtension = true; // FIXME: implement #assert |
| 631 | break; |
| 632 | case tok::pp_unassert: |
| 633 | //isExtension = true; // FIXME: implement #unassert |
| 634 | break; |
| 635 | } |
| 636 | break; |
| 637 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 638 | |
Chris Lattner | 42aa16c | 2009-03-18 21:00:25 +0000 | [diff] [blame] | 639 | // If this is a .S file, treat unknown # directives as non-preprocessor |
| 640 | // directives. This is important because # may be a comment or introduce |
| 641 | // various pseudo-ops. Just return the # token and push back the following |
| 642 | // token to be lexed next time. |
| 643 | if (getLangOptions().AsmPreprocessor) { |
Daniel Dunbar | 3d399a0 | 2009-07-13 21:48:50 +0000 | [diff] [blame] | 644 | Token *Toks = new Token[2]; |
Chris Lattner | 42aa16c | 2009-03-18 21:00:25 +0000 | [diff] [blame] | 645 | // Return the # and the token after it. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 646 | Toks[0] = SavedHash; |
Chris Lattner | 42aa16c | 2009-03-18 21:00:25 +0000 | [diff] [blame] | 647 | Toks[1] = Result; |
| 648 | // Enter this token stream so that we re-lex the tokens. Make sure to |
| 649 | // enable macro expansion, in case the token after the # is an identifier |
| 650 | // that is expanded. |
| 651 | EnterTokenStream(Toks, 2, false, true); |
| 652 | return; |
| 653 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 654 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 655 | // If we reached here, the preprocessing token is not valid! |
| 656 | Diag(Result, diag::err_pp_invalid_directive); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 657 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 658 | // Read the rest of the PP line. |
| 659 | DiscardUntilEndOfDirective(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 660 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 661 | // Okay, we're done parsing the directive. |
| 662 | } |
| 663 | |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 664 | /// GetLineValue - Convert a numeric token into an unsigned value, emitting |
| 665 | /// Diagnostic DiagID if it is invalid, and returning the value in Val. |
| 666 | static bool GetLineValue(Token &DigitTok, unsigned &Val, |
| 667 | unsigned DiagID, Preprocessor &PP) { |
| 668 | if (DigitTok.isNot(tok::numeric_constant)) { |
| 669 | PP.Diag(DigitTok, DiagID); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 670 | |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 671 | if (DigitTok.isNot(tok::eom)) |
| 672 | PP.DiscardUntilEndOfDirective(); |
| 673 | return true; |
| 674 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 675 | |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 676 | llvm::SmallString<64> IntegerBuffer; |
| 677 | IntegerBuffer.resize(DigitTok.getLength()); |
| 678 | const char *DigitTokBegin = &IntegerBuffer[0]; |
Douglas Gregor | 453091c | 2010-03-16 22:30:13 +0000 | [diff] [blame] | 679 | bool Invalid = false; |
| 680 | unsigned ActualLength = PP.getSpelling(DigitTok, DigitTokBegin, &Invalid); |
| 681 | if (Invalid) |
| 682 | return true; |
| 683 | |
Chris Lattner | dc8c90d | 2009-04-18 18:35:15 +0000 | [diff] [blame] | 684 | // Verify that we have a simple digit-sequence, and compute the value. This |
| 685 | // is always a simple digit string computed in decimal, so we do this manually |
| 686 | // here. |
| 687 | Val = 0; |
| 688 | for (unsigned i = 0; i != ActualLength; ++i) { |
| 689 | if (!isdigit(DigitTokBegin[i])) { |
| 690 | PP.Diag(PP.AdvanceToTokenCharacter(DigitTok.getLocation(), i), |
| 691 | diag::err_pp_line_digit_sequence); |
| 692 | PP.DiscardUntilEndOfDirective(); |
| 693 | return true; |
| 694 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 695 | |
Chris Lattner | dc8c90d | 2009-04-18 18:35:15 +0000 | [diff] [blame] | 696 | unsigned NextVal = Val*10+(DigitTokBegin[i]-'0'); |
| 697 | if (NextVal < Val) { // overflow. |
| 698 | PP.Diag(DigitTok, DiagID); |
| 699 | PP.DiscardUntilEndOfDirective(); |
| 700 | return true; |
| 701 | } |
| 702 | Val = NextVal; |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 703 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 704 | |
| 705 | // Reject 0, this is needed both by #line numbers and flags. |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 706 | if (Val == 0) { |
| 707 | PP.Diag(DigitTok, DiagID); |
| 708 | PP.DiscardUntilEndOfDirective(); |
| 709 | return true; |
| 710 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 711 | |
Chris Lattner | dc8c90d | 2009-04-18 18:35:15 +0000 | [diff] [blame] | 712 | if (DigitTokBegin[0] == '0') |
| 713 | PP.Diag(DigitTok.getLocation(), diag::warn_pp_line_decimal); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 714 | |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 715 | return false; |
| 716 | } |
| 717 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 718 | /// HandleLineDirective - Handle #line directive: C99 6.10.4. The two |
Chris Lattner | 359cc44 | 2009-01-26 05:29:08 +0000 | [diff] [blame] | 719 | /// acceptable forms are: |
| 720 | /// # line digit-sequence |
| 721 | /// # line digit-sequence "s-char-sequence" |
| 722 | void Preprocessor::HandleLineDirective(Token &Tok) { |
| 723 | // Read the line # and string argument. Per C99 6.10.4p5, these tokens are |
| 724 | // expanded. |
| 725 | Token DigitTok; |
| 726 | Lex(DigitTok); |
| 727 | |
Chris Lattner | 359cc44 | 2009-01-26 05:29:08 +0000 | [diff] [blame] | 728 | // Validate the number and convert it to an unsigned. |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 729 | unsigned LineNo; |
Chris Lattner | dc8c90d | 2009-04-18 18:35:15 +0000 | [diff] [blame] | 730 | if (GetLineValue(DigitTok, LineNo, diag::err_pp_line_requires_integer,*this)) |
Chris Lattner | 359cc44 | 2009-01-26 05:29:08 +0000 | [diff] [blame] | 731 | return; |
Chris Lattner | 359cc44 | 2009-01-26 05:29:08 +0000 | [diff] [blame] | 732 | |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 733 | // Enforce C99 6.10.4p3: "The digit sequence shall not specify ... a |
| 734 | // number greater than 2147483647". C90 requires that the line # be <= 32767. |
Chris Lattner | 359cc44 | 2009-01-26 05:29:08 +0000 | [diff] [blame] | 735 | unsigned LineLimit = Features.C99 ? 2147483648U : 32768U; |
| 736 | if (LineNo >= LineLimit) |
| 737 | Diag(DigitTok, diag::ext_pp_line_too_big) << LineLimit; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 738 | |
Chris Lattner | 5b9a504 | 2009-01-26 07:57:50 +0000 | [diff] [blame] | 739 | int FilenameID = -1; |
Chris Lattner | 359cc44 | 2009-01-26 05:29:08 +0000 | [diff] [blame] | 740 | Token StrTok; |
| 741 | Lex(StrTok); |
| 742 | |
| 743 | // If the StrTok is "eom", then it wasn't present. Otherwise, it must be a |
| 744 | // string followed by eom. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 745 | if (StrTok.is(tok::eom)) |
Chris Lattner | 359cc44 | 2009-01-26 05:29:08 +0000 | [diff] [blame] | 746 | ; // ok |
| 747 | else if (StrTok.isNot(tok::string_literal)) { |
| 748 | Diag(StrTok, diag::err_pp_line_invalid_filename); |
| 749 | DiscardUntilEndOfDirective(); |
| 750 | return; |
| 751 | } else { |
Chris Lattner | 5b9a504 | 2009-01-26 07:57:50 +0000 | [diff] [blame] | 752 | // Parse and validate the string, converting it into a unique ID. |
| 753 | StringLiteralParser Literal(&StrTok, 1, *this); |
| 754 | assert(!Literal.AnyWide && "Didn't allow wide strings in"); |
| 755 | if (Literal.hadError) |
| 756 | return DiscardUntilEndOfDirective(); |
| 757 | if (Literal.Pascal) { |
| 758 | Diag(StrTok, diag::err_pp_linemarker_invalid_filename); |
| 759 | return DiscardUntilEndOfDirective(); |
| 760 | } |
| 761 | FilenameID = SourceMgr.getLineTableFilenameID(Literal.GetString(), |
| 762 | Literal.GetStringLength()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 763 | |
Chris Lattner | ab82f41 | 2009-04-17 23:30:53 +0000 | [diff] [blame] | 764 | // Verify that there is nothing after the string, other than EOM. Because |
| 765 | // of C99 6.10.4p5, macros that expand to empty tokens are ok. |
| 766 | CheckEndOfDirective("line", true); |
Chris Lattner | 359cc44 | 2009-01-26 05:29:08 +0000 | [diff] [blame] | 767 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 768 | |
Chris Lattner | 4c4ea17 | 2009-02-03 21:52:55 +0000 | [diff] [blame] | 769 | SourceMgr.AddLineNote(DigitTok.getLocation(), LineNo, FilenameID); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 770 | |
Chris Lattner | 1662938 | 2009-03-27 17:13:49 +0000 | [diff] [blame] | 771 | if (Callbacks) |
Chris Lattner | 86d0ef7 | 2010-04-14 04:28:50 +0000 | [diff] [blame] | 772 | Callbacks->FileChanged(CurPPLexer->getSourceLocation(), |
| 773 | PPCallbacks::RenameFile, |
Chris Lattner | 1662938 | 2009-03-27 17:13:49 +0000 | [diff] [blame] | 774 | SrcMgr::C_User); |
Chris Lattner | 359cc44 | 2009-01-26 05:29:08 +0000 | [diff] [blame] | 775 | } |
| 776 | |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 777 | /// ReadLineMarkerFlags - Parse and validate any flags at the end of a GNU line |
| 778 | /// marker directive. |
| 779 | static bool ReadLineMarkerFlags(bool &IsFileEntry, bool &IsFileExit, |
| 780 | bool &IsSystemHeader, bool &IsExternCHeader, |
| 781 | Preprocessor &PP) { |
| 782 | unsigned FlagVal; |
| 783 | Token FlagTok; |
| 784 | PP.Lex(FlagTok); |
| 785 | if (FlagTok.is(tok::eom)) return false; |
| 786 | if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag, PP)) |
| 787 | return true; |
| 788 | |
| 789 | if (FlagVal == 1) { |
| 790 | IsFileEntry = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 791 | |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 792 | PP.Lex(FlagTok); |
| 793 | if (FlagTok.is(tok::eom)) return false; |
| 794 | if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag,PP)) |
| 795 | return true; |
| 796 | } else if (FlagVal == 2) { |
| 797 | IsFileExit = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 798 | |
Chris Lattner | 137b6a6 | 2009-02-04 06:25:26 +0000 | [diff] [blame] | 799 | SourceManager &SM = PP.getSourceManager(); |
| 800 | // If we are leaving the current presumed file, check to make sure the |
| 801 | // presumed include stack isn't empty! |
| 802 | FileID CurFileID = |
| 803 | SM.getDecomposedInstantiationLoc(FlagTok.getLocation()).first; |
| 804 | PresumedLoc PLoc = SM.getPresumedLoc(FlagTok.getLocation()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 805 | |
Chris Lattner | 137b6a6 | 2009-02-04 06:25:26 +0000 | [diff] [blame] | 806 | // If there is no include loc (main file) or if the include loc is in a |
| 807 | // different physical file, then we aren't in a "1" line marker flag region. |
| 808 | SourceLocation IncLoc = PLoc.getIncludeLoc(); |
| 809 | if (IncLoc.isInvalid() || |
| 810 | SM.getDecomposedInstantiationLoc(IncLoc).first != CurFileID) { |
| 811 | PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_pop); |
| 812 | PP.DiscardUntilEndOfDirective(); |
| 813 | return true; |
| 814 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 815 | |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 816 | PP.Lex(FlagTok); |
| 817 | if (FlagTok.is(tok::eom)) return false; |
| 818 | if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag,PP)) |
| 819 | return true; |
| 820 | } |
| 821 | |
| 822 | // We must have 3 if there are still flags. |
| 823 | if (FlagVal != 3) { |
| 824 | PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_flag); |
Chris Lattner | 5b9a504 | 2009-01-26 07:57:50 +0000 | [diff] [blame] | 825 | PP.DiscardUntilEndOfDirective(); |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 826 | return true; |
| 827 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 828 | |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 829 | IsSystemHeader = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 830 | |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 831 | PP.Lex(FlagTok); |
| 832 | if (FlagTok.is(tok::eom)) return false; |
Chris Lattner | 9d79eba | 2009-02-04 05:21:58 +0000 | [diff] [blame] | 833 | if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag, PP)) |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 834 | return true; |
| 835 | |
| 836 | // We must have 4 if there is yet another flag. |
| 837 | if (FlagVal != 4) { |
| 838 | PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_flag); |
Chris Lattner | 5b9a504 | 2009-01-26 07:57:50 +0000 | [diff] [blame] | 839 | PP.DiscardUntilEndOfDirective(); |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 840 | return true; |
| 841 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 842 | |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 843 | IsExternCHeader = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 844 | |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 845 | PP.Lex(FlagTok); |
| 846 | if (FlagTok.is(tok::eom)) return false; |
| 847 | |
| 848 | // There are no more valid flags here. |
| 849 | PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_flag); |
Chris Lattner | 5b9a504 | 2009-01-26 07:57:50 +0000 | [diff] [blame] | 850 | PP.DiscardUntilEndOfDirective(); |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 851 | return true; |
| 852 | } |
| 853 | |
| 854 | /// HandleDigitDirective - Handle a GNU line marker directive, whose syntax is |
| 855 | /// one of the following forms: |
| 856 | /// |
| 857 | /// # 42 |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 858 | /// # 42 "file" ('1' | '2')? |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 859 | /// # 42 "file" ('1' | '2')? '3' '4'? |
| 860 | /// |
| 861 | void Preprocessor::HandleDigitDirective(Token &DigitTok) { |
| 862 | // Validate the number and convert it to an unsigned. GNU does not have a |
| 863 | // line # limit other than it fit in 32-bits. |
| 864 | unsigned LineNo; |
| 865 | if (GetLineValue(DigitTok, LineNo, diag::err_pp_linemarker_requires_integer, |
| 866 | *this)) |
| 867 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 868 | |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 869 | Token StrTok; |
| 870 | Lex(StrTok); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 871 | |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 872 | bool IsFileEntry = false, IsFileExit = false; |
| 873 | bool IsSystemHeader = false, IsExternCHeader = false; |
Chris Lattner | 5b9a504 | 2009-01-26 07:57:50 +0000 | [diff] [blame] | 874 | int FilenameID = -1; |
| 875 | |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 876 | // If the StrTok is "eom", then it wasn't present. Otherwise, it must be a |
| 877 | // string followed by eom. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 878 | if (StrTok.is(tok::eom)) |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 879 | ; // ok |
| 880 | else if (StrTok.isNot(tok::string_literal)) { |
| 881 | Diag(StrTok, diag::err_pp_linemarker_invalid_filename); |
Chris Lattner | 5b9a504 | 2009-01-26 07:57:50 +0000 | [diff] [blame] | 882 | return DiscardUntilEndOfDirective(); |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 883 | } else { |
Chris Lattner | 5b9a504 | 2009-01-26 07:57:50 +0000 | [diff] [blame] | 884 | // Parse and validate the string, converting it into a unique ID. |
| 885 | StringLiteralParser Literal(&StrTok, 1, *this); |
| 886 | assert(!Literal.AnyWide && "Didn't allow wide strings in"); |
| 887 | if (Literal.hadError) |
| 888 | return DiscardUntilEndOfDirective(); |
| 889 | if (Literal.Pascal) { |
| 890 | Diag(StrTok, diag::err_pp_linemarker_invalid_filename); |
| 891 | return DiscardUntilEndOfDirective(); |
| 892 | } |
| 893 | FilenameID = SourceMgr.getLineTableFilenameID(Literal.GetString(), |
| 894 | Literal.GetStringLength()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 895 | |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 896 | // If a filename was present, read any flags that are present. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 897 | if (ReadLineMarkerFlags(IsFileEntry, IsFileExit, |
Chris Lattner | 5b9a504 | 2009-01-26 07:57:50 +0000 | [diff] [blame] | 898 | IsSystemHeader, IsExternCHeader, *this)) |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 899 | return; |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 900 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 901 | |
Chris Lattner | 9d79eba | 2009-02-04 05:21:58 +0000 | [diff] [blame] | 902 | // Create a line note with this information. |
| 903 | SourceMgr.AddLineNote(DigitTok.getLocation(), LineNo, FilenameID, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 904 | IsFileEntry, IsFileExit, |
Chris Lattner | 9d79eba | 2009-02-04 05:21:58 +0000 | [diff] [blame] | 905 | IsSystemHeader, IsExternCHeader); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 906 | |
Chris Lattner | 1662938 | 2009-03-27 17:13:49 +0000 | [diff] [blame] | 907 | // If the preprocessor has callbacks installed, notify them of the #line |
| 908 | // change. This is used so that the line marker comes out in -E mode for |
| 909 | // example. |
| 910 | if (Callbacks) { |
| 911 | PPCallbacks::FileChangeReason Reason = PPCallbacks::RenameFile; |
| 912 | if (IsFileEntry) |
| 913 | Reason = PPCallbacks::EnterFile; |
| 914 | else if (IsFileExit) |
| 915 | Reason = PPCallbacks::ExitFile; |
| 916 | SrcMgr::CharacteristicKind FileKind = SrcMgr::C_User; |
| 917 | if (IsExternCHeader) |
| 918 | FileKind = SrcMgr::C_ExternCSystem; |
| 919 | else if (IsSystemHeader) |
| 920 | FileKind = SrcMgr::C_System; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 921 | |
Chris Lattner | 86d0ef7 | 2010-04-14 04:28:50 +0000 | [diff] [blame] | 922 | Callbacks->FileChanged(CurPPLexer->getSourceLocation(), Reason, FileKind); |
Chris Lattner | 1662938 | 2009-03-27 17:13:49 +0000 | [diff] [blame] | 923 | } |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 924 | } |
| 925 | |
| 926 | |
Chris Lattner | 099dd05 | 2009-01-26 05:30:54 +0000 | [diff] [blame] | 927 | /// HandleUserDiagnosticDirective - Handle a #warning or #error directive. |
| 928 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 929 | void Preprocessor::HandleUserDiagnosticDirective(Token &Tok, |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 930 | bool isWarning) { |
Chris Lattner | 099dd05 | 2009-01-26 05:30:54 +0000 | [diff] [blame] | 931 | // PTH doesn't emit #warning or #error directives. |
| 932 | if (CurPTHLexer) |
Chris Lattner | 359cc44 | 2009-01-26 05:29:08 +0000 | [diff] [blame] | 933 | return CurPTHLexer->DiscardToEndOfLine(); |
| 934 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 935 | // Read the rest of the line raw. We do this because we don't want macros |
| 936 | // to be expanded and we don't require that the tokens be valid preprocessing |
| 937 | // tokens. For example, this is allowed: "#warning ` 'foo". GCC does |
| 938 | // collapse multiple consequtive white space between tokens, but this isn't |
| 939 | // specified by the standard. |
Chris Lattner | 359cc44 | 2009-01-26 05:29:08 +0000 | [diff] [blame] | 940 | std::string Message = CurLexer->ReadToEndOfLine(); |
| 941 | if (isWarning) |
| 942 | Diag(Tok, diag::pp_hash_warning) << Message; |
| 943 | else |
| 944 | Diag(Tok, diag::err_pp_hash_error) << Message; |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 945 | } |
| 946 | |
| 947 | /// HandleIdentSCCSDirective - Handle a #ident/#sccs directive. |
| 948 | /// |
| 949 | void Preprocessor::HandleIdentSCCSDirective(Token &Tok) { |
| 950 | // Yes, this directive is an extension. |
| 951 | Diag(Tok, diag::ext_pp_ident_directive); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 952 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 953 | // Read the string argument. |
| 954 | Token StrTok; |
| 955 | Lex(StrTok); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 956 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 957 | // If the token kind isn't a string, it's a malformed directive. |
| 958 | if (StrTok.isNot(tok::string_literal) && |
Chris Lattner | 3692b09 | 2008-11-18 07:59:24 +0000 | [diff] [blame] | 959 | StrTok.isNot(tok::wide_string_literal)) { |
| 960 | Diag(StrTok, diag::err_pp_malformed_ident); |
Chris Lattner | 099dd05 | 2009-01-26 05:30:54 +0000 | [diff] [blame] | 961 | if (StrTok.isNot(tok::eom)) |
| 962 | DiscardUntilEndOfDirective(); |
Chris Lattner | 3692b09 | 2008-11-18 07:59:24 +0000 | [diff] [blame] | 963 | return; |
| 964 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 965 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 966 | // Verify that there is nothing after the string, other than EOM. |
Chris Lattner | 35410d5 | 2009-04-14 05:07:49 +0000 | [diff] [blame] | 967 | CheckEndOfDirective("ident"); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 968 | |
Douglas Gregor | 453091c | 2010-03-16 22:30:13 +0000 | [diff] [blame] | 969 | if (Callbacks) { |
| 970 | bool Invalid = false; |
| 971 | std::string Str = getSpelling(StrTok, &Invalid); |
| 972 | if (!Invalid) |
| 973 | Callbacks->Ident(Tok.getLocation(), Str); |
| 974 | } |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 975 | } |
| 976 | |
| 977 | //===----------------------------------------------------------------------===// |
| 978 | // Preprocessor Include Directive Handling. |
| 979 | //===----------------------------------------------------------------------===// |
| 980 | |
| 981 | /// GetIncludeFilenameSpelling - Turn the specified lexer token into a fully |
| 982 | /// checked and spelled filename, e.g. as an operand of #include. This returns |
| 983 | /// true if the input filename was in <>'s or false if it were in ""'s. The |
| 984 | /// caller is expected to provide a buffer that is large enough to hold the |
| 985 | /// spelling of the filename, but is also expected to handle the case when |
| 986 | /// this method decides to use a different buffer. |
| 987 | bool Preprocessor::GetIncludeFilenameSpelling(SourceLocation Loc, |
Chris Lattner | a139481 | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 988 | llvm::StringRef &Buffer) { |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 989 | // Get the text form of the filename. |
Chris Lattner | a139481 | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 990 | assert(!Buffer.empty() && "Can't have tokens with empty spellings!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 991 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 992 | // Make sure the filename is <x> or "x". |
| 993 | bool isAngled; |
Chris Lattner | a139481 | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 994 | if (Buffer[0] == '<') { |
| 995 | if (Buffer.back() != '>') { |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 996 | Diag(Loc, diag::err_pp_expects_filename); |
Chris Lattner | a139481 | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 997 | Buffer = llvm::StringRef(); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 998 | return true; |
| 999 | } |
| 1000 | isAngled = true; |
Chris Lattner | a139481 | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 1001 | } else if (Buffer[0] == '"') { |
| 1002 | if (Buffer.back() != '"') { |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1003 | Diag(Loc, diag::err_pp_expects_filename); |
Chris Lattner | a139481 | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 1004 | Buffer = llvm::StringRef(); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1005 | return true; |
| 1006 | } |
| 1007 | isAngled = false; |
| 1008 | } else { |
| 1009 | Diag(Loc, diag::err_pp_expects_filename); |
Chris Lattner | a139481 | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 1010 | Buffer = llvm::StringRef(); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1011 | return true; |
| 1012 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1013 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1014 | // Diagnose #include "" as invalid. |
Chris Lattner | a139481 | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 1015 | if (Buffer.size() <= 2) { |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1016 | Diag(Loc, diag::err_pp_empty_filename); |
Chris Lattner | a139481 | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 1017 | Buffer = llvm::StringRef(); |
| 1018 | return true; |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1019 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1020 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1021 | // Skip the brackets. |
Chris Lattner | a139481 | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 1022 | Buffer = Buffer.substr(1, Buffer.size()-2); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1023 | return isAngled; |
| 1024 | } |
| 1025 | |
| 1026 | /// ConcatenateIncludeName - Handle cases where the #include name is expanded |
| 1027 | /// from a macro as multiple tokens, which need to be glued together. This |
| 1028 | /// occurs for code like: |
| 1029 | /// #define FOO <a/b.h> |
| 1030 | /// #include FOO |
| 1031 | /// because in this case, "<a/b.h>" is returned as 7 tokens, not one. |
| 1032 | /// |
| 1033 | /// This code concatenates and consumes tokens up to the '>' token. It returns |
| 1034 | /// false if the > was found, otherwise it returns true if it finds and consumes |
| 1035 | /// the EOM marker. |
John Thompson | a28cc09 | 2009-10-30 13:49:06 +0000 | [diff] [blame] | 1036 | bool Preprocessor::ConcatenateIncludeName( |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1037 | llvm::SmallString<128> &FilenameBuffer) { |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1038 | Token CurTok; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1039 | |
John Thompson | a28cc09 | 2009-10-30 13:49:06 +0000 | [diff] [blame] | 1040 | Lex(CurTok); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1041 | while (CurTok.isNot(tok::eom)) { |
| 1042 | // Append the spelling of this token to the buffer. If there was a space |
| 1043 | // before it, add it now. |
| 1044 | if (CurTok.hasLeadingSpace()) |
| 1045 | FilenameBuffer.push_back(' '); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1046 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1047 | // Get the spelling of the token, directly into FilenameBuffer if possible. |
| 1048 | unsigned PreAppendSize = FilenameBuffer.size(); |
| 1049 | FilenameBuffer.resize(PreAppendSize+CurTok.getLength()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1050 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1051 | const char *BufPtr = &FilenameBuffer[PreAppendSize]; |
John Thompson | a28cc09 | 2009-10-30 13:49:06 +0000 | [diff] [blame] | 1052 | unsigned ActualLen = getSpelling(CurTok, BufPtr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1053 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1054 | // If the token was spelled somewhere else, copy it into FilenameBuffer. |
| 1055 | if (BufPtr != &FilenameBuffer[PreAppendSize]) |
| 1056 | memcpy(&FilenameBuffer[PreAppendSize], BufPtr, ActualLen); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1057 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1058 | // Resize FilenameBuffer to the correct size. |
| 1059 | if (CurTok.getLength() != ActualLen) |
| 1060 | FilenameBuffer.resize(PreAppendSize+ActualLen); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1061 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1062 | // If we found the '>' marker, return success. |
| 1063 | if (CurTok.is(tok::greater)) |
| 1064 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1065 | |
John Thompson | a28cc09 | 2009-10-30 13:49:06 +0000 | [diff] [blame] | 1066 | Lex(CurTok); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1067 | } |
| 1068 | |
| 1069 | // If we hit the eom marker, emit an error and return true so that the caller |
| 1070 | // knows the EOM has been read. |
John Thompson | a28cc09 | 2009-10-30 13:49:06 +0000 | [diff] [blame] | 1071 | Diag(CurTok.getLocation(), diag::err_pp_expects_filename); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1072 | return true; |
| 1073 | } |
| 1074 | |
| 1075 | /// HandleIncludeDirective - The "#include" tokens have just been read, read the |
| 1076 | /// file to be included from the lexer, then include it! This is a common |
| 1077 | /// routine with functionality shared between #include, #include_next and |
Chris Lattner | 7218183 | 2008-09-26 20:12:23 +0000 | [diff] [blame] | 1078 | /// #import. LookupFrom is set when this is a #include_next directive, it |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1079 | /// specifies the file to start searching from. |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1080 | void Preprocessor::HandleIncludeDirective(Token &IncludeTok, |
| 1081 | const DirectoryLookup *LookupFrom, |
| 1082 | bool isImport) { |
| 1083 | |
| 1084 | Token FilenameTok; |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 1085 | CurPPLexer->LexIncludeFilename(FilenameTok); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1086 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1087 | // Reserve a buffer to get the spelling. |
Chris Lattner | a139481 | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 1088 | llvm::SmallString<128> FilenameBuffer; |
| 1089 | llvm::StringRef Filename; |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1090 | |
| 1091 | switch (FilenameTok.getKind()) { |
| 1092 | case tok::eom: |
| 1093 | // If the token kind is EOM, the error has already been diagnosed. |
| 1094 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1095 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1096 | case tok::angle_string_literal: |
Benjamin Kramer | ddeea56 | 2010-02-27 13:44:12 +0000 | [diff] [blame] | 1097 | case tok::string_literal: |
| 1098 | Filename = getSpelling(FilenameTok, FilenameBuffer); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1099 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1100 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1101 | case tok::less: |
| 1102 | // This could be a <foo/bar.h> file coming from a macro expansion. In this |
| 1103 | // case, glue the tokens together into FilenameBuffer and interpret those. |
| 1104 | FilenameBuffer.push_back('<'); |
John Thompson | a28cc09 | 2009-10-30 13:49:06 +0000 | [diff] [blame] | 1105 | if (ConcatenateIncludeName(FilenameBuffer)) |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1106 | return; // Found <eom> but no ">"? Diagnostic already emitted. |
Chris Lattner | a139481 | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 1107 | Filename = FilenameBuffer.str(); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1108 | break; |
| 1109 | default: |
| 1110 | Diag(FilenameTok.getLocation(), diag::err_pp_expects_filename); |
| 1111 | DiscardUntilEndOfDirective(); |
| 1112 | return; |
| 1113 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1114 | |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1115 | bool isAngled = |
Chris Lattner | a139481 | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 1116 | GetIncludeFilenameSpelling(FilenameTok.getLocation(), Filename); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1117 | // If GetIncludeFilenameSpelling set the start ptr to null, there was an |
| 1118 | // error. |
Chris Lattner | a139481 | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 1119 | if (Filename.empty()) { |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1120 | DiscardUntilEndOfDirective(); |
| 1121 | return; |
| 1122 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1123 | |
Chris Lattner | 9cb51ce | 2009-04-17 23:56:52 +0000 | [diff] [blame] | 1124 | // Verify that there is nothing after the filename, other than EOM. Note that |
| 1125 | // we allow macros that expand to nothing after the filename, because this |
| 1126 | // falls into the category of "#include pp-tokens new-line" specified in |
| 1127 | // C99 6.10.2p4. |
Daniel Dunbar | e013d68 | 2009-10-18 20:26:12 +0000 | [diff] [blame] | 1128 | CheckEndOfDirective(IncludeTok.getIdentifierInfo()->getNameStart(), true); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1129 | |
| 1130 | // Check that we don't have infinite #include recursion. |
Chris Lattner | 3692b09 | 2008-11-18 07:59:24 +0000 | [diff] [blame] | 1131 | if (IncludeMacroStack.size() == MaxAllowedIncludeStackDepth-1) { |
| 1132 | Diag(FilenameTok, diag::err_pp_include_too_deep); |
| 1133 | return; |
| 1134 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1135 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1136 | // Search include directories. |
| 1137 | const DirectoryLookup *CurDir; |
Chris Lattner | f45b646 | 2010-01-22 00:14:44 +0000 | [diff] [blame] | 1138 | const FileEntry *File = LookupFile(Filename, isAngled, LookupFrom, CurDir); |
Chris Lattner | 3692b09 | 2008-11-18 07:59:24 +0000 | [diff] [blame] | 1139 | if (File == 0) { |
Chris Lattner | a139481 | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 1140 | Diag(FilenameTok, diag::err_pp_file_not_found) << Filename; |
Chris Lattner | 3692b09 | 2008-11-18 07:59:24 +0000 | [diff] [blame] | 1141 | return; |
| 1142 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1143 | |
Chris Lattner | 7218183 | 2008-09-26 20:12:23 +0000 | [diff] [blame] | 1144 | // The #included file will be considered to be a system header if either it is |
| 1145 | // in a system include directory, or if the #includer is a system include |
| 1146 | // header. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1147 | SrcMgr::CharacteristicKind FileCharacter = |
Chris Lattner | 0b9e736 | 2008-09-26 21:18:42 +0000 | [diff] [blame] | 1148 | std::max(HeaderInfo.getFileDirFlavor(File), |
Chris Lattner | 693faa6 | 2009-01-19 07:59:15 +0000 | [diff] [blame] | 1149 | SourceMgr.getFileCharacteristic(FilenameTok.getLocation())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1150 | |
Chris Lattner | 6fbe3eb | 2010-04-19 20:44:31 +0000 | [diff] [blame] | 1151 | // Ask HeaderInfo if we should enter this #include file. If not, #including |
| 1152 | // this file will have no effect. |
| 1153 | if (!HeaderInfo.ShouldEnterIncludeFile(File, isImport)) { |
Chris Lattner | e127a0d | 2010-04-20 20:35:58 +0000 | [diff] [blame] | 1154 | if (Callbacks) |
Chris Lattner | 6fbe3eb | 2010-04-19 20:44:31 +0000 | [diff] [blame] | 1155 | Callbacks->FileSkipped(*File, FilenameTok, FileCharacter); |
Chris Lattner | 6fbe3eb | 2010-04-19 20:44:31 +0000 | [diff] [blame] | 1156 | return; |
| 1157 | } |
| 1158 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1159 | // Look up the file, create a File ID for it. |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 1160 | FileID FID = SourceMgr.createFileID(File, FilenameTok.getLocation(), |
| 1161 | FileCharacter); |
| 1162 | if (FID.isInvalid()) { |
Chris Lattner | a139481 | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 1163 | Diag(FilenameTok, diag::err_pp_file_not_found) << Filename; |
Chris Lattner | 56b05c8 | 2008-11-18 08:02:48 +0000 | [diff] [blame] | 1164 | return; |
| 1165 | } |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1166 | |
| 1167 | // Finally, if all is good, enter the new file! |
Chris Lattner | e127a0d | 2010-04-20 20:35:58 +0000 | [diff] [blame] | 1168 | EnterSourceFile(FID, CurDir, FilenameTok.getLocation()); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1169 | } |
| 1170 | |
| 1171 | /// HandleIncludeNextDirective - Implements #include_next. |
| 1172 | /// |
| 1173 | void Preprocessor::HandleIncludeNextDirective(Token &IncludeNextTok) { |
| 1174 | Diag(IncludeNextTok, diag::ext_pp_include_next_directive); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1175 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1176 | // #include_next is like #include, except that we start searching after |
| 1177 | // the current found directory. If we can't do this, issue a |
| 1178 | // diagnostic. |
| 1179 | const DirectoryLookup *Lookup = CurDirLookup; |
| 1180 | if (isInPrimaryFile()) { |
| 1181 | Lookup = 0; |
| 1182 | Diag(IncludeNextTok, diag::pp_include_next_in_primary); |
| 1183 | } else if (Lookup == 0) { |
| 1184 | Diag(IncludeNextTok, diag::pp_include_next_absolute_path); |
| 1185 | } else { |
| 1186 | // Start looking up in the next directory. |
| 1187 | ++Lookup; |
| 1188 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1189 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1190 | return HandleIncludeDirective(IncludeNextTok, Lookup); |
| 1191 | } |
| 1192 | |
| 1193 | /// HandleImportDirective - Implements #import. |
| 1194 | /// |
| 1195 | void Preprocessor::HandleImportDirective(Token &ImportTok) { |
Chris Lattner | b627c8d | 2009-03-06 04:28:03 +0000 | [diff] [blame] | 1196 | if (!Features.ObjC1) // #import is standard for ObjC. |
| 1197 | Diag(ImportTok, diag::ext_pp_import_directive); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1198 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1199 | return HandleIncludeDirective(ImportTok, 0, true); |
| 1200 | } |
| 1201 | |
Chris Lattner | de07665 | 2009-04-08 18:46:40 +0000 | [diff] [blame] | 1202 | /// HandleIncludeMacrosDirective - The -imacros command line option turns into a |
| 1203 | /// pseudo directive in the predefines buffer. This handles it by sucking all |
| 1204 | /// tokens through the preprocessor and discarding them (only keeping the side |
| 1205 | /// effects on the preprocessor). |
| 1206 | void Preprocessor::HandleIncludeMacrosDirective(Token &IncludeMacrosTok) { |
| 1207 | // This directive should only occur in the predefines buffer. If not, emit an |
| 1208 | // error and reject it. |
| 1209 | SourceLocation Loc = IncludeMacrosTok.getLocation(); |
| 1210 | if (strcmp(SourceMgr.getBufferName(Loc), "<built-in>") != 0) { |
| 1211 | Diag(IncludeMacrosTok.getLocation(), |
| 1212 | diag::pp_include_macros_out_of_predefines); |
| 1213 | DiscardUntilEndOfDirective(); |
| 1214 | return; |
| 1215 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1216 | |
Chris Lattner | fd10511 | 2009-04-08 20:53:24 +0000 | [diff] [blame] | 1217 | // Treat this as a normal #include for checking purposes. If this is |
| 1218 | // successful, it will push a new lexer onto the include stack. |
| 1219 | HandleIncludeDirective(IncludeMacrosTok, 0, false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1220 | |
Chris Lattner | fd10511 | 2009-04-08 20:53:24 +0000 | [diff] [blame] | 1221 | Token TmpTok; |
| 1222 | do { |
| 1223 | Lex(TmpTok); |
| 1224 | assert(TmpTok.isNot(tok::eof) && "Didn't find end of -imacros!"); |
| 1225 | } while (TmpTok.isNot(tok::hashhash)); |
Chris Lattner | de07665 | 2009-04-08 18:46:40 +0000 | [diff] [blame] | 1226 | } |
| 1227 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1228 | //===----------------------------------------------------------------------===// |
| 1229 | // Preprocessor Macro Directive Handling. |
| 1230 | //===----------------------------------------------------------------------===// |
| 1231 | |
| 1232 | /// ReadMacroDefinitionArgList - The ( starting an argument list of a macro |
| 1233 | /// definition has just been read. Lex the rest of the arguments and the |
| 1234 | /// closing ), updating MI with what we learn. Return true if an error occurs |
| 1235 | /// parsing the arg list. |
| 1236 | bool Preprocessor::ReadMacroDefinitionArgList(MacroInfo *MI) { |
| 1237 | llvm::SmallVector<IdentifierInfo*, 32> Arguments; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1238 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1239 | Token Tok; |
| 1240 | while (1) { |
| 1241 | LexUnexpandedToken(Tok); |
| 1242 | switch (Tok.getKind()) { |
| 1243 | case tok::r_paren: |
| 1244 | // Found the end of the argument list. |
Chris Lattner | cf29e07 | 2009-02-20 22:31:31 +0000 | [diff] [blame] | 1245 | if (Arguments.empty()) // #define FOO() |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1246 | return false; |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1247 | // Otherwise we have #define FOO(A,) |
| 1248 | Diag(Tok, diag::err_pp_expected_ident_in_arg_list); |
| 1249 | return true; |
| 1250 | case tok::ellipsis: // #define X(... -> C99 varargs |
| 1251 | // Warn if use of C99 feature in non-C99 mode. |
| 1252 | if (!Features.C99) Diag(Tok, diag::ext_variadic_macro); |
| 1253 | |
| 1254 | // Lex the token after the identifier. |
| 1255 | LexUnexpandedToken(Tok); |
| 1256 | if (Tok.isNot(tok::r_paren)) { |
| 1257 | Diag(Tok, diag::err_pp_missing_rparen_in_macro_def); |
| 1258 | return true; |
| 1259 | } |
| 1260 | // Add the __VA_ARGS__ identifier as an argument. |
| 1261 | Arguments.push_back(Ident__VA_ARGS__); |
| 1262 | MI->setIsC99Varargs(); |
Chris Lattner | 685befe | 2009-02-20 22:46:43 +0000 | [diff] [blame] | 1263 | MI->setArgumentList(&Arguments[0], Arguments.size(), BP); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1264 | return false; |
| 1265 | case tok::eom: // #define X( |
| 1266 | Diag(Tok, diag::err_pp_missing_rparen_in_macro_def); |
| 1267 | return true; |
| 1268 | default: |
| 1269 | // Handle keywords and identifiers here to accept things like |
| 1270 | // #define Foo(for) for. |
| 1271 | IdentifierInfo *II = Tok.getIdentifierInfo(); |
| 1272 | if (II == 0) { |
| 1273 | // #define X(1 |
| 1274 | Diag(Tok, diag::err_pp_invalid_tok_in_arg_list); |
| 1275 | return true; |
| 1276 | } |
| 1277 | |
| 1278 | // If this is already used as an argument, it is used multiple times (e.g. |
| 1279 | // #define X(A,A. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1280 | if (std::find(Arguments.begin(), Arguments.end(), II) != |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1281 | Arguments.end()) { // C99 6.10.3p6 |
Chris Lattner | 6cf3ed7 | 2008-11-19 07:33:58 +0000 | [diff] [blame] | 1282 | Diag(Tok, diag::err_pp_duplicate_name_in_arg_list) << II; |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1283 | return true; |
| 1284 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1285 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1286 | // Add the argument to the macro info. |
| 1287 | Arguments.push_back(II); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1288 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1289 | // Lex the token after the identifier. |
| 1290 | LexUnexpandedToken(Tok); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1291 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1292 | switch (Tok.getKind()) { |
| 1293 | default: // #define X(A B |
| 1294 | Diag(Tok, diag::err_pp_expected_comma_in_arg_list); |
| 1295 | return true; |
| 1296 | case tok::r_paren: // #define X(A) |
Chris Lattner | 685befe | 2009-02-20 22:46:43 +0000 | [diff] [blame] | 1297 | MI->setArgumentList(&Arguments[0], Arguments.size(), BP); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1298 | return false; |
| 1299 | case tok::comma: // #define X(A, |
| 1300 | break; |
| 1301 | case tok::ellipsis: // #define X(A... -> GCC extension |
| 1302 | // Diagnose extension. |
| 1303 | Diag(Tok, diag::ext_named_variadic_macro); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1304 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1305 | // Lex the token after the identifier. |
| 1306 | LexUnexpandedToken(Tok); |
| 1307 | if (Tok.isNot(tok::r_paren)) { |
| 1308 | Diag(Tok, diag::err_pp_missing_rparen_in_macro_def); |
| 1309 | return true; |
| 1310 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1311 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1312 | MI->setIsGNUVarargs(); |
Chris Lattner | 685befe | 2009-02-20 22:46:43 +0000 | [diff] [blame] | 1313 | MI->setArgumentList(&Arguments[0], Arguments.size(), BP); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1314 | return false; |
| 1315 | } |
| 1316 | } |
| 1317 | } |
| 1318 | } |
| 1319 | |
| 1320 | /// HandleDefineDirective - Implements #define. This consumes the entire macro |
| 1321 | /// line then lets the caller lex the next real token. |
| 1322 | void Preprocessor::HandleDefineDirective(Token &DefineTok) { |
| 1323 | ++NumDefined; |
| 1324 | |
| 1325 | Token MacroNameTok; |
| 1326 | ReadMacroName(MacroNameTok, 1); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1327 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1328 | // Error reading macro name? If so, diagnostic already issued. |
| 1329 | if (MacroNameTok.is(tok::eom)) |
| 1330 | return; |
| 1331 | |
Chris Lattner | 2451b52 | 2009-04-21 04:46:33 +0000 | [diff] [blame] | 1332 | Token LastTok = MacroNameTok; |
| 1333 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1334 | // If we are supposed to keep comments in #defines, reenable comment saving |
| 1335 | // mode. |
Ted Kremenek | ac6b06d | 2008-11-18 00:43:07 +0000 | [diff] [blame] | 1336 | if (CurLexer) CurLexer->SetCommentRetentionState(KeepMacroComments); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1337 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1338 | // Create the new macro. |
Ted Kremenek | 0ea7672 | 2008-12-15 19:56:42 +0000 | [diff] [blame] | 1339 | MacroInfo *MI = AllocateMacroInfo(MacroNameTok.getLocation()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1340 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1341 | Token Tok; |
| 1342 | LexUnexpandedToken(Tok); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1343 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1344 | // If this is a function-like macro definition, parse the argument list, |
| 1345 | // marking each of the identifiers as being used as macro arguments. Also, |
| 1346 | // check other constraints on the first token of the macro body. |
| 1347 | if (Tok.is(tok::eom)) { |
| 1348 | // If there is no body to this macro, we have no special handling here. |
Chris Lattner | 6272bcf | 2009-04-18 02:23:25 +0000 | [diff] [blame] | 1349 | } else if (Tok.hasLeadingSpace()) { |
| 1350 | // This is a normal token with leading space. Clear the leading space |
| 1351 | // marker on the first token to get proper expansion. |
| 1352 | Tok.clearFlag(Token::LeadingSpace); |
| 1353 | } else if (Tok.is(tok::l_paren)) { |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1354 | // This is a function-like macro definition. Read the argument list. |
| 1355 | MI->setIsFunctionLike(); |
| 1356 | if (ReadMacroDefinitionArgList(MI)) { |
| 1357 | // Forget about MI. |
Ted Kremenek | 0ea7672 | 2008-12-15 19:56:42 +0000 | [diff] [blame] | 1358 | ReleaseMacroInfo(MI); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1359 | // Throw away the rest of the line. |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 1360 | if (CurPPLexer->ParsingPreprocessorDirective) |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1361 | DiscardUntilEndOfDirective(); |
| 1362 | return; |
| 1363 | } |
| 1364 | |
Chris Lattner | 8fde597 | 2009-04-19 18:26:34 +0000 | [diff] [blame] | 1365 | // If this is a definition of a variadic C99 function-like macro, not using |
| 1366 | // the GNU named varargs extension, enabled __VA_ARGS__. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1367 | |
Chris Lattner | 8fde597 | 2009-04-19 18:26:34 +0000 | [diff] [blame] | 1368 | // "Poison" __VA_ARGS__, which can only appear in the expansion of a macro. |
| 1369 | // This gets unpoisoned where it is allowed. |
| 1370 | assert(Ident__VA_ARGS__->isPoisoned() && "__VA_ARGS__ should be poisoned!"); |
| 1371 | if (MI->isC99Varargs()) |
| 1372 | Ident__VA_ARGS__->setIsPoisoned(false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1373 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1374 | // Read the first token after the arg list for down below. |
| 1375 | LexUnexpandedToken(Tok); |
Chris Lattner | 6272bcf | 2009-04-18 02:23:25 +0000 | [diff] [blame] | 1376 | } else if (Features.C99) { |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1377 | // C99 requires whitespace between the macro definition and the body. Emit |
| 1378 | // a diagnostic for something like "#define X+". |
Chris Lattner | 6272bcf | 2009-04-18 02:23:25 +0000 | [diff] [blame] | 1379 | Diag(Tok, diag::ext_c99_whitespace_required_after_macro_name); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1380 | } else { |
Chris Lattner | 6272bcf | 2009-04-18 02:23:25 +0000 | [diff] [blame] | 1381 | // C90 6.8 TC1 says: "In the definition of an object-like macro, if the |
| 1382 | // first character of a replacement list is not a character required by |
| 1383 | // subclause 5.2.1, then there shall be white-space separation between the |
| 1384 | // identifier and the replacement list.". 5.2.1 lists this set: |
| 1385 | // "A-Za-z0-9!"#%&'()*+,_./:;<=>?[\]^_{|}~" as well as whitespace, which |
| 1386 | // is irrelevant here. |
| 1387 | bool isInvalid = false; |
| 1388 | if (Tok.is(tok::at)) // @ is not in the list above. |
| 1389 | isInvalid = true; |
| 1390 | else if (Tok.is(tok::unknown)) { |
| 1391 | // If we have an unknown token, it is something strange like "`". Since |
| 1392 | // all of valid characters would have lexed into a single character |
| 1393 | // token of some sort, we know this is not a valid case. |
| 1394 | isInvalid = true; |
| 1395 | } |
| 1396 | if (isInvalid) |
| 1397 | Diag(Tok, diag::ext_missing_whitespace_after_macro_name); |
| 1398 | else |
| 1399 | Diag(Tok, diag::warn_missing_whitespace_after_macro_name); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1400 | } |
Chris Lattner | 2451b52 | 2009-04-21 04:46:33 +0000 | [diff] [blame] | 1401 | |
| 1402 | if (!Tok.is(tok::eom)) |
| 1403 | LastTok = Tok; |
| 1404 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1405 | // Read the rest of the macro body. |
| 1406 | if (MI->isObjectLike()) { |
| 1407 | // Object-like macros are very simple, just read their body. |
| 1408 | while (Tok.isNot(tok::eom)) { |
Chris Lattner | 2451b52 | 2009-04-21 04:46:33 +0000 | [diff] [blame] | 1409 | LastTok = Tok; |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1410 | MI->AddTokenToBody(Tok); |
| 1411 | // Get the next token of the macro. |
| 1412 | LexUnexpandedToken(Tok); |
| 1413 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1414 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1415 | } else { |
Chris Lattner | 3240469 | 2009-05-25 17:16:10 +0000 | [diff] [blame] | 1416 | // Otherwise, read the body of a function-like macro. While we are at it, |
| 1417 | // check C99 6.10.3.2p1: ensure that # operators are followed by macro |
| 1418 | // parameters in function-like macro expansions. |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1419 | while (Tok.isNot(tok::eom)) { |
Chris Lattner | 2451b52 | 2009-04-21 04:46:33 +0000 | [diff] [blame] | 1420 | LastTok = Tok; |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1421 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1422 | if (Tok.isNot(tok::hash)) { |
Chris Lattner | 3240469 | 2009-05-25 17:16:10 +0000 | [diff] [blame] | 1423 | MI->AddTokenToBody(Tok); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1424 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1425 | // Get the next token of the macro. |
| 1426 | LexUnexpandedToken(Tok); |
| 1427 | continue; |
| 1428 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1429 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1430 | // Get the next token of the macro. |
| 1431 | LexUnexpandedToken(Tok); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1432 | |
Chris Lattner | 3240469 | 2009-05-25 17:16:10 +0000 | [diff] [blame] | 1433 | // Check for a valid macro arg identifier. |
| 1434 | if (Tok.getIdentifierInfo() == 0 || |
| 1435 | MI->getArgumentNum(Tok.getIdentifierInfo()) == -1) { |
| 1436 | |
| 1437 | // If this is assembler-with-cpp mode, we accept random gibberish after |
| 1438 | // the '#' because '#' is often a comment character. However, change |
| 1439 | // the kind of the token to tok::unknown so that the preprocessor isn't |
| 1440 | // confused. |
| 1441 | if (getLangOptions().AsmPreprocessor && Tok.isNot(tok::eom)) { |
| 1442 | LastTok.setKind(tok::unknown); |
| 1443 | } else { |
| 1444 | Diag(Tok, diag::err_pp_stringize_not_parameter); |
| 1445 | ReleaseMacroInfo(MI); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1446 | |
Chris Lattner | 3240469 | 2009-05-25 17:16:10 +0000 | [diff] [blame] | 1447 | // Disable __VA_ARGS__ again. |
| 1448 | Ident__VA_ARGS__->setIsPoisoned(true); |
| 1449 | return; |
| 1450 | } |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1451 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1452 | |
Chris Lattner | 3240469 | 2009-05-25 17:16:10 +0000 | [diff] [blame] | 1453 | // Things look ok, add the '#' and param name tokens to the macro. |
| 1454 | MI->AddTokenToBody(LastTok); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1455 | MI->AddTokenToBody(Tok); |
Chris Lattner | 3240469 | 2009-05-25 17:16:10 +0000 | [diff] [blame] | 1456 | LastTok = Tok; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1457 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1458 | // Get the next token of the macro. |
| 1459 | LexUnexpandedToken(Tok); |
| 1460 | } |
| 1461 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1462 | |
| 1463 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1464 | // Disable __VA_ARGS__ again. |
| 1465 | Ident__VA_ARGS__->setIsPoisoned(true); |
| 1466 | |
| 1467 | // Check that there is no paste (##) operator at the begining or end of the |
| 1468 | // replacement list. |
| 1469 | unsigned NumTokens = MI->getNumTokens(); |
| 1470 | if (NumTokens != 0) { |
| 1471 | if (MI->getReplacementToken(0).is(tok::hashhash)) { |
| 1472 | Diag(MI->getReplacementToken(0), diag::err_paste_at_start); |
Ted Kremenek | 0ea7672 | 2008-12-15 19:56:42 +0000 | [diff] [blame] | 1473 | ReleaseMacroInfo(MI); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1474 | return; |
| 1475 | } |
| 1476 | if (MI->getReplacementToken(NumTokens-1).is(tok::hashhash)) { |
| 1477 | Diag(MI->getReplacementToken(NumTokens-1), diag::err_paste_at_end); |
Ted Kremenek | 0ea7672 | 2008-12-15 19:56:42 +0000 | [diff] [blame] | 1478 | ReleaseMacroInfo(MI); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1479 | return; |
| 1480 | } |
| 1481 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1482 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1483 | // If this is the primary source file, remember that this macro hasn't been |
| 1484 | // used yet. |
| 1485 | if (isInPrimaryFile()) |
| 1486 | MI->setIsUsed(false); |
Chris Lattner | 2451b52 | 2009-04-21 04:46:33 +0000 | [diff] [blame] | 1487 | |
| 1488 | MI->setDefinitionEndLoc(LastTok.getLocation()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1489 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1490 | // Finally, if this identifier already had a macro defined for it, verify that |
| 1491 | // the macro bodies are identical and free the old definition. |
| 1492 | if (MacroInfo *OtherMI = getMacroInfo(MacroNameTok.getIdentifierInfo())) { |
Chris Lattner | 41c3ae1 | 2009-01-16 19:50:11 +0000 | [diff] [blame] | 1493 | // It is very common for system headers to have tons of macro redefinitions |
| 1494 | // and for warnings to be disabled in system headers. If this is the case, |
| 1495 | // then don't bother calling MacroInfo::isIdenticalTo. |
Chris Lattner | 7f549df | 2009-03-13 21:17:23 +0000 | [diff] [blame] | 1496 | if (!getDiagnostics().getSuppressSystemWarnings() || |
Chris Lattner | 41c3ae1 | 2009-01-16 19:50:11 +0000 | [diff] [blame] | 1497 | !SourceMgr.isInSystemHeader(DefineTok.getLocation())) { |
| 1498 | if (!OtherMI->isUsed()) |
| 1499 | Diag(OtherMI->getDefinitionLoc(), diag::pp_macro_not_used); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1500 | |
Chris Lattner | f47724b | 2010-08-17 15:55:45 +0000 | [diff] [blame] | 1501 | // Macros must be identical. This means all tokens and whitespace |
Chris Lattner | 41c3ae1 | 2009-01-16 19:50:11 +0000 | [diff] [blame] | 1502 | // separation must be the same. C99 6.10.3.2. |
Chris Lattner | f47724b | 2010-08-17 15:55:45 +0000 | [diff] [blame] | 1503 | if (!OtherMI->isAllowRedefinitionsWithoutWarning() && |
Eli Friedman | a7e6845 | 2010-08-22 01:00:03 +0000 | [diff] [blame] | 1504 | !MI->isIdenticalTo(*OtherMI, *this)) { |
Chris Lattner | 41c3ae1 | 2009-01-16 19:50:11 +0000 | [diff] [blame] | 1505 | Diag(MI->getDefinitionLoc(), diag::ext_pp_macro_redef) |
| 1506 | << MacroNameTok.getIdentifierInfo(); |
| 1507 | Diag(OtherMI->getDefinitionLoc(), diag::note_previous_definition); |
| 1508 | } |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1509 | } |
Ted Kremenek | 0ea7672 | 2008-12-15 19:56:42 +0000 | [diff] [blame] | 1510 | ReleaseMacroInfo(OtherMI); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1511 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1512 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1513 | setMacroInfo(MacroNameTok.getIdentifierInfo(), MI); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1514 | |
Chris Lattner | f4a72b0 | 2009-04-12 01:39:54 +0000 | [diff] [blame] | 1515 | // If the callbacks want to know, tell them about the macro definition. |
| 1516 | if (Callbacks) |
| 1517 | Callbacks->MacroDefined(MacroNameTok.getIdentifierInfo(), MI); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1518 | } |
| 1519 | |
| 1520 | /// HandleUndefDirective - Implements #undef. |
| 1521 | /// |
| 1522 | void Preprocessor::HandleUndefDirective(Token &UndefTok) { |
| 1523 | ++NumUndefined; |
| 1524 | |
| 1525 | Token MacroNameTok; |
| 1526 | ReadMacroName(MacroNameTok, 2); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1527 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1528 | // Error reading macro name? If so, diagnostic already issued. |
| 1529 | if (MacroNameTok.is(tok::eom)) |
| 1530 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1531 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1532 | // Check to see if this is the last token on the #undef line. |
Chris Lattner | 35410d5 | 2009-04-14 05:07:49 +0000 | [diff] [blame] | 1533 | CheckEndOfDirective("undef"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1534 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1535 | // Okay, we finally have a valid identifier to undef. |
| 1536 | MacroInfo *MI = getMacroInfo(MacroNameTok.getIdentifierInfo()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1537 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1538 | // If the macro is not defined, this is a noop undef, just return. |
| 1539 | if (MI == 0) return; |
| 1540 | |
| 1541 | if (!MI->isUsed()) |
| 1542 | Diag(MI->getDefinitionLoc(), diag::pp_macro_not_used); |
Chris Lattner | 41c1747 | 2009-04-21 03:42:09 +0000 | [diff] [blame] | 1543 | |
| 1544 | // If the callbacks want to know, tell them about the macro #undef. |
| 1545 | if (Callbacks) |
Benjamin Kramer | 2f05449 | 2010-08-07 22:27:00 +0000 | [diff] [blame] | 1546 | Callbacks->MacroUndefined(MacroNameTok.getLocation(), |
| 1547 | MacroNameTok.getIdentifierInfo(), MI); |
Chris Lattner | 41c1747 | 2009-04-21 03:42:09 +0000 | [diff] [blame] | 1548 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1549 | // Free macro definition. |
Ted Kremenek | 0ea7672 | 2008-12-15 19:56:42 +0000 | [diff] [blame] | 1550 | ReleaseMacroInfo(MI); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1551 | setMacroInfo(MacroNameTok.getIdentifierInfo(), 0); |
| 1552 | } |
| 1553 | |
| 1554 | |
| 1555 | //===----------------------------------------------------------------------===// |
| 1556 | // Preprocessor Conditional Directive Handling. |
| 1557 | //===----------------------------------------------------------------------===// |
| 1558 | |
| 1559 | /// HandleIfdefDirective - Implements the #ifdef/#ifndef directive. isIfndef is |
| 1560 | /// true when this is a #ifndef directive. ReadAnyTokensBeforeDirective is true |
| 1561 | /// if any tokens have been returned or pp-directives activated before this |
| 1562 | /// #ifndef has been lexed. |
| 1563 | /// |
| 1564 | void Preprocessor::HandleIfdefDirective(Token &Result, bool isIfndef, |
| 1565 | bool ReadAnyTokensBeforeDirective) { |
| 1566 | ++NumIf; |
| 1567 | Token DirectiveTok = Result; |
| 1568 | |
| 1569 | Token MacroNameTok; |
| 1570 | ReadMacroName(MacroNameTok); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1571 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1572 | // Error reading macro name? If so, diagnostic already issued. |
| 1573 | if (MacroNameTok.is(tok::eom)) { |
| 1574 | // Skip code until we get to #endif. This helps with recovery by not |
| 1575 | // emitting an error when the #endif is reached. |
| 1576 | SkipExcludedConditionalBlock(DirectiveTok.getLocation(), |
| 1577 | /*Foundnonskip*/false, /*FoundElse*/false); |
| 1578 | return; |
| 1579 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1580 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1581 | // Check to see if this is the last token on the #if[n]def line. |
Chris Lattner | 35410d5 | 2009-04-14 05:07:49 +0000 | [diff] [blame] | 1582 | CheckEndOfDirective(isIfndef ? "ifndef" : "ifdef"); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1583 | |
Chris Lattner | 13d283d | 2010-02-12 08:03:27 +0000 | [diff] [blame] | 1584 | IdentifierInfo *MII = MacroNameTok.getIdentifierInfo(); |
| 1585 | MacroInfo *MI = getMacroInfo(MII); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1586 | |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 1587 | if (CurPPLexer->getConditionalStackDepth() == 0) { |
Chris Lattner | 13d283d | 2010-02-12 08:03:27 +0000 | [diff] [blame] | 1588 | // If the start of a top-level #ifdef and if the macro is not defined, |
| 1589 | // inform MIOpt that this might be the start of a proper include guard. |
| 1590 | // Otherwise it is some other form of unknown conditional which we can't |
| 1591 | // handle. |
| 1592 | if (!ReadAnyTokensBeforeDirective && MI == 0) { |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1593 | assert(isIfndef && "#ifdef shouldn't reach here"); |
Chris Lattner | 13d283d | 2010-02-12 08:03:27 +0000 | [diff] [blame] | 1594 | CurPPLexer->MIOpt.EnterTopLevelIFNDEF(MII); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1595 | } else |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 1596 | CurPPLexer->MIOpt.EnterTopLevelConditional(); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1597 | } |
| 1598 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1599 | // If there is a macro, process it. |
| 1600 | if (MI) // Mark it used. |
| 1601 | MI->setIsUsed(true); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1602 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1603 | // Should we include the stuff contained by this directive? |
| 1604 | if (!MI == isIfndef) { |
| 1605 | // Yes, remember that we are inside a conditional, then lex the next token. |
Chris Lattner | 1d9c54d | 2009-12-14 04:54:40 +0000 | [diff] [blame] | 1606 | CurPPLexer->pushConditionalLevel(DirectiveTok.getLocation(), |
| 1607 | /*wasskip*/false, /*foundnonskip*/true, |
| 1608 | /*foundelse*/false); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1609 | } else { |
| 1610 | // No, skip the contents of this block and return the first token after it. |
| 1611 | SkipExcludedConditionalBlock(DirectiveTok.getLocation(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1612 | /*Foundnonskip*/false, |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1613 | /*FoundElse*/false); |
| 1614 | } |
| 1615 | } |
| 1616 | |
| 1617 | /// HandleIfDirective - Implements the #if directive. |
| 1618 | /// |
| 1619 | void Preprocessor::HandleIfDirective(Token &IfToken, |
| 1620 | bool ReadAnyTokensBeforeDirective) { |
| 1621 | ++NumIf; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1622 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1623 | // Parse and evaluation the conditional expression. |
| 1624 | IdentifierInfo *IfNDefMacro = 0; |
| 1625 | bool ConditionalTrue = EvaluateDirectiveExpression(IfNDefMacro); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1626 | |
Nuno Lopes | 0049db6 | 2008-06-01 18:31:24 +0000 | [diff] [blame] | 1627 | |
| 1628 | // If this condition is equivalent to #ifndef X, and if this is the first |
| 1629 | // directive seen, handle it for the multiple-include optimization. |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 1630 | if (CurPPLexer->getConditionalStackDepth() == 0) { |
Chris Lattner | 13d283d | 2010-02-12 08:03:27 +0000 | [diff] [blame] | 1631 | if (!ReadAnyTokensBeforeDirective && IfNDefMacro && ConditionalTrue) |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 1632 | CurPPLexer->MIOpt.EnterTopLevelIFNDEF(IfNDefMacro); |
Nuno Lopes | 0049db6 | 2008-06-01 18:31:24 +0000 | [diff] [blame] | 1633 | else |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 1634 | CurPPLexer->MIOpt.EnterTopLevelConditional(); |
Nuno Lopes | 0049db6 | 2008-06-01 18:31:24 +0000 | [diff] [blame] | 1635 | } |
| 1636 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1637 | // Should we include the stuff contained by this directive? |
| 1638 | if (ConditionalTrue) { |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1639 | // Yes, remember that we are inside a conditional, then lex the next token. |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 1640 | CurPPLexer->pushConditionalLevel(IfToken.getLocation(), /*wasskip*/false, |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1641 | /*foundnonskip*/true, /*foundelse*/false); |
| 1642 | } else { |
| 1643 | // No, skip the contents of this block and return the first token after it. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1644 | SkipExcludedConditionalBlock(IfToken.getLocation(), /*Foundnonskip*/false, |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1645 | /*FoundElse*/false); |
| 1646 | } |
| 1647 | } |
| 1648 | |
| 1649 | /// HandleEndifDirective - Implements the #endif directive. |
| 1650 | /// |
| 1651 | void Preprocessor::HandleEndifDirective(Token &EndifToken) { |
| 1652 | ++NumEndif; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1653 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1654 | // Check that this is the whole directive. |
Chris Lattner | 35410d5 | 2009-04-14 05:07:49 +0000 | [diff] [blame] | 1655 | CheckEndOfDirective("endif"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1656 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1657 | PPConditionalInfo CondInfo; |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 1658 | if (CurPPLexer->popConditionalLevel(CondInfo)) { |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1659 | // No conditionals on the stack: this is an #endif without an #if. |
Chris Lattner | 3692b09 | 2008-11-18 07:59:24 +0000 | [diff] [blame] | 1660 | Diag(EndifToken, diag::err_pp_endif_without_if); |
| 1661 | return; |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1662 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1663 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1664 | // If this the end of a top-level #endif, inform MIOpt. |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 1665 | if (CurPPLexer->getConditionalStackDepth() == 0) |
| 1666 | CurPPLexer->MIOpt.ExitTopLevelConditional(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1667 | |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 1668 | assert(!CondInfo.WasSkipping && !CurPPLexer->LexingRawMode && |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1669 | "This code should only be reachable in the non-skipping case!"); |
| 1670 | } |
| 1671 | |
| 1672 | |
| 1673 | void Preprocessor::HandleElseDirective(Token &Result) { |
| 1674 | ++NumElse; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1675 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1676 | // #else directive in a non-skipping conditional... start skipping. |
Chris Lattner | 35410d5 | 2009-04-14 05:07:49 +0000 | [diff] [blame] | 1677 | CheckEndOfDirective("else"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1678 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1679 | PPConditionalInfo CI; |
Chris Lattner | 3692b09 | 2008-11-18 07:59:24 +0000 | [diff] [blame] | 1680 | if (CurPPLexer->popConditionalLevel(CI)) { |
| 1681 | Diag(Result, diag::pp_err_else_without_if); |
| 1682 | return; |
| 1683 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1684 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1685 | // If this is a top-level #else, inform the MIOpt. |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 1686 | if (CurPPLexer->getConditionalStackDepth() == 0) |
| 1687 | CurPPLexer->MIOpt.EnterTopLevelConditional(); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1688 | |
| 1689 | // If this is a #else with a #else before it, report the error. |
| 1690 | if (CI.FoundElse) Diag(Result, diag::pp_err_else_after_else); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1691 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1692 | // Finally, skip the rest of the contents of this block and return the first |
| 1693 | // token after it. |
| 1694 | return SkipExcludedConditionalBlock(CI.IfLoc, /*Foundnonskip*/true, |
| 1695 | /*FoundElse*/true); |
| 1696 | } |
| 1697 | |
| 1698 | void Preprocessor::HandleElifDirective(Token &ElifToken) { |
| 1699 | ++NumElse; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1700 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1701 | // #elif directive in a non-skipping conditional... start skipping. |
| 1702 | // We don't care what the condition is, because we will always skip it (since |
| 1703 | // the block immediately before it was included). |
| 1704 | DiscardUntilEndOfDirective(); |
| 1705 | |
| 1706 | PPConditionalInfo CI; |
Chris Lattner | 3692b09 | 2008-11-18 07:59:24 +0000 | [diff] [blame] | 1707 | if (CurPPLexer->popConditionalLevel(CI)) { |
| 1708 | Diag(ElifToken, diag::pp_err_elif_without_if); |
| 1709 | return; |
| 1710 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1711 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1712 | // If this is a top-level #elif, inform the MIOpt. |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 1713 | if (CurPPLexer->getConditionalStackDepth() == 0) |
| 1714 | CurPPLexer->MIOpt.EnterTopLevelConditional(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1715 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1716 | // If this is a #elif with a #else before it, report the error. |
| 1717 | if (CI.FoundElse) Diag(ElifToken, diag::pp_err_elif_after_else); |
| 1718 | |
| 1719 | // Finally, skip the rest of the contents of this block and return the first |
| 1720 | // token after it. |
| 1721 | return SkipExcludedConditionalBlock(CI.IfLoc, /*Foundnonskip*/true, |
| 1722 | /*FoundElse*/CI.FoundElse); |
| 1723 | } |