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 | //===----------------------------------------------------------------------===// |
James Dennett | dc20169 | 2012-06-22 05:46:07 +0000 | [diff] [blame] | 9 | /// |
| 10 | /// \file |
| 11 | /// \brief Implements # directive processing for the Preprocessor. |
| 12 | /// |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "clang/Lex/Preprocessor.h" |
Chris Lattner | 6e29014 | 2009-11-30 04:18:44 +0000 | [diff] [blame] | 16 | #include "clang/Basic/FileManager.h" |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 17 | #include "clang/Basic/SourceManager.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 18 | #include "clang/Lex/CodeCompletionHandler.h" |
| 19 | #include "clang/Lex/HeaderSearch.h" |
| 20 | #include "clang/Lex/LexDiagnostic.h" |
| 21 | #include "clang/Lex/LiteralSupport.h" |
| 22 | #include "clang/Lex/MacroInfo.h" |
| 23 | #include "clang/Lex/ModuleLoader.h" |
| 24 | #include "clang/Lex/Pragma.h" |
Chris Lattner | 359cc44 | 2009-01-26 05:29:08 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/APInt.h" |
Douglas Gregor | e3a8256 | 2011-11-30 18:02:36 +0000 | [diff] [blame] | 26 | #include "llvm/Support/ErrorHandling.h" |
Aaron Ballman | 31672b1 | 2013-01-16 19:32:21 +0000 | [diff] [blame] | 27 | #include "llvm/Support/SaveAndRestore.h" |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 28 | using namespace clang; |
| 29 | |
| 30 | //===----------------------------------------------------------------------===// |
| 31 | // Utility Methods for Preprocessor Directive Handling. |
| 32 | //===----------------------------------------------------------------------===// |
| 33 | |
Chris Lattner | f47724b | 2010-08-17 15:55:45 +0000 | [diff] [blame] | 34 | MacroInfo *Preprocessor::AllocateMacroInfo() { |
Ted Kremenek | 9714a23 | 2010-10-19 22:15:20 +0000 | [diff] [blame] | 35 | MacroInfoChain *MIChain; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 36 | |
Ted Kremenek | 9714a23 | 2010-10-19 22:15:20 +0000 | [diff] [blame] | 37 | if (MICache) { |
| 38 | MIChain = MICache; |
| 39 | MICache = MICache->Next; |
Ted Kremenek | af8fa25 | 2010-10-19 18:16:54 +0000 | [diff] [blame] | 40 | } |
Ted Kremenek | 9714a23 | 2010-10-19 22:15:20 +0000 | [diff] [blame] | 41 | else { |
| 42 | MIChain = BP.Allocate<MacroInfoChain>(); |
| 43 | } |
| 44 | |
| 45 | MIChain->Next = MIChainHead; |
| 46 | MIChain->Prev = 0; |
| 47 | if (MIChainHead) |
| 48 | MIChainHead->Prev = MIChain; |
| 49 | MIChainHead = MIChain; |
| 50 | |
| 51 | return &(MIChain->MI); |
Chris Lattner | f47724b | 2010-08-17 15:55:45 +0000 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | MacroInfo *Preprocessor::AllocateMacroInfo(SourceLocation L) { |
| 55 | MacroInfo *MI = AllocateMacroInfo(); |
Ted Kremenek | 0ea7672 | 2008-12-15 19:56:42 +0000 | [diff] [blame] | 56 | new (MI) MacroInfo(L); |
| 57 | return MI; |
| 58 | } |
| 59 | |
Argyrios Kyrtzidis | baa74bd | 2013-03-22 21:12:51 +0000 | [diff] [blame] | 60 | MacroInfo *Preprocessor::AllocateDeserializedMacroInfo(SourceLocation L, |
| 61 | unsigned SubModuleID) { |
| 62 | LLVM_STATIC_ASSERT(llvm::AlignOf<MacroInfo>::Alignment >= sizeof(SubModuleID), |
| 63 | "alignment for MacroInfo is less than the ID"); |
| 64 | MacroInfo *MI = |
| 65 | (MacroInfo*)BP.Allocate(sizeof(MacroInfo) + sizeof(SubModuleID), |
| 66 | llvm::AlignOf<MacroInfo>::Alignment); |
| 67 | new (MI) MacroInfo(L); |
| 68 | MI->FromASTFile = true; |
| 69 | MI->setOwningModuleID(SubModuleID); |
| 70 | return MI; |
| 71 | } |
| 72 | |
Argyrios Kyrtzidis | c56fff7 | 2013-03-26 17:17:01 +0000 | [diff] [blame] | 73 | DefMacroDirective * |
| 74 | Preprocessor::AllocateDefMacroDirective(MacroInfo *MI, SourceLocation Loc, |
| 75 | bool isImported) { |
| 76 | DefMacroDirective *MD = BP.Allocate<DefMacroDirective>(); |
| 77 | new (MD) DefMacroDirective(MI, Loc, isImported); |
| 78 | return MD; |
| 79 | } |
| 80 | |
| 81 | UndefMacroDirective * |
| 82 | Preprocessor::AllocateUndefMacroDirective(SourceLocation UndefLoc) { |
| 83 | UndefMacroDirective *MD = BP.Allocate<UndefMacroDirective>(); |
| 84 | new (MD) UndefMacroDirective(UndefLoc); |
| 85 | return MD; |
| 86 | } |
| 87 | |
| 88 | VisibilityMacroDirective * |
| 89 | Preprocessor::AllocateVisibilityMacroDirective(SourceLocation Loc, |
| 90 | bool isPublic) { |
| 91 | VisibilityMacroDirective *MD = BP.Allocate<VisibilityMacroDirective>(); |
| 92 | new (MD) VisibilityMacroDirective(Loc, isPublic); |
Argyrios Kyrtzidis | 9818a1d | 2013-02-20 00:54:57 +0000 | [diff] [blame] | 93 | return MD; |
Chris Lattner | f47724b | 2010-08-17 15:55:45 +0000 | [diff] [blame] | 94 | } |
| 95 | |
James Dennett | dc20169 | 2012-06-22 05:46:07 +0000 | [diff] [blame] | 96 | /// \brief Release the specified MacroInfo to be reused for allocating |
| 97 | /// new MacroInfo objects. |
Chris Lattner | 2c1ab90 | 2010-08-18 16:08:51 +0000 | [diff] [blame] | 98 | void Preprocessor::ReleaseMacroInfo(MacroInfo *MI) { |
Ted Kremenek | 9714a23 | 2010-10-19 22:15:20 +0000 | [diff] [blame] | 99 | MacroInfoChain *MIChain = (MacroInfoChain*) MI; |
| 100 | if (MacroInfoChain *Prev = MIChain->Prev) { |
| 101 | MacroInfoChain *Next = MIChain->Next; |
| 102 | Prev->Next = Next; |
| 103 | if (Next) |
| 104 | Next->Prev = Prev; |
| 105 | } |
| 106 | else { |
| 107 | assert(MIChainHead == MIChain); |
| 108 | MIChainHead = MIChain->Next; |
| 109 | MIChainHead->Prev = 0; |
| 110 | } |
| 111 | MIChain->Next = MICache; |
| 112 | MICache = MIChain; |
Chris Lattner | 0301b3f | 2009-02-20 22:19:20 +0000 | [diff] [blame] | 113 | |
Ted Kremenek | 9714a23 | 2010-10-19 22:15:20 +0000 | [diff] [blame] | 114 | MI->Destroy(); |
| 115 | } |
Chris Lattner | 0301b3f | 2009-02-20 22:19:20 +0000 | [diff] [blame] | 116 | |
James Dennett | dc20169 | 2012-06-22 05:46:07 +0000 | [diff] [blame] | 117 | /// \brief Read and discard all tokens remaining on the current line until |
| 118 | /// the tok::eod token is found. |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 119 | void Preprocessor::DiscardUntilEndOfDirective() { |
| 120 | Token Tmp; |
| 121 | do { |
| 122 | LexUnexpandedToken(Tmp); |
Peter Collingbourne | a5ef584 | 2011-02-22 13:49:06 +0000 | [diff] [blame] | 123 | assert(Tmp.isNot(tok::eof) && "EOF seen while discarding directive tokens"); |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 124 | } while (Tmp.isNot(tok::eod)); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 125 | } |
| 126 | |
James Dennett | dc20169 | 2012-06-22 05:46:07 +0000 | [diff] [blame] | 127 | /// \brief Lex and validate a macro name, which occurs after a |
| 128 | /// \#define or \#undef. |
| 129 | /// |
| 130 | /// This sets the token kind to eod and discards the rest |
| 131 | /// of the macro line if the macro name is invalid. \p isDefineUndef is 1 if |
| 132 | /// this is due to a a \#define, 2 if \#undef directive, 0 if it is something |
| 133 | /// else (e.g. \#ifdef). |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 134 | void Preprocessor::ReadMacroName(Token &MacroNameTok, char isDefineUndef) { |
| 135 | // Read the token, don't allow macro expansion on it. |
| 136 | LexUnexpandedToken(MacroNameTok); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 137 | |
Douglas Gregor | 1fbb447 | 2010-08-24 20:21:13 +0000 | [diff] [blame] | 138 | if (MacroNameTok.is(tok::code_completion)) { |
| 139 | if (CodeComplete) |
| 140 | CodeComplete->CodeCompleteMacroName(isDefineUndef == 1); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 141 | setCodeCompletionReached(); |
Douglas Gregor | 1fbb447 | 2010-08-24 20:21:13 +0000 | [diff] [blame] | 142 | LexUnexpandedToken(MacroNameTok); |
Douglas Gregor | 1fbb447 | 2010-08-24 20:21:13 +0000 | [diff] [blame] | 143 | } |
| 144 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 145 | // Missing macro name? |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 146 | if (MacroNameTok.is(tok::eod)) { |
Chris Lattner | 3692b09 | 2008-11-18 07:59:24 +0000 | [diff] [blame] | 147 | Diag(MacroNameTok, diag::err_pp_missing_macro_name); |
| 148 | return; |
| 149 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 150 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 151 | IdentifierInfo *II = MacroNameTok.getIdentifierInfo(); |
| 152 | if (II == 0) { |
Douglas Gregor | 453091c | 2010-03-16 22:30:13 +0000 | [diff] [blame] | 153 | bool Invalid = false; |
| 154 | std::string Spelling = getSpelling(MacroNameTok, &Invalid); |
| 155 | if (Invalid) |
| 156 | return; |
Nico Weber | f4fb07e | 2012-02-29 22:54:43 +0000 | [diff] [blame] | 157 | |
Chris Lattner | 9485d23 | 2008-12-13 20:12:40 +0000 | [diff] [blame] | 158 | const IdentifierInfo &Info = Identifiers.get(Spelling); |
Nico Weber | f4fb07e | 2012-02-29 22:54:43 +0000 | [diff] [blame] | 159 | |
| 160 | // Allow #defining |and| and friends in microsoft mode. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 161 | if (Info.isCPlusPlusOperatorKeyword() && getLangOpts().MicrosoftMode) { |
Nico Weber | f4fb07e | 2012-02-29 22:54:43 +0000 | [diff] [blame] | 162 | MacroNameTok.setIdentifierInfo(getIdentifierInfo(Spelling)); |
| 163 | return; |
| 164 | } |
| 165 | |
Chris Lattner | 9485d23 | 2008-12-13 20:12:40 +0000 | [diff] [blame] | 166 | if (Info.isCPlusPlusOperatorKeyword()) |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 167 | // C++ 2.5p2: Alternative tokens behave the same as its primary token |
| 168 | // except for their spellings. |
Chris Lattner | 56b05c8 | 2008-11-18 08:02:48 +0000 | [diff] [blame] | 169 | Diag(MacroNameTok, diag::err_pp_operator_used_as_macro_name) << Spelling; |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 170 | else |
| 171 | Diag(MacroNameTok, diag::err_pp_macro_not_identifier); |
| 172 | // Fall through on error. |
| 173 | } else if (isDefineUndef && II->getPPKeywordID() == tok::pp_defined) { |
Richard Smith | eed55e6 | 2013-03-06 00:46:00 +0000 | [diff] [blame] | 174 | // Error if defining "defined": C99 6.10.8/4, C++ [cpp.predefined]p4. |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 175 | Diag(MacroNameTok, diag::err_defined_macro_name); |
Richard Smith | eed55e6 | 2013-03-06 00:46:00 +0000 | [diff] [blame] | 176 | } else if (isDefineUndef == 2 && II->hasMacroDefinition() && |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 177 | getMacroInfo(II)->isBuiltinMacro()) { |
Richard Smith | eed55e6 | 2013-03-06 00:46:00 +0000 | [diff] [blame] | 178 | // Warn if undefining "__LINE__" and other builtins, per C99 6.10.8/4 |
| 179 | // and C++ [cpp.predefined]p4], but allow it as an extension. |
| 180 | Diag(MacroNameTok, diag::ext_pp_undef_builtin_macro); |
| 181 | return; |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 182 | } else { |
| 183 | // Okay, we got a good identifier node. Return it. |
| 184 | return; |
| 185 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 186 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 187 | // Invalid macro name, read and discard the rest of the line. Then set the |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 188 | // token kind to tok::eod. |
| 189 | MacroNameTok.setKind(tok::eod); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 190 | return DiscardUntilEndOfDirective(); |
| 191 | } |
| 192 | |
James Dennett | dc20169 | 2012-06-22 05:46:07 +0000 | [diff] [blame] | 193 | /// \brief Ensure that the next token is a tok::eod token. |
| 194 | /// |
| 195 | /// If not, emit a diagnostic and consume up until the eod. If EnableMacros is |
Chris Lattner | ab82f41 | 2009-04-17 23:30:53 +0000 | [diff] [blame] | 196 | /// true, then we consider macros that expand to zero tokens as being ok. |
| 197 | void Preprocessor::CheckEndOfDirective(const char *DirType, bool EnableMacros) { |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 198 | Token Tmp; |
Chris Lattner | ab82f41 | 2009-04-17 23:30:53 +0000 | [diff] [blame] | 199 | // Lex unexpanded tokens for most directives: macros might expand to zero |
| 200 | // tokens, causing us to miss diagnosing invalid lines. Some directives (like |
| 201 | // #line) allow empty macros. |
| 202 | if (EnableMacros) |
| 203 | Lex(Tmp); |
| 204 | else |
| 205 | LexUnexpandedToken(Tmp); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 206 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 207 | // There should be no tokens after the directive, but we allow them as an |
| 208 | // extension. |
| 209 | while (Tmp.is(tok::comment)) // Skip comments in -C mode. |
| 210 | LexUnexpandedToken(Tmp); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 211 | |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 212 | if (Tmp.isNot(tok::eod)) { |
Chris Lattner | 959875a | 2009-04-14 05:15:20 +0000 | [diff] [blame] | 213 | // Add a fixit in GNU/C99/C++ mode. Don't offer a fixit for strict-C89, |
Peter Collingbourne | b2eb53d | 2011-02-22 13:49:00 +0000 | [diff] [blame] | 214 | // or if this is a macro-style preprocessing directive, because it is more |
| 215 | // trouble than it is worth to insert /**/ and check that there is no /**/ |
| 216 | // in the range also. |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 217 | FixItHint Hint; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 218 | if ((LangOpts.GNUMode || LangOpts.C99 || LangOpts.CPlusPlus) && |
Peter Collingbourne | b2eb53d | 2011-02-22 13:49:00 +0000 | [diff] [blame] | 219 | !CurTokenLexer) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 220 | Hint = FixItHint::CreateInsertion(Tmp.getLocation(),"//"); |
| 221 | Diag(Tmp, diag::ext_pp_extra_tokens_at_eol) << DirType << Hint; |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 222 | DiscardUntilEndOfDirective(); |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | |
| 227 | |
James Dennett | dc20169 | 2012-06-22 05:46:07 +0000 | [diff] [blame] | 228 | /// SkipExcludedConditionalBlock - We just read a \#if or related directive and |
| 229 | /// decided that the subsequent tokens are in the \#if'd out portion of the |
| 230 | /// file. Lex the rest of the file, until we see an \#endif. If |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 231 | /// FoundNonSkipPortion is true, then we have already emitted code for part of |
James Dennett | dc20169 | 2012-06-22 05:46:07 +0000 | [diff] [blame] | 232 | /// this \#if directive, so \#else/\#elif blocks should never be entered. |
| 233 | /// If ElseOk is true, then \#else directives are ok, if not, then we have |
| 234 | /// already seen one so a \#else directive is a duplicate. When this returns, |
| 235 | /// the caller can lex the first valid token. |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 236 | void Preprocessor::SkipExcludedConditionalBlock(SourceLocation IfTokenLoc, |
| 237 | bool FoundNonSkipPortion, |
Argyrios Kyrtzidis | 6b4ff04 | 2011-09-27 17:32:05 +0000 | [diff] [blame] | 238 | bool FoundElse, |
| 239 | SourceLocation ElseLoc) { |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 240 | ++NumSkipped; |
Ted Kremenek | f6452c5 | 2008-11-18 01:04:47 +0000 | [diff] [blame] | 241 | assert(CurTokenLexer == 0 && CurPPLexer && "Lexing a macro, not a file?"); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 242 | |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 243 | CurPPLexer->pushConditionalLevel(IfTokenLoc, /*isSkipping*/false, |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 244 | FoundNonSkipPortion, FoundElse); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 245 | |
Ted Kremenek | 268ee70 | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 246 | if (CurPTHLexer) { |
| 247 | PTHSkipExcludedConditionalBlock(); |
| 248 | return; |
| 249 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 250 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 251 | // Enter raw mode to disable identifier lookup (and thus macro expansion), |
| 252 | // disabling warnings, etc. |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 253 | CurPPLexer->LexingRawMode = true; |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 254 | Token Tok; |
| 255 | while (1) { |
Chris Lattner | 2c6b193 | 2010-01-18 22:33:01 +0000 | [diff] [blame] | 256 | CurLexer->Lex(Tok); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 257 | |
Douglas Gregor | f44e854 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 258 | if (Tok.is(tok::code_completion)) { |
| 259 | if (CodeComplete) |
| 260 | CodeComplete->CodeCompleteInConditionalExclusion(); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 261 | setCodeCompletionReached(); |
Douglas Gregor | f44e854 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 262 | continue; |
| 263 | } |
| 264 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 265 | // If this is the end of the buffer, we have an error. |
| 266 | if (Tok.is(tok::eof)) { |
| 267 | // Emit errors for each unterminated conditional on the stack, including |
| 268 | // the current one. |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 269 | while (!CurPPLexer->ConditionalStack.empty()) { |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 270 | if (CurLexer->getFileLoc() != CodeCompletionFileLoc) |
Douglas Gregor | 2d474ba | 2010-08-12 17:04:55 +0000 | [diff] [blame] | 271 | Diag(CurPPLexer->ConditionalStack.back().IfLoc, |
| 272 | diag::err_pp_unterminated_conditional); |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 273 | CurPPLexer->ConditionalStack.pop_back(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 274 | } |
| 275 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 276 | // Just return and let the caller lex after this #include. |
| 277 | break; |
| 278 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 279 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 280 | // If this token is not a preprocessor directive, just skip it. |
| 281 | if (Tok.isNot(tok::hash) || !Tok.isAtStartOfLine()) |
| 282 | continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 283 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 284 | // We just parsed a # character at the start of a line, so we're in |
| 285 | // directive mode. Tell the lexer this so any newlines we see will be |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 286 | // converted into an EOD token (this terminates the macro). |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 287 | CurPPLexer->ParsingPreprocessorDirective = true; |
Jordan Rose | c7d1ca5 | 2013-02-22 00:32:00 +0000 | [diff] [blame] | 288 | if (CurLexer) CurLexer->SetKeepWhitespaceMode(false); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 289 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 290 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 291 | // Read the next token, the directive flavor. |
| 292 | LexUnexpandedToken(Tok); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 293 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 294 | // If this isn't an identifier directive (e.g. is "# 1\n" or "#\n", or |
| 295 | // something bogus), skip it. |
Abramo Bagnara | c4bf2b9 | 2010-12-22 08:23:18 +0000 | [diff] [blame] | 296 | if (Tok.isNot(tok::raw_identifier)) { |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 297 | CurPPLexer->ParsingPreprocessorDirective = false; |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 298 | // Restore comment saving mode. |
Jordan Rose | 6aad4a3 | 2013-02-21 18:53:19 +0000 | [diff] [blame] | 299 | if (CurLexer) CurLexer->resetExtendedTokenMode(); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 300 | continue; |
| 301 | } |
| 302 | |
| 303 | // If the first letter isn't i or e, it isn't intesting to us. We know that |
| 304 | // this is safe in the face of spelling differences, because there is no way |
| 305 | // to spell an i/e in a strange way that is another letter. Skipping this |
| 306 | // allows us to avoid looking up the identifier info for #define/#undef and |
| 307 | // other common directives. |
Abramo Bagnara | c4bf2b9 | 2010-12-22 08:23:18 +0000 | [diff] [blame] | 308 | const char *RawCharData = Tok.getRawIdentifierData(); |
| 309 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 310 | char FirstChar = RawCharData[0]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 311 | if (FirstChar >= 'a' && FirstChar <= 'z' && |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 312 | FirstChar != 'i' && FirstChar != 'e') { |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 313 | CurPPLexer->ParsingPreprocessorDirective = false; |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 314 | // Restore comment saving mode. |
Jordan Rose | 6aad4a3 | 2013-02-21 18:53:19 +0000 | [diff] [blame] | 315 | if (CurLexer) CurLexer->resetExtendedTokenMode(); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 316 | continue; |
| 317 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 318 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 319 | // Get the identifier name without trigraphs or embedded newlines. Note |
| 320 | // that we can't use Tok.getIdentifierInfo() because its lookup is disabled |
| 321 | // when skipping. |
Benjamin Kramer | b939a4e | 2009-12-31 13:32:38 +0000 | [diff] [blame] | 322 | char DirectiveBuf[20]; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 323 | StringRef Directive; |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 324 | if (!Tok.needsCleaning() && Tok.getLength() < 20) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 325 | Directive = StringRef(RawCharData, Tok.getLength()); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 326 | } else { |
| 327 | std::string DirectiveStr = getSpelling(Tok); |
Benjamin Kramer | b939a4e | 2009-12-31 13:32:38 +0000 | [diff] [blame] | 328 | unsigned IdLen = DirectiveStr.size(); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 329 | if (IdLen >= 20) { |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 330 | CurPPLexer->ParsingPreprocessorDirective = false; |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 331 | // Restore comment saving mode. |
Jordan Rose | 6aad4a3 | 2013-02-21 18:53:19 +0000 | [diff] [blame] | 332 | if (CurLexer) CurLexer->resetExtendedTokenMode(); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 333 | continue; |
| 334 | } |
Benjamin Kramer | b939a4e | 2009-12-31 13:32:38 +0000 | [diff] [blame] | 335 | memcpy(DirectiveBuf, &DirectiveStr[0], IdLen); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 336 | Directive = StringRef(DirectiveBuf, IdLen); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 337 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 338 | |
Benjamin Kramer | b939a4e | 2009-12-31 13:32:38 +0000 | [diff] [blame] | 339 | if (Directive.startswith("if")) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 340 | StringRef Sub = Directive.substr(2); |
Benjamin Kramer | b939a4e | 2009-12-31 13:32:38 +0000 | [diff] [blame] | 341 | if (Sub.empty() || // "if" |
| 342 | Sub == "def" || // "ifdef" |
| 343 | Sub == "ndef") { // "ifndef" |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 344 | // We know the entire #if/#ifdef/#ifndef block will be skipped, don't |
| 345 | // bother parsing the condition. |
| 346 | DiscardUntilEndOfDirective(); |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 347 | CurPPLexer->pushConditionalLevel(Tok.getLocation(), /*wasskipping*/true, |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 348 | /*foundnonskip*/false, |
Chandler Carruth | 3a1a874 | 2011-01-03 17:40:17 +0000 | [diff] [blame] | 349 | /*foundelse*/false); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 350 | } |
Benjamin Kramer | b939a4e | 2009-12-31 13:32:38 +0000 | [diff] [blame] | 351 | } else if (Directive[0] == 'e') { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 352 | StringRef Sub = Directive.substr(1); |
Benjamin Kramer | b939a4e | 2009-12-31 13:32:38 +0000 | [diff] [blame] | 353 | if (Sub == "ndif") { // "endif" |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 354 | PPConditionalInfo CondInfo; |
| 355 | CondInfo.WasSkipping = true; // Silence bogus warning. |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 356 | bool InCond = CurPPLexer->popConditionalLevel(CondInfo); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 357 | (void)InCond; // Silence warning in no-asserts mode. |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 358 | assert(!InCond && "Can't be skipping if not in a conditional!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 359 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 360 | // If we popped the outermost skipping block, we're done skipping! |
Argyrios Kyrtzidis | bb66066 | 2012-03-05 05:48:09 +0000 | [diff] [blame] | 361 | if (!CondInfo.WasSkipping) { |
Richard Smith | bc9e558 | 2012-06-24 23:56:26 +0000 | [diff] [blame] | 362 | // Restore the value of LexingRawMode so that trailing comments |
| 363 | // are handled correctly, if we've reached the outermost block. |
| 364 | CurPPLexer->LexingRawMode = false; |
Richard Smith | 986f317 | 2012-06-21 00:35:03 +0000 | [diff] [blame] | 365 | CheckEndOfDirective("endif"); |
Richard Smith | bc9e558 | 2012-06-24 23:56:26 +0000 | [diff] [blame] | 366 | CurPPLexer->LexingRawMode = true; |
Argyrios Kyrtzidis | bb66066 | 2012-03-05 05:48:09 +0000 | [diff] [blame] | 367 | if (Callbacks) |
| 368 | Callbacks->Endif(Tok.getLocation(), CondInfo.IfLoc); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 369 | break; |
Richard Smith | 986f317 | 2012-06-21 00:35:03 +0000 | [diff] [blame] | 370 | } else { |
| 371 | DiscardUntilEndOfDirective(); |
Argyrios Kyrtzidis | bb66066 | 2012-03-05 05:48:09 +0000 | [diff] [blame] | 372 | } |
Benjamin Kramer | b939a4e | 2009-12-31 13:32:38 +0000 | [diff] [blame] | 373 | } else if (Sub == "lse") { // "else". |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 374 | // #else directive in a skipping conditional. If not in some other |
| 375 | // skipping conditional, and if #else hasn't already been seen, enter it |
| 376 | // as a non-skipping conditional. |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 377 | PPConditionalInfo &CondInfo = CurPPLexer->peekConditionalLevel(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 378 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 379 | // If this is a #else with a #else before it, report the error. |
| 380 | if (CondInfo.FoundElse) Diag(Tok, diag::pp_err_else_after_else); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 381 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 382 | // Note that we've seen a #else in this conditional. |
| 383 | CondInfo.FoundElse = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 384 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 385 | // If the conditional is at the top level, and the #if block wasn't |
| 386 | // entered, enter the #else block now. |
| 387 | if (!CondInfo.WasSkipping && !CondInfo.FoundNonSkip) { |
| 388 | CondInfo.FoundNonSkip = true; |
Richard Smith | bc9e558 | 2012-06-24 23:56:26 +0000 | [diff] [blame] | 389 | // Restore the value of LexingRawMode so that trailing comments |
| 390 | // are handled correctly. |
| 391 | CurPPLexer->LexingRawMode = false; |
Argyrios Kyrtzidis | e26224e | 2011-05-21 04:26:04 +0000 | [diff] [blame] | 392 | CheckEndOfDirective("else"); |
Richard Smith | bc9e558 | 2012-06-24 23:56:26 +0000 | [diff] [blame] | 393 | CurPPLexer->LexingRawMode = true; |
Argyrios Kyrtzidis | bb66066 | 2012-03-05 05:48:09 +0000 | [diff] [blame] | 394 | if (Callbacks) |
| 395 | Callbacks->Else(Tok.getLocation(), CondInfo.IfLoc); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 396 | break; |
Argyrios Kyrtzidis | e26224e | 2011-05-21 04:26:04 +0000 | [diff] [blame] | 397 | } else { |
| 398 | DiscardUntilEndOfDirective(); // C99 6.10p4. |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 399 | } |
Benjamin Kramer | b939a4e | 2009-12-31 13:32:38 +0000 | [diff] [blame] | 400 | } else if (Sub == "lif") { // "elif". |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 401 | PPConditionalInfo &CondInfo = CurPPLexer->peekConditionalLevel(); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 402 | |
| 403 | bool ShouldEnter; |
Chandler Carruth | 3a1a874 | 2011-01-03 17:40:17 +0000 | [diff] [blame] | 404 | const SourceLocation ConditionalBegin = CurPPLexer->getSourceLocation(); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 405 | // If this is in a skipping block or if we're already handled this #if |
| 406 | // block, don't bother parsing the condition. |
| 407 | if (CondInfo.WasSkipping || CondInfo.FoundNonSkip) { |
| 408 | DiscardUntilEndOfDirective(); |
| 409 | ShouldEnter = false; |
| 410 | } else { |
| 411 | // Restore the value of LexingRawMode so that identifiers are |
| 412 | // looked up, etc, inside the #elif expression. |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 413 | assert(CurPPLexer->LexingRawMode && "We have to be skipping here!"); |
| 414 | CurPPLexer->LexingRawMode = false; |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 415 | IdentifierInfo *IfNDefMacro = 0; |
| 416 | ShouldEnter = EvaluateDirectiveExpression(IfNDefMacro); |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 417 | CurPPLexer->LexingRawMode = true; |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 418 | } |
Chandler Carruth | 3a1a874 | 2011-01-03 17:40:17 +0000 | [diff] [blame] | 419 | const SourceLocation ConditionalEnd = CurPPLexer->getSourceLocation(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 420 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 421 | // If this is a #elif with a #else before it, report the error. |
| 422 | if (CondInfo.FoundElse) Diag(Tok, diag::pp_err_elif_after_else); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 423 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 424 | // If this condition is true, enter it! |
| 425 | if (ShouldEnter) { |
| 426 | CondInfo.FoundNonSkip = true; |
Argyrios Kyrtzidis | bb66066 | 2012-03-05 05:48:09 +0000 | [diff] [blame] | 427 | if (Callbacks) |
| 428 | Callbacks->Elif(Tok.getLocation(), |
| 429 | SourceRange(ConditionalBegin, ConditionalEnd), |
| 430 | CondInfo.IfLoc); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 431 | break; |
| 432 | } |
| 433 | } |
| 434 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 435 | |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 436 | CurPPLexer->ParsingPreprocessorDirective = false; |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 437 | // Restore comment saving mode. |
Jordan Rose | 6aad4a3 | 2013-02-21 18:53:19 +0000 | [diff] [blame] | 438 | if (CurLexer) CurLexer->resetExtendedTokenMode(); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 439 | } |
| 440 | |
| 441 | // Finally, if we are out of the conditional (saw an #endif or ran off the end |
| 442 | // of the file, just stop skipping and return to lexing whatever came after |
| 443 | // the #if block. |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 444 | CurPPLexer->LexingRawMode = false; |
Argyrios Kyrtzidis | 6b4ff04 | 2011-09-27 17:32:05 +0000 | [diff] [blame] | 445 | |
| 446 | if (Callbacks) { |
| 447 | SourceLocation BeginLoc = ElseLoc.isValid() ? ElseLoc : IfTokenLoc; |
| 448 | Callbacks->SourceRangeSkipped(SourceRange(BeginLoc, Tok.getLocation())); |
| 449 | } |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 450 | } |
| 451 | |
Ted Kremenek | 268ee70 | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 452 | void Preprocessor::PTHSkipExcludedConditionalBlock() { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 453 | |
| 454 | while (1) { |
Ted Kremenek | 268ee70 | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 455 | assert(CurPTHLexer); |
| 456 | assert(CurPTHLexer->LexingRawMode == false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 457 | |
Ted Kremenek | 268ee70 | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 458 | // Skip to the next '#else', '#elif', or #endif. |
| 459 | if (CurPTHLexer->SkipBlock()) { |
| 460 | // We have reached an #endif. Both the '#' and 'endif' tokens |
| 461 | // have been consumed by the PTHLexer. Just pop off the condition level. |
| 462 | PPConditionalInfo CondInfo; |
| 463 | bool InCond = CurPTHLexer->popConditionalLevel(CondInfo); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 464 | (void)InCond; // Silence warning in no-asserts mode. |
Ted Kremenek | 268ee70 | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 465 | assert(!InCond && "Can't be skipping if not in a conditional!"); |
| 466 | break; |
| 467 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 468 | |
Ted Kremenek | 268ee70 | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 469 | // We have reached a '#else' or '#elif'. Lex the next token to get |
| 470 | // the directive flavor. |
| 471 | Token Tok; |
| 472 | LexUnexpandedToken(Tok); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 473 | |
Ted Kremenek | 268ee70 | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 474 | // We can actually look up the IdentifierInfo here since we aren't in |
| 475 | // raw mode. |
| 476 | tok::PPKeywordKind K = Tok.getIdentifierInfo()->getPPKeywordID(); |
| 477 | |
| 478 | if (K == tok::pp_else) { |
| 479 | // #else: Enter the else condition. We aren't in a nested condition |
| 480 | // since we skip those. We're always in the one matching the last |
| 481 | // blocked we skipped. |
| 482 | PPConditionalInfo &CondInfo = CurPTHLexer->peekConditionalLevel(); |
| 483 | // Note that we've seen a #else in this conditional. |
| 484 | CondInfo.FoundElse = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 485 | |
Ted Kremenek | 268ee70 | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 486 | // If the #if block wasn't entered then enter the #else block now. |
| 487 | if (!CondInfo.FoundNonSkip) { |
| 488 | CondInfo.FoundNonSkip = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 489 | |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 490 | // Scan until the eod token. |
Ted Kremenek | e5680f3 | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 491 | CurPTHLexer->ParsingPreprocessorDirective = true; |
Daniel Dunbar | 8533bd5 | 2009-04-13 17:57:49 +0000 | [diff] [blame] | 492 | DiscardUntilEndOfDirective(); |
Ted Kremenek | e5680f3 | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 493 | CurPTHLexer->ParsingPreprocessorDirective = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 494 | |
Ted Kremenek | 268ee70 | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 495 | break; |
| 496 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 497 | |
Ted Kremenek | 268ee70 | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 498 | // Otherwise skip this block. |
| 499 | continue; |
| 500 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 501 | |
Ted Kremenek | 268ee70 | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 502 | assert(K == tok::pp_elif); |
| 503 | PPConditionalInfo &CondInfo = CurPTHLexer->peekConditionalLevel(); |
| 504 | |
| 505 | // If this is a #elif with a #else before it, report the error. |
| 506 | if (CondInfo.FoundElse) |
| 507 | Diag(Tok, diag::pp_err_elif_after_else); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 508 | |
Ted Kremenek | 268ee70 | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 509 | // 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] | 510 | // block, don't bother parsing the condition. We just skip this block. |
Ted Kremenek | 268ee70 | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 511 | if (CondInfo.FoundNonSkip) |
| 512 | continue; |
| 513 | |
| 514 | // Evaluate the condition of the #elif. |
| 515 | IdentifierInfo *IfNDefMacro = 0; |
| 516 | CurPTHLexer->ParsingPreprocessorDirective = true; |
| 517 | bool ShouldEnter = EvaluateDirectiveExpression(IfNDefMacro); |
| 518 | CurPTHLexer->ParsingPreprocessorDirective = false; |
| 519 | |
| 520 | // If this condition is true, enter it! |
| 521 | if (ShouldEnter) { |
| 522 | CondInfo.FoundNonSkip = true; |
| 523 | break; |
| 524 | } |
| 525 | |
| 526 | // Otherwise, skip this block and go to the next one. |
| 527 | continue; |
| 528 | } |
| 529 | } |
| 530 | |
Chandler Carruth | b5142bb | 2011-03-16 18:34:36 +0000 | [diff] [blame] | 531 | const FileEntry *Preprocessor::LookupFile( |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 532 | StringRef Filename, |
Chandler Carruth | b5142bb | 2011-03-16 18:34:36 +0000 | [diff] [blame] | 533 | bool isAngled, |
| 534 | const DirectoryLookup *FromDir, |
| 535 | const DirectoryLookup *&CurDir, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 536 | SmallVectorImpl<char> *SearchPath, |
Douglas Gregor | fba18aa | 2011-09-15 22:00:41 +0000 | [diff] [blame] | 537 | SmallVectorImpl<char> *RelativePath, |
Douglas Gregor | 1a4761e | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 538 | Module **SuggestedModule, |
Douglas Gregor | 1c2e933 | 2011-11-20 17:46:46 +0000 | [diff] [blame] | 539 | bool SkipCache) { |
Chris Lattner | 1072509 | 2008-03-09 04:17:44 +0000 | [diff] [blame] | 540 | // If the header lookup mechanism may be relative to the current file, pass in |
| 541 | // info about where the current file is. |
Douglas Gregor | 10fe93d | 2010-08-08 07:49:23 +0000 | [diff] [blame] | 542 | const FileEntry *CurFileEnt = 0; |
Chris Lattner | 1072509 | 2008-03-09 04:17:44 +0000 | [diff] [blame] | 543 | if (!FromDir) { |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 544 | FileID FID = getCurrentFileLexer()->getFileID(); |
Douglas Gregor | 10fe93d | 2010-08-08 07:49:23 +0000 | [diff] [blame] | 545 | CurFileEnt = SourceMgr.getFileEntryForID(FID); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 546 | |
Chris Lattner | be5c64d | 2009-02-04 19:45:07 +0000 | [diff] [blame] | 547 | // If there is no file entry associated with this file, it must be the |
| 548 | // 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] | 549 | // it won't be scanned for preprocessor directives. If we have the |
| 550 | // predefines buffer, resolve #include references (which come from the |
| 551 | // -include command line argument) as if they came from the main file, this |
| 552 | // affects file lookup etc. |
| 553 | if (CurFileEnt == 0) { |
Chris Lattner | be5c64d | 2009-02-04 19:45:07 +0000 | [diff] [blame] | 554 | FID = SourceMgr.getMainFileID(); |
| 555 | CurFileEnt = SourceMgr.getFileEntryForID(FID); |
| 556 | } |
Chris Lattner | 1072509 | 2008-03-09 04:17:44 +0000 | [diff] [blame] | 557 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 558 | |
Chris Lattner | 1072509 | 2008-03-09 04:17:44 +0000 | [diff] [blame] | 559 | // Do a standard file entry lookup. |
| 560 | CurDir = CurDirLookup; |
Chandler Carruth | b5142bb | 2011-03-16 18:34:36 +0000 | [diff] [blame] | 561 | const FileEntry *FE = HeaderInfo.LookupFile( |
Manuel Klimek | 7412494 | 2011-04-26 21:50:03 +0000 | [diff] [blame] | 562 | Filename, isAngled, FromDir, CurDir, CurFileEnt, |
Douglas Gregor | 1c2e933 | 2011-11-20 17:46:46 +0000 | [diff] [blame] | 563 | SearchPath, RelativePath, SuggestedModule, SkipCache); |
Chris Lattner | f45b646 | 2010-01-22 00:14:44 +0000 | [diff] [blame] | 564 | if (FE) return FE; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 565 | |
Chris Lattner | 1072509 | 2008-03-09 04:17:44 +0000 | [diff] [blame] | 566 | // Otherwise, see if this is a subframework header. If so, this is relative |
| 567 | // to one of the headers on the #include stack. Walk the list of the current |
| 568 | // headers on the #include stack and pass them to HeaderInfo. |
Ted Kremenek | 81d24e1 | 2008-11-20 16:19:53 +0000 | [diff] [blame] | 569 | if (IsFileLexer()) { |
Ted Kremenek | 41938c8 | 2008-11-19 21:57:25 +0000 | [diff] [blame] | 570 | if ((CurFileEnt = SourceMgr.getFileEntryForID(CurPPLexer->getFileID()))) |
Chandler Carruth | b5142bb | 2011-03-16 18:34:36 +0000 | [diff] [blame] | 571 | if ((FE = HeaderInfo.LookupSubframeworkHeader(Filename, CurFileEnt, |
Douglas Gregor | 1b58c74 | 2013-02-08 00:10:48 +0000 | [diff] [blame] | 572 | SearchPath, RelativePath, |
| 573 | SuggestedModule))) |
Chris Lattner | 1072509 | 2008-03-09 04:17:44 +0000 | [diff] [blame] | 574 | return FE; |
| 575 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 576 | |
Chris Lattner | 1072509 | 2008-03-09 04:17:44 +0000 | [diff] [blame] | 577 | for (unsigned i = 0, e = IncludeMacroStack.size(); i != e; ++i) { |
| 578 | IncludeStackInfo &ISEntry = IncludeMacroStack[e-i-1]; |
Ted Kremenek | 81d24e1 | 2008-11-20 16:19:53 +0000 | [diff] [blame] | 579 | if (IsFileLexer(ISEntry)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 580 | if ((CurFileEnt = |
Ted Kremenek | 41938c8 | 2008-11-19 21:57:25 +0000 | [diff] [blame] | 581 | SourceMgr.getFileEntryForID(ISEntry.ThePPLexer->getFileID()))) |
Manuel Klimek | 7412494 | 2011-04-26 21:50:03 +0000 | [diff] [blame] | 582 | if ((FE = HeaderInfo.LookupSubframeworkHeader( |
Douglas Gregor | 1b58c74 | 2013-02-08 00:10:48 +0000 | [diff] [blame] | 583 | Filename, CurFileEnt, SearchPath, RelativePath, |
| 584 | SuggestedModule))) |
Chris Lattner | 1072509 | 2008-03-09 04:17:44 +0000 | [diff] [blame] | 585 | return FE; |
| 586 | } |
| 587 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 588 | |
Chris Lattner | 1072509 | 2008-03-09 04:17:44 +0000 | [diff] [blame] | 589 | // Otherwise, we really couldn't find the file. |
| 590 | return 0; |
| 591 | } |
| 592 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 593 | |
| 594 | //===----------------------------------------------------------------------===// |
| 595 | // Preprocessor Directive Handling. |
| 596 | //===----------------------------------------------------------------------===// |
| 597 | |
David Blaikie | 8c0b378 | 2012-06-06 18:52:13 +0000 | [diff] [blame] | 598 | class Preprocessor::ResetMacroExpansionHelper { |
| 599 | public: |
| 600 | ResetMacroExpansionHelper(Preprocessor *pp) |
| 601 | : PP(pp), save(pp->DisableMacroExpansion) { |
| 602 | if (pp->MacroExpansionInDirectivesOverride) |
| 603 | pp->DisableMacroExpansion = false; |
| 604 | } |
| 605 | ~ResetMacroExpansionHelper() { |
| 606 | PP->DisableMacroExpansion = save; |
| 607 | } |
| 608 | private: |
| 609 | Preprocessor *PP; |
| 610 | bool save; |
| 611 | }; |
| 612 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 613 | /// HandleDirective - This callback is invoked when the lexer sees a # token |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 614 | /// at the start of a line. This consumes the directive, modifies the |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 615 | /// lexer/preprocessor state, and advances the lexer(s) so that the next token |
| 616 | /// read is the correct one. |
| 617 | void Preprocessor::HandleDirective(Token &Result) { |
| 618 | // FIXME: Traditional: # with whitespace before it not recognized by K&R? |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 619 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 620 | // We just parsed a # character at the start of a line, so we're in directive |
| 621 | // mode. Tell the lexer this so any newlines we see will be converted into an |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 622 | // EOD token (which terminates the directive). |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 623 | CurPPLexer->ParsingPreprocessorDirective = true; |
Jordan Rose | 6aad4a3 | 2013-02-21 18:53:19 +0000 | [diff] [blame] | 624 | if (CurLexer) CurLexer->SetKeepWhitespaceMode(false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 625 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 626 | ++NumDirectives; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 627 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 628 | // 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] | 629 | // 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] | 630 | // pp-directive. |
Chris Lattner | 1d9c54d | 2009-12-14 04:54:40 +0000 | [diff] [blame] | 631 | bool ReadAnyTokensBeforeDirective =CurPPLexer->MIOpt.getHasReadAnyTokensVal(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 632 | |
Chris Lattner | 42aa16c | 2009-03-18 21:00:25 +0000 | [diff] [blame] | 633 | // Save the '#' token in case we need to return it later. |
| 634 | Token SavedHash = Result; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 635 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 636 | // Read the next token, the directive flavor. This isn't expanded due to |
| 637 | // C99 6.10.3p8. |
| 638 | LexUnexpandedToken(Result); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 639 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 640 | // C99 6.10.3p11: Is this preprocessor directive in macro invocation? e.g.: |
| 641 | // #define A(x) #x |
| 642 | // A(abc |
| 643 | // #warning blah |
| 644 | // def) |
Richard Smith | a3ca4d6 | 2011-12-16 22:50:01 +0000 | [diff] [blame] | 645 | // If so, the user is relying on undefined behavior, emit a diagnostic. Do |
| 646 | // not support this for #include-like directives, since that can result in |
| 647 | // terrible diagnostics, and does not work in GCC. |
| 648 | if (InMacroArgs) { |
| 649 | if (IdentifierInfo *II = Result.getIdentifierInfo()) { |
| 650 | switch (II->getPPKeywordID()) { |
| 651 | case tok::pp_include: |
| 652 | case tok::pp_import: |
| 653 | case tok::pp_include_next: |
| 654 | case tok::pp___include_macros: |
| 655 | Diag(Result, diag::err_embedded_include) << II->getName(); |
| 656 | DiscardUntilEndOfDirective(); |
| 657 | return; |
| 658 | default: |
| 659 | break; |
| 660 | } |
| 661 | } |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 662 | Diag(Result, diag::ext_embedded_directive); |
Richard Smith | a3ca4d6 | 2011-12-16 22:50:01 +0000 | [diff] [blame] | 663 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 664 | |
David Blaikie | 8c0b378 | 2012-06-06 18:52:13 +0000 | [diff] [blame] | 665 | // Temporarily enable macro expansion if set so |
| 666 | // and reset to previous state when returning from this function. |
| 667 | ResetMacroExpansionHelper helper(this); |
| 668 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 669 | switch (Result.getKind()) { |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 670 | case tok::eod: |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 671 | return; // null directive. |
Douglas Gregor | f44e854 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 672 | case tok::code_completion: |
| 673 | if (CodeComplete) |
| 674 | CodeComplete->CodeCompleteDirective( |
| 675 | CurPPLexer->getConditionalStackDepth() > 0); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 676 | setCodeCompletionReached(); |
Douglas Gregor | f44e854 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 677 | return; |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 678 | case tok::numeric_constant: // # 7 GNU line marker directive. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 679 | if (getLangOpts().AsmPreprocessor) |
Chris Lattner | 5f607c4 | 2009-03-18 20:41:10 +0000 | [diff] [blame] | 680 | break; // # 4 is not a preprocessor directive in .S files. |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 681 | return HandleDigitDirective(Result); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 682 | default: |
| 683 | IdentifierInfo *II = Result.getIdentifierInfo(); |
| 684 | if (II == 0) break; // Not an identifier. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 685 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 686 | // Ask what the preprocessor keyword ID is. |
| 687 | switch (II->getPPKeywordID()) { |
| 688 | default: break; |
| 689 | // C99 6.10.1 - Conditional Inclusion. |
| 690 | case tok::pp_if: |
| 691 | return HandleIfDirective(Result, ReadAnyTokensBeforeDirective); |
| 692 | case tok::pp_ifdef: |
| 693 | return HandleIfdefDirective(Result, false, true/*not valid for miopt*/); |
| 694 | case tok::pp_ifndef: |
| 695 | return HandleIfdefDirective(Result, true, ReadAnyTokensBeforeDirective); |
| 696 | case tok::pp_elif: |
| 697 | return HandleElifDirective(Result); |
| 698 | case tok::pp_else: |
| 699 | return HandleElseDirective(Result); |
| 700 | case tok::pp_endif: |
| 701 | return HandleEndifDirective(Result); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 702 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 703 | // C99 6.10.2 - Source File Inclusion. |
| 704 | case tok::pp_include: |
Douglas Gregor | ecdcb88 | 2010-10-20 22:00:55 +0000 | [diff] [blame] | 705 | // Handle #include. |
| 706 | return HandleIncludeDirective(SavedHash.getLocation(), Result); |
Chris Lattner | b8e240e | 2009-04-08 18:24:34 +0000 | [diff] [blame] | 707 | case tok::pp___include_macros: |
Douglas Gregor | ecdcb88 | 2010-10-20 22:00:55 +0000 | [diff] [blame] | 708 | // Handle -imacros. |
| 709 | return HandleIncludeMacrosDirective(SavedHash.getLocation(), Result); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 710 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 711 | // C99 6.10.3 - Macro Replacement. |
| 712 | case tok::pp_define: |
| 713 | return HandleDefineDirective(Result); |
| 714 | case tok::pp_undef: |
| 715 | return HandleUndefDirective(Result); |
| 716 | |
| 717 | // C99 6.10.4 - Line Control. |
| 718 | case tok::pp_line: |
Chris Lattner | 359cc44 | 2009-01-26 05:29:08 +0000 | [diff] [blame] | 719 | return HandleLineDirective(Result); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 720 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 721 | // C99 6.10.5 - Error Directive. |
| 722 | case tok::pp_error: |
| 723 | return HandleUserDiagnosticDirective(Result, false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 724 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 725 | // C99 6.10.6 - Pragma Directive. |
| 726 | case tok::pp_pragma: |
Douglas Gregor | 80c60f7 | 2010-09-09 22:45:38 +0000 | [diff] [blame] | 727 | return HandlePragmaDirective(PIK_HashPragma); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 728 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 729 | // GNU Extensions. |
| 730 | case tok::pp_import: |
Douglas Gregor | ecdcb88 | 2010-10-20 22:00:55 +0000 | [diff] [blame] | 731 | return HandleImportDirective(SavedHash.getLocation(), Result); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 732 | case tok::pp_include_next: |
Douglas Gregor | ecdcb88 | 2010-10-20 22:00:55 +0000 | [diff] [blame] | 733 | return HandleIncludeNextDirective(SavedHash.getLocation(), Result); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 734 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 735 | case tok::pp_warning: |
| 736 | Diag(Result, diag::ext_pp_warning_directive); |
| 737 | return HandleUserDiagnosticDirective(Result, true); |
| 738 | case tok::pp_ident: |
| 739 | return HandleIdentSCCSDirective(Result); |
| 740 | case tok::pp_sccs: |
| 741 | return HandleIdentSCCSDirective(Result); |
| 742 | case tok::pp_assert: |
| 743 | //isExtension = true; // FIXME: implement #assert |
| 744 | break; |
| 745 | case tok::pp_unassert: |
| 746 | //isExtension = true; // FIXME: implement #unassert |
| 747 | break; |
Douglas Gregor | 7143aab | 2011-09-01 17:04:32 +0000 | [diff] [blame] | 748 | |
Douglas Gregor | 1ac13c3 | 2012-01-03 19:48:16 +0000 | [diff] [blame] | 749 | case tok::pp___public_macro: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 750 | if (getLangOpts().Modules) |
Douglas Gregor | 94ad28b | 2012-01-03 18:24:14 +0000 | [diff] [blame] | 751 | return HandleMacroPublicDirective(Result); |
| 752 | break; |
| 753 | |
Douglas Gregor | 1ac13c3 | 2012-01-03 19:48:16 +0000 | [diff] [blame] | 754 | case tok::pp___private_macro: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 755 | if (getLangOpts().Modules) |
Douglas Gregor | 94ad28b | 2012-01-03 18:24:14 +0000 | [diff] [blame] | 756 | return HandleMacroPrivateDirective(Result); |
| 757 | break; |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 758 | } |
| 759 | break; |
| 760 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 761 | |
Chris Lattner | 42aa16c | 2009-03-18 21:00:25 +0000 | [diff] [blame] | 762 | // If this is a .S file, treat unknown # directives as non-preprocessor |
| 763 | // directives. This is important because # may be a comment or introduce |
| 764 | // various pseudo-ops. Just return the # token and push back the following |
| 765 | // token to be lexed next time. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 766 | if (getLangOpts().AsmPreprocessor) { |
Daniel Dunbar | 3d399a0 | 2009-07-13 21:48:50 +0000 | [diff] [blame] | 767 | Token *Toks = new Token[2]; |
Chris Lattner | 42aa16c | 2009-03-18 21:00:25 +0000 | [diff] [blame] | 768 | // Return the # and the token after it. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 769 | Toks[0] = SavedHash; |
Chris Lattner | 42aa16c | 2009-03-18 21:00:25 +0000 | [diff] [blame] | 770 | Toks[1] = Result; |
Chris Lattner | ba3ca52 | 2011-01-06 05:01:51 +0000 | [diff] [blame] | 771 | |
| 772 | // If the second token is a hashhash token, then we need to translate it to |
| 773 | // unknown so the token lexer doesn't try to perform token pasting. |
| 774 | if (Result.is(tok::hashhash)) |
| 775 | Toks[1].setKind(tok::unknown); |
| 776 | |
Chris Lattner | 42aa16c | 2009-03-18 21:00:25 +0000 | [diff] [blame] | 777 | // Enter this token stream so that we re-lex the tokens. Make sure to |
| 778 | // enable macro expansion, in case the token after the # is an identifier |
| 779 | // that is expanded. |
| 780 | EnterTokenStream(Toks, 2, false, true); |
| 781 | return; |
| 782 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 783 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 784 | // If we reached here, the preprocessing token is not valid! |
| 785 | Diag(Result, diag::err_pp_invalid_directive); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 786 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 787 | // Read the rest of the PP line. |
| 788 | DiscardUntilEndOfDirective(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 789 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 790 | // Okay, we're done parsing the directive. |
| 791 | } |
| 792 | |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 793 | /// GetLineValue - Convert a numeric token into an unsigned value, emitting |
| 794 | /// Diagnostic DiagID if it is invalid, and returning the value in Val. |
| 795 | static bool GetLineValue(Token &DigitTok, unsigned &Val, |
Michael Ilseman | ec27608 | 2013-04-10 01:04:18 +0000 | [diff] [blame] | 796 | unsigned DiagID, Preprocessor &PP, |
| 797 | bool IsGNULineDirective=false) { |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 798 | if (DigitTok.isNot(tok::numeric_constant)) { |
| 799 | PP.Diag(DigitTok, DiagID); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 800 | |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 801 | if (DigitTok.isNot(tok::eod)) |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 802 | PP.DiscardUntilEndOfDirective(); |
| 803 | return true; |
| 804 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 805 | |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 806 | SmallString<64> IntegerBuffer; |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 807 | IntegerBuffer.resize(DigitTok.getLength()); |
| 808 | const char *DigitTokBegin = &IntegerBuffer[0]; |
Douglas Gregor | 453091c | 2010-03-16 22:30:13 +0000 | [diff] [blame] | 809 | bool Invalid = false; |
| 810 | unsigned ActualLength = PP.getSpelling(DigitTok, DigitTokBegin, &Invalid); |
| 811 | if (Invalid) |
| 812 | return true; |
| 813 | |
Chris Lattner | dc8c90d | 2009-04-18 18:35:15 +0000 | [diff] [blame] | 814 | // Verify that we have a simple digit-sequence, and compute the value. This |
| 815 | // is always a simple digit string computed in decimal, so we do this manually |
| 816 | // here. |
| 817 | Val = 0; |
| 818 | for (unsigned i = 0; i != ActualLength; ++i) { |
Jordan Rose | 3f6f51e | 2013-02-08 22:30:41 +0000 | [diff] [blame] | 819 | if (!isDigit(DigitTokBegin[i])) { |
Chris Lattner | dc8c90d | 2009-04-18 18:35:15 +0000 | [diff] [blame] | 820 | PP.Diag(PP.AdvanceToTokenCharacter(DigitTok.getLocation(), i), |
Michael Ilseman | ec27608 | 2013-04-10 01:04:18 +0000 | [diff] [blame] | 821 | diag::err_pp_line_digit_sequence) << IsGNULineDirective; |
Chris Lattner | dc8c90d | 2009-04-18 18:35:15 +0000 | [diff] [blame] | 822 | PP.DiscardUntilEndOfDirective(); |
| 823 | return true; |
| 824 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 825 | |
Chris Lattner | dc8c90d | 2009-04-18 18:35:15 +0000 | [diff] [blame] | 826 | unsigned NextVal = Val*10+(DigitTokBegin[i]-'0'); |
| 827 | if (NextVal < Val) { // overflow. |
| 828 | PP.Diag(DigitTok, DiagID); |
| 829 | PP.DiscardUntilEndOfDirective(); |
| 830 | return true; |
| 831 | } |
| 832 | Val = NextVal; |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 833 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 834 | |
Fariborz Jahanian | 540f9ae | 2012-06-26 21:19:20 +0000 | [diff] [blame] | 835 | if (DigitTokBegin[0] == '0' && Val) |
Michael Ilseman | ec27608 | 2013-04-10 01:04:18 +0000 | [diff] [blame] | 836 | PP.Diag(DigitTok.getLocation(), diag::warn_pp_line_decimal) |
| 837 | << IsGNULineDirective; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 838 | |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 839 | return false; |
| 840 | } |
| 841 | |
James Dennett | dc20169 | 2012-06-22 05:46:07 +0000 | [diff] [blame] | 842 | /// \brief Handle a \#line directive: C99 6.10.4. |
| 843 | /// |
| 844 | /// The two acceptable forms are: |
| 845 | /// \verbatim |
Chris Lattner | 359cc44 | 2009-01-26 05:29:08 +0000 | [diff] [blame] | 846 | /// # line digit-sequence |
| 847 | /// # line digit-sequence "s-char-sequence" |
James Dennett | dc20169 | 2012-06-22 05:46:07 +0000 | [diff] [blame] | 848 | /// \endverbatim |
Chris Lattner | 359cc44 | 2009-01-26 05:29:08 +0000 | [diff] [blame] | 849 | void Preprocessor::HandleLineDirective(Token &Tok) { |
| 850 | // Read the line # and string argument. Per C99 6.10.4p5, these tokens are |
| 851 | // expanded. |
| 852 | Token DigitTok; |
| 853 | Lex(DigitTok); |
| 854 | |
Chris Lattner | 359cc44 | 2009-01-26 05:29:08 +0000 | [diff] [blame] | 855 | // Validate the number and convert it to an unsigned. |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 856 | unsigned LineNo; |
Chris Lattner | dc8c90d | 2009-04-18 18:35:15 +0000 | [diff] [blame] | 857 | if (GetLineValue(DigitTok, LineNo, diag::err_pp_line_requires_integer,*this)) |
Chris Lattner | 359cc44 | 2009-01-26 05:29:08 +0000 | [diff] [blame] | 858 | return; |
Fariborz Jahanian | 540f9ae | 2012-06-26 21:19:20 +0000 | [diff] [blame] | 859 | |
| 860 | if (LineNo == 0) |
| 861 | Diag(DigitTok, diag::ext_pp_line_zero); |
Chris Lattner | 359cc44 | 2009-01-26 05:29:08 +0000 | [diff] [blame] | 862 | |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 863 | // Enforce C99 6.10.4p3: "The digit sequence shall not specify ... a |
| 864 | // number greater than 2147483647". C90 requires that the line # be <= 32767. |
Eli Friedman | 158ebfb | 2011-10-10 23:35:28 +0000 | [diff] [blame] | 865 | unsigned LineLimit = 32768U; |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 866 | if (LangOpts.C99 || LangOpts.CPlusPlus11) |
Eli Friedman | 158ebfb | 2011-10-10 23:35:28 +0000 | [diff] [blame] | 867 | LineLimit = 2147483648U; |
Chris Lattner | 359cc44 | 2009-01-26 05:29:08 +0000 | [diff] [blame] | 868 | if (LineNo >= LineLimit) |
| 869 | Diag(DigitTok, diag::ext_pp_line_too_big) << LineLimit; |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 870 | else if (LangOpts.CPlusPlus11 && LineNo >= 32768U) |
Richard Smith | 661a996 | 2011-10-15 01:18:56 +0000 | [diff] [blame] | 871 | Diag(DigitTok, diag::warn_cxx98_compat_pp_line_too_big); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 872 | |
Chris Lattner | 5b9a504 | 2009-01-26 07:57:50 +0000 | [diff] [blame] | 873 | int FilenameID = -1; |
Chris Lattner | 359cc44 | 2009-01-26 05:29:08 +0000 | [diff] [blame] | 874 | Token StrTok; |
| 875 | Lex(StrTok); |
| 876 | |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 877 | // If the StrTok is "eod", then it wasn't present. Otherwise, it must be a |
| 878 | // string followed by eod. |
| 879 | if (StrTok.is(tok::eod)) |
Chris Lattner | 359cc44 | 2009-01-26 05:29:08 +0000 | [diff] [blame] | 880 | ; // ok |
| 881 | else if (StrTok.isNot(tok::string_literal)) { |
| 882 | Diag(StrTok, diag::err_pp_line_invalid_filename); |
Richard Smith | 99831e4 | 2012-03-06 03:21:47 +0000 | [diff] [blame] | 883 | return DiscardUntilEndOfDirective(); |
| 884 | } else if (StrTok.hasUDSuffix()) { |
| 885 | Diag(StrTok, diag::err_invalid_string_udl); |
| 886 | return DiscardUntilEndOfDirective(); |
Chris Lattner | 359cc44 | 2009-01-26 05:29:08 +0000 | [diff] [blame] | 887 | } else { |
Chris Lattner | 5b9a504 | 2009-01-26 07:57:50 +0000 | [diff] [blame] | 888 | // Parse and validate the string, converting it into a unique ID. |
| 889 | StringLiteralParser Literal(&StrTok, 1, *this); |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 890 | assert(Literal.isAscii() && "Didn't allow wide strings in"); |
Chris Lattner | 5b9a504 | 2009-01-26 07:57:50 +0000 | [diff] [blame] | 891 | if (Literal.hadError) |
| 892 | return DiscardUntilEndOfDirective(); |
| 893 | if (Literal.Pascal) { |
| 894 | Diag(StrTok, diag::err_pp_linemarker_invalid_filename); |
| 895 | return DiscardUntilEndOfDirective(); |
| 896 | } |
Jay Foad | 65aa688 | 2011-06-21 15:13:30 +0000 | [diff] [blame] | 897 | FilenameID = SourceMgr.getLineTableFilenameID(Literal.GetString()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 898 | |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 899 | // Verify that there is nothing after the string, other than EOD. Because |
Chris Lattner | ab82f41 | 2009-04-17 23:30:53 +0000 | [diff] [blame] | 900 | // of C99 6.10.4p5, macros that expand to empty tokens are ok. |
| 901 | CheckEndOfDirective("line", true); |
Chris Lattner | 359cc44 | 2009-01-26 05:29:08 +0000 | [diff] [blame] | 902 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 903 | |
Chris Lattner | 4c4ea17 | 2009-02-03 21:52:55 +0000 | [diff] [blame] | 904 | SourceMgr.AddLineNote(DigitTok.getLocation(), LineNo, FilenameID); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 905 | |
Chris Lattner | 1662938 | 2009-03-27 17:13:49 +0000 | [diff] [blame] | 906 | if (Callbacks) |
Chris Lattner | 86d0ef7 | 2010-04-14 04:28:50 +0000 | [diff] [blame] | 907 | Callbacks->FileChanged(CurPPLexer->getSourceLocation(), |
| 908 | PPCallbacks::RenameFile, |
Chris Lattner | 1662938 | 2009-03-27 17:13:49 +0000 | [diff] [blame] | 909 | SrcMgr::C_User); |
Chris Lattner | 359cc44 | 2009-01-26 05:29:08 +0000 | [diff] [blame] | 910 | } |
| 911 | |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 912 | /// ReadLineMarkerFlags - Parse and validate any flags at the end of a GNU line |
| 913 | /// marker directive. |
| 914 | static bool ReadLineMarkerFlags(bool &IsFileEntry, bool &IsFileExit, |
| 915 | bool &IsSystemHeader, bool &IsExternCHeader, |
| 916 | Preprocessor &PP) { |
| 917 | unsigned FlagVal; |
| 918 | Token FlagTok; |
| 919 | PP.Lex(FlagTok); |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 920 | if (FlagTok.is(tok::eod)) return false; |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 921 | if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag, PP)) |
| 922 | return true; |
| 923 | |
| 924 | if (FlagVal == 1) { |
| 925 | IsFileEntry = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 926 | |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 927 | PP.Lex(FlagTok); |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 928 | if (FlagTok.is(tok::eod)) return false; |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 929 | if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag,PP)) |
| 930 | return true; |
| 931 | } else if (FlagVal == 2) { |
| 932 | IsFileExit = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 933 | |
Chris Lattner | 137b6a6 | 2009-02-04 06:25:26 +0000 | [diff] [blame] | 934 | SourceManager &SM = PP.getSourceManager(); |
| 935 | // If we are leaving the current presumed file, check to make sure the |
| 936 | // presumed include stack isn't empty! |
| 937 | FileID CurFileID = |
Chandler Carruth | e7b2b6e | 2011-07-25 20:52:32 +0000 | [diff] [blame] | 938 | SM.getDecomposedExpansionLoc(FlagTok.getLocation()).first; |
Chris Lattner | 137b6a6 | 2009-02-04 06:25:26 +0000 | [diff] [blame] | 939 | PresumedLoc PLoc = SM.getPresumedLoc(FlagTok.getLocation()); |
Douglas Gregor | cb7b1e1 | 2010-11-12 07:15:47 +0000 | [diff] [blame] | 940 | if (PLoc.isInvalid()) |
| 941 | return true; |
| 942 | |
Chris Lattner | 137b6a6 | 2009-02-04 06:25:26 +0000 | [diff] [blame] | 943 | // If there is no include loc (main file) or if the include loc is in a |
| 944 | // different physical file, then we aren't in a "1" line marker flag region. |
| 945 | SourceLocation IncLoc = PLoc.getIncludeLoc(); |
| 946 | if (IncLoc.isInvalid() || |
Chandler Carruth | e7b2b6e | 2011-07-25 20:52:32 +0000 | [diff] [blame] | 947 | SM.getDecomposedExpansionLoc(IncLoc).first != CurFileID) { |
Chris Lattner | 137b6a6 | 2009-02-04 06:25:26 +0000 | [diff] [blame] | 948 | PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_pop); |
| 949 | PP.DiscardUntilEndOfDirective(); |
| 950 | return true; |
| 951 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 952 | |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 953 | PP.Lex(FlagTok); |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 954 | if (FlagTok.is(tok::eod)) return false; |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 955 | if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag,PP)) |
| 956 | return true; |
| 957 | } |
| 958 | |
| 959 | // We must have 3 if there are still flags. |
| 960 | if (FlagVal != 3) { |
| 961 | PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_flag); |
Chris Lattner | 5b9a504 | 2009-01-26 07:57:50 +0000 | [diff] [blame] | 962 | PP.DiscardUntilEndOfDirective(); |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 963 | return true; |
| 964 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 965 | |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 966 | IsSystemHeader = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 967 | |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 968 | PP.Lex(FlagTok); |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 969 | if (FlagTok.is(tok::eod)) return false; |
Chris Lattner | 9d79eba | 2009-02-04 05:21:58 +0000 | [diff] [blame] | 970 | if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag, PP)) |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 971 | return true; |
| 972 | |
| 973 | // We must have 4 if there is yet another flag. |
| 974 | if (FlagVal != 4) { |
| 975 | PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_flag); |
Chris Lattner | 5b9a504 | 2009-01-26 07:57:50 +0000 | [diff] [blame] | 976 | PP.DiscardUntilEndOfDirective(); |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 977 | return true; |
| 978 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 979 | |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 980 | IsExternCHeader = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 981 | |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 982 | PP.Lex(FlagTok); |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 983 | if (FlagTok.is(tok::eod)) return false; |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 984 | |
| 985 | // There are no more valid flags here. |
| 986 | PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_flag); |
Chris Lattner | 5b9a504 | 2009-01-26 07:57:50 +0000 | [diff] [blame] | 987 | PP.DiscardUntilEndOfDirective(); |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 988 | return true; |
| 989 | } |
| 990 | |
| 991 | /// HandleDigitDirective - Handle a GNU line marker directive, whose syntax is |
| 992 | /// one of the following forms: |
| 993 | /// |
| 994 | /// # 42 |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 995 | /// # 42 "file" ('1' | '2')? |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 996 | /// # 42 "file" ('1' | '2')? '3' '4'? |
| 997 | /// |
| 998 | void Preprocessor::HandleDigitDirective(Token &DigitTok) { |
| 999 | // Validate the number and convert it to an unsigned. GNU does not have a |
| 1000 | // line # limit other than it fit in 32-bits. |
| 1001 | unsigned LineNo; |
| 1002 | if (GetLineValue(DigitTok, LineNo, diag::err_pp_linemarker_requires_integer, |
Michael Ilseman | ec27608 | 2013-04-10 01:04:18 +0000 | [diff] [blame] | 1003 | *this, true)) |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 1004 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1005 | |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 1006 | Token StrTok; |
| 1007 | Lex(StrTok); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1008 | |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 1009 | bool IsFileEntry = false, IsFileExit = false; |
| 1010 | bool IsSystemHeader = false, IsExternCHeader = false; |
Chris Lattner | 5b9a504 | 2009-01-26 07:57:50 +0000 | [diff] [blame] | 1011 | int FilenameID = -1; |
| 1012 | |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 1013 | // If the StrTok is "eod", then it wasn't present. Otherwise, it must be a |
| 1014 | // string followed by eod. |
| 1015 | if (StrTok.is(tok::eod)) |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 1016 | ; // ok |
| 1017 | else if (StrTok.isNot(tok::string_literal)) { |
| 1018 | Diag(StrTok, diag::err_pp_linemarker_invalid_filename); |
Chris Lattner | 5b9a504 | 2009-01-26 07:57:50 +0000 | [diff] [blame] | 1019 | return DiscardUntilEndOfDirective(); |
Richard Smith | 99831e4 | 2012-03-06 03:21:47 +0000 | [diff] [blame] | 1020 | } else if (StrTok.hasUDSuffix()) { |
| 1021 | Diag(StrTok, diag::err_invalid_string_udl); |
| 1022 | return DiscardUntilEndOfDirective(); |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 1023 | } else { |
Chris Lattner | 5b9a504 | 2009-01-26 07:57:50 +0000 | [diff] [blame] | 1024 | // Parse and validate the string, converting it into a unique ID. |
| 1025 | StringLiteralParser Literal(&StrTok, 1, *this); |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 1026 | assert(Literal.isAscii() && "Didn't allow wide strings in"); |
Chris Lattner | 5b9a504 | 2009-01-26 07:57:50 +0000 | [diff] [blame] | 1027 | if (Literal.hadError) |
| 1028 | return DiscardUntilEndOfDirective(); |
| 1029 | if (Literal.Pascal) { |
| 1030 | Diag(StrTok, diag::err_pp_linemarker_invalid_filename); |
| 1031 | return DiscardUntilEndOfDirective(); |
| 1032 | } |
Jay Foad | 65aa688 | 2011-06-21 15:13:30 +0000 | [diff] [blame] | 1033 | FilenameID = SourceMgr.getLineTableFilenameID(Literal.GetString()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1034 | |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 1035 | // If a filename was present, read any flags that are present. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1036 | if (ReadLineMarkerFlags(IsFileEntry, IsFileExit, |
Chris Lattner | 5b9a504 | 2009-01-26 07:57:50 +0000 | [diff] [blame] | 1037 | IsSystemHeader, IsExternCHeader, *this)) |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 1038 | return; |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 1039 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1040 | |
Chris Lattner | 9d79eba | 2009-02-04 05:21:58 +0000 | [diff] [blame] | 1041 | // Create a line note with this information. |
| 1042 | SourceMgr.AddLineNote(DigitTok.getLocation(), LineNo, FilenameID, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1043 | IsFileEntry, IsFileExit, |
Chris Lattner | 9d79eba | 2009-02-04 05:21:58 +0000 | [diff] [blame] | 1044 | IsSystemHeader, IsExternCHeader); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1045 | |
Chris Lattner | 1662938 | 2009-03-27 17:13:49 +0000 | [diff] [blame] | 1046 | // If the preprocessor has callbacks installed, notify them of the #line |
| 1047 | // change. This is used so that the line marker comes out in -E mode for |
| 1048 | // example. |
| 1049 | if (Callbacks) { |
| 1050 | PPCallbacks::FileChangeReason Reason = PPCallbacks::RenameFile; |
| 1051 | if (IsFileEntry) |
| 1052 | Reason = PPCallbacks::EnterFile; |
| 1053 | else if (IsFileExit) |
| 1054 | Reason = PPCallbacks::ExitFile; |
| 1055 | SrcMgr::CharacteristicKind FileKind = SrcMgr::C_User; |
| 1056 | if (IsExternCHeader) |
| 1057 | FileKind = SrcMgr::C_ExternCSystem; |
| 1058 | else if (IsSystemHeader) |
| 1059 | FileKind = SrcMgr::C_System; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1060 | |
Chris Lattner | 86d0ef7 | 2010-04-14 04:28:50 +0000 | [diff] [blame] | 1061 | Callbacks->FileChanged(CurPPLexer->getSourceLocation(), Reason, FileKind); |
Chris Lattner | 1662938 | 2009-03-27 17:13:49 +0000 | [diff] [blame] | 1062 | } |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 1063 | } |
| 1064 | |
| 1065 | |
Chris Lattner | 099dd05 | 2009-01-26 05:30:54 +0000 | [diff] [blame] | 1066 | /// HandleUserDiagnosticDirective - Handle a #warning or #error directive. |
| 1067 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1068 | void Preprocessor::HandleUserDiagnosticDirective(Token &Tok, |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1069 | bool isWarning) { |
Chris Lattner | 099dd05 | 2009-01-26 05:30:54 +0000 | [diff] [blame] | 1070 | // PTH doesn't emit #warning or #error directives. |
| 1071 | if (CurPTHLexer) |
Chris Lattner | 359cc44 | 2009-01-26 05:29:08 +0000 | [diff] [blame] | 1072 | return CurPTHLexer->DiscardToEndOfLine(); |
| 1073 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1074 | // Read the rest of the line raw. We do this because we don't want macros |
| 1075 | // to be expanded and we don't require that the tokens be valid preprocessing |
| 1076 | // tokens. For example, this is allowed: "#warning ` 'foo". GCC does |
| 1077 | // collapse multiple consequtive white space between tokens, but this isn't |
| 1078 | // specified by the standard. |
Benjamin Kramer | 3093b20 | 2012-05-18 19:32:16 +0000 | [diff] [blame] | 1079 | SmallString<128> Message; |
| 1080 | CurLexer->ReadToEndOfLine(&Message); |
Ted Kremenek | 34a2c42 | 2012-02-02 00:16:13 +0000 | [diff] [blame] | 1081 | |
| 1082 | // Find the first non-whitespace character, so that we can make the |
| 1083 | // diagnostic more succinct. |
Benjamin Kramer | 3093b20 | 2012-05-18 19:32:16 +0000 | [diff] [blame] | 1084 | StringRef Msg = Message.str().ltrim(" "); |
| 1085 | |
Chris Lattner | 359cc44 | 2009-01-26 05:29:08 +0000 | [diff] [blame] | 1086 | if (isWarning) |
Ted Kremenek | 34a2c42 | 2012-02-02 00:16:13 +0000 | [diff] [blame] | 1087 | Diag(Tok, diag::pp_hash_warning) << Msg; |
Chris Lattner | 359cc44 | 2009-01-26 05:29:08 +0000 | [diff] [blame] | 1088 | else |
Ted Kremenek | 34a2c42 | 2012-02-02 00:16:13 +0000 | [diff] [blame] | 1089 | Diag(Tok, diag::err_pp_hash_error) << Msg; |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1090 | } |
| 1091 | |
| 1092 | /// HandleIdentSCCSDirective - Handle a #ident/#sccs directive. |
| 1093 | /// |
| 1094 | void Preprocessor::HandleIdentSCCSDirective(Token &Tok) { |
| 1095 | // Yes, this directive is an extension. |
| 1096 | Diag(Tok, diag::ext_pp_ident_directive); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1097 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1098 | // Read the string argument. |
| 1099 | Token StrTok; |
| 1100 | Lex(StrTok); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1101 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1102 | // If the token kind isn't a string, it's a malformed directive. |
| 1103 | if (StrTok.isNot(tok::string_literal) && |
Chris Lattner | 3692b09 | 2008-11-18 07:59:24 +0000 | [diff] [blame] | 1104 | StrTok.isNot(tok::wide_string_literal)) { |
| 1105 | Diag(StrTok, diag::err_pp_malformed_ident); |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 1106 | if (StrTok.isNot(tok::eod)) |
Chris Lattner | 099dd05 | 2009-01-26 05:30:54 +0000 | [diff] [blame] | 1107 | DiscardUntilEndOfDirective(); |
Chris Lattner | 3692b09 | 2008-11-18 07:59:24 +0000 | [diff] [blame] | 1108 | return; |
| 1109 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1110 | |
Richard Smith | 99831e4 | 2012-03-06 03:21:47 +0000 | [diff] [blame] | 1111 | if (StrTok.hasUDSuffix()) { |
| 1112 | Diag(StrTok, diag::err_invalid_string_udl); |
| 1113 | return DiscardUntilEndOfDirective(); |
| 1114 | } |
| 1115 | |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 1116 | // Verify that there is nothing after the string, other than EOD. |
Chris Lattner | 35410d5 | 2009-04-14 05:07:49 +0000 | [diff] [blame] | 1117 | CheckEndOfDirective("ident"); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1118 | |
Douglas Gregor | 453091c | 2010-03-16 22:30:13 +0000 | [diff] [blame] | 1119 | if (Callbacks) { |
| 1120 | bool Invalid = false; |
| 1121 | std::string Str = getSpelling(StrTok, &Invalid); |
| 1122 | if (!Invalid) |
| 1123 | Callbacks->Ident(Tok.getLocation(), Str); |
| 1124 | } |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1125 | } |
| 1126 | |
Douglas Gregor | 94ad28b | 2012-01-03 18:24:14 +0000 | [diff] [blame] | 1127 | /// \brief Handle a #public directive. |
| 1128 | void Preprocessor::HandleMacroPublicDirective(Token &Tok) { |
Douglas Gregor | 7143aab | 2011-09-01 17:04:32 +0000 | [diff] [blame] | 1129 | Token MacroNameTok; |
| 1130 | ReadMacroName(MacroNameTok, 2); |
| 1131 | |
| 1132 | // Error reading macro name? If so, diagnostic already issued. |
| 1133 | if (MacroNameTok.is(tok::eod)) |
| 1134 | return; |
| 1135 | |
Douglas Gregor | 1ac13c3 | 2012-01-03 19:48:16 +0000 | [diff] [blame] | 1136 | // Check to see if this is the last token on the #__public_macro line. |
| 1137 | CheckEndOfDirective("__public_macro"); |
Douglas Gregor | 7143aab | 2011-09-01 17:04:32 +0000 | [diff] [blame] | 1138 | |
Argyrios Kyrtzidis | 9317ab9 | 2013-03-22 21:12:57 +0000 | [diff] [blame] | 1139 | IdentifierInfo *II = MacroNameTok.getIdentifierInfo(); |
Douglas Gregor | 7143aab | 2011-09-01 17:04:32 +0000 | [diff] [blame] | 1140 | // Okay, we finally have a valid identifier to undef. |
Argyrios Kyrtzidis | 9317ab9 | 2013-03-22 21:12:57 +0000 | [diff] [blame] | 1141 | MacroDirective *MD = getMacroDirective(II); |
Douglas Gregor | 7143aab | 2011-09-01 17:04:32 +0000 | [diff] [blame] | 1142 | |
| 1143 | // If the macro is not defined, this is an error. |
Argyrios Kyrtzidis | 9818a1d | 2013-02-20 00:54:57 +0000 | [diff] [blame] | 1144 | if (MD == 0) { |
Argyrios Kyrtzidis | 9317ab9 | 2013-03-22 21:12:57 +0000 | [diff] [blame] | 1145 | Diag(MacroNameTok, diag::err_pp_visibility_non_macro) << II; |
Douglas Gregor | 7143aab | 2011-09-01 17:04:32 +0000 | [diff] [blame] | 1146 | return; |
| 1147 | } |
| 1148 | |
| 1149 | // Note that this macro has now been exported. |
Argyrios Kyrtzidis | c56fff7 | 2013-03-26 17:17:01 +0000 | [diff] [blame] | 1150 | appendMacroDirective(II, AllocateVisibilityMacroDirective( |
| 1151 | MacroNameTok.getLocation(), /*IsPublic=*/true)); |
Douglas Gregor | aa93a87 | 2011-10-17 15:32:29 +0000 | [diff] [blame] | 1152 | } |
| 1153 | |
Douglas Gregor | 94ad28b | 2012-01-03 18:24:14 +0000 | [diff] [blame] | 1154 | /// \brief Handle a #private directive. |
Douglas Gregor | aa93a87 | 2011-10-17 15:32:29 +0000 | [diff] [blame] | 1155 | void Preprocessor::HandleMacroPrivateDirective(Token &Tok) { |
| 1156 | Token MacroNameTok; |
| 1157 | ReadMacroName(MacroNameTok, 2); |
| 1158 | |
| 1159 | // Error reading macro name? If so, diagnostic already issued. |
| 1160 | if (MacroNameTok.is(tok::eod)) |
| 1161 | return; |
| 1162 | |
Douglas Gregor | 1ac13c3 | 2012-01-03 19:48:16 +0000 | [diff] [blame] | 1163 | // Check to see if this is the last token on the #__private_macro line. |
| 1164 | CheckEndOfDirective("__private_macro"); |
Douglas Gregor | aa93a87 | 2011-10-17 15:32:29 +0000 | [diff] [blame] | 1165 | |
Argyrios Kyrtzidis | 9317ab9 | 2013-03-22 21:12:57 +0000 | [diff] [blame] | 1166 | IdentifierInfo *II = MacroNameTok.getIdentifierInfo(); |
Douglas Gregor | aa93a87 | 2011-10-17 15:32:29 +0000 | [diff] [blame] | 1167 | // Okay, we finally have a valid identifier to undef. |
Argyrios Kyrtzidis | 9317ab9 | 2013-03-22 21:12:57 +0000 | [diff] [blame] | 1168 | MacroDirective *MD = getMacroDirective(II); |
Douglas Gregor | aa93a87 | 2011-10-17 15:32:29 +0000 | [diff] [blame] | 1169 | |
| 1170 | // If the macro is not defined, this is an error. |
Argyrios Kyrtzidis | 9818a1d | 2013-02-20 00:54:57 +0000 | [diff] [blame] | 1171 | if (MD == 0) { |
Argyrios Kyrtzidis | 9317ab9 | 2013-03-22 21:12:57 +0000 | [diff] [blame] | 1172 | Diag(MacroNameTok, diag::err_pp_visibility_non_macro) << II; |
Douglas Gregor | aa93a87 | 2011-10-17 15:32:29 +0000 | [diff] [blame] | 1173 | return; |
| 1174 | } |
| 1175 | |
| 1176 | // Note that this macro has now been marked private. |
Argyrios Kyrtzidis | c56fff7 | 2013-03-26 17:17:01 +0000 | [diff] [blame] | 1177 | appendMacroDirective(II, AllocateVisibilityMacroDirective( |
| 1178 | MacroNameTok.getLocation(), /*IsPublic=*/false)); |
Douglas Gregor | 7143aab | 2011-09-01 17:04:32 +0000 | [diff] [blame] | 1179 | } |
| 1180 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1181 | //===----------------------------------------------------------------------===// |
| 1182 | // Preprocessor Include Directive Handling. |
| 1183 | //===----------------------------------------------------------------------===// |
| 1184 | |
| 1185 | /// GetIncludeFilenameSpelling - Turn the specified lexer token into a fully |
James Dennett | dc20169 | 2012-06-22 05:46:07 +0000 | [diff] [blame] | 1186 | /// checked and spelled filename, e.g. as an operand of \#include. This returns |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1187 | /// true if the input filename was in <>'s or false if it were in ""'s. The |
| 1188 | /// caller is expected to provide a buffer that is large enough to hold the |
| 1189 | /// spelling of the filename, but is also expected to handle the case when |
| 1190 | /// this method decides to use a different buffer. |
| 1191 | bool Preprocessor::GetIncludeFilenameSpelling(SourceLocation Loc, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1192 | StringRef &Buffer) { |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1193 | // Get the text form of the filename. |
Chris Lattner | a139481 | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 1194 | assert(!Buffer.empty() && "Can't have tokens with empty spellings!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1195 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1196 | // Make sure the filename is <x> or "x". |
| 1197 | bool isAngled; |
Chris Lattner | a139481 | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 1198 | if (Buffer[0] == '<') { |
| 1199 | if (Buffer.back() != '>') { |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1200 | Diag(Loc, diag::err_pp_expects_filename); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1201 | Buffer = StringRef(); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1202 | return true; |
| 1203 | } |
| 1204 | isAngled = true; |
Chris Lattner | a139481 | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 1205 | } else if (Buffer[0] == '"') { |
| 1206 | if (Buffer.back() != '"') { |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1207 | Diag(Loc, diag::err_pp_expects_filename); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1208 | Buffer = StringRef(); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1209 | return true; |
| 1210 | } |
| 1211 | isAngled = false; |
| 1212 | } else { |
| 1213 | Diag(Loc, diag::err_pp_expects_filename); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1214 | Buffer = StringRef(); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1215 | return true; |
| 1216 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1217 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1218 | // Diagnose #include "" as invalid. |
Chris Lattner | a139481 | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 1219 | if (Buffer.size() <= 2) { |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1220 | Diag(Loc, diag::err_pp_empty_filename); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1221 | Buffer = StringRef(); |
Chris Lattner | a139481 | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 1222 | return true; |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1223 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1224 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1225 | // Skip the brackets. |
Chris Lattner | a139481 | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 1226 | Buffer = Buffer.substr(1, Buffer.size()-2); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1227 | return isAngled; |
| 1228 | } |
| 1229 | |
James Dennett | dc20169 | 2012-06-22 05:46:07 +0000 | [diff] [blame] | 1230 | /// \brief Handle cases where the \#include name is expanded from a macro |
| 1231 | /// as multiple tokens, which need to be glued together. |
| 1232 | /// |
| 1233 | /// This occurs for code like: |
| 1234 | /// \code |
| 1235 | /// \#define FOO <a/b.h> |
| 1236 | /// \#include FOO |
| 1237 | /// \endcode |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1238 | /// because in this case, "<a/b.h>" is returned as 7 tokens, not one. |
| 1239 | /// |
| 1240 | /// This code concatenates and consumes tokens up to the '>' token. It returns |
| 1241 | /// false if the > was found, otherwise it returns true if it finds and consumes |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 1242 | /// the EOD marker. |
John Thompson | a28cc09 | 2009-10-30 13:49:06 +0000 | [diff] [blame] | 1243 | bool Preprocessor::ConcatenateIncludeName( |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 1244 | SmallString<128> &FilenameBuffer, |
Douglas Gregor | ecdcb88 | 2010-10-20 22:00:55 +0000 | [diff] [blame] | 1245 | SourceLocation &End) { |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1246 | Token CurTok; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1247 | |
John Thompson | a28cc09 | 2009-10-30 13:49:06 +0000 | [diff] [blame] | 1248 | Lex(CurTok); |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 1249 | while (CurTok.isNot(tok::eod)) { |
Douglas Gregor | ecdcb88 | 2010-10-20 22:00:55 +0000 | [diff] [blame] | 1250 | End = CurTok.getLocation(); |
| 1251 | |
Douglas Gregor | 25bb03b | 2010-12-09 23:35:36 +0000 | [diff] [blame] | 1252 | // FIXME: Provide code completion for #includes. |
| 1253 | if (CurTok.is(tok::code_completion)) { |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 1254 | setCodeCompletionReached(); |
Douglas Gregor | 25bb03b | 2010-12-09 23:35:36 +0000 | [diff] [blame] | 1255 | Lex(CurTok); |
| 1256 | continue; |
| 1257 | } |
| 1258 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1259 | // Append the spelling of this token to the buffer. If there was a space |
| 1260 | // before it, add it now. |
| 1261 | if (CurTok.hasLeadingSpace()) |
| 1262 | FilenameBuffer.push_back(' '); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1263 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1264 | // Get the spelling of the token, directly into FilenameBuffer if possible. |
| 1265 | unsigned PreAppendSize = FilenameBuffer.size(); |
| 1266 | FilenameBuffer.resize(PreAppendSize+CurTok.getLength()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1267 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1268 | const char *BufPtr = &FilenameBuffer[PreAppendSize]; |
John Thompson | a28cc09 | 2009-10-30 13:49:06 +0000 | [diff] [blame] | 1269 | unsigned ActualLen = getSpelling(CurTok, BufPtr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1270 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1271 | // If the token was spelled somewhere else, copy it into FilenameBuffer. |
| 1272 | if (BufPtr != &FilenameBuffer[PreAppendSize]) |
| 1273 | memcpy(&FilenameBuffer[PreAppendSize], BufPtr, ActualLen); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1274 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1275 | // Resize FilenameBuffer to the correct size. |
| 1276 | if (CurTok.getLength() != ActualLen) |
| 1277 | FilenameBuffer.resize(PreAppendSize+ActualLen); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1278 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1279 | // If we found the '>' marker, return success. |
| 1280 | if (CurTok.is(tok::greater)) |
| 1281 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1282 | |
John Thompson | a28cc09 | 2009-10-30 13:49:06 +0000 | [diff] [blame] | 1283 | Lex(CurTok); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1284 | } |
| 1285 | |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 1286 | // If we hit the eod marker, emit an error and return true so that the caller |
| 1287 | // knows the EOD has been read. |
John Thompson | a28cc09 | 2009-10-30 13:49:06 +0000 | [diff] [blame] | 1288 | Diag(CurTok.getLocation(), diag::err_pp_expects_filename); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1289 | return true; |
| 1290 | } |
| 1291 | |
James Dennett | dc20169 | 2012-06-22 05:46:07 +0000 | [diff] [blame] | 1292 | /// HandleIncludeDirective - The "\#include" tokens have just been read, read |
| 1293 | /// the file to be included from the lexer, then include it! This is a common |
| 1294 | /// routine with functionality shared between \#include, \#include_next and |
| 1295 | /// \#import. LookupFrom is set when this is a \#include_next directive, it |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1296 | /// specifies the file to start searching from. |
Douglas Gregor | ecdcb88 | 2010-10-20 22:00:55 +0000 | [diff] [blame] | 1297 | void Preprocessor::HandleIncludeDirective(SourceLocation HashLoc, |
| 1298 | Token &IncludeTok, |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1299 | const DirectoryLookup *LookupFrom, |
| 1300 | bool isImport) { |
| 1301 | |
| 1302 | Token FilenameTok; |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 1303 | CurPPLexer->LexIncludeFilename(FilenameTok); |
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 | // Reserve a buffer to get the spelling. |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 1306 | SmallString<128> FilenameBuffer; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1307 | StringRef Filename; |
Douglas Gregor | ecdcb88 | 2010-10-20 22:00:55 +0000 | [diff] [blame] | 1308 | SourceLocation End; |
Douglas Gregor | e3a8256 | 2011-11-30 18:02:36 +0000 | [diff] [blame] | 1309 | SourceLocation CharEnd; // the end of this directive, in characters |
Douglas Gregor | ecdcb88 | 2010-10-20 22:00:55 +0000 | [diff] [blame] | 1310 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1311 | switch (FilenameTok.getKind()) { |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 1312 | case tok::eod: |
| 1313 | // If the token kind is EOD, the error has already been diagnosed. |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1314 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1315 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1316 | case tok::angle_string_literal: |
Benjamin Kramer | ddeea56 | 2010-02-27 13:44:12 +0000 | [diff] [blame] | 1317 | case tok::string_literal: |
| 1318 | Filename = getSpelling(FilenameTok, FilenameBuffer); |
Douglas Gregor | ecdcb88 | 2010-10-20 22:00:55 +0000 | [diff] [blame] | 1319 | End = FilenameTok.getLocation(); |
Argyrios Kyrtzidis | cfa1caa | 2012-11-01 17:52:58 +0000 | [diff] [blame] | 1320 | CharEnd = End.getLocWithOffset(FilenameTok.getLength()); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1321 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1322 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1323 | case tok::less: |
| 1324 | // This could be a <foo/bar.h> file coming from a macro expansion. In this |
| 1325 | // case, glue the tokens together into FilenameBuffer and interpret those. |
| 1326 | FilenameBuffer.push_back('<'); |
Douglas Gregor | ecdcb88 | 2010-10-20 22:00:55 +0000 | [diff] [blame] | 1327 | if (ConcatenateIncludeName(FilenameBuffer, End)) |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 1328 | return; // Found <eod> but no ">"? Diagnostic already emitted. |
Chris Lattner | a139481 | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 1329 | Filename = FilenameBuffer.str(); |
Argyrios Kyrtzidis | cfa1caa | 2012-11-01 17:52:58 +0000 | [diff] [blame] | 1330 | CharEnd = End.getLocWithOffset(1); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1331 | break; |
| 1332 | default: |
| 1333 | Diag(FilenameTok.getLocation(), diag::err_pp_expects_filename); |
| 1334 | DiscardUntilEndOfDirective(); |
| 1335 | return; |
| 1336 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1337 | |
Argyrios Kyrtzidis | f8afcff | 2012-09-29 01:06:10 +0000 | [diff] [blame] | 1338 | CharSourceRange FilenameRange |
| 1339 | = CharSourceRange::getCharRange(FilenameTok.getLocation(), CharEnd); |
Aaron Ballman | 4c55c54 | 2012-03-02 22:51:54 +0000 | [diff] [blame] | 1340 | StringRef OriginalFilename = Filename; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1341 | bool isAngled = |
Chris Lattner | a139481 | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 1342 | GetIncludeFilenameSpelling(FilenameTok.getLocation(), Filename); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1343 | // If GetIncludeFilenameSpelling set the start ptr to null, there was an |
| 1344 | // error. |
Chris Lattner | a139481 | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 1345 | if (Filename.empty()) { |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1346 | DiscardUntilEndOfDirective(); |
| 1347 | return; |
| 1348 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1349 | |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 1350 | // Verify that there is nothing after the filename, other than EOD. Note that |
Chris Lattner | 9cb51ce | 2009-04-17 23:56:52 +0000 | [diff] [blame] | 1351 | // we allow macros that expand to nothing after the filename, because this |
| 1352 | // falls into the category of "#include pp-tokens new-line" specified in |
| 1353 | // C99 6.10.2p4. |
Daniel Dunbar | e013d68 | 2009-10-18 20:26:12 +0000 | [diff] [blame] | 1354 | CheckEndOfDirective(IncludeTok.getIdentifierInfo()->getNameStart(), true); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1355 | |
| 1356 | // Check that we don't have infinite #include recursion. |
Chris Lattner | 3692b09 | 2008-11-18 07:59:24 +0000 | [diff] [blame] | 1357 | if (IncludeMacroStack.size() == MaxAllowedIncludeStackDepth-1) { |
| 1358 | Diag(FilenameTok, diag::err_pp_include_too_deep); |
| 1359 | return; |
| 1360 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1361 | |
John McCall | 8dfac0b | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 1362 | // Complain about attempts to #include files in an audit pragma. |
| 1363 | if (PragmaARCCFCodeAuditedLoc.isValid()) { |
| 1364 | Diag(HashLoc, diag::err_pp_include_in_arc_cf_code_audited); |
| 1365 | Diag(PragmaARCCFCodeAuditedLoc, diag::note_pragma_entered_here); |
| 1366 | |
| 1367 | // Immediately leave the pragma. |
| 1368 | PragmaARCCFCodeAuditedLoc = SourceLocation(); |
| 1369 | } |
| 1370 | |
Aaron Ballman | 4c55c54 | 2012-03-02 22:51:54 +0000 | [diff] [blame] | 1371 | if (HeaderInfo.HasIncludeAliasMap()) { |
| 1372 | // Map the filename with the brackets still attached. If the name doesn't |
| 1373 | // map to anything, fall back on the filename we've already gotten the |
| 1374 | // spelling for. |
| 1375 | StringRef NewName = HeaderInfo.MapHeaderToIncludeAlias(OriginalFilename); |
| 1376 | if (!NewName.empty()) |
| 1377 | Filename = NewName; |
| 1378 | } |
| 1379 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1380 | // Search include directories. |
| 1381 | const DirectoryLookup *CurDir; |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 1382 | SmallString<1024> SearchPath; |
| 1383 | SmallString<1024> RelativePath; |
Chandler Carruth | b5142bb | 2011-03-16 18:34:36 +0000 | [diff] [blame] | 1384 | // We get the raw path only if we have 'Callbacks' to which we later pass |
| 1385 | // the path. |
Douglas Gregor | 1a4761e | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 1386 | Module *SuggestedModule = 0; |
Chandler Carruth | b5142bb | 2011-03-16 18:34:36 +0000 | [diff] [blame] | 1387 | const FileEntry *File = LookupFile( |
Manuel Klimek | 7412494 | 2011-04-26 21:50:03 +0000 | [diff] [blame] | 1388 | Filename, isAngled, LookupFrom, CurDir, |
Douglas Gregor | fba18aa | 2011-09-15 22:00:41 +0000 | [diff] [blame] | 1389 | Callbacks ? &SearchPath : NULL, Callbacks ? &RelativePath : NULL, |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1390 | getLangOpts().Modules? &SuggestedModule : 0); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1391 | |
Douglas Gregor | 8cfbe6a | 2011-11-30 18:12:06 +0000 | [diff] [blame] | 1392 | if (Callbacks) { |
| 1393 | if (!File) { |
| 1394 | // Give the clients a chance to recover. |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 1395 | SmallString<128> RecoveryPath; |
Douglas Gregor | 8cfbe6a | 2011-11-30 18:12:06 +0000 | [diff] [blame] | 1396 | if (Callbacks->FileNotFound(Filename, RecoveryPath)) { |
| 1397 | if (const DirectoryEntry *DE = FileMgr.getDirectory(RecoveryPath)) { |
| 1398 | // Add the recovery path to the list of search paths. |
Daniel Dunbar | 1ea6bc0 | 2013-01-25 01:50:28 +0000 | [diff] [blame] | 1399 | DirectoryLookup DL(DE, SrcMgr::C_User, false); |
Douglas Gregor | 8cfbe6a | 2011-11-30 18:12:06 +0000 | [diff] [blame] | 1400 | HeaderInfo.AddSearchPath(DL, isAngled); |
| 1401 | |
| 1402 | // Try the lookup again, skipping the cache. |
| 1403 | File = LookupFile(Filename, isAngled, LookupFrom, CurDir, 0, 0, |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1404 | getLangOpts().Modules? &SuggestedModule : 0, |
Douglas Gregor | 8cfbe6a | 2011-11-30 18:12:06 +0000 | [diff] [blame] | 1405 | /*SkipCache*/true); |
| 1406 | } |
| 1407 | } |
| 1408 | } |
| 1409 | |
Argyrios Kyrtzidis | f8afcff | 2012-09-29 01:06:10 +0000 | [diff] [blame] | 1410 | if (!SuggestedModule) { |
| 1411 | // Notify the callback object that we've seen an inclusion directive. |
| 1412 | Callbacks->InclusionDirective(HashLoc, IncludeTok, Filename, isAngled, |
| 1413 | FilenameRange, File, |
| 1414 | SearchPath, RelativePath, |
| 1415 | /*ImportedModule=*/0); |
| 1416 | } |
Douglas Gregor | 8cfbe6a | 2011-11-30 18:12:06 +0000 | [diff] [blame] | 1417 | } |
| 1418 | |
| 1419 | if (File == 0) { |
Aaron Ballman | a52f5a3 | 2012-07-17 23:19:16 +0000 | [diff] [blame] | 1420 | if (!SuppressIncludeNotFoundError) { |
| 1421 | // If the file could not be located and it was included via angle |
| 1422 | // brackets, we can attempt a lookup as though it were a quoted path to |
| 1423 | // provide the user with a possible fixit. |
| 1424 | if (isAngled) { |
| 1425 | File = LookupFile(Filename, false, LookupFrom, CurDir, |
| 1426 | Callbacks ? &SearchPath : 0, |
| 1427 | Callbacks ? &RelativePath : 0, |
| 1428 | getLangOpts().Modules ? &SuggestedModule : 0); |
| 1429 | if (File) { |
| 1430 | SourceRange Range(FilenameTok.getLocation(), CharEnd); |
| 1431 | Diag(FilenameTok, diag::err_pp_file_not_found_not_fatal) << |
| 1432 | Filename << |
| 1433 | FixItHint::CreateReplacement(Range, "\"" + Filename.str() + "\""); |
| 1434 | } |
| 1435 | } |
| 1436 | // If the file is still not found, just go with the vanilla diagnostic |
| 1437 | if (!File) |
| 1438 | Diag(FilenameTok, diag::err_pp_file_not_found) << Filename; |
| 1439 | } |
| 1440 | if (!File) |
| 1441 | return; |
Douglas Gregor | 8cfbe6a | 2011-11-30 18:12:06 +0000 | [diff] [blame] | 1442 | } |
| 1443 | |
Douglas Gregor | fba18aa | 2011-09-15 22:00:41 +0000 | [diff] [blame] | 1444 | // If we are supposed to import a module rather than including the header, |
| 1445 | // do so now. |
Douglas Gregor | c69c42e | 2011-11-17 22:44:56 +0000 | [diff] [blame] | 1446 | if (SuggestedModule) { |
Douglas Gregor | 3d3589d | 2011-11-30 00:36:36 +0000 | [diff] [blame] | 1447 | // Compute the module access path corresponding to this module. |
| 1448 | // FIXME: Should we have a second loadModule() overload to avoid this |
| 1449 | // extra lookup step? |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 1450 | SmallVector<std::pair<IdentifierInfo *, SourceLocation>, 2> Path; |
Douglas Gregor | 1a4761e | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 1451 | for (Module *Mod = SuggestedModule; Mod; Mod = Mod->Parent) |
Douglas Gregor | 3d3589d | 2011-11-30 00:36:36 +0000 | [diff] [blame] | 1452 | Path.push_back(std::make_pair(getIdentifierInfo(Mod->Name), |
| 1453 | FilenameTok.getLocation())); |
| 1454 | std::reverse(Path.begin(), Path.end()); |
| 1455 | |
Douglas Gregor | e3a8256 | 2011-11-30 18:02:36 +0000 | [diff] [blame] | 1456 | // Warn that we're replacing the include/import with a module import. |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 1457 | SmallString<128> PathString; |
Douglas Gregor | e3a8256 | 2011-11-30 18:02:36 +0000 | [diff] [blame] | 1458 | for (unsigned I = 0, N = Path.size(); I != N; ++I) { |
| 1459 | if (I) |
| 1460 | PathString += '.'; |
| 1461 | PathString += Path[I].first->getName(); |
| 1462 | } |
| 1463 | int IncludeKind = 0; |
| 1464 | |
| 1465 | switch (IncludeTok.getIdentifierInfo()->getPPKeywordID()) { |
| 1466 | case tok::pp_include: |
| 1467 | IncludeKind = 0; |
| 1468 | break; |
| 1469 | |
| 1470 | case tok::pp_import: |
| 1471 | IncludeKind = 1; |
| 1472 | break; |
| 1473 | |
Douglas Gregor | edee969 | 2011-11-30 18:03:26 +0000 | [diff] [blame] | 1474 | case tok::pp_include_next: |
| 1475 | IncludeKind = 2; |
| 1476 | break; |
Douglas Gregor | e3a8256 | 2011-11-30 18:02:36 +0000 | [diff] [blame] | 1477 | |
| 1478 | case tok::pp___include_macros: |
| 1479 | IncludeKind = 3; |
| 1480 | break; |
| 1481 | |
| 1482 | default: |
| 1483 | llvm_unreachable("unknown include directive kind"); |
Douglas Gregor | e3a8256 | 2011-11-30 18:02:36 +0000 | [diff] [blame] | 1484 | } |
| 1485 | |
Douglas Gregor | 5e3f922 | 2011-12-08 17:01:29 +0000 | [diff] [blame] | 1486 | // Determine whether we are actually building the module that this |
| 1487 | // include directive maps to. |
| 1488 | bool BuildingImportedModule |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1489 | = Path[0].first->getName() == getLangOpts().CurrentModule; |
Douglas Gregor | 5e3f922 | 2011-12-08 17:01:29 +0000 | [diff] [blame] | 1490 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1491 | if (!BuildingImportedModule && getLangOpts().ObjC2) { |
Douglas Gregor | 5e3f922 | 2011-12-08 17:01:29 +0000 | [diff] [blame] | 1492 | // If we're not building the imported module, warn that we're going |
| 1493 | // to automatically turn this inclusion directive into a module import. |
Douglas Gregor | c13a34b | 2012-01-03 19:32:59 +0000 | [diff] [blame] | 1494 | // We only do this in Objective-C, where we have a module-import syntax. |
Douglas Gregor | 5e3f922 | 2011-12-08 17:01:29 +0000 | [diff] [blame] | 1495 | CharSourceRange ReplaceRange(SourceRange(HashLoc, CharEnd), |
| 1496 | /*IsTokenRange=*/false); |
| 1497 | Diag(HashLoc, diag::warn_auto_module_import) |
| 1498 | << IncludeKind << PathString |
| 1499 | << FixItHint::CreateReplacement(ReplaceRange, |
Douglas Gregor | 1b257af | 2012-12-11 22:11:52 +0000 | [diff] [blame] | 1500 | "@import " + PathString.str().str() + ";"); |
Douglas Gregor | 5e3f922 | 2011-12-08 17:01:29 +0000 | [diff] [blame] | 1501 | } |
Douglas Gregor | e3a8256 | 2011-11-30 18:02:36 +0000 | [diff] [blame] | 1502 | |
Douglas Gregor | 3d3589d | 2011-11-30 00:36:36 +0000 | [diff] [blame] | 1503 | // Load the module. |
Douglas Gregor | 5e35693 | 2011-12-01 17:11:21 +0000 | [diff] [blame] | 1504 | // If this was an #__include_macros directive, only make macros visible. |
| 1505 | Module::NameVisibilityKind Visibility |
| 1506 | = (IncludeKind == 3)? Module::MacrosVisible : Module::AllVisible; |
Douglas Gregor | 463d909 | 2012-11-29 23:55:25 +0000 | [diff] [blame] | 1507 | ModuleLoadResult Imported |
Douglas Gregor | 305dc3e | 2011-12-20 00:28:52 +0000 | [diff] [blame] | 1508 | = TheModuleLoader.loadModule(IncludeTok.getLocation(), Path, Visibility, |
| 1509 | /*IsIncludeDirective=*/true); |
Argyrios Kyrtzidis | eb788e9 | 2012-09-29 01:06:01 +0000 | [diff] [blame] | 1510 | assert((Imported == 0 || Imported == SuggestedModule) && |
| 1511 | "the imported module is different than the suggested one"); |
Douglas Gregor | 5e3f922 | 2011-12-08 17:01:29 +0000 | [diff] [blame] | 1512 | |
| 1513 | // If this header isn't part of the module we're building, we're done. |
Argyrios Kyrtzidis | f8afcff | 2012-09-29 01:06:10 +0000 | [diff] [blame] | 1514 | if (!BuildingImportedModule && Imported) { |
| 1515 | if (Callbacks) { |
| 1516 | Callbacks->InclusionDirective(HashLoc, IncludeTok, Filename, isAngled, |
| 1517 | FilenameRange, File, |
| 1518 | SearchPath, RelativePath, Imported); |
| 1519 | } |
Douglas Gregor | 5e3f922 | 2011-12-08 17:01:29 +0000 | [diff] [blame] | 1520 | return; |
Argyrios Kyrtzidis | f8afcff | 2012-09-29 01:06:10 +0000 | [diff] [blame] | 1521 | } |
Douglas Gregor | 463d909 | 2012-11-29 23:55:25 +0000 | [diff] [blame] | 1522 | |
| 1523 | // If we failed to find a submodule that we expected to find, we can |
| 1524 | // continue. Otherwise, there's an error in the included file, so we |
| 1525 | // don't want to include it. |
| 1526 | if (!BuildingImportedModule && !Imported.isMissingExpected()) { |
| 1527 | return; |
| 1528 | } |
Argyrios Kyrtzidis | f8afcff | 2012-09-29 01:06:10 +0000 | [diff] [blame] | 1529 | } |
| 1530 | |
| 1531 | if (Callbacks && SuggestedModule) { |
| 1532 | // We didn't notify the callback object that we've seen an inclusion |
| 1533 | // directive before. Now that we are parsing the include normally and not |
| 1534 | // turning it to a module import, notify the callback object. |
| 1535 | Callbacks->InclusionDirective(HashLoc, IncludeTok, Filename, isAngled, |
| 1536 | FilenameRange, File, |
| 1537 | SearchPath, RelativePath, |
| 1538 | /*ImportedModule=*/0); |
Douglas Gregor | fba18aa | 2011-09-15 22:00:41 +0000 | [diff] [blame] | 1539 | } |
| 1540 | |
Chris Lattner | 7218183 | 2008-09-26 20:12:23 +0000 | [diff] [blame] | 1541 | // The #included file will be considered to be a system header if either it is |
| 1542 | // in a system include directory, or if the #includer is a system include |
| 1543 | // header. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1544 | SrcMgr::CharacteristicKind FileCharacter = |
Chris Lattner | 0b9e736 | 2008-09-26 21:18:42 +0000 | [diff] [blame] | 1545 | std::max(HeaderInfo.getFileDirFlavor(File), |
Chris Lattner | 693faa6 | 2009-01-19 07:59:15 +0000 | [diff] [blame] | 1546 | SourceMgr.getFileCharacteristic(FilenameTok.getLocation())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1547 | |
Chris Lattner | 6fbe3eb | 2010-04-19 20:44:31 +0000 | [diff] [blame] | 1548 | // Ask HeaderInfo if we should enter this #include file. If not, #including |
| 1549 | // this file will have no effect. |
| 1550 | if (!HeaderInfo.ShouldEnterIncludeFile(File, isImport)) { |
Chris Lattner | e127a0d | 2010-04-20 20:35:58 +0000 | [diff] [blame] | 1551 | if (Callbacks) |
Chris Lattner | 6fbe3eb | 2010-04-19 20:44:31 +0000 | [diff] [blame] | 1552 | Callbacks->FileSkipped(*File, FilenameTok, FileCharacter); |
Chris Lattner | 6fbe3eb | 2010-04-19 20:44:31 +0000 | [diff] [blame] | 1553 | return; |
| 1554 | } |
| 1555 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1556 | // Look up the file, create a File ID for it. |
Argyrios Kyrtzidis | db81d38 | 2012-03-27 18:47:48 +0000 | [diff] [blame] | 1557 | SourceLocation IncludePos = End; |
| 1558 | // If the filename string was the result of macro expansions, set the include |
| 1559 | // position on the file where it will be included and after the expansions. |
| 1560 | if (IncludePos.isMacroID()) |
| 1561 | IncludePos = SourceMgr.getExpansionRange(IncludePos).second; |
| 1562 | FileID FID = SourceMgr.createFileID(File, IncludePos, FileCharacter); |
Peter Collingbourne | d57b7ff | 2011-06-30 16:41:03 +0000 | [diff] [blame] | 1563 | assert(!FID.isInvalid() && "Expected valid file ID"); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1564 | |
| 1565 | // Finally, if all is good, enter the new file! |
Chris Lattner | e127a0d | 2010-04-20 20:35:58 +0000 | [diff] [blame] | 1566 | EnterSourceFile(FID, CurDir, FilenameTok.getLocation()); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1567 | } |
| 1568 | |
James Dennett | dc20169 | 2012-06-22 05:46:07 +0000 | [diff] [blame] | 1569 | /// HandleIncludeNextDirective - Implements \#include_next. |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1570 | /// |
Douglas Gregor | ecdcb88 | 2010-10-20 22:00:55 +0000 | [diff] [blame] | 1571 | void Preprocessor::HandleIncludeNextDirective(SourceLocation HashLoc, |
| 1572 | Token &IncludeNextTok) { |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1573 | Diag(IncludeNextTok, diag::ext_pp_include_next_directive); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1574 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1575 | // #include_next is like #include, except that we start searching after |
| 1576 | // the current found directory. If we can't do this, issue a |
| 1577 | // diagnostic. |
| 1578 | const DirectoryLookup *Lookup = CurDirLookup; |
| 1579 | if (isInPrimaryFile()) { |
| 1580 | Lookup = 0; |
| 1581 | Diag(IncludeNextTok, diag::pp_include_next_in_primary); |
| 1582 | } else if (Lookup == 0) { |
| 1583 | Diag(IncludeNextTok, diag::pp_include_next_absolute_path); |
| 1584 | } else { |
| 1585 | // Start looking up in the next directory. |
| 1586 | ++Lookup; |
| 1587 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1588 | |
Douglas Gregor | ecdcb88 | 2010-10-20 22:00:55 +0000 | [diff] [blame] | 1589 | return HandleIncludeDirective(HashLoc, IncludeNextTok, Lookup); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1590 | } |
| 1591 | |
James Dennett | dc20169 | 2012-06-22 05:46:07 +0000 | [diff] [blame] | 1592 | /// HandleMicrosoftImportDirective - Implements \#import for Microsoft Mode |
Aaron Ballman | 4207eda | 2012-03-18 03:10:37 +0000 | [diff] [blame] | 1593 | void Preprocessor::HandleMicrosoftImportDirective(Token &Tok) { |
| 1594 | // The Microsoft #import directive takes a type library and generates header |
| 1595 | // files from it, and includes those. This is beyond the scope of what clang |
| 1596 | // does, so we ignore it and error out. However, #import can optionally have |
| 1597 | // trailing attributes that span multiple lines. We're going to eat those |
| 1598 | // so we can continue processing from there. |
| 1599 | Diag(Tok, diag::err_pp_import_directive_ms ); |
| 1600 | |
| 1601 | // Read tokens until we get to the end of the directive. Note that the |
| 1602 | // directive can be split over multiple lines using the backslash character. |
| 1603 | DiscardUntilEndOfDirective(); |
| 1604 | } |
| 1605 | |
James Dennett | dc20169 | 2012-06-22 05:46:07 +0000 | [diff] [blame] | 1606 | /// HandleImportDirective - Implements \#import. |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1607 | /// |
Douglas Gregor | ecdcb88 | 2010-10-20 22:00:55 +0000 | [diff] [blame] | 1608 | void Preprocessor::HandleImportDirective(SourceLocation HashLoc, |
| 1609 | Token &ImportTok) { |
Aaron Ballman | 4207eda | 2012-03-18 03:10:37 +0000 | [diff] [blame] | 1610 | if (!LangOpts.ObjC1) { // #import is standard for ObjC. |
| 1611 | if (LangOpts.MicrosoftMode) |
| 1612 | return HandleMicrosoftImportDirective(ImportTok); |
Chris Lattner | b627c8d | 2009-03-06 04:28:03 +0000 | [diff] [blame] | 1613 | Diag(ImportTok, diag::ext_pp_import_directive); |
Aaron Ballman | 4207eda | 2012-03-18 03:10:37 +0000 | [diff] [blame] | 1614 | } |
Douglas Gregor | ecdcb88 | 2010-10-20 22:00:55 +0000 | [diff] [blame] | 1615 | return HandleIncludeDirective(HashLoc, ImportTok, 0, true); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1616 | } |
| 1617 | |
Chris Lattner | de07665 | 2009-04-08 18:46:40 +0000 | [diff] [blame] | 1618 | /// HandleIncludeMacrosDirective - The -imacros command line option turns into a |
| 1619 | /// pseudo directive in the predefines buffer. This handles it by sucking all |
| 1620 | /// tokens through the preprocessor and discarding them (only keeping the side |
| 1621 | /// effects on the preprocessor). |
Douglas Gregor | ecdcb88 | 2010-10-20 22:00:55 +0000 | [diff] [blame] | 1622 | void Preprocessor::HandleIncludeMacrosDirective(SourceLocation HashLoc, |
| 1623 | Token &IncludeMacrosTok) { |
Chris Lattner | de07665 | 2009-04-08 18:46:40 +0000 | [diff] [blame] | 1624 | // This directive should only occur in the predefines buffer. If not, emit an |
| 1625 | // error and reject it. |
| 1626 | SourceLocation Loc = IncludeMacrosTok.getLocation(); |
| 1627 | if (strcmp(SourceMgr.getBufferName(Loc), "<built-in>") != 0) { |
| 1628 | Diag(IncludeMacrosTok.getLocation(), |
| 1629 | diag::pp_include_macros_out_of_predefines); |
| 1630 | DiscardUntilEndOfDirective(); |
| 1631 | return; |
| 1632 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1633 | |
Chris Lattner | fd10511 | 2009-04-08 20:53:24 +0000 | [diff] [blame] | 1634 | // Treat this as a normal #include for checking purposes. If this is |
| 1635 | // successful, it will push a new lexer onto the include stack. |
Douglas Gregor | ecdcb88 | 2010-10-20 22:00:55 +0000 | [diff] [blame] | 1636 | HandleIncludeDirective(HashLoc, IncludeMacrosTok, 0, false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1637 | |
Chris Lattner | fd10511 | 2009-04-08 20:53:24 +0000 | [diff] [blame] | 1638 | Token TmpTok; |
| 1639 | do { |
| 1640 | Lex(TmpTok); |
| 1641 | assert(TmpTok.isNot(tok::eof) && "Didn't find end of -imacros!"); |
| 1642 | } while (TmpTok.isNot(tok::hashhash)); |
Chris Lattner | de07665 | 2009-04-08 18:46:40 +0000 | [diff] [blame] | 1643 | } |
| 1644 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1645 | //===----------------------------------------------------------------------===// |
| 1646 | // Preprocessor Macro Directive Handling. |
| 1647 | //===----------------------------------------------------------------------===// |
| 1648 | |
| 1649 | /// ReadMacroDefinitionArgList - The ( starting an argument list of a macro |
| 1650 | /// definition has just been read. Lex the rest of the arguments and the |
| 1651 | /// closing ), updating MI with what we learn. Return true if an error occurs |
| 1652 | /// parsing the arg list. |
Abramo Bagnara | e2e8768 | 2012-03-31 20:17:27 +0000 | [diff] [blame] | 1653 | bool Preprocessor::ReadMacroDefinitionArgList(MacroInfo *MI, Token &Tok) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1654 | SmallVector<IdentifierInfo*, 32> Arguments; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1655 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1656 | while (1) { |
| 1657 | LexUnexpandedToken(Tok); |
| 1658 | switch (Tok.getKind()) { |
| 1659 | case tok::r_paren: |
| 1660 | // Found the end of the argument list. |
Chris Lattner | cf29e07 | 2009-02-20 22:31:31 +0000 | [diff] [blame] | 1661 | if (Arguments.empty()) // #define FOO() |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1662 | return false; |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1663 | // Otherwise we have #define FOO(A,) |
| 1664 | Diag(Tok, diag::err_pp_expected_ident_in_arg_list); |
| 1665 | return true; |
| 1666 | case tok::ellipsis: // #define X(... -> C99 varargs |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1667 | if (!LangOpts.C99) |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 1668 | Diag(Tok, LangOpts.CPlusPlus11 ? |
Richard Smith | 661a996 | 2011-10-15 01:18:56 +0000 | [diff] [blame] | 1669 | diag::warn_cxx98_compat_variadic_macro : |
| 1670 | diag::ext_variadic_macro); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1671 | |
Joey Gouly | 617bb31 | 2013-01-17 17:35:00 +0000 | [diff] [blame] | 1672 | // OpenCL v1.2 s6.9.e: variadic macros are not supported. |
| 1673 | if (LangOpts.OpenCL) { |
| 1674 | Diag(Tok, diag::err_pp_opencl_variadic_macros); |
| 1675 | return true; |
| 1676 | } |
| 1677 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1678 | // Lex the token after the identifier. |
| 1679 | LexUnexpandedToken(Tok); |
| 1680 | if (Tok.isNot(tok::r_paren)) { |
| 1681 | Diag(Tok, diag::err_pp_missing_rparen_in_macro_def); |
| 1682 | return true; |
| 1683 | } |
| 1684 | // Add the __VA_ARGS__ identifier as an argument. |
| 1685 | Arguments.push_back(Ident__VA_ARGS__); |
| 1686 | MI->setIsC99Varargs(); |
Chris Lattner | 685befe | 2009-02-20 22:46:43 +0000 | [diff] [blame] | 1687 | MI->setArgumentList(&Arguments[0], Arguments.size(), BP); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1688 | return false; |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 1689 | case tok::eod: // #define X( |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1690 | Diag(Tok, diag::err_pp_missing_rparen_in_macro_def); |
| 1691 | return true; |
| 1692 | default: |
| 1693 | // Handle keywords and identifiers here to accept things like |
| 1694 | // #define Foo(for) for. |
| 1695 | IdentifierInfo *II = Tok.getIdentifierInfo(); |
| 1696 | if (II == 0) { |
| 1697 | // #define X(1 |
| 1698 | Diag(Tok, diag::err_pp_invalid_tok_in_arg_list); |
| 1699 | return true; |
| 1700 | } |
| 1701 | |
| 1702 | // If this is already used as an argument, it is used multiple times (e.g. |
| 1703 | // #define X(A,A. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1704 | if (std::find(Arguments.begin(), Arguments.end(), II) != |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1705 | Arguments.end()) { // C99 6.10.3p6 |
Chris Lattner | 6cf3ed7 | 2008-11-19 07:33:58 +0000 | [diff] [blame] | 1706 | Diag(Tok, diag::err_pp_duplicate_name_in_arg_list) << II; |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1707 | return true; |
| 1708 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1709 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1710 | // Add the argument to the macro info. |
| 1711 | Arguments.push_back(II); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1712 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1713 | // Lex the token after the identifier. |
| 1714 | LexUnexpandedToken(Tok); |
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 | switch (Tok.getKind()) { |
| 1717 | default: // #define X(A B |
| 1718 | Diag(Tok, diag::err_pp_expected_comma_in_arg_list); |
| 1719 | return true; |
| 1720 | case tok::r_paren: // #define X(A) |
Chris Lattner | 685befe | 2009-02-20 22:46:43 +0000 | [diff] [blame] | 1721 | MI->setArgumentList(&Arguments[0], Arguments.size(), BP); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1722 | return false; |
| 1723 | case tok::comma: // #define X(A, |
| 1724 | break; |
| 1725 | case tok::ellipsis: // #define X(A... -> GCC extension |
| 1726 | // Diagnose extension. |
| 1727 | Diag(Tok, diag::ext_named_variadic_macro); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1728 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1729 | // Lex the token after the identifier. |
| 1730 | LexUnexpandedToken(Tok); |
| 1731 | if (Tok.isNot(tok::r_paren)) { |
| 1732 | Diag(Tok, diag::err_pp_missing_rparen_in_macro_def); |
| 1733 | return true; |
| 1734 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1735 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1736 | MI->setIsGNUVarargs(); |
Chris Lattner | 685befe | 2009-02-20 22:46:43 +0000 | [diff] [blame] | 1737 | MI->setArgumentList(&Arguments[0], Arguments.size(), BP); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1738 | return false; |
| 1739 | } |
| 1740 | } |
| 1741 | } |
| 1742 | } |
| 1743 | |
James Dennett | dc20169 | 2012-06-22 05:46:07 +0000 | [diff] [blame] | 1744 | /// HandleDefineDirective - Implements \#define. This consumes the entire macro |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1745 | /// line then lets the caller lex the next real token. |
| 1746 | void Preprocessor::HandleDefineDirective(Token &DefineTok) { |
| 1747 | ++NumDefined; |
| 1748 | |
| 1749 | Token MacroNameTok; |
| 1750 | ReadMacroName(MacroNameTok, 1); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1751 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1752 | // Error reading macro name? If so, diagnostic already issued. |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 1753 | if (MacroNameTok.is(tok::eod)) |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1754 | return; |
| 1755 | |
Chris Lattner | 2451b52 | 2009-04-21 04:46:33 +0000 | [diff] [blame] | 1756 | Token LastTok = MacroNameTok; |
| 1757 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1758 | // If we are supposed to keep comments in #defines, reenable comment saving |
| 1759 | // mode. |
Ted Kremenek | ac6b06d | 2008-11-18 00:43:07 +0000 | [diff] [blame] | 1760 | if (CurLexer) CurLexer->SetCommentRetentionState(KeepMacroComments); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1761 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1762 | // Create the new macro. |
Ted Kremenek | 0ea7672 | 2008-12-15 19:56:42 +0000 | [diff] [blame] | 1763 | MacroInfo *MI = AllocateMacroInfo(MacroNameTok.getLocation()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1764 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1765 | Token Tok; |
| 1766 | LexUnexpandedToken(Tok); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1767 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1768 | // If this is a function-like macro definition, parse the argument list, |
| 1769 | // marking each of the identifiers as being used as macro arguments. Also, |
| 1770 | // check other constraints on the first token of the macro body. |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 1771 | if (Tok.is(tok::eod)) { |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1772 | // 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] | 1773 | } else if (Tok.hasLeadingSpace()) { |
| 1774 | // This is a normal token with leading space. Clear the leading space |
| 1775 | // marker on the first token to get proper expansion. |
| 1776 | Tok.clearFlag(Token::LeadingSpace); |
| 1777 | } else if (Tok.is(tok::l_paren)) { |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1778 | // This is a function-like macro definition. Read the argument list. |
| 1779 | MI->setIsFunctionLike(); |
Abramo Bagnara | e2e8768 | 2012-03-31 20:17:27 +0000 | [diff] [blame] | 1780 | if (ReadMacroDefinitionArgList(MI, LastTok)) { |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1781 | // Forget about MI. |
Ted Kremenek | 0ea7672 | 2008-12-15 19:56:42 +0000 | [diff] [blame] | 1782 | ReleaseMacroInfo(MI); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1783 | // Throw away the rest of the line. |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 1784 | if (CurPPLexer->ParsingPreprocessorDirective) |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1785 | DiscardUntilEndOfDirective(); |
| 1786 | return; |
| 1787 | } |
| 1788 | |
Chris Lattner | 8fde597 | 2009-04-19 18:26:34 +0000 | [diff] [blame] | 1789 | // If this is a definition of a variadic C99 function-like macro, not using |
| 1790 | // the GNU named varargs extension, enabled __VA_ARGS__. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1791 | |
Chris Lattner | 8fde597 | 2009-04-19 18:26:34 +0000 | [diff] [blame] | 1792 | // "Poison" __VA_ARGS__, which can only appear in the expansion of a macro. |
| 1793 | // This gets unpoisoned where it is allowed. |
| 1794 | assert(Ident__VA_ARGS__->isPoisoned() && "__VA_ARGS__ should be poisoned!"); |
| 1795 | if (MI->isC99Varargs()) |
| 1796 | Ident__VA_ARGS__->setIsPoisoned(false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1797 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1798 | // Read the first token after the arg list for down below. |
| 1799 | LexUnexpandedToken(Tok); |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 1800 | } else if (LangOpts.C99 || LangOpts.CPlusPlus11) { |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1801 | // C99 requires whitespace between the macro definition and the body. Emit |
| 1802 | // a diagnostic for something like "#define X+". |
Chris Lattner | 6272bcf | 2009-04-18 02:23:25 +0000 | [diff] [blame] | 1803 | Diag(Tok, diag::ext_c99_whitespace_required_after_macro_name); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1804 | } else { |
Chris Lattner | 6272bcf | 2009-04-18 02:23:25 +0000 | [diff] [blame] | 1805 | // C90 6.8 TC1 says: "In the definition of an object-like macro, if the |
| 1806 | // first character of a replacement list is not a character required by |
| 1807 | // subclause 5.2.1, then there shall be white-space separation between the |
| 1808 | // identifier and the replacement list.". 5.2.1 lists this set: |
| 1809 | // "A-Za-z0-9!"#%&'()*+,_./:;<=>?[\]^_{|}~" as well as whitespace, which |
| 1810 | // is irrelevant here. |
| 1811 | bool isInvalid = false; |
| 1812 | if (Tok.is(tok::at)) // @ is not in the list above. |
| 1813 | isInvalid = true; |
| 1814 | else if (Tok.is(tok::unknown)) { |
| 1815 | // If we have an unknown token, it is something strange like "`". Since |
| 1816 | // all of valid characters would have lexed into a single character |
| 1817 | // token of some sort, we know this is not a valid case. |
| 1818 | isInvalid = true; |
| 1819 | } |
| 1820 | if (isInvalid) |
| 1821 | Diag(Tok, diag::ext_missing_whitespace_after_macro_name); |
| 1822 | else |
| 1823 | Diag(Tok, diag::warn_missing_whitespace_after_macro_name); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1824 | } |
Chris Lattner | 2451b52 | 2009-04-21 04:46:33 +0000 | [diff] [blame] | 1825 | |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 1826 | if (!Tok.is(tok::eod)) |
Chris Lattner | 2451b52 | 2009-04-21 04:46:33 +0000 | [diff] [blame] | 1827 | LastTok = Tok; |
| 1828 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1829 | // Read the rest of the macro body. |
| 1830 | if (MI->isObjectLike()) { |
| 1831 | // Object-like macros are very simple, just read their body. |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 1832 | while (Tok.isNot(tok::eod)) { |
Chris Lattner | 2451b52 | 2009-04-21 04:46:33 +0000 | [diff] [blame] | 1833 | LastTok = Tok; |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1834 | MI->AddTokenToBody(Tok); |
| 1835 | // Get the next token of the macro. |
| 1836 | LexUnexpandedToken(Tok); |
| 1837 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1838 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1839 | } else { |
Chris Lattner | 3240469 | 2009-05-25 17:16:10 +0000 | [diff] [blame] | 1840 | // Otherwise, read the body of a function-like macro. While we are at it, |
| 1841 | // check C99 6.10.3.2p1: ensure that # operators are followed by macro |
| 1842 | // parameters in function-like macro expansions. |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 1843 | while (Tok.isNot(tok::eod)) { |
Chris Lattner | 2451b52 | 2009-04-21 04:46:33 +0000 | [diff] [blame] | 1844 | LastTok = Tok; |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1845 | |
Eli Friedman | 4fa4b48 | 2012-11-14 02:18:46 +0000 | [diff] [blame] | 1846 | if (Tok.isNot(tok::hash) && Tok.isNot(tok::hashhash)) { |
Chris Lattner | 3240469 | 2009-05-25 17:16:10 +0000 | [diff] [blame] | 1847 | MI->AddTokenToBody(Tok); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1848 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1849 | // Get the next token of the macro. |
| 1850 | LexUnexpandedToken(Tok); |
| 1851 | continue; |
| 1852 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1853 | |
Eli Friedman | 4fa4b48 | 2012-11-14 02:18:46 +0000 | [diff] [blame] | 1854 | if (Tok.is(tok::hashhash)) { |
| 1855 | |
| 1856 | // If we see token pasting, check if it looks like the gcc comma |
| 1857 | // pasting extension. We'll use this information to suppress |
| 1858 | // diagnostics later on. |
| 1859 | |
| 1860 | // Get the next token of the macro. |
| 1861 | LexUnexpandedToken(Tok); |
| 1862 | |
| 1863 | if (Tok.is(tok::eod)) { |
| 1864 | MI->AddTokenToBody(LastTok); |
| 1865 | break; |
| 1866 | } |
| 1867 | |
| 1868 | unsigned NumTokens = MI->getNumTokens(); |
| 1869 | if (NumTokens && Tok.getIdentifierInfo() == Ident__VA_ARGS__ && |
| 1870 | MI->getReplacementToken(NumTokens-1).is(tok::comma)) |
| 1871 | MI->setHasCommaPasting(); |
| 1872 | |
| 1873 | // Things look ok, add the '##' and param name tokens to the macro. |
| 1874 | MI->AddTokenToBody(LastTok); |
| 1875 | MI->AddTokenToBody(Tok); |
| 1876 | LastTok = Tok; |
| 1877 | |
| 1878 | // Get the next token of the macro. |
| 1879 | LexUnexpandedToken(Tok); |
| 1880 | continue; |
| 1881 | } |
| 1882 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1883 | // Get the next token of the macro. |
| 1884 | LexUnexpandedToken(Tok); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1885 | |
Chris Lattner | 3240469 | 2009-05-25 17:16:10 +0000 | [diff] [blame] | 1886 | // Check for a valid macro arg identifier. |
| 1887 | if (Tok.getIdentifierInfo() == 0 || |
| 1888 | MI->getArgumentNum(Tok.getIdentifierInfo()) == -1) { |
| 1889 | |
| 1890 | // If this is assembler-with-cpp mode, we accept random gibberish after |
| 1891 | // the '#' because '#' is often a comment character. However, change |
| 1892 | // the kind of the token to tok::unknown so that the preprocessor isn't |
| 1893 | // confused. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1894 | if (getLangOpts().AsmPreprocessor && Tok.isNot(tok::eod)) { |
Chris Lattner | 3240469 | 2009-05-25 17:16:10 +0000 | [diff] [blame] | 1895 | LastTok.setKind(tok::unknown); |
| 1896 | } else { |
| 1897 | Diag(Tok, diag::err_pp_stringize_not_parameter); |
| 1898 | ReleaseMacroInfo(MI); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1899 | |
Chris Lattner | 3240469 | 2009-05-25 17:16:10 +0000 | [diff] [blame] | 1900 | // Disable __VA_ARGS__ again. |
| 1901 | Ident__VA_ARGS__->setIsPoisoned(true); |
| 1902 | return; |
| 1903 | } |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1904 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1905 | |
Chris Lattner | 3240469 | 2009-05-25 17:16:10 +0000 | [diff] [blame] | 1906 | // Things look ok, add the '#' and param name tokens to the macro. |
| 1907 | MI->AddTokenToBody(LastTok); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1908 | MI->AddTokenToBody(Tok); |
Chris Lattner | 3240469 | 2009-05-25 17:16:10 +0000 | [diff] [blame] | 1909 | LastTok = Tok; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1910 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1911 | // Get the next token of the macro. |
| 1912 | LexUnexpandedToken(Tok); |
| 1913 | } |
| 1914 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1915 | |
| 1916 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1917 | // Disable __VA_ARGS__ again. |
| 1918 | Ident__VA_ARGS__->setIsPoisoned(true); |
| 1919 | |
Chris Lattner | fc8f0e1 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 1920 | // Check that there is no paste (##) operator at the beginning or end of the |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1921 | // replacement list. |
| 1922 | unsigned NumTokens = MI->getNumTokens(); |
| 1923 | if (NumTokens != 0) { |
| 1924 | if (MI->getReplacementToken(0).is(tok::hashhash)) { |
| 1925 | Diag(MI->getReplacementToken(0), diag::err_paste_at_start); |
Ted Kremenek | 0ea7672 | 2008-12-15 19:56:42 +0000 | [diff] [blame] | 1926 | ReleaseMacroInfo(MI); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1927 | return; |
| 1928 | } |
| 1929 | if (MI->getReplacementToken(NumTokens-1).is(tok::hashhash)) { |
| 1930 | Diag(MI->getReplacementToken(NumTokens-1), diag::err_paste_at_end); |
Ted Kremenek | 0ea7672 | 2008-12-15 19:56:42 +0000 | [diff] [blame] | 1931 | ReleaseMacroInfo(MI); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1932 | return; |
| 1933 | } |
| 1934 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1935 | |
Chris Lattner | 2451b52 | 2009-04-21 04:46:33 +0000 | [diff] [blame] | 1936 | MI->setDefinitionEndLoc(LastTok.getLocation()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1937 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1938 | // Finally, if this identifier already had a macro defined for it, verify that |
Alexander Kornienko | 8a64bb5 | 2012-08-29 00:20:03 +0000 | [diff] [blame] | 1939 | // the macro bodies are identical, and issue diagnostics if they are not. |
Argyrios Kyrtzidis | 9818a1d | 2013-02-20 00:54:57 +0000 | [diff] [blame] | 1940 | if (const MacroInfo *OtherMI=getMacroInfo(MacroNameTok.getIdentifierInfo())) { |
Chris Lattner | 41c3ae1 | 2009-01-16 19:50:11 +0000 | [diff] [blame] | 1941 | // It is very common for system headers to have tons of macro redefinitions |
| 1942 | // and for warnings to be disabled in system headers. If this is the case, |
| 1943 | // then don't bother calling MacroInfo::isIdenticalTo. |
Chris Lattner | 7f549df | 2009-03-13 21:17:23 +0000 | [diff] [blame] | 1944 | if (!getDiagnostics().getSuppressSystemWarnings() || |
Chris Lattner | 41c3ae1 | 2009-01-16 19:50:11 +0000 | [diff] [blame] | 1945 | !SourceMgr.isInSystemHeader(DefineTok.getLocation())) { |
Argyrios Kyrtzidis | a33e050 | 2011-01-18 19:50:15 +0000 | [diff] [blame] | 1946 | if (!OtherMI->isUsed() && OtherMI->isWarnIfUnused()) |
Chris Lattner | 41c3ae1 | 2009-01-16 19:50:11 +0000 | [diff] [blame] | 1947 | Diag(OtherMI->getDefinitionLoc(), diag::pp_macro_not_used); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1948 | |
Richard Smith | eed55e6 | 2013-03-06 00:46:00 +0000 | [diff] [blame] | 1949 | // Warn if defining "__LINE__" and other builtins, per C99 6.10.8/4 and |
| 1950 | // C++ [cpp.predefined]p4, but allow it as an extension. |
| 1951 | if (OtherMI->isBuiltinMacro()) |
| 1952 | Diag(MacroNameTok, diag::ext_pp_redef_builtin_macro); |
Chris Lattner | f47724b | 2010-08-17 15:55:45 +0000 | [diff] [blame] | 1953 | // Macros must be identical. This means all tokens and whitespace |
Argyrios Kyrtzidis | bd25ff8 | 2013-04-03 17:39:30 +0000 | [diff] [blame] | 1954 | // separation must be the same. C99 6.10.3p2. |
Richard Smith | eed55e6 | 2013-03-06 00:46:00 +0000 | [diff] [blame] | 1955 | else if (!OtherMI->isAllowRedefinitionsWithoutWarning() && |
Argyrios Kyrtzidis | bd25ff8 | 2013-04-03 17:39:30 +0000 | [diff] [blame] | 1956 | !MI->isIdenticalTo(*OtherMI, *this, /*Syntactic=*/LangOpts.MicrosoftExt)) { |
Chris Lattner | 41c3ae1 | 2009-01-16 19:50:11 +0000 | [diff] [blame] | 1957 | Diag(MI->getDefinitionLoc(), diag::ext_pp_macro_redef) |
| 1958 | << MacroNameTok.getIdentifierInfo(); |
| 1959 | Diag(OtherMI->getDefinitionLoc(), diag::note_previous_definition); |
| 1960 | } |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1961 | } |
Argyrios Kyrtzidis | a33e050 | 2011-01-18 19:50:15 +0000 | [diff] [blame] | 1962 | if (OtherMI->isWarnIfUnused()) |
| 1963 | WarnUnusedMacroLocs.erase(OtherMI->getDefinitionLoc()); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1964 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1965 | |
Argyrios Kyrtzidis | c56fff7 | 2013-03-26 17:17:01 +0000 | [diff] [blame] | 1966 | DefMacroDirective *MD = |
| 1967 | appendDefMacroDirective(MacroNameTok.getIdentifierInfo(), MI); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1968 | |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 1969 | assert(!MI->isUsed()); |
| 1970 | // If we need warning for not using the macro, add its location in the |
| 1971 | // warn-because-unused-macro set. If it gets used it will be removed from set. |
| 1972 | if (isInPrimaryFile() && // don't warn for include'd macros. |
| 1973 | Diags->getDiagnosticLevel(diag::pp_macro_not_used, |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 1974 | MI->getDefinitionLoc()) != DiagnosticsEngine::Ignored) { |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 1975 | MI->setIsWarnIfUnused(true); |
| 1976 | WarnUnusedMacroLocs.insert(MI->getDefinitionLoc()); |
| 1977 | } |
| 1978 | |
Chris Lattner | f4a72b0 | 2009-04-12 01:39:54 +0000 | [diff] [blame] | 1979 | // If the callbacks want to know, tell them about the macro definition. |
| 1980 | if (Callbacks) |
Argyrios Kyrtzidis | c515978 | 2013-02-24 00:05:14 +0000 | [diff] [blame] | 1981 | Callbacks->MacroDefined(MacroNameTok, MD); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1982 | } |
| 1983 | |
James Dennett | dc20169 | 2012-06-22 05:46:07 +0000 | [diff] [blame] | 1984 | /// HandleUndefDirective - Implements \#undef. |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1985 | /// |
| 1986 | void Preprocessor::HandleUndefDirective(Token &UndefTok) { |
| 1987 | ++NumUndefined; |
| 1988 | |
| 1989 | Token MacroNameTok; |
| 1990 | ReadMacroName(MacroNameTok, 2); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1991 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1992 | // Error reading macro name? If so, diagnostic already issued. |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 1993 | if (MacroNameTok.is(tok::eod)) |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1994 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1995 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1996 | // 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] | 1997 | CheckEndOfDirective("undef"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1998 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1999 | // Okay, we finally have a valid identifier to undef. |
Argyrios Kyrtzidis | 9818a1d | 2013-02-20 00:54:57 +0000 | [diff] [blame] | 2000 | MacroDirective *MD = getMacroDirective(MacroNameTok.getIdentifierInfo()); |
Argyrios Kyrtzidis | c56fff7 | 2013-03-26 17:17:01 +0000 | [diff] [blame] | 2001 | const MacroInfo *MI = MD ? MD->getMacroInfo() : 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2002 | |
Argyrios Kyrtzidis | 3684547 | 2013-01-16 16:52:44 +0000 | [diff] [blame] | 2003 | // If the callbacks want to know, tell them about the macro #undef. |
| 2004 | // Note: no matter if the macro was defined or not. |
| 2005 | if (Callbacks) |
Argyrios Kyrtzidis | c515978 | 2013-02-24 00:05:14 +0000 | [diff] [blame] | 2006 | Callbacks->MacroUndefined(MacroNameTok, MD); |
Argyrios Kyrtzidis | 3684547 | 2013-01-16 16:52:44 +0000 | [diff] [blame] | 2007 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 2008 | // If the macro is not defined, this is a noop undef, just return. |
| 2009 | if (MI == 0) return; |
| 2010 | |
Argyrios Kyrtzidis | 1f8dcfc | 2011-07-11 20:39:47 +0000 | [diff] [blame] | 2011 | if (!MI->isUsed() && MI->isWarnIfUnused()) |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 2012 | Diag(MI->getDefinitionLoc(), diag::pp_macro_not_used); |
Chris Lattner | 41c1747 | 2009-04-21 03:42:09 +0000 | [diff] [blame] | 2013 | |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 2014 | if (MI->isWarnIfUnused()) |
| 2015 | WarnUnusedMacroLocs.erase(MI->getDefinitionLoc()); |
| 2016 | |
Argyrios Kyrtzidis | c56fff7 | 2013-03-26 17:17:01 +0000 | [diff] [blame] | 2017 | appendMacroDirective(MacroNameTok.getIdentifierInfo(), |
| 2018 | AllocateUndefMacroDirective(MacroNameTok.getLocation())); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 2019 | } |
| 2020 | |
| 2021 | |
| 2022 | //===----------------------------------------------------------------------===// |
| 2023 | // Preprocessor Conditional Directive Handling. |
| 2024 | //===----------------------------------------------------------------------===// |
| 2025 | |
James Dennett | dc20169 | 2012-06-22 05:46:07 +0000 | [diff] [blame] | 2026 | /// HandleIfdefDirective - Implements the \#ifdef/\#ifndef directive. isIfndef |
| 2027 | /// is true when this is a \#ifndef directive. ReadAnyTokensBeforeDirective is |
| 2028 | /// true if any tokens have been returned or pp-directives activated before this |
| 2029 | /// \#ifndef has been lexed. |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 2030 | /// |
| 2031 | void Preprocessor::HandleIfdefDirective(Token &Result, bool isIfndef, |
| 2032 | bool ReadAnyTokensBeforeDirective) { |
| 2033 | ++NumIf; |
| 2034 | Token DirectiveTok = Result; |
| 2035 | |
| 2036 | Token MacroNameTok; |
| 2037 | ReadMacroName(MacroNameTok); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2038 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 2039 | // Error reading macro name? If so, diagnostic already issued. |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 2040 | if (MacroNameTok.is(tok::eod)) { |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 2041 | // Skip code until we get to #endif. This helps with recovery by not |
| 2042 | // emitting an error when the #endif is reached. |
| 2043 | SkipExcludedConditionalBlock(DirectiveTok.getLocation(), |
| 2044 | /*Foundnonskip*/false, /*FoundElse*/false); |
| 2045 | return; |
| 2046 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2047 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 2048 | // 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] | 2049 | CheckEndOfDirective(isIfndef ? "ifndef" : "ifdef"); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 2050 | |
Chris Lattner | 13d283d | 2010-02-12 08:03:27 +0000 | [diff] [blame] | 2051 | IdentifierInfo *MII = MacroNameTok.getIdentifierInfo(); |
Argyrios Kyrtzidis | c515978 | 2013-02-24 00:05:14 +0000 | [diff] [blame] | 2052 | MacroDirective *MD = getMacroDirective(MII); |
Argyrios Kyrtzidis | c56fff7 | 2013-03-26 17:17:01 +0000 | [diff] [blame] | 2053 | MacroInfo *MI = MD ? MD->getMacroInfo() : 0; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2054 | |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 2055 | if (CurPPLexer->getConditionalStackDepth() == 0) { |
Chris Lattner | 13d283d | 2010-02-12 08:03:27 +0000 | [diff] [blame] | 2056 | // If the start of a top-level #ifdef and if the macro is not defined, |
| 2057 | // inform MIOpt that this might be the start of a proper include guard. |
| 2058 | // Otherwise it is some other form of unknown conditional which we can't |
| 2059 | // handle. |
| 2060 | if (!ReadAnyTokensBeforeDirective && MI == 0) { |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 2061 | assert(isIfndef && "#ifdef shouldn't reach here"); |
Chris Lattner | 13d283d | 2010-02-12 08:03:27 +0000 | [diff] [blame] | 2062 | CurPPLexer->MIOpt.EnterTopLevelIFNDEF(MII); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 2063 | } else |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 2064 | CurPPLexer->MIOpt.EnterTopLevelConditional(); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 2065 | } |
| 2066 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 2067 | // If there is a macro, process it. |
| 2068 | if (MI) // Mark it used. |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 2069 | markMacroAsUsed(MI); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2070 | |
Argyrios Kyrtzidis | bb66066 | 2012-03-05 05:48:09 +0000 | [diff] [blame] | 2071 | if (Callbacks) { |
| 2072 | if (isIfndef) |
Argyrios Kyrtzidis | c515978 | 2013-02-24 00:05:14 +0000 | [diff] [blame] | 2073 | Callbacks->Ifndef(DirectiveTok.getLocation(), MacroNameTok, MD); |
Argyrios Kyrtzidis | bb66066 | 2012-03-05 05:48:09 +0000 | [diff] [blame] | 2074 | else |
Argyrios Kyrtzidis | c515978 | 2013-02-24 00:05:14 +0000 | [diff] [blame] | 2075 | Callbacks->Ifdef(DirectiveTok.getLocation(), MacroNameTok, MD); |
Argyrios Kyrtzidis | bb66066 | 2012-03-05 05:48:09 +0000 | [diff] [blame] | 2076 | } |
| 2077 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 2078 | // Should we include the stuff contained by this directive? |
| 2079 | if (!MI == isIfndef) { |
| 2080 | // 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] | 2081 | CurPPLexer->pushConditionalLevel(DirectiveTok.getLocation(), |
| 2082 | /*wasskip*/false, /*foundnonskip*/true, |
| 2083 | /*foundelse*/false); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 2084 | } else { |
Craig Silverstein | 08985b9 | 2010-11-06 01:19:03 +0000 | [diff] [blame] | 2085 | // No, skip the contents of this block. |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 2086 | SkipExcludedConditionalBlock(DirectiveTok.getLocation(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2087 | /*Foundnonskip*/false, |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 2088 | /*FoundElse*/false); |
| 2089 | } |
| 2090 | } |
| 2091 | |
James Dennett | dc20169 | 2012-06-22 05:46:07 +0000 | [diff] [blame] | 2092 | /// HandleIfDirective - Implements the \#if directive. |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 2093 | /// |
| 2094 | void Preprocessor::HandleIfDirective(Token &IfToken, |
| 2095 | bool ReadAnyTokensBeforeDirective) { |
| 2096 | ++NumIf; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2097 | |
Craig Silverstein | 08985b9 | 2010-11-06 01:19:03 +0000 | [diff] [blame] | 2098 | // Parse and evaluate the conditional expression. |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 2099 | IdentifierInfo *IfNDefMacro = 0; |
Craig Silverstein | 08985b9 | 2010-11-06 01:19:03 +0000 | [diff] [blame] | 2100 | const SourceLocation ConditionalBegin = CurPPLexer->getSourceLocation(); |
| 2101 | const bool ConditionalTrue = EvaluateDirectiveExpression(IfNDefMacro); |
| 2102 | const SourceLocation ConditionalEnd = CurPPLexer->getSourceLocation(); |
Nuno Lopes | 0049db6 | 2008-06-01 18:31:24 +0000 | [diff] [blame] | 2103 | |
| 2104 | // If this condition is equivalent to #ifndef X, and if this is the first |
| 2105 | // directive seen, handle it for the multiple-include optimization. |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 2106 | if (CurPPLexer->getConditionalStackDepth() == 0) { |
Chris Lattner | 13d283d | 2010-02-12 08:03:27 +0000 | [diff] [blame] | 2107 | if (!ReadAnyTokensBeforeDirective && IfNDefMacro && ConditionalTrue) |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 2108 | CurPPLexer->MIOpt.EnterTopLevelIFNDEF(IfNDefMacro); |
Nuno Lopes | 0049db6 | 2008-06-01 18:31:24 +0000 | [diff] [blame] | 2109 | else |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 2110 | CurPPLexer->MIOpt.EnterTopLevelConditional(); |
Nuno Lopes | 0049db6 | 2008-06-01 18:31:24 +0000 | [diff] [blame] | 2111 | } |
| 2112 | |
Argyrios Kyrtzidis | bb66066 | 2012-03-05 05:48:09 +0000 | [diff] [blame] | 2113 | if (Callbacks) |
| 2114 | Callbacks->If(IfToken.getLocation(), |
| 2115 | SourceRange(ConditionalBegin, ConditionalEnd)); |
| 2116 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 2117 | // Should we include the stuff contained by this directive? |
| 2118 | if (ConditionalTrue) { |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 2119 | // 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] | 2120 | CurPPLexer->pushConditionalLevel(IfToken.getLocation(), /*wasskip*/false, |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 2121 | /*foundnonskip*/true, /*foundelse*/false); |
| 2122 | } else { |
Craig Silverstein | 08985b9 | 2010-11-06 01:19:03 +0000 | [diff] [blame] | 2123 | // No, skip the contents of this block. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2124 | SkipExcludedConditionalBlock(IfToken.getLocation(), /*Foundnonskip*/false, |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 2125 | /*FoundElse*/false); |
| 2126 | } |
| 2127 | } |
| 2128 | |
James Dennett | dc20169 | 2012-06-22 05:46:07 +0000 | [diff] [blame] | 2129 | /// HandleEndifDirective - Implements the \#endif directive. |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 2130 | /// |
| 2131 | void Preprocessor::HandleEndifDirective(Token &EndifToken) { |
| 2132 | ++NumEndif; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2133 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 2134 | // Check that this is the whole directive. |
Chris Lattner | 35410d5 | 2009-04-14 05:07:49 +0000 | [diff] [blame] | 2135 | CheckEndOfDirective("endif"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2136 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 2137 | PPConditionalInfo CondInfo; |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 2138 | if (CurPPLexer->popConditionalLevel(CondInfo)) { |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 2139 | // No conditionals on the stack: this is an #endif without an #if. |
Chris Lattner | 3692b09 | 2008-11-18 07:59:24 +0000 | [diff] [blame] | 2140 | Diag(EndifToken, diag::err_pp_endif_without_if); |
| 2141 | return; |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 2142 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2143 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 2144 | // If this the end of a top-level #endif, inform MIOpt. |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 2145 | if (CurPPLexer->getConditionalStackDepth() == 0) |
| 2146 | CurPPLexer->MIOpt.ExitTopLevelConditional(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2147 | |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 2148 | assert(!CondInfo.WasSkipping && !CurPPLexer->LexingRawMode && |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 2149 | "This code should only be reachable in the non-skipping case!"); |
Craig Silverstein | 08985b9 | 2010-11-06 01:19:03 +0000 | [diff] [blame] | 2150 | |
| 2151 | if (Callbacks) |
Argyrios Kyrtzidis | bb66066 | 2012-03-05 05:48:09 +0000 | [diff] [blame] | 2152 | Callbacks->Endif(EndifToken.getLocation(), CondInfo.IfLoc); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 2153 | } |
| 2154 | |
James Dennett | dc20169 | 2012-06-22 05:46:07 +0000 | [diff] [blame] | 2155 | /// HandleElseDirective - Implements the \#else directive. |
Craig Silverstein | 08985b9 | 2010-11-06 01:19:03 +0000 | [diff] [blame] | 2156 | /// |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 2157 | void Preprocessor::HandleElseDirective(Token &Result) { |
| 2158 | ++NumElse; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2159 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 2160 | // #else directive in a non-skipping conditional... start skipping. |
Chris Lattner | 35410d5 | 2009-04-14 05:07:49 +0000 | [diff] [blame] | 2161 | CheckEndOfDirective("else"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2162 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 2163 | PPConditionalInfo CI; |
Chris Lattner | 3692b09 | 2008-11-18 07:59:24 +0000 | [diff] [blame] | 2164 | if (CurPPLexer->popConditionalLevel(CI)) { |
| 2165 | Diag(Result, diag::pp_err_else_without_if); |
| 2166 | return; |
| 2167 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2168 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 2169 | // If this is a top-level #else, inform the MIOpt. |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 2170 | if (CurPPLexer->getConditionalStackDepth() == 0) |
| 2171 | CurPPLexer->MIOpt.EnterTopLevelConditional(); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 2172 | |
| 2173 | // If this is a #else with a #else before it, report the error. |
| 2174 | if (CI.FoundElse) Diag(Result, diag::pp_err_else_after_else); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2175 | |
Argyrios Kyrtzidis | bb66066 | 2012-03-05 05:48:09 +0000 | [diff] [blame] | 2176 | if (Callbacks) |
| 2177 | Callbacks->Else(Result.getLocation(), CI.IfLoc); |
| 2178 | |
Craig Silverstein | 08985b9 | 2010-11-06 01:19:03 +0000 | [diff] [blame] | 2179 | // Finally, skip the rest of the contents of this block. |
| 2180 | SkipExcludedConditionalBlock(CI.IfLoc, /*Foundnonskip*/true, |
Argyrios Kyrtzidis | 6b4ff04 | 2011-09-27 17:32:05 +0000 | [diff] [blame] | 2181 | /*FoundElse*/true, Result.getLocation()); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 2182 | } |
| 2183 | |
James Dennett | dc20169 | 2012-06-22 05:46:07 +0000 | [diff] [blame] | 2184 | /// HandleElifDirective - Implements the \#elif directive. |
Craig Silverstein | 08985b9 | 2010-11-06 01:19:03 +0000 | [diff] [blame] | 2185 | /// |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 2186 | void Preprocessor::HandleElifDirective(Token &ElifToken) { |
| 2187 | ++NumElse; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2188 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 2189 | // #elif directive in a non-skipping conditional... start skipping. |
| 2190 | // We don't care what the condition is, because we will always skip it (since |
| 2191 | // the block immediately before it was included). |
Craig Silverstein | 08985b9 | 2010-11-06 01:19:03 +0000 | [diff] [blame] | 2192 | const SourceLocation ConditionalBegin = CurPPLexer->getSourceLocation(); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 2193 | DiscardUntilEndOfDirective(); |
Craig Silverstein | 08985b9 | 2010-11-06 01:19:03 +0000 | [diff] [blame] | 2194 | const SourceLocation ConditionalEnd = CurPPLexer->getSourceLocation(); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 2195 | |
| 2196 | PPConditionalInfo CI; |
Chris Lattner | 3692b09 | 2008-11-18 07:59:24 +0000 | [diff] [blame] | 2197 | if (CurPPLexer->popConditionalLevel(CI)) { |
| 2198 | Diag(ElifToken, diag::pp_err_elif_without_if); |
| 2199 | return; |
| 2200 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2201 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 2202 | // If this is a top-level #elif, inform the MIOpt. |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 2203 | if (CurPPLexer->getConditionalStackDepth() == 0) |
| 2204 | CurPPLexer->MIOpt.EnterTopLevelConditional(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2205 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 2206 | // If this is a #elif with a #else before it, report the error. |
| 2207 | if (CI.FoundElse) Diag(ElifToken, diag::pp_err_elif_after_else); |
Argyrios Kyrtzidis | bb66066 | 2012-03-05 05:48:09 +0000 | [diff] [blame] | 2208 | |
| 2209 | if (Callbacks) |
| 2210 | Callbacks->Elif(ElifToken.getLocation(), |
| 2211 | SourceRange(ConditionalBegin, ConditionalEnd), CI.IfLoc); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 2212 | |
Craig Silverstein | 08985b9 | 2010-11-06 01:19:03 +0000 | [diff] [blame] | 2213 | // Finally, skip the rest of the contents of this block. |
| 2214 | SkipExcludedConditionalBlock(CI.IfLoc, /*Foundnonskip*/true, |
Argyrios Kyrtzidis | 6b4ff04 | 2011-09-27 17:32:05 +0000 | [diff] [blame] | 2215 | /*FoundElse*/CI.FoundElse, |
| 2216 | ElifToken.getLocation()); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 2217 | } |