Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- Pragma.cpp - Pragma registration and handling --------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the PragmaHandler/PragmaTable interfaces and implements |
| 11 | // pragma related methods of the Preprocessor class. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "clang/Lex/Pragma.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 16 | #include "clang/Lex/HeaderSearch.h" |
Chris Lattner | a9d9145 | 2009-01-16 18:59:23 +0000 | [diff] [blame] | 17 | #include "clang/Lex/LiteralSupport.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 18 | #include "clang/Lex/Preprocessor.h" |
Chris Lattner | f47724b | 2010-08-17 15:55:45 +0000 | [diff] [blame] | 19 | #include "clang/Lex/MacroInfo.h" |
Chris Lattner | 500d329 | 2009-01-29 05:15:15 +0000 | [diff] [blame] | 20 | #include "clang/Lex/LexDiagnostic.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 21 | #include "clang/Basic/FileManager.h" |
| 22 | #include "clang/Basic/SourceManager.h" |
Daniel Dunbar | ff759a6 | 2010-08-18 23:09:23 +0000 | [diff] [blame] | 23 | #include "llvm/Support/CrashRecoveryContext.h" |
Daniel Dunbar | 5505413 | 2010-08-17 22:32:48 +0000 | [diff] [blame] | 24 | #include "llvm/Support/ErrorHandling.h" |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 25 | #include <algorithm> |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 26 | using namespace clang; |
| 27 | |
| 28 | // Out-of-line destructor to provide a home for the class. |
| 29 | PragmaHandler::~PragmaHandler() { |
| 30 | } |
| 31 | |
| 32 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | c72cc50 | 2010-06-11 20:10:12 +0000 | [diff] [blame] | 33 | // EmptyPragmaHandler Implementation. |
| 34 | //===----------------------------------------------------------------------===// |
| 35 | |
Argyrios Kyrtzidis | 9b36c3f | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 36 | EmptyPragmaHandler::EmptyPragmaHandler() {} |
Daniel Dunbar | c72cc50 | 2010-06-11 20:10:12 +0000 | [diff] [blame] | 37 | |
| 38 | void EmptyPragmaHandler::HandlePragma(Preprocessor &PP, Token &FirstToken) {} |
| 39 | |
| 40 | //===----------------------------------------------------------------------===// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 41 | // PragmaNamespace Implementation. |
| 42 | //===----------------------------------------------------------------------===// |
| 43 | |
| 44 | |
| 45 | PragmaNamespace::~PragmaNamespace() { |
Argyrios Kyrtzidis | 9b36c3f | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 46 | for (llvm::StringMap<PragmaHandler*>::iterator |
| 47 | I = Handlers.begin(), E = Handlers.end(); I != E; ++I) |
| 48 | delete I->second; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | /// FindHandler - Check to see if there is already a handler for the |
| 52 | /// specified name. If not, return the handler for the null identifier if it |
| 53 | /// exists, otherwise return null. If IgnoreNull is true (the default) then |
| 54 | /// the null handler isn't returned on failure to match. |
Argyrios Kyrtzidis | 9b36c3f | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 55 | PragmaHandler *PragmaNamespace::FindHandler(llvm::StringRef Name, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 56 | bool IgnoreNull) const { |
Argyrios Kyrtzidis | 9b36c3f | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 57 | if (PragmaHandler *Handler = Handlers.lookup(Name)) |
| 58 | return Handler; |
| 59 | return IgnoreNull ? 0 : Handlers.lookup(llvm::StringRef()); |
| 60 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 61 | |
Argyrios Kyrtzidis | 9b36c3f | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 62 | void PragmaNamespace::AddPragma(PragmaHandler *Handler) { |
| 63 | assert(!Handlers.lookup(Handler->getName()) && |
| 64 | "A handler with this name is already registered in this namespace"); |
| 65 | llvm::StringMapEntry<PragmaHandler *> &Entry = |
| 66 | Handlers.GetOrCreateValue(Handler->getName()); |
| 67 | Entry.setValue(Handler); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 68 | } |
| 69 | |
Daniel Dunbar | 4095080 | 2008-10-04 19:17:46 +0000 | [diff] [blame] | 70 | void PragmaNamespace::RemovePragmaHandler(PragmaHandler *Handler) { |
Argyrios Kyrtzidis | 9b36c3f | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 71 | assert(Handlers.lookup(Handler->getName()) && |
| 72 | "Handler not registered in this namespace"); |
| 73 | Handlers.erase(Handler->getName()); |
Daniel Dunbar | 4095080 | 2008-10-04 19:17:46 +0000 | [diff] [blame] | 74 | } |
| 75 | |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 76 | void PragmaNamespace::HandlePragma(Preprocessor &PP, Token &Tok) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 77 | // Read the 'namespace' that the directive is in, e.g. STDC. Do not macro |
| 78 | // expand it, the user can have a STDC #define, that should not affect this. |
| 79 | PP.LexUnexpandedToken(Tok); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 80 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 81 | // Get the handler for this token. If there is no handler, ignore the pragma. |
Argyrios Kyrtzidis | 9b36c3f | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 82 | PragmaHandler *Handler |
| 83 | = FindHandler(Tok.getIdentifierInfo() ? Tok.getIdentifierInfo()->getName() |
| 84 | : llvm::StringRef(), |
| 85 | /*IgnoreNull=*/false); |
Chris Lattner | af7cdf4 | 2009-04-19 21:10:26 +0000 | [diff] [blame] | 86 | if (Handler == 0) { |
| 87 | PP.Diag(Tok, diag::warn_pragma_ignored); |
| 88 | return; |
| 89 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 90 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 91 | // Otherwise, pass it down. |
| 92 | Handler->HandlePragma(PP, Tok); |
| 93 | } |
| 94 | |
| 95 | //===----------------------------------------------------------------------===// |
| 96 | // Preprocessor Pragma Directive Handling. |
| 97 | //===----------------------------------------------------------------------===// |
| 98 | |
| 99 | /// HandlePragmaDirective - The "#pragma" directive has been parsed. Lex the |
| 100 | /// rest of the pragma, passing it to the registered pragma handlers. |
| 101 | void Preprocessor::HandlePragmaDirective() { |
| 102 | ++NumPragma; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 103 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 104 | // Invoke the first level of pragma handlers which reads the namespace id. |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 105 | Token Tok; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 106 | PragmaHandlers->HandlePragma(*this, Tok); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 107 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 108 | // If the pragma handler didn't read the rest of the line, consume it now. |
Chris Lattner | 027cff6 | 2009-06-18 05:55:53 +0000 | [diff] [blame] | 109 | if (CurPPLexer && CurPPLexer->ParsingPreprocessorDirective) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 110 | DiscardUntilEndOfDirective(); |
| 111 | } |
| 112 | |
| 113 | /// Handle_Pragma - Read a _Pragma directive, slice it up, process it, then |
| 114 | /// return the first token after the directive. The _Pragma token has just |
| 115 | /// been read into 'Tok'. |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 116 | void Preprocessor::Handle_Pragma(Token &Tok) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 117 | // Remember the pragma token location. |
| 118 | SourceLocation PragmaLoc = Tok.getLocation(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 119 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 120 | // Read the '('. |
| 121 | Lex(Tok); |
Chris Lattner | 3692b09 | 2008-11-18 07:59:24 +0000 | [diff] [blame] | 122 | if (Tok.isNot(tok::l_paren)) { |
| 123 | Diag(PragmaLoc, diag::err__Pragma_malformed); |
| 124 | return; |
| 125 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 126 | |
| 127 | // Read the '"..."'. |
| 128 | Lex(Tok); |
Chris Lattner | 3692b09 | 2008-11-18 07:59:24 +0000 | [diff] [blame] | 129 | if (Tok.isNot(tok::string_literal) && Tok.isNot(tok::wide_string_literal)) { |
| 130 | Diag(PragmaLoc, diag::err__Pragma_malformed); |
| 131 | return; |
| 132 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 133 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 134 | // Remember the string. |
| 135 | std::string StrVal = getSpelling(Tok); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 136 | |
| 137 | // Read the ')'. |
| 138 | Lex(Tok); |
Chris Lattner | 3692b09 | 2008-11-18 07:59:24 +0000 | [diff] [blame] | 139 | if (Tok.isNot(tok::r_paren)) { |
| 140 | Diag(PragmaLoc, diag::err__Pragma_malformed); |
| 141 | return; |
| 142 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 143 | |
Chris Lattner | e7fb484 | 2009-02-15 20:52:18 +0000 | [diff] [blame] | 144 | SourceLocation RParenLoc = Tok.getLocation(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 145 | |
Chris Lattner | a9d9145 | 2009-01-16 18:59:23 +0000 | [diff] [blame] | 146 | // The _Pragma is lexically sound. Destringize according to C99 6.10.9.1: |
| 147 | // "The string literal is destringized by deleting the L prefix, if present, |
| 148 | // deleting the leading and trailing double-quotes, replacing each escape |
| 149 | // sequence \" by a double-quote, and replacing each escape sequence \\ by a |
| 150 | // single backslash." |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 151 | if (StrVal[0] == 'L') // Remove L prefix. |
| 152 | StrVal.erase(StrVal.begin()); |
| 153 | assert(StrVal[0] == '"' && StrVal[StrVal.size()-1] == '"' && |
| 154 | "Invalid string token!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 155 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 156 | // Remove the front quote, replacing it with a space, so that the pragma |
| 157 | // contents appear to have a space before them. |
| 158 | StrVal[0] = ' '; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 159 | |
Chris Lattner | 1fa4953 | 2009-03-08 08:08:45 +0000 | [diff] [blame] | 160 | // Replace the terminating quote with a \n. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 161 | StrVal[StrVal.size()-1] = '\n'; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 162 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 163 | // Remove escaped quotes and escapes. |
| 164 | for (unsigned i = 0, e = StrVal.size(); i != e-1; ++i) { |
| 165 | if (StrVal[i] == '\\' && |
| 166 | (StrVal[i+1] == '\\' || StrVal[i+1] == '"')) { |
| 167 | // \\ -> '\' and \" -> '"'. |
| 168 | StrVal.erase(StrVal.begin()+i); |
| 169 | --e; |
| 170 | } |
| 171 | } |
John McCall | 1ef8a2e | 2010-08-28 22:34:47 +0000 | [diff] [blame] | 172 | |
| 173 | Handle_Pragma(StrVal, PragmaLoc, RParenLoc); |
| 174 | |
| 175 | // Finally, return whatever came after the pragma directive. |
| 176 | return Lex(Tok); |
| 177 | } |
| 178 | |
| 179 | /// HandleMicrosoft__pragma - Like Handle_Pragma except the pragma text |
| 180 | /// is not enclosed within a string literal. |
| 181 | void Preprocessor::HandleMicrosoft__pragma(Token &Tok) { |
| 182 | // Remember the pragma token location. |
| 183 | SourceLocation PragmaLoc = Tok.getLocation(); |
| 184 | |
| 185 | // Read the '('. |
| 186 | Lex(Tok); |
| 187 | if (Tok.isNot(tok::l_paren)) { |
| 188 | Diag(PragmaLoc, diag::err__Pragma_malformed); |
| 189 | return; |
| 190 | } |
| 191 | |
| 192 | // Get the tokens enclosed within the __pragma(). |
| 193 | llvm::SmallVector<Token, 32> PragmaToks; |
| 194 | int NumParens = 0; |
| 195 | Lex(Tok); |
| 196 | while (Tok.isNot(tok::eof)) { |
| 197 | if (Tok.is(tok::l_paren)) |
| 198 | NumParens++; |
| 199 | else if (Tok.is(tok::r_paren) && NumParens-- == 0) |
| 200 | break; |
| 201 | PragmaToks.push_back(Tok); |
| 202 | Lex(Tok); |
| 203 | } |
| 204 | |
John McCall | 3da92a9 | 2010-08-29 01:09:54 +0000 | [diff] [blame] | 205 | if (Tok.is(tok::eof)) { |
| 206 | Diag(PragmaLoc, diag::err_unterminated___pragma); |
| 207 | return; |
| 208 | } |
| 209 | |
John McCall | 1ef8a2e | 2010-08-28 22:34:47 +0000 | [diff] [blame] | 210 | // Build the pragma string. |
| 211 | std::string StrVal = " "; |
| 212 | for (llvm::SmallVector<Token, 32>::iterator I = |
| 213 | PragmaToks.begin(), E = PragmaToks.end(); I != E; ++I) { |
| 214 | StrVal += getSpelling(*I); |
| 215 | } |
| 216 | |
| 217 | SourceLocation RParenLoc = Tok.getLocation(); |
| 218 | |
| 219 | Handle_Pragma(StrVal, PragmaLoc, RParenLoc); |
| 220 | |
| 221 | // Finally, return whatever came after the pragma directive. |
| 222 | return Lex(Tok); |
| 223 | } |
| 224 | |
| 225 | void Preprocessor::Handle_Pragma(const std::string &StrVal, |
| 226 | SourceLocation PragmaLoc, |
| 227 | SourceLocation RParenLoc) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 228 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 229 | // Plop the string (including the newline and trailing null) into a buffer |
| 230 | // where we can lex it. |
Chris Lattner | 47246be | 2009-01-26 19:29:26 +0000 | [diff] [blame] | 231 | Token TmpTok; |
| 232 | TmpTok.startToken(); |
| 233 | CreateString(&StrVal[0], StrVal.size(), TmpTok); |
| 234 | SourceLocation TokLoc = TmpTok.getLocation(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 235 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 236 | // Make and enter a lexer object so that we lex and expand the tokens just |
| 237 | // like any others. |
Chris Lattner | e7fb484 | 2009-02-15 20:52:18 +0000 | [diff] [blame] | 238 | Lexer *TL = Lexer::Create_PragmaLexer(TokLoc, PragmaLoc, RParenLoc, |
Chris Lattner | 1fa4953 | 2009-03-08 08:08:45 +0000 | [diff] [blame] | 239 | StrVal.size(), *this); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 240 | |
| 241 | EnterSourceFileWithLexer(TL, 0); |
| 242 | |
| 243 | // With everything set up, lex this as a #pragma directive. |
| 244 | HandlePragmaDirective(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | |
| 248 | |
| 249 | /// HandlePragmaOnce - Handle #pragma once. OnceTok is the 'once'. |
| 250 | /// |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 251 | void Preprocessor::HandlePragmaOnce(Token &OnceTok) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 252 | if (isInPrimaryFile()) { |
| 253 | Diag(OnceTok, diag::pp_pragma_once_in_main_file); |
| 254 | return; |
| 255 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 256 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 257 | // Get the current file lexer we're looking at. Ignore _Pragma 'files' etc. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 258 | // Mark the file as a once-only file now. |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 259 | HeaderInfo.MarkFileIncludeOnce(getCurrentFileLexer()->getFileEntry()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 260 | } |
| 261 | |
Chris Lattner | 2243449 | 2007-12-19 19:38:36 +0000 | [diff] [blame] | 262 | void Preprocessor::HandlePragmaMark() { |
Ted Kremenek | 17ff58a | 2008-11-19 22:21:33 +0000 | [diff] [blame] | 263 | assert(CurPPLexer && "No current lexer?"); |
Chris Lattner | 6896a37 | 2009-06-15 05:02:34 +0000 | [diff] [blame] | 264 | if (CurLexer) |
| 265 | CurLexer->ReadToEndOfLine(); |
| 266 | else |
| 267 | CurPTHLexer->DiscardToEndOfLine(); |
Chris Lattner | 2243449 | 2007-12-19 19:38:36 +0000 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 271 | /// HandlePragmaPoison - Handle #pragma GCC poison. PoisonTok is the 'poison'. |
| 272 | /// |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 273 | void Preprocessor::HandlePragmaPoison(Token &PoisonTok) { |
| 274 | Token Tok; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 275 | |
| 276 | while (1) { |
| 277 | // Read the next token to poison. While doing this, pretend that we are |
| 278 | // skipping while reading the identifier to poison. |
| 279 | // This avoids errors on code like: |
| 280 | // #pragma GCC poison X |
| 281 | // #pragma GCC poison X |
Ted Kremenek | 68a91d5 | 2008-11-18 01:12:54 +0000 | [diff] [blame] | 282 | if (CurPPLexer) CurPPLexer->LexingRawMode = true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 283 | LexUnexpandedToken(Tok); |
Ted Kremenek | 68a91d5 | 2008-11-18 01:12:54 +0000 | [diff] [blame] | 284 | if (CurPPLexer) CurPPLexer->LexingRawMode = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 285 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 286 | // If we reached the end of line, we're done. |
Chris Lattner | 22f6bbc | 2007-10-09 18:02:16 +0000 | [diff] [blame] | 287 | if (Tok.is(tok::eom)) return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 288 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 289 | // Can only poison identifiers. |
Chris Lattner | 22f6bbc | 2007-10-09 18:02:16 +0000 | [diff] [blame] | 290 | if (Tok.isNot(tok::identifier)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 291 | Diag(Tok, diag::err_pp_invalid_poison); |
| 292 | return; |
| 293 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 294 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 295 | // Look up the identifier info for the token. We disabled identifier lookup |
| 296 | // by saying we're skipping contents, so we need to do this manually. |
| 297 | IdentifierInfo *II = LookUpIdentifierInfo(Tok); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 298 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 299 | // Already poisoned. |
| 300 | if (II->isPoisoned()) continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 301 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 302 | // If this is a macro identifier, emit a warning. |
Chris Lattner | 0edde55 | 2007-10-07 08:04:56 +0000 | [diff] [blame] | 303 | if (II->hasMacroDefinition()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 304 | Diag(Tok, diag::pp_poisoning_existing_macro); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 305 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 306 | // Finally, poison it! |
| 307 | II->setIsPoisoned(); |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | /// HandlePragmaSystemHeader - Implement #pragma GCC system_header. We know |
| 312 | /// that the whole directive has been parsed. |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 313 | void Preprocessor::HandlePragmaSystemHeader(Token &SysHeaderTok) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 314 | if (isInPrimaryFile()) { |
| 315 | Diag(SysHeaderTok, diag::pp_pragma_sysheader_in_main_file); |
| 316 | return; |
| 317 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 318 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 319 | // Get the current file lexer we're looking at. Ignore _Pragma 'files' etc. |
Ted Kremenek | 35c10c2 | 2008-11-20 01:45:11 +0000 | [diff] [blame] | 320 | PreprocessorLexer *TheLexer = getCurrentFileLexer(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 321 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 322 | // Mark the file as a system header. |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 323 | HeaderInfo.MarkFileSystemHeader(TheLexer->getFileEntry()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 324 | |
| 325 | |
Chris Lattner | 6896a37 | 2009-06-15 05:02:34 +0000 | [diff] [blame] | 326 | PresumedLoc PLoc = SourceMgr.getPresumedLoc(SysHeaderTok.getLocation()); |
| 327 | unsigned FilenameLen = strlen(PLoc.getFilename()); |
| 328 | unsigned FilenameID = SourceMgr.getLineTableFilenameID(PLoc.getFilename(), |
| 329 | FilenameLen); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 330 | |
Chris Lattner | 6896a37 | 2009-06-15 05:02:34 +0000 | [diff] [blame] | 331 | // Emit a line marker. This will change any source locations from this point |
| 332 | // forward to realize they are in a system header. |
| 333 | // Create a line note with this information. |
| 334 | SourceMgr.AddLineNote(SysHeaderTok.getLocation(), PLoc.getLine(), FilenameID, |
| 335 | false, false, true, false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 336 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 337 | // Notify the client, if desired, that we are in a new source file. |
| 338 | if (Callbacks) |
Ted Kremenek | 35c10c2 | 2008-11-20 01:45:11 +0000 | [diff] [blame] | 339 | Callbacks->FileChanged(SysHeaderTok.getLocation(), |
Chris Lattner | 0b9e736 | 2008-09-26 21:18:42 +0000 | [diff] [blame] | 340 | PPCallbacks::SystemHeaderPragma, SrcMgr::C_System); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | /// HandlePragmaDependency - Handle #pragma GCC dependency "foo" blah. |
| 344 | /// |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 345 | void Preprocessor::HandlePragmaDependency(Token &DependencyTok) { |
| 346 | Token FilenameTok; |
Ted Kremenek | 68a91d5 | 2008-11-18 01:12:54 +0000 | [diff] [blame] | 347 | CurPPLexer->LexIncludeFilename(FilenameTok); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 348 | |
| 349 | // If the token kind is EOM, the error has already been diagnosed. |
Chris Lattner | 22f6bbc | 2007-10-09 18:02:16 +0000 | [diff] [blame] | 350 | if (FilenameTok.is(tok::eom)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 351 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 352 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 353 | // Reserve a buffer to get the spelling. |
Chris Lattner | a139481 | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 354 | llvm::SmallString<128> FilenameBuffer; |
Douglas Gregor | 453091c | 2010-03-16 22:30:13 +0000 | [diff] [blame] | 355 | bool Invalid = false; |
| 356 | llvm::StringRef Filename = getSpelling(FilenameTok, FilenameBuffer, &Invalid); |
| 357 | if (Invalid) |
| 358 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 359 | |
Chris Lattner | a139481 | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 360 | bool isAngled = |
| 361 | GetIncludeFilenameSpelling(FilenameTok.getLocation(), Filename); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 362 | // If GetIncludeFilenameSpelling set the start ptr to null, there was an |
| 363 | // error. |
Chris Lattner | a139481 | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 364 | if (Filename.empty()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 365 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 366 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 367 | // Search include directories for this file. |
| 368 | const DirectoryLookup *CurDir; |
Chris Lattner | f45b646 | 2010-01-22 00:14:44 +0000 | [diff] [blame] | 369 | const FileEntry *File = LookupFile(Filename, isAngled, 0, CurDir); |
Chris Lattner | 56b05c8 | 2008-11-18 08:02:48 +0000 | [diff] [blame] | 370 | if (File == 0) { |
Chris Lattner | a139481 | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 371 | Diag(FilenameTok, diag::err_pp_file_not_found) << Filename; |
Chris Lattner | 56b05c8 | 2008-11-18 08:02:48 +0000 | [diff] [blame] | 372 | return; |
| 373 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 374 | |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 375 | const FileEntry *CurFile = getCurrentFileLexer()->getFileEntry(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 376 | |
| 377 | // If this file is older than the file it depends on, emit a diagnostic. |
| 378 | if (CurFile && CurFile->getModificationTime() < File->getModificationTime()) { |
| 379 | // Lex tokens at the end of the message and include them in the message. |
| 380 | std::string Message; |
| 381 | Lex(DependencyTok); |
Chris Lattner | 22f6bbc | 2007-10-09 18:02:16 +0000 | [diff] [blame] | 382 | while (DependencyTok.isNot(tok::eom)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 383 | Message += getSpelling(DependencyTok) + " "; |
| 384 | Lex(DependencyTok); |
| 385 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 386 | |
Chris Lattner | 96de259 | 2010-09-05 23:16:09 +0000 | [diff] [blame] | 387 | // Remove the trailing ' ' if present. |
| 388 | if (!Message.empty()) |
| 389 | Message.erase(Message.end()-1); |
Chris Lattner | 56b05c8 | 2008-11-18 08:02:48 +0000 | [diff] [blame] | 390 | Diag(FilenameTok, diag::pp_out_of_date_dependency) << Message; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 391 | } |
| 392 | } |
| 393 | |
Chris Lattner | 636c5ef | 2009-01-16 08:21:25 +0000 | [diff] [blame] | 394 | /// HandlePragmaComment - Handle the microsoft #pragma comment extension. The |
| 395 | /// syntax is: |
| 396 | /// #pragma comment(linker, "foo") |
| 397 | /// 'linker' is one of five identifiers: compiler, exestr, lib, linker, user. |
| 398 | /// "foo" is a string, which is fully macro expanded, and permits string |
Gabor Greif | d7ee349 | 2009-03-17 11:39:38 +0000 | [diff] [blame] | 399 | /// concatenation, embedded escape characters etc. See MSDN for more details. |
Chris Lattner | 636c5ef | 2009-01-16 08:21:25 +0000 | [diff] [blame] | 400 | void Preprocessor::HandlePragmaComment(Token &Tok) { |
| 401 | SourceLocation CommentLoc = Tok.getLocation(); |
| 402 | Lex(Tok); |
| 403 | if (Tok.isNot(tok::l_paren)) { |
| 404 | Diag(CommentLoc, diag::err_pragma_comment_malformed); |
| 405 | return; |
| 406 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 407 | |
Chris Lattner | 636c5ef | 2009-01-16 08:21:25 +0000 | [diff] [blame] | 408 | // Read the identifier. |
| 409 | Lex(Tok); |
| 410 | if (Tok.isNot(tok::identifier)) { |
| 411 | Diag(CommentLoc, diag::err_pragma_comment_malformed); |
| 412 | return; |
| 413 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 414 | |
Chris Lattner | 636c5ef | 2009-01-16 08:21:25 +0000 | [diff] [blame] | 415 | // Verify that this is one of the 5 whitelisted options. |
| 416 | // FIXME: warn that 'exestr' is deprecated. |
| 417 | const IdentifierInfo *II = Tok.getIdentifierInfo(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 418 | if (!II->isStr("compiler") && !II->isStr("exestr") && !II->isStr("lib") && |
Chris Lattner | 636c5ef | 2009-01-16 08:21:25 +0000 | [diff] [blame] | 419 | !II->isStr("linker") && !II->isStr("user")) { |
| 420 | Diag(Tok.getLocation(), diag::err_pragma_comment_unknown_kind); |
| 421 | return; |
| 422 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 423 | |
Chris Lattner | a9d9145 | 2009-01-16 18:59:23 +0000 | [diff] [blame] | 424 | // Read the optional string if present. |
Chris Lattner | 636c5ef | 2009-01-16 08:21:25 +0000 | [diff] [blame] | 425 | Lex(Tok); |
Chris Lattner | a9d9145 | 2009-01-16 18:59:23 +0000 | [diff] [blame] | 426 | std::string ArgumentString; |
Chris Lattner | 636c5ef | 2009-01-16 08:21:25 +0000 | [diff] [blame] | 427 | if (Tok.is(tok::comma)) { |
Chris Lattner | a9d9145 | 2009-01-16 18:59:23 +0000 | [diff] [blame] | 428 | Lex(Tok); // eat the comma. |
Chris Lattner | 636c5ef | 2009-01-16 08:21:25 +0000 | [diff] [blame] | 429 | |
| 430 | // We need at least one string. |
Chris Lattner | edaf877 | 2009-04-19 23:16:58 +0000 | [diff] [blame] | 431 | if (Tok.isNot(tok::string_literal)) { |
Chris Lattner | 636c5ef | 2009-01-16 08:21:25 +0000 | [diff] [blame] | 432 | Diag(Tok.getLocation(), diag::err_pragma_comment_malformed); |
| 433 | return; |
| 434 | } |
| 435 | |
| 436 | // String concatenation allows multiple strings, which can even come from |
| 437 | // macro expansion. |
| 438 | // "foo " "bar" "Baz" |
Chris Lattner | a9d9145 | 2009-01-16 18:59:23 +0000 | [diff] [blame] | 439 | llvm::SmallVector<Token, 4> StrToks; |
Chris Lattner | edaf877 | 2009-04-19 23:16:58 +0000 | [diff] [blame] | 440 | while (Tok.is(tok::string_literal)) { |
Chris Lattner | a9d9145 | 2009-01-16 18:59:23 +0000 | [diff] [blame] | 441 | StrToks.push_back(Tok); |
Chris Lattner | 636c5ef | 2009-01-16 08:21:25 +0000 | [diff] [blame] | 442 | Lex(Tok); |
Chris Lattner | a9d9145 | 2009-01-16 18:59:23 +0000 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | // Concatenate and parse the strings. |
| 446 | StringLiteralParser Literal(&StrToks[0], StrToks.size(), *this); |
| 447 | assert(!Literal.AnyWide && "Didn't allow wide strings in"); |
| 448 | if (Literal.hadError) |
| 449 | return; |
| 450 | if (Literal.Pascal) { |
| 451 | Diag(StrToks[0].getLocation(), diag::err_pragma_comment_malformed); |
| 452 | return; |
| 453 | } |
| 454 | |
| 455 | ArgumentString = std::string(Literal.GetString(), |
| 456 | Literal.GetString()+Literal.GetStringLength()); |
Chris Lattner | 636c5ef | 2009-01-16 08:21:25 +0000 | [diff] [blame] | 457 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 458 | |
Chris Lattner | a9d9145 | 2009-01-16 18:59:23 +0000 | [diff] [blame] | 459 | // FIXME: If the kind is "compiler" warn if the string is present (it is |
| 460 | // ignored). |
| 461 | // FIXME: 'lib' requires a comment string. |
| 462 | // FIXME: 'linker' requires a comment string, and has a specific list of |
| 463 | // things that are allowable. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 464 | |
Chris Lattner | 636c5ef | 2009-01-16 08:21:25 +0000 | [diff] [blame] | 465 | if (Tok.isNot(tok::r_paren)) { |
| 466 | Diag(Tok.getLocation(), diag::err_pragma_comment_malformed); |
| 467 | return; |
| 468 | } |
Chris Lattner | a9d9145 | 2009-01-16 18:59:23 +0000 | [diff] [blame] | 469 | Lex(Tok); // eat the r_paren. |
Chris Lattner | 636c5ef | 2009-01-16 08:21:25 +0000 | [diff] [blame] | 470 | |
| 471 | if (Tok.isNot(tok::eom)) { |
| 472 | Diag(Tok.getLocation(), diag::err_pragma_comment_malformed); |
| 473 | return; |
| 474 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 475 | |
Chris Lattner | a9d9145 | 2009-01-16 18:59:23 +0000 | [diff] [blame] | 476 | // If the pragma is lexically sound, notify any interested PPCallbacks. |
Chris Lattner | 172e336 | 2009-01-16 19:01:46 +0000 | [diff] [blame] | 477 | if (Callbacks) |
| 478 | Callbacks->PragmaComment(CommentLoc, II, ArgumentString); |
Chris Lattner | 636c5ef | 2009-01-16 08:21:25 +0000 | [diff] [blame] | 479 | } |
| 480 | |
Chris Lattner | abfe094 | 2010-06-26 17:11:39 +0000 | [diff] [blame] | 481 | /// HandlePragmaMessage - Handle the microsoft #pragma message extension. The |
| 482 | /// syntax is: |
| 483 | /// #pragma message(messagestring) |
| 484 | /// messagestring is a string, which is fully macro expanded, and permits string |
| 485 | /// concatenation, embedded escape characters etc. See MSDN for more details. |
| 486 | void Preprocessor::HandlePragmaMessage(Token &Tok) { |
| 487 | SourceLocation MessageLoc = Tok.getLocation(); |
| 488 | Lex(Tok); |
| 489 | if (Tok.isNot(tok::l_paren)) { |
| 490 | Diag(MessageLoc, diag::err_pragma_message_malformed); |
| 491 | return; |
| 492 | } |
Chris Lattner | 636c5ef | 2009-01-16 08:21:25 +0000 | [diff] [blame] | 493 | |
Chris Lattner | abfe094 | 2010-06-26 17:11:39 +0000 | [diff] [blame] | 494 | // Read the string. |
| 495 | Lex(Tok); |
| 496 | |
| 497 | |
| 498 | // We need at least one string. |
| 499 | if (Tok.isNot(tok::string_literal)) { |
| 500 | Diag(Tok.getLocation(), diag::err_pragma_message_malformed); |
| 501 | return; |
| 502 | } |
| 503 | |
| 504 | // String concatenation allows multiple strings, which can even come from |
| 505 | // macro expansion. |
| 506 | // "foo " "bar" "Baz" |
| 507 | llvm::SmallVector<Token, 4> StrToks; |
| 508 | while (Tok.is(tok::string_literal)) { |
| 509 | StrToks.push_back(Tok); |
| 510 | Lex(Tok); |
| 511 | } |
| 512 | |
| 513 | // Concatenate and parse the strings. |
| 514 | StringLiteralParser Literal(&StrToks[0], StrToks.size(), *this); |
| 515 | assert(!Literal.AnyWide && "Didn't allow wide strings in"); |
| 516 | if (Literal.hadError) |
| 517 | return; |
| 518 | if (Literal.Pascal) { |
| 519 | Diag(StrToks[0].getLocation(), diag::err_pragma_message_malformed); |
| 520 | return; |
| 521 | } |
| 522 | |
| 523 | llvm::StringRef MessageString(Literal.GetString(), Literal.GetStringLength()); |
| 524 | |
| 525 | if (Tok.isNot(tok::r_paren)) { |
| 526 | Diag(Tok.getLocation(), diag::err_pragma_message_malformed); |
| 527 | return; |
| 528 | } |
| 529 | Lex(Tok); // eat the r_paren. |
| 530 | |
| 531 | if (Tok.isNot(tok::eom)) { |
| 532 | Diag(Tok.getLocation(), diag::err_pragma_message_malformed); |
| 533 | return; |
| 534 | } |
| 535 | |
| 536 | // Output the message. |
| 537 | Diag(MessageLoc, diag::warn_pragma_message) << MessageString; |
| 538 | |
| 539 | // If the pragma is lexically sound, notify any interested PPCallbacks. |
| 540 | if (Callbacks) |
| 541 | Callbacks->PragmaMessage(MessageLoc, MessageString); |
| 542 | } |
Chris Lattner | 636c5ef | 2009-01-16 08:21:25 +0000 | [diff] [blame] | 543 | |
Chris Lattner | f47724b | 2010-08-17 15:55:45 +0000 | [diff] [blame] | 544 | /// ParsePragmaPushOrPopMacro - Handle parsing of pragma push_macro/pop_macro. |
| 545 | /// Return the IdentifierInfo* associated with the macro to push or pop. |
| 546 | IdentifierInfo *Preprocessor::ParsePragmaPushOrPopMacro(Token &Tok) { |
| 547 | // Remember the pragma token location. |
| 548 | Token PragmaTok = Tok; |
| 549 | |
| 550 | // Read the '('. |
| 551 | Lex(Tok); |
| 552 | if (Tok.isNot(tok::l_paren)) { |
| 553 | Diag(PragmaTok.getLocation(), diag::err_pragma_push_pop_macro_malformed) |
| 554 | << getSpelling(PragmaTok); |
| 555 | return 0; |
| 556 | } |
| 557 | |
| 558 | // Read the macro name string. |
| 559 | Lex(Tok); |
| 560 | if (Tok.isNot(tok::string_literal)) { |
| 561 | Diag(PragmaTok.getLocation(), diag::err_pragma_push_pop_macro_malformed) |
| 562 | << getSpelling(PragmaTok); |
| 563 | return 0; |
| 564 | } |
| 565 | |
| 566 | // Remember the macro string. |
| 567 | std::string StrVal = getSpelling(Tok); |
| 568 | |
| 569 | // Read the ')'. |
| 570 | Lex(Tok); |
| 571 | if (Tok.isNot(tok::r_paren)) { |
| 572 | Diag(PragmaTok.getLocation(), diag::err_pragma_push_pop_macro_malformed) |
| 573 | << getSpelling(PragmaTok); |
| 574 | return 0; |
| 575 | } |
| 576 | |
| 577 | assert(StrVal[0] == '"' && StrVal[StrVal.size()-1] == '"' && |
| 578 | "Invalid string token!"); |
| 579 | |
| 580 | // Create a Token from the string. |
| 581 | Token MacroTok; |
| 582 | MacroTok.startToken(); |
| 583 | MacroTok.setKind(tok::identifier); |
| 584 | CreateString(&StrVal[1], StrVal.size() - 2, MacroTok); |
| 585 | |
| 586 | // Get the IdentifierInfo of MacroToPushTok. |
| 587 | return LookUpIdentifierInfo(MacroTok); |
| 588 | } |
| 589 | |
| 590 | /// HandlePragmaPushMacro - Handle #pragma push_macro. |
| 591 | /// The syntax is: |
| 592 | /// #pragma push_macro("macro") |
| 593 | void Preprocessor::HandlePragmaPushMacro(Token &PushMacroTok) { |
| 594 | // Parse the pragma directive and get the macro IdentifierInfo*. |
| 595 | IdentifierInfo *IdentInfo = ParsePragmaPushOrPopMacro(PushMacroTok); |
| 596 | if (!IdentInfo) return; |
| 597 | |
| 598 | // Get the MacroInfo associated with IdentInfo. |
| 599 | MacroInfo *MI = getMacroInfo(IdentInfo); |
| 600 | |
| 601 | MacroInfo *MacroCopyToPush = 0; |
| 602 | if (MI) { |
| 603 | // Make a clone of MI. |
| 604 | MacroCopyToPush = CloneMacroInfo(*MI); |
| 605 | |
| 606 | // Allow the original MacroInfo to be redefined later. |
| 607 | MI->setIsAllowRedefinitionsWithoutWarning(true); |
| 608 | } |
| 609 | |
| 610 | // Push the cloned MacroInfo so we can retrieve it later. |
| 611 | PragmaPushMacroInfo[IdentInfo].push_back(MacroCopyToPush); |
| 612 | } |
| 613 | |
| 614 | /// HandlePragmaPopMacro - Handle #pragma push_macro. |
| 615 | /// The syntax is: |
| 616 | /// #pragma pop_macro("macro") |
| 617 | void Preprocessor::HandlePragmaPopMacro(Token &PopMacroTok) { |
| 618 | SourceLocation MessageLoc = PopMacroTok.getLocation(); |
| 619 | |
| 620 | // Parse the pragma directive and get the macro IdentifierInfo*. |
| 621 | IdentifierInfo *IdentInfo = ParsePragmaPushOrPopMacro(PopMacroTok); |
| 622 | if (!IdentInfo) return; |
| 623 | |
| 624 | // Find the vector<MacroInfo*> associated with the macro. |
| 625 | llvm::DenseMap<IdentifierInfo*, std::vector<MacroInfo*> >::iterator iter = |
| 626 | PragmaPushMacroInfo.find(IdentInfo); |
| 627 | if (iter != PragmaPushMacroInfo.end()) { |
| 628 | // Release the MacroInfo currently associated with IdentInfo. |
| 629 | MacroInfo *CurrentMI = getMacroInfo(IdentInfo); |
| 630 | if (CurrentMI) ReleaseMacroInfo(CurrentMI); |
| 631 | |
| 632 | // Get the MacroInfo we want to reinstall. |
| 633 | MacroInfo *MacroToReInstall = iter->second.back(); |
| 634 | |
| 635 | // Reinstall the previously pushed macro. |
| 636 | setMacroInfo(IdentInfo, MacroToReInstall); |
| 637 | |
| 638 | // Pop PragmaPushMacroInfo stack. |
| 639 | iter->second.pop_back(); |
| 640 | if (iter->second.size() == 0) |
| 641 | PragmaPushMacroInfo.erase(iter); |
| 642 | } else { |
| 643 | Diag(MessageLoc, diag::warn_pragma_pop_macro_no_push) |
| 644 | << IdentInfo->getName(); |
| 645 | } |
| 646 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 647 | |
| 648 | /// AddPragmaHandler - Add the specified pragma handler to the preprocessor. |
| 649 | /// If 'Namespace' is non-null, then it is a token required to exist on the |
| 650 | /// pragma line before the pragma string starts, e.g. "STDC" or "GCC". |
Argyrios Kyrtzidis | 9b36c3f | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 651 | void Preprocessor::AddPragmaHandler(llvm::StringRef Namespace, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 652 | PragmaHandler *Handler) { |
| 653 | PragmaNamespace *InsertNS = PragmaHandlers; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 654 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 655 | // If this is specified to be in a namespace, step down into it. |
Argyrios Kyrtzidis | 9b36c3f | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 656 | if (!Namespace.empty()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 657 | // If there is already a pragma handler with the name of this namespace, |
| 658 | // we either have an error (directive with the same name as a namespace) or |
| 659 | // we already have the namespace to insert into. |
Argyrios Kyrtzidis | 9b36c3f | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 660 | if (PragmaHandler *Existing = PragmaHandlers->FindHandler(Namespace)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 661 | InsertNS = Existing->getIfNamespace(); |
| 662 | assert(InsertNS != 0 && "Cannot have a pragma namespace and pragma" |
| 663 | " handler with the same name!"); |
| 664 | } else { |
| 665 | // Otherwise, this namespace doesn't exist yet, create and insert the |
| 666 | // handler for it. |
Argyrios Kyrtzidis | 9b36c3f | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 667 | InsertNS = new PragmaNamespace(Namespace); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 668 | PragmaHandlers->AddPragma(InsertNS); |
| 669 | } |
| 670 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 671 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 672 | // Check to make sure we don't already have a pragma for this identifier. |
| 673 | assert(!InsertNS->FindHandler(Handler->getName()) && |
| 674 | "Pragma handler already exists for this identifier!"); |
| 675 | InsertNS->AddPragma(Handler); |
| 676 | } |
| 677 | |
Daniel Dunbar | 4095080 | 2008-10-04 19:17:46 +0000 | [diff] [blame] | 678 | /// RemovePragmaHandler - Remove the specific pragma handler from the |
| 679 | /// preprocessor. If \arg Namespace is non-null, then it should be the |
| 680 | /// namespace that \arg Handler was added to. It is an error to remove |
| 681 | /// a handler that has not been registered. |
Argyrios Kyrtzidis | 9b36c3f | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 682 | void Preprocessor::RemovePragmaHandler(llvm::StringRef Namespace, |
Daniel Dunbar | 4095080 | 2008-10-04 19:17:46 +0000 | [diff] [blame] | 683 | PragmaHandler *Handler) { |
| 684 | PragmaNamespace *NS = PragmaHandlers; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 685 | |
Daniel Dunbar | 4095080 | 2008-10-04 19:17:46 +0000 | [diff] [blame] | 686 | // If this is specified to be in a namespace, step down into it. |
Argyrios Kyrtzidis | 9b36c3f | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 687 | if (!Namespace.empty()) { |
| 688 | PragmaHandler *Existing = PragmaHandlers->FindHandler(Namespace); |
Daniel Dunbar | 4095080 | 2008-10-04 19:17:46 +0000 | [diff] [blame] | 689 | assert(Existing && "Namespace containing handler does not exist!"); |
| 690 | |
| 691 | NS = Existing->getIfNamespace(); |
| 692 | assert(NS && "Invalid namespace, registered as a regular pragma handler!"); |
| 693 | } |
| 694 | |
| 695 | NS->RemovePragmaHandler(Handler); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 696 | |
Daniel Dunbar | 4095080 | 2008-10-04 19:17:46 +0000 | [diff] [blame] | 697 | // If this is a non-default namespace and it is now empty, remove |
| 698 | // it. |
| 699 | if (NS != PragmaHandlers && NS->IsEmpty()) |
| 700 | PragmaHandlers->RemovePragmaHandler(NS); |
| 701 | } |
| 702 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 703 | namespace { |
Chris Lattner | 2243449 | 2007-12-19 19:38:36 +0000 | [diff] [blame] | 704 | /// PragmaOnceHandler - "#pragma once" marks the file as atomically included. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 705 | struct PragmaOnceHandler : public PragmaHandler { |
Argyrios Kyrtzidis | 9b36c3f | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 706 | PragmaOnceHandler() : PragmaHandler("once") {} |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 707 | virtual void HandlePragma(Preprocessor &PP, Token &OnceTok) { |
Chris Lattner | 35410d5 | 2009-04-14 05:07:49 +0000 | [diff] [blame] | 708 | PP.CheckEndOfDirective("pragma once"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 709 | PP.HandlePragmaOnce(OnceTok); |
| 710 | } |
| 711 | }; |
| 712 | |
Chris Lattner | 2243449 | 2007-12-19 19:38:36 +0000 | [diff] [blame] | 713 | /// PragmaMarkHandler - "#pragma mark ..." is ignored by the compiler, and the |
| 714 | /// rest of the line is not lexed. |
| 715 | struct PragmaMarkHandler : public PragmaHandler { |
Argyrios Kyrtzidis | 9b36c3f | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 716 | PragmaMarkHandler() : PragmaHandler("mark") {} |
Chris Lattner | 2243449 | 2007-12-19 19:38:36 +0000 | [diff] [blame] | 717 | virtual void HandlePragma(Preprocessor &PP, Token &MarkTok) { |
| 718 | PP.HandlePragmaMark(); |
| 719 | } |
| 720 | }; |
| 721 | |
| 722 | /// PragmaPoisonHandler - "#pragma poison x" marks x as not usable. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 723 | struct PragmaPoisonHandler : public PragmaHandler { |
Argyrios Kyrtzidis | 9b36c3f | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 724 | PragmaPoisonHandler() : PragmaHandler("poison") {} |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 725 | virtual void HandlePragma(Preprocessor &PP, Token &PoisonTok) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 726 | PP.HandlePragmaPoison(PoisonTok); |
| 727 | } |
| 728 | }; |
| 729 | |
Chris Lattner | 2243449 | 2007-12-19 19:38:36 +0000 | [diff] [blame] | 730 | /// PragmaSystemHeaderHandler - "#pragma system_header" marks the current file |
| 731 | /// as a system header, which silences warnings in it. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 732 | struct PragmaSystemHeaderHandler : public PragmaHandler { |
Argyrios Kyrtzidis | 9b36c3f | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 733 | PragmaSystemHeaderHandler() : PragmaHandler("system_header") {} |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 734 | virtual void HandlePragma(Preprocessor &PP, Token &SHToken) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 735 | PP.HandlePragmaSystemHeader(SHToken); |
Chris Lattner | 35410d5 | 2009-04-14 05:07:49 +0000 | [diff] [blame] | 736 | PP.CheckEndOfDirective("pragma"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 737 | } |
| 738 | }; |
| 739 | struct PragmaDependencyHandler : public PragmaHandler { |
Argyrios Kyrtzidis | 9b36c3f | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 740 | PragmaDependencyHandler() : PragmaHandler("dependency") {} |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 741 | virtual void HandlePragma(Preprocessor &PP, Token &DepToken) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 742 | PP.HandlePragmaDependency(DepToken); |
| 743 | } |
| 744 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 745 | |
Daniel Dunbar | abf7b72 | 2010-07-28 15:40:33 +0000 | [diff] [blame] | 746 | struct PragmaDebugHandler : public PragmaHandler { |
| 747 | PragmaDebugHandler() : PragmaHandler("__debug") {} |
| 748 | virtual void HandlePragma(Preprocessor &PP, Token &DepToken) { |
| 749 | Token Tok; |
| 750 | PP.LexUnexpandedToken(Tok); |
| 751 | if (Tok.isNot(tok::identifier)) { |
Douglas Gregor | 6493a4d | 2010-08-30 15:15:34 +0000 | [diff] [blame] | 752 | PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid); |
Daniel Dunbar | abf7b72 | 2010-07-28 15:40:33 +0000 | [diff] [blame] | 753 | return; |
| 754 | } |
| 755 | IdentifierInfo *II = Tok.getIdentifierInfo(); |
| 756 | |
Daniel Dunbar | 5505413 | 2010-08-17 22:32:48 +0000 | [diff] [blame] | 757 | if (II->isStr("assert")) { |
| 758 | assert(0 && "This is an assertion!"); |
Daniel Dunbar | abf7b72 | 2010-07-28 15:40:33 +0000 | [diff] [blame] | 759 | } else if (II->isStr("crash")) { |
Daniel Dunbar | 5505413 | 2010-08-17 22:32:48 +0000 | [diff] [blame] | 760 | *(volatile int*) 0x11 = 0; |
| 761 | } else if (II->isStr("llvm_fatal_error")) { |
| 762 | llvm::report_fatal_error("#pragma clang __debug llvm_fatal_error"); |
| 763 | } else if (II->isStr("llvm_unreachable")) { |
| 764 | llvm_unreachable("#pragma clang __debug llvm_unreachable"); |
| 765 | } else if (II->isStr("overflow_stack")) { |
| 766 | DebugOverflowStack(); |
Daniel Dunbar | ff759a6 | 2010-08-18 23:09:23 +0000 | [diff] [blame] | 767 | } else if (II->isStr("handle_crash")) { |
| 768 | llvm::CrashRecoveryContext *CRC =llvm::CrashRecoveryContext::GetCurrent(); |
| 769 | if (CRC) |
| 770 | CRC->HandleCrash(); |
Daniel Dunbar | 5505413 | 2010-08-17 22:32:48 +0000 | [diff] [blame] | 771 | } else { |
| 772 | PP.Diag(Tok, diag::warn_pragma_debug_unexpected_command) |
| 773 | << II->getName(); |
Daniel Dunbar | abf7b72 | 2010-07-28 15:40:33 +0000 | [diff] [blame] | 774 | } |
| 775 | } |
| 776 | |
| 777 | void DebugOverflowStack() { |
| 778 | DebugOverflowStack(); |
| 779 | } |
Daniel Dunbar | abf7b72 | 2010-07-28 15:40:33 +0000 | [diff] [blame] | 780 | }; |
| 781 | |
Chris Lattner | edaf877 | 2009-04-19 23:16:58 +0000 | [diff] [blame] | 782 | /// PragmaDiagnosticHandler - e.g. '#pragma GCC diagnostic ignored "-Wformat"' |
| 783 | struct PragmaDiagnosticHandler : public PragmaHandler { |
Chris Lattner | 04ae2df | 2009-07-12 21:18:45 +0000 | [diff] [blame] | 784 | public: |
Douglas Gregor | 6493a4d | 2010-08-30 15:15:34 +0000 | [diff] [blame] | 785 | explicit PragmaDiagnosticHandler() : PragmaHandler("diagnostic") {} |
Chris Lattner | edaf877 | 2009-04-19 23:16:58 +0000 | [diff] [blame] | 786 | virtual void HandlePragma(Preprocessor &PP, Token &DiagToken) { |
| 787 | Token Tok; |
| 788 | PP.LexUnexpandedToken(Tok); |
| 789 | if (Tok.isNot(tok::identifier)) { |
Douglas Gregor | 6493a4d | 2010-08-30 15:15:34 +0000 | [diff] [blame] | 790 | PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid); |
Chris Lattner | edaf877 | 2009-04-19 23:16:58 +0000 | [diff] [blame] | 791 | return; |
| 792 | } |
| 793 | IdentifierInfo *II = Tok.getIdentifierInfo(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 794 | |
Chris Lattner | edaf877 | 2009-04-19 23:16:58 +0000 | [diff] [blame] | 795 | diag::Mapping Map; |
| 796 | if (II->isStr("warning")) |
| 797 | Map = diag::MAP_WARNING; |
| 798 | else if (II->isStr("error")) |
| 799 | Map = diag::MAP_ERROR; |
| 800 | else if (II->isStr("ignored")) |
| 801 | Map = diag::MAP_IGNORE; |
| 802 | else if (II->isStr("fatal")) |
| 803 | Map = diag::MAP_FATAL; |
Douglas Gregor | 6493a4d | 2010-08-30 15:15:34 +0000 | [diff] [blame] | 804 | else if (II->isStr("pop")) { |
| 805 | if (!PP.getDiagnostics().popMappings()) |
| 806 | PP.Diag(Tok, diag::warn_pragma_diagnostic_cannot_pop); |
Chris Lattner | 04ae2df | 2009-07-12 21:18:45 +0000 | [diff] [blame] | 807 | |
Douglas Gregor | 6493a4d | 2010-08-30 15:15:34 +0000 | [diff] [blame] | 808 | return; |
| 809 | } else if (II->isStr("push")) { |
| 810 | PP.getDiagnostics().pushMappings(); |
Chris Lattner | 04ae2df | 2009-07-12 21:18:45 +0000 | [diff] [blame] | 811 | return; |
| 812 | } else { |
Douglas Gregor | 6493a4d | 2010-08-30 15:15:34 +0000 | [diff] [blame] | 813 | PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid); |
Chris Lattner | edaf877 | 2009-04-19 23:16:58 +0000 | [diff] [blame] | 814 | return; |
| 815 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 816 | |
Chris Lattner | edaf877 | 2009-04-19 23:16:58 +0000 | [diff] [blame] | 817 | PP.LexUnexpandedToken(Tok); |
| 818 | |
| 819 | // We need at least one string. |
| 820 | if (Tok.isNot(tok::string_literal)) { |
| 821 | PP.Diag(Tok.getLocation(), diag::warn_pragma_diagnostic_invalid_token); |
| 822 | return; |
| 823 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 824 | |
Chris Lattner | edaf877 | 2009-04-19 23:16:58 +0000 | [diff] [blame] | 825 | // String concatenation allows multiple strings, which can even come from |
| 826 | // macro expansion. |
| 827 | // "foo " "bar" "Baz" |
| 828 | llvm::SmallVector<Token, 4> StrToks; |
| 829 | while (Tok.is(tok::string_literal)) { |
| 830 | StrToks.push_back(Tok); |
| 831 | PP.LexUnexpandedToken(Tok); |
| 832 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 833 | |
Chris Lattner | edaf877 | 2009-04-19 23:16:58 +0000 | [diff] [blame] | 834 | if (Tok.isNot(tok::eom)) { |
| 835 | PP.Diag(Tok.getLocation(), diag::warn_pragma_diagnostic_invalid_token); |
| 836 | return; |
| 837 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 838 | |
Chris Lattner | edaf877 | 2009-04-19 23:16:58 +0000 | [diff] [blame] | 839 | // Concatenate and parse the strings. |
| 840 | StringLiteralParser Literal(&StrToks[0], StrToks.size(), PP); |
| 841 | assert(!Literal.AnyWide && "Didn't allow wide strings in"); |
| 842 | if (Literal.hadError) |
| 843 | return; |
| 844 | if (Literal.Pascal) { |
Douglas Gregor | 6493a4d | 2010-08-30 15:15:34 +0000 | [diff] [blame] | 845 | PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid); |
Chris Lattner | edaf877 | 2009-04-19 23:16:58 +0000 | [diff] [blame] | 846 | return; |
| 847 | } |
Chris Lattner | 04ae2df | 2009-07-12 21:18:45 +0000 | [diff] [blame] | 848 | |
Chris Lattner | edaf877 | 2009-04-19 23:16:58 +0000 | [diff] [blame] | 849 | std::string WarningName(Literal.GetString(), |
| 850 | Literal.GetString()+Literal.GetStringLength()); |
| 851 | |
| 852 | if (WarningName.size() < 3 || WarningName[0] != '-' || |
| 853 | WarningName[1] != 'W') { |
| 854 | PP.Diag(StrToks[0].getLocation(), |
| 855 | diag::warn_pragma_diagnostic_invalid_option); |
| 856 | return; |
| 857 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 858 | |
Chris Lattner | edaf877 | 2009-04-19 23:16:58 +0000 | [diff] [blame] | 859 | if (PP.getDiagnostics().setDiagnosticGroupMapping(WarningName.c_str()+2, |
| 860 | Map)) |
| 861 | PP.Diag(StrToks[0].getLocation(), |
| 862 | diag::warn_pragma_diagnostic_unknown_warning) << WarningName; |
| 863 | } |
| 864 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 865 | |
Chris Lattner | 636c5ef | 2009-01-16 08:21:25 +0000 | [diff] [blame] | 866 | /// PragmaCommentHandler - "#pragma comment ...". |
| 867 | struct PragmaCommentHandler : public PragmaHandler { |
Argyrios Kyrtzidis | 9b36c3f | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 868 | PragmaCommentHandler() : PragmaHandler("comment") {} |
Chris Lattner | 636c5ef | 2009-01-16 08:21:25 +0000 | [diff] [blame] | 869 | virtual void HandlePragma(Preprocessor &PP, Token &CommentTok) { |
| 870 | PP.HandlePragmaComment(CommentTok); |
| 871 | } |
| 872 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 873 | |
Chris Lattner | abfe094 | 2010-06-26 17:11:39 +0000 | [diff] [blame] | 874 | /// PragmaMessageHandler - "#pragma message("...")". |
| 875 | struct PragmaMessageHandler : public PragmaHandler { |
Argyrios Kyrtzidis | 9b36c3f | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 876 | PragmaMessageHandler() : PragmaHandler("message") {} |
Chris Lattner | abfe094 | 2010-06-26 17:11:39 +0000 | [diff] [blame] | 877 | virtual void HandlePragma(Preprocessor &PP, Token &CommentTok) { |
| 878 | PP.HandlePragmaMessage(CommentTok); |
| 879 | } |
| 880 | }; |
| 881 | |
Chris Lattner | f47724b | 2010-08-17 15:55:45 +0000 | [diff] [blame] | 882 | /// PragmaPushMacroHandler - "#pragma push_macro" saves the value of the |
| 883 | /// macro on the top of the stack. |
| 884 | struct PragmaPushMacroHandler : public PragmaHandler { |
| 885 | PragmaPushMacroHandler() : PragmaHandler("push_macro") {} |
| 886 | virtual void HandlePragma(Preprocessor &PP, Token &PushMacroTok) { |
| 887 | PP.HandlePragmaPushMacro(PushMacroTok); |
| 888 | } |
| 889 | }; |
| 890 | |
| 891 | |
| 892 | /// PragmaPopMacroHandler - "#pragma pop_macro" sets the value of the |
| 893 | /// macro to the value on the top of the stack. |
| 894 | struct PragmaPopMacroHandler : public PragmaHandler { |
| 895 | PragmaPopMacroHandler() : PragmaHandler("pop_macro") {} |
| 896 | virtual void HandlePragma(Preprocessor &PP, Token &PopMacroTok) { |
| 897 | PP.HandlePragmaPopMacro(PopMacroTok); |
| 898 | } |
| 899 | }; |
| 900 | |
Chris Lattner | 062f232 | 2009-04-19 21:20:35 +0000 | [diff] [blame] | 901 | // Pragma STDC implementations. |
Chris Lattner | 6c5cf4a | 2009-04-19 21:50:08 +0000 | [diff] [blame] | 902 | |
| 903 | enum STDCSetting { |
| 904 | STDC_ON, STDC_OFF, STDC_DEFAULT, STDC_INVALID |
| 905 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 906 | |
Chris Lattner | 6c5cf4a | 2009-04-19 21:50:08 +0000 | [diff] [blame] | 907 | static STDCSetting LexOnOffSwitch(Preprocessor &PP) { |
| 908 | Token Tok; |
| 909 | PP.LexUnexpandedToken(Tok); |
| 910 | |
| 911 | if (Tok.isNot(tok::identifier)) { |
| 912 | PP.Diag(Tok, diag::ext_stdc_pragma_syntax); |
| 913 | return STDC_INVALID; |
| 914 | } |
| 915 | IdentifierInfo *II = Tok.getIdentifierInfo(); |
| 916 | STDCSetting Result; |
| 917 | if (II->isStr("ON")) |
| 918 | Result = STDC_ON; |
| 919 | else if (II->isStr("OFF")) |
| 920 | Result = STDC_OFF; |
| 921 | else if (II->isStr("DEFAULT")) |
| 922 | Result = STDC_DEFAULT; |
| 923 | else { |
| 924 | PP.Diag(Tok, diag::ext_stdc_pragma_syntax); |
| 925 | return STDC_INVALID; |
| 926 | } |
| 927 | |
| 928 | // Verify that this is followed by EOM. |
| 929 | PP.LexUnexpandedToken(Tok); |
| 930 | if (Tok.isNot(tok::eom)) |
| 931 | PP.Diag(Tok, diag::ext_stdc_pragma_syntax_eom); |
| 932 | return Result; |
| 933 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 934 | |
Chris Lattner | 062f232 | 2009-04-19 21:20:35 +0000 | [diff] [blame] | 935 | /// PragmaSTDC_FP_CONTRACTHandler - "#pragma STDC FP_CONTRACT ...". |
| 936 | struct PragmaSTDC_FP_CONTRACTHandler : public PragmaHandler { |
Argyrios Kyrtzidis | 9b36c3f | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 937 | PragmaSTDC_FP_CONTRACTHandler() : PragmaHandler("FP_CONTRACT") {} |
Chris Lattner | f545be5 | 2009-04-19 21:25:37 +0000 | [diff] [blame] | 938 | virtual void HandlePragma(Preprocessor &PP, Token &Tok) { |
Chris Lattner | 6c5cf4a | 2009-04-19 21:50:08 +0000 | [diff] [blame] | 939 | // We just ignore the setting of FP_CONTRACT. Since we don't do contractions |
| 940 | // at all, our default is OFF and setting it to ON is an optimization hint |
| 941 | // we can safely ignore. When we support -ffma or something, we would need |
| 942 | // to diagnose that we are ignoring FMA. |
| 943 | LexOnOffSwitch(PP); |
Chris Lattner | 062f232 | 2009-04-19 21:20:35 +0000 | [diff] [blame] | 944 | } |
| 945 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 946 | |
Chris Lattner | 062f232 | 2009-04-19 21:20:35 +0000 | [diff] [blame] | 947 | /// PragmaSTDC_FENV_ACCESSHandler - "#pragma STDC FENV_ACCESS ...". |
| 948 | struct PragmaSTDC_FENV_ACCESSHandler : public PragmaHandler { |
Argyrios Kyrtzidis | 9b36c3f | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 949 | PragmaSTDC_FENV_ACCESSHandler() : PragmaHandler("FENV_ACCESS") {} |
Chris Lattner | f545be5 | 2009-04-19 21:25:37 +0000 | [diff] [blame] | 950 | virtual void HandlePragma(Preprocessor &PP, Token &Tok) { |
Chris Lattner | 4d8aac3 | 2009-04-19 21:55:32 +0000 | [diff] [blame] | 951 | if (LexOnOffSwitch(PP) == STDC_ON) |
| 952 | PP.Diag(Tok, diag::warn_stdc_fenv_access_not_supported); |
Chris Lattner | 062f232 | 2009-04-19 21:20:35 +0000 | [diff] [blame] | 953 | } |
| 954 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 955 | |
Chris Lattner | 062f232 | 2009-04-19 21:20:35 +0000 | [diff] [blame] | 956 | /// PragmaSTDC_CX_LIMITED_RANGEHandler - "#pragma STDC CX_LIMITED_RANGE ...". |
| 957 | struct PragmaSTDC_CX_LIMITED_RANGEHandler : public PragmaHandler { |
Argyrios Kyrtzidis | 9b36c3f | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 958 | PragmaSTDC_CX_LIMITED_RANGEHandler() |
| 959 | : PragmaHandler("CX_LIMITED_RANGE") {} |
Chris Lattner | f545be5 | 2009-04-19 21:25:37 +0000 | [diff] [blame] | 960 | virtual void HandlePragma(Preprocessor &PP, Token &Tok) { |
Chris Lattner | 6c5cf4a | 2009-04-19 21:50:08 +0000 | [diff] [blame] | 961 | LexOnOffSwitch(PP); |
Chris Lattner | 062f232 | 2009-04-19 21:20:35 +0000 | [diff] [blame] | 962 | } |
| 963 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 964 | |
Chris Lattner | 062f232 | 2009-04-19 21:20:35 +0000 | [diff] [blame] | 965 | /// PragmaSTDC_UnknownHandler - "#pragma STDC ...". |
| 966 | struct PragmaSTDC_UnknownHandler : public PragmaHandler { |
Argyrios Kyrtzidis | 9b36c3f | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 967 | PragmaSTDC_UnknownHandler() {} |
Chris Lattner | f545be5 | 2009-04-19 21:25:37 +0000 | [diff] [blame] | 968 | virtual void HandlePragma(Preprocessor &PP, Token &UnknownTok) { |
Chris Lattner | 6c5cf4a | 2009-04-19 21:50:08 +0000 | [diff] [blame] | 969 | // C99 6.10.6p2, unknown forms are not allowed. |
Chris Lattner | f545be5 | 2009-04-19 21:25:37 +0000 | [diff] [blame] | 970 | PP.Diag(UnknownTok, diag::ext_stdc_pragma_ignored); |
Chris Lattner | 062f232 | 2009-04-19 21:20:35 +0000 | [diff] [blame] | 971 | } |
| 972 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 973 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 974 | } // end anonymous namespace |
| 975 | |
| 976 | |
| 977 | /// RegisterBuiltinPragmas - Install the standard preprocessor pragmas: |
| 978 | /// #pragma GCC poison/system_header/dependency and #pragma once. |
| 979 | void Preprocessor::RegisterBuiltinPragmas() { |
Argyrios Kyrtzidis | 9b36c3f | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 980 | AddPragmaHandler(new PragmaOnceHandler()); |
| 981 | AddPragmaHandler(new PragmaMarkHandler()); |
Chris Lattner | f47724b | 2010-08-17 15:55:45 +0000 | [diff] [blame] | 982 | AddPragmaHandler(new PragmaPushMacroHandler()); |
| 983 | AddPragmaHandler(new PragmaPopMacroHandler()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 984 | |
Chris Lattner | e8fa06e | 2009-05-12 18:21:11 +0000 | [diff] [blame] | 985 | // #pragma GCC ... |
Argyrios Kyrtzidis | 9b36c3f | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 986 | AddPragmaHandler("GCC", new PragmaPoisonHandler()); |
| 987 | AddPragmaHandler("GCC", new PragmaSystemHeaderHandler()); |
| 988 | AddPragmaHandler("GCC", new PragmaDependencyHandler()); |
Douglas Gregor | 6493a4d | 2010-08-30 15:15:34 +0000 | [diff] [blame] | 989 | AddPragmaHandler("GCC", new PragmaDiagnosticHandler()); |
Chris Lattner | e8fa06e | 2009-05-12 18:21:11 +0000 | [diff] [blame] | 990 | // #pragma clang ... |
Argyrios Kyrtzidis | 9b36c3f | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 991 | AddPragmaHandler("clang", new PragmaPoisonHandler()); |
| 992 | AddPragmaHandler("clang", new PragmaSystemHeaderHandler()); |
Daniel Dunbar | abf7b72 | 2010-07-28 15:40:33 +0000 | [diff] [blame] | 993 | AddPragmaHandler("clang", new PragmaDebugHandler()); |
Argyrios Kyrtzidis | 9b36c3f | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 994 | AddPragmaHandler("clang", new PragmaDependencyHandler()); |
Douglas Gregor | 6493a4d | 2010-08-30 15:15:34 +0000 | [diff] [blame] | 995 | AddPragmaHandler("clang", new PragmaDiagnosticHandler()); |
Chris Lattner | e8fa06e | 2009-05-12 18:21:11 +0000 | [diff] [blame] | 996 | |
Argyrios Kyrtzidis | 9b36c3f | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 997 | AddPragmaHandler("STDC", new PragmaSTDC_FP_CONTRACTHandler()); |
| 998 | AddPragmaHandler("STDC", new PragmaSTDC_FENV_ACCESSHandler()); |
| 999 | AddPragmaHandler("STDC", new PragmaSTDC_CX_LIMITED_RANGEHandler()); |
Chris Lattner | 062f232 | 2009-04-19 21:20:35 +0000 | [diff] [blame] | 1000 | AddPragmaHandler("STDC", new PragmaSTDC_UnknownHandler()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1001 | |
Chris Lattner | 636c5ef | 2009-01-16 08:21:25 +0000 | [diff] [blame] | 1002 | // MS extensions. |
Chris Lattner | abfe094 | 2010-06-26 17:11:39 +0000 | [diff] [blame] | 1003 | if (Features.Microsoft) { |
Argyrios Kyrtzidis | 9b36c3f | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 1004 | AddPragmaHandler(new PragmaCommentHandler()); |
| 1005 | AddPragmaHandler(new PragmaMessageHandler()); |
Chris Lattner | abfe094 | 2010-06-26 17:11:39 +0000 | [diff] [blame] | 1006 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1007 | } |