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/Basic/FileManager.h" |
| 17 | #include "clang/Basic/SourceManager.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 18 | #include "clang/Lex/HeaderSearch.h" |
| 19 | #include "clang/Lex/LexDiagnostic.h" |
| 20 | #include "clang/Lex/LiteralSupport.h" |
| 21 | #include "clang/Lex/MacroInfo.h" |
| 22 | #include "clang/Lex/Preprocessor.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 | |
Douglas Gregor | 80c60f7 | 2010-09-09 22:45:38 +0000 | [diff] [blame] | 38 | void EmptyPragmaHandler::HandlePragma(Preprocessor &PP, |
| 39 | PragmaIntroducerKind Introducer, |
| 40 | Token &FirstToken) {} |
Daniel Dunbar | c72cc50 | 2010-06-11 20:10:12 +0000 | [diff] [blame] | 41 | |
| 42 | //===----------------------------------------------------------------------===// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 43 | // PragmaNamespace Implementation. |
| 44 | //===----------------------------------------------------------------------===// |
| 45 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 46 | PragmaNamespace::~PragmaNamespace() { |
Argyrios Kyrtzidis | 9b36c3f | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 47 | for (llvm::StringMap<PragmaHandler*>::iterator |
| 48 | I = Handlers.begin(), E = Handlers.end(); I != E; ++I) |
| 49 | delete I->second; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | /// FindHandler - Check to see if there is already a handler for the |
| 53 | /// specified name. If not, return the handler for the null identifier if it |
| 54 | /// exists, otherwise return null. If IgnoreNull is true (the default) then |
| 55 | /// the null handler isn't returned on failure to match. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 56 | PragmaHandler *PragmaNamespace::FindHandler(StringRef Name, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 57 | bool IgnoreNull) const { |
Argyrios Kyrtzidis | 9b36c3f | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 58 | if (PragmaHandler *Handler = Handlers.lookup(Name)) |
| 59 | return Handler; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 60 | return IgnoreNull ? 0 : Handlers.lookup(StringRef()); |
Argyrios Kyrtzidis | 9b36c3f | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 61 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 62 | |
Argyrios Kyrtzidis | 9b36c3f | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 63 | void PragmaNamespace::AddPragma(PragmaHandler *Handler) { |
| 64 | assert(!Handlers.lookup(Handler->getName()) && |
| 65 | "A handler with this name is already registered in this namespace"); |
| 66 | llvm::StringMapEntry<PragmaHandler *> &Entry = |
| 67 | Handlers.GetOrCreateValue(Handler->getName()); |
| 68 | Entry.setValue(Handler); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 69 | } |
| 70 | |
Daniel Dunbar | 4095080 | 2008-10-04 19:17:46 +0000 | [diff] [blame] | 71 | void PragmaNamespace::RemovePragmaHandler(PragmaHandler *Handler) { |
Argyrios Kyrtzidis | 9b36c3f | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 72 | assert(Handlers.lookup(Handler->getName()) && |
| 73 | "Handler not registered in this namespace"); |
| 74 | Handlers.erase(Handler->getName()); |
Daniel Dunbar | 4095080 | 2008-10-04 19:17:46 +0000 | [diff] [blame] | 75 | } |
| 76 | |
Douglas Gregor | 80c60f7 | 2010-09-09 22:45:38 +0000 | [diff] [blame] | 77 | void PragmaNamespace::HandlePragma(Preprocessor &PP, |
| 78 | PragmaIntroducerKind Introducer, |
| 79 | Token &Tok) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 80 | // Read the 'namespace' that the directive is in, e.g. STDC. Do not macro |
| 81 | // expand it, the user can have a STDC #define, that should not affect this. |
| 82 | PP.LexUnexpandedToken(Tok); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 83 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 84 | // 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] | 85 | PragmaHandler *Handler |
| 86 | = FindHandler(Tok.getIdentifierInfo() ? Tok.getIdentifierInfo()->getName() |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 87 | : StringRef(), |
Argyrios Kyrtzidis | 9b36c3f | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 88 | /*IgnoreNull=*/false); |
Chris Lattner | af7cdf4 | 2009-04-19 21:10:26 +0000 | [diff] [blame] | 89 | if (Handler == 0) { |
| 90 | PP.Diag(Tok, diag::warn_pragma_ignored); |
| 91 | return; |
| 92 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 93 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 94 | // Otherwise, pass it down. |
Douglas Gregor | 80c60f7 | 2010-09-09 22:45:38 +0000 | [diff] [blame] | 95 | Handler->HandlePragma(PP, Introducer, Tok); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | //===----------------------------------------------------------------------===// |
| 99 | // Preprocessor Pragma Directive Handling. |
| 100 | //===----------------------------------------------------------------------===// |
| 101 | |
James Dennett | b6e95b7 | 2012-06-17 03:26:26 +0000 | [diff] [blame] | 102 | /// HandlePragmaDirective - The "\#pragma" directive has been parsed. Lex the |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 103 | /// rest of the pragma, passing it to the registered pragma handlers. |
Douglas Gregor | 80c60f7 | 2010-09-09 22:45:38 +0000 | [diff] [blame] | 104 | void Preprocessor::HandlePragmaDirective(unsigned Introducer) { |
Jordan Rose | 6fe6a49 | 2012-06-08 18:06:21 +0000 | [diff] [blame] | 105 | if (!PragmasEnabled) |
| 106 | return; |
| 107 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 108 | ++NumPragma; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 109 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 110 | // Invoke the first level of pragma handlers which reads the namespace id. |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 111 | Token Tok; |
Douglas Gregor | 80c60f7 | 2010-09-09 22:45:38 +0000 | [diff] [blame] | 112 | PragmaHandlers->HandlePragma(*this, PragmaIntroducerKind(Introducer), Tok); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 113 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 114 | // If the pragma handler didn't read the rest of the line, consume it now. |
Peter Collingbourne | b2eb53d | 2011-02-22 13:49:00 +0000 | [diff] [blame] | 115 | if ((CurTokenLexer && CurTokenLexer->isParsingPreprocessorDirective()) |
| 116 | || (CurPPLexer && CurPPLexer->ParsingPreprocessorDirective)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 117 | DiscardUntilEndOfDirective(); |
| 118 | } |
| 119 | |
Argyrios Kyrtzidis | 14e6455 | 2012-04-03 16:47:40 +0000 | [diff] [blame] | 120 | namespace { |
| 121 | /// \brief Helper class for \see Preprocessor::Handle_Pragma. |
| 122 | class LexingFor_PragmaRAII { |
| 123 | Preprocessor &PP; |
| 124 | bool InMacroArgPreExpansion; |
| 125 | bool Failed; |
| 126 | Token &OutTok; |
| 127 | Token PragmaTok; |
| 128 | |
| 129 | public: |
| 130 | LexingFor_PragmaRAII(Preprocessor &PP, bool InMacroArgPreExpansion, |
| 131 | Token &Tok) |
| 132 | : PP(PP), InMacroArgPreExpansion(InMacroArgPreExpansion), |
| 133 | Failed(false), OutTok(Tok) { |
| 134 | if (InMacroArgPreExpansion) { |
| 135 | PragmaTok = OutTok; |
| 136 | PP.EnableBacktrackAtThisPos(); |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | ~LexingFor_PragmaRAII() { |
| 141 | if (InMacroArgPreExpansion) { |
| 142 | if (Failed) { |
| 143 | PP.CommitBacktrackedTokens(); |
| 144 | } else { |
| 145 | PP.Backtrack(); |
| 146 | OutTok = PragmaTok; |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | void failed() { |
| 152 | Failed = true; |
| 153 | } |
| 154 | }; |
| 155 | } |
| 156 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 157 | /// Handle_Pragma - Read a _Pragma directive, slice it up, process it, then |
| 158 | /// return the first token after the directive. The _Pragma token has just |
| 159 | /// been read into 'Tok'. |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 160 | void Preprocessor::Handle_Pragma(Token &Tok) { |
Argyrios Kyrtzidis | 14e6455 | 2012-04-03 16:47:40 +0000 | [diff] [blame] | 161 | |
| 162 | // This works differently if we are pre-expanding a macro argument. |
| 163 | // In that case we don't actually "activate" the pragma now, we only lex it |
| 164 | // until we are sure it is lexically correct and then we backtrack so that |
| 165 | // we activate the pragma whenever we encounter the tokens again in the token |
| 166 | // stream. This ensures that we will activate it in the correct location |
| 167 | // or that we will ignore it if it never enters the token stream, e.g: |
| 168 | // |
| 169 | // #define EMPTY(x) |
| 170 | // #define INACTIVE(x) EMPTY(x) |
| 171 | // INACTIVE(_Pragma("clang diagnostic ignored \"-Wconversion\"")) |
| 172 | |
| 173 | LexingFor_PragmaRAII _PragmaLexing(*this, InMacroArgPreExpansion, Tok); |
| 174 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 175 | // Remember the pragma token location. |
| 176 | SourceLocation PragmaLoc = Tok.getLocation(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 177 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 178 | // Read the '('. |
| 179 | Lex(Tok); |
Chris Lattner | 3692b09 | 2008-11-18 07:59:24 +0000 | [diff] [blame] | 180 | if (Tok.isNot(tok::l_paren)) { |
| 181 | Diag(PragmaLoc, diag::err__Pragma_malformed); |
Argyrios Kyrtzidis | 14e6455 | 2012-04-03 16:47:40 +0000 | [diff] [blame] | 182 | return _PragmaLexing.failed(); |
Chris Lattner | 3692b09 | 2008-11-18 07:59:24 +0000 | [diff] [blame] | 183 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 184 | |
| 185 | // Read the '"..."'. |
| 186 | Lex(Tok); |
Chris Lattner | 3692b09 | 2008-11-18 07:59:24 +0000 | [diff] [blame] | 187 | if (Tok.isNot(tok::string_literal) && Tok.isNot(tok::wide_string_literal)) { |
| 188 | Diag(PragmaLoc, diag::err__Pragma_malformed); |
Richard Smith | 99831e4 | 2012-03-06 03:21:47 +0000 | [diff] [blame] | 189 | // Skip this token, and the ')', if present. |
| 190 | if (Tok.isNot(tok::r_paren)) |
| 191 | Lex(Tok); |
| 192 | if (Tok.is(tok::r_paren)) |
| 193 | Lex(Tok); |
Argyrios Kyrtzidis | 14e6455 | 2012-04-03 16:47:40 +0000 | [diff] [blame] | 194 | return _PragmaLexing.failed(); |
Richard Smith | 99831e4 | 2012-03-06 03:21:47 +0000 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | if (Tok.hasUDSuffix()) { |
| 198 | Diag(Tok, diag::err_invalid_string_udl); |
| 199 | // Skip this token, and the ')', if present. |
| 200 | Lex(Tok); |
| 201 | if (Tok.is(tok::r_paren)) |
| 202 | Lex(Tok); |
Argyrios Kyrtzidis | 14e6455 | 2012-04-03 16:47:40 +0000 | [diff] [blame] | 203 | return _PragmaLexing.failed(); |
Chris Lattner | 3692b09 | 2008-11-18 07:59:24 +0000 | [diff] [blame] | 204 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 205 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 206 | // Remember the string. |
Argyrios Kyrtzidis | 14e6455 | 2012-04-03 16:47:40 +0000 | [diff] [blame] | 207 | Token StrTok = Tok; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 208 | |
| 209 | // Read the ')'. |
| 210 | Lex(Tok); |
Chris Lattner | 3692b09 | 2008-11-18 07:59:24 +0000 | [diff] [blame] | 211 | if (Tok.isNot(tok::r_paren)) { |
| 212 | Diag(PragmaLoc, diag::err__Pragma_malformed); |
Argyrios Kyrtzidis | 14e6455 | 2012-04-03 16:47:40 +0000 | [diff] [blame] | 213 | return _PragmaLexing.failed(); |
Chris Lattner | 3692b09 | 2008-11-18 07:59:24 +0000 | [diff] [blame] | 214 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 215 | |
Argyrios Kyrtzidis | 14e6455 | 2012-04-03 16:47:40 +0000 | [diff] [blame] | 216 | if (InMacroArgPreExpansion) |
| 217 | return; |
| 218 | |
Chris Lattner | e7fb484 | 2009-02-15 20:52:18 +0000 | [diff] [blame] | 219 | SourceLocation RParenLoc = Tok.getLocation(); |
Argyrios Kyrtzidis | 14e6455 | 2012-04-03 16:47:40 +0000 | [diff] [blame] | 220 | std::string StrVal = getSpelling(StrTok); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 221 | |
Chris Lattner | a9d9145 | 2009-01-16 18:59:23 +0000 | [diff] [blame] | 222 | // The _Pragma is lexically sound. Destringize according to C99 6.10.9.1: |
| 223 | // "The string literal is destringized by deleting the L prefix, if present, |
| 224 | // deleting the leading and trailing double-quotes, replacing each escape |
| 225 | // sequence \" by a double-quote, and replacing each escape sequence \\ by a |
| 226 | // single backslash." |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 227 | if (StrVal[0] == 'L') // Remove L prefix. |
| 228 | StrVal.erase(StrVal.begin()); |
| 229 | assert(StrVal[0] == '"' && StrVal[StrVal.size()-1] == '"' && |
| 230 | "Invalid string token!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 231 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 232 | // Remove the front quote, replacing it with a space, so that the pragma |
| 233 | // contents appear to have a space before them. |
| 234 | StrVal[0] = ' '; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 235 | |
Chris Lattner | 1fa4953 | 2009-03-08 08:08:45 +0000 | [diff] [blame] | 236 | // Replace the terminating quote with a \n. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 237 | StrVal[StrVal.size()-1] = '\n'; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 238 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 239 | // Remove escaped quotes and escapes. |
| 240 | for (unsigned i = 0, e = StrVal.size(); i != e-1; ++i) { |
| 241 | if (StrVal[i] == '\\' && |
| 242 | (StrVal[i+1] == '\\' || StrVal[i+1] == '"')) { |
| 243 | // \\ -> '\' and \" -> '"'. |
| 244 | StrVal.erase(StrVal.begin()+i); |
| 245 | --e; |
| 246 | } |
| 247 | } |
John McCall | 1ef8a2e | 2010-08-28 22:34:47 +0000 | [diff] [blame] | 248 | |
Peter Collingbourne | a5ef584 | 2011-02-22 13:49:06 +0000 | [diff] [blame] | 249 | // Plop the string (including the newline and trailing null) into a buffer |
| 250 | // where we can lex it. |
| 251 | Token TmpTok; |
| 252 | TmpTok.startToken(); |
Dmitri Gribenko | 374b383 | 2012-09-24 21:07:17 +0000 | [diff] [blame] | 253 | CreateString(StrVal, TmpTok); |
Peter Collingbourne | a5ef584 | 2011-02-22 13:49:06 +0000 | [diff] [blame] | 254 | SourceLocation TokLoc = TmpTok.getLocation(); |
| 255 | |
| 256 | // Make and enter a lexer object so that we lex and expand the tokens just |
| 257 | // like any others. |
| 258 | Lexer *TL = Lexer::Create_PragmaLexer(TokLoc, PragmaLoc, RParenLoc, |
| 259 | StrVal.size(), *this); |
| 260 | |
| 261 | EnterSourceFileWithLexer(TL, 0); |
| 262 | |
| 263 | // With everything set up, lex this as a #pragma directive. |
| 264 | HandlePragmaDirective(PIK__Pragma); |
John McCall | 1ef8a2e | 2010-08-28 22:34:47 +0000 | [diff] [blame] | 265 | |
| 266 | // Finally, return whatever came after the pragma directive. |
| 267 | return Lex(Tok); |
| 268 | } |
| 269 | |
| 270 | /// HandleMicrosoft__pragma - Like Handle_Pragma except the pragma text |
| 271 | /// is not enclosed within a string literal. |
| 272 | void Preprocessor::HandleMicrosoft__pragma(Token &Tok) { |
| 273 | // Remember the pragma token location. |
| 274 | SourceLocation PragmaLoc = Tok.getLocation(); |
| 275 | |
| 276 | // Read the '('. |
| 277 | Lex(Tok); |
| 278 | if (Tok.isNot(tok::l_paren)) { |
| 279 | Diag(PragmaLoc, diag::err__Pragma_malformed); |
| 280 | return; |
| 281 | } |
| 282 | |
Peter Collingbourne | a5ef584 | 2011-02-22 13:49:06 +0000 | [diff] [blame] | 283 | // Get the tokens enclosed within the __pragma(), as well as the final ')'. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 284 | SmallVector<Token, 32> PragmaToks; |
John McCall | 1ef8a2e | 2010-08-28 22:34:47 +0000 | [diff] [blame] | 285 | int NumParens = 0; |
| 286 | Lex(Tok); |
| 287 | while (Tok.isNot(tok::eof)) { |
Peter Collingbourne | a5ef584 | 2011-02-22 13:49:06 +0000 | [diff] [blame] | 288 | PragmaToks.push_back(Tok); |
John McCall | 1ef8a2e | 2010-08-28 22:34:47 +0000 | [diff] [blame] | 289 | if (Tok.is(tok::l_paren)) |
| 290 | NumParens++; |
| 291 | else if (Tok.is(tok::r_paren) && NumParens-- == 0) |
| 292 | break; |
John McCall | 1ef8a2e | 2010-08-28 22:34:47 +0000 | [diff] [blame] | 293 | Lex(Tok); |
| 294 | } |
| 295 | |
John McCall | 3da92a9 | 2010-08-29 01:09:54 +0000 | [diff] [blame] | 296 | if (Tok.is(tok::eof)) { |
| 297 | Diag(PragmaLoc, diag::err_unterminated___pragma); |
| 298 | return; |
| 299 | } |
| 300 | |
Peter Collingbourne | a5ef584 | 2011-02-22 13:49:06 +0000 | [diff] [blame] | 301 | PragmaToks.front().setFlag(Token::LeadingSpace); |
John McCall | 1ef8a2e | 2010-08-28 22:34:47 +0000 | [diff] [blame] | 302 | |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 303 | // Replace the ')' with an EOD to mark the end of the pragma. |
| 304 | PragmaToks.back().setKind(tok::eod); |
Peter Collingbourne | a5ef584 | 2011-02-22 13:49:06 +0000 | [diff] [blame] | 305 | |
| 306 | Token *TokArray = new Token[PragmaToks.size()]; |
| 307 | std::copy(PragmaToks.begin(), PragmaToks.end(), TokArray); |
| 308 | |
| 309 | // Push the tokens onto the stack. |
| 310 | EnterTokenStream(TokArray, PragmaToks.size(), true, true); |
| 311 | |
| 312 | // With everything set up, lex this as a #pragma directive. |
| 313 | HandlePragmaDirective(PIK___pragma); |
John McCall | 1ef8a2e | 2010-08-28 22:34:47 +0000 | [diff] [blame] | 314 | |
| 315 | // Finally, return whatever came after the pragma directive. |
| 316 | return Lex(Tok); |
| 317 | } |
| 318 | |
James Dennett | b6e95b7 | 2012-06-17 03:26:26 +0000 | [diff] [blame] | 319 | /// HandlePragmaOnce - Handle \#pragma once. OnceTok is the 'once'. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 320 | /// |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 321 | void Preprocessor::HandlePragmaOnce(Token &OnceTok) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 322 | if (isInPrimaryFile()) { |
| 323 | Diag(OnceTok, diag::pp_pragma_once_in_main_file); |
| 324 | return; |
| 325 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 326 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 327 | // 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] | 328 | // Mark the file as a once-only file now. |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 329 | HeaderInfo.MarkFileIncludeOnce(getCurrentFileLexer()->getFileEntry()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 330 | } |
| 331 | |
Chris Lattner | 2243449 | 2007-12-19 19:38:36 +0000 | [diff] [blame] | 332 | void Preprocessor::HandlePragmaMark() { |
Ted Kremenek | 17ff58a | 2008-11-19 22:21:33 +0000 | [diff] [blame] | 333 | assert(CurPPLexer && "No current lexer?"); |
Chris Lattner | 6896a37 | 2009-06-15 05:02:34 +0000 | [diff] [blame] | 334 | if (CurLexer) |
| 335 | CurLexer->ReadToEndOfLine(); |
| 336 | else |
| 337 | CurPTHLexer->DiscardToEndOfLine(); |
Chris Lattner | 2243449 | 2007-12-19 19:38:36 +0000 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | |
James Dennett | b6e95b7 | 2012-06-17 03:26:26 +0000 | [diff] [blame] | 341 | /// HandlePragmaPoison - Handle \#pragma GCC poison. PoisonTok is the 'poison'. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 342 | /// |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 343 | void Preprocessor::HandlePragmaPoison(Token &PoisonTok) { |
| 344 | Token Tok; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 345 | |
| 346 | while (1) { |
| 347 | // Read the next token to poison. While doing this, pretend that we are |
| 348 | // skipping while reading the identifier to poison. |
| 349 | // This avoids errors on code like: |
| 350 | // #pragma GCC poison X |
| 351 | // #pragma GCC poison X |
Ted Kremenek | 68a91d5 | 2008-11-18 01:12:54 +0000 | [diff] [blame] | 352 | if (CurPPLexer) CurPPLexer->LexingRawMode = true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 353 | LexUnexpandedToken(Tok); |
Ted Kremenek | 68a91d5 | 2008-11-18 01:12:54 +0000 | [diff] [blame] | 354 | if (CurPPLexer) CurPPLexer->LexingRawMode = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 355 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 356 | // If we reached the end of line, we're done. |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 357 | if (Tok.is(tok::eod)) return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 358 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 359 | // Can only poison identifiers. |
Abramo Bagnara | c4bf2b9 | 2010-12-22 08:23:18 +0000 | [diff] [blame] | 360 | if (Tok.isNot(tok::raw_identifier)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 361 | Diag(Tok, diag::err_pp_invalid_poison); |
| 362 | return; |
| 363 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 364 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 365 | // Look up the identifier info for the token. We disabled identifier lookup |
| 366 | // by saying we're skipping contents, so we need to do this manually. |
| 367 | IdentifierInfo *II = LookUpIdentifierInfo(Tok); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 368 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 369 | // Already poisoned. |
| 370 | if (II->isPoisoned()) continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 371 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 372 | // If this is a macro identifier, emit a warning. |
Chris Lattner | 0edde55 | 2007-10-07 08:04:56 +0000 | [diff] [blame] | 373 | if (II->hasMacroDefinition()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 374 | Diag(Tok, diag::pp_poisoning_existing_macro); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 375 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 376 | // Finally, poison it! |
| 377 | II->setIsPoisoned(); |
Douglas Gregor | eee242f | 2011-10-27 09:33:13 +0000 | [diff] [blame] | 378 | if (II->isFromAST()) |
| 379 | II->setChangedSinceDeserialization(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 380 | } |
| 381 | } |
| 382 | |
James Dennett | b6e95b7 | 2012-06-17 03:26:26 +0000 | [diff] [blame] | 383 | /// HandlePragmaSystemHeader - Implement \#pragma GCC system_header. We know |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 384 | /// that the whole directive has been parsed. |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 385 | void Preprocessor::HandlePragmaSystemHeader(Token &SysHeaderTok) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 386 | if (isInPrimaryFile()) { |
| 387 | Diag(SysHeaderTok, diag::pp_pragma_sysheader_in_main_file); |
| 388 | return; |
| 389 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 390 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 391 | // 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] | 392 | PreprocessorLexer *TheLexer = getCurrentFileLexer(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 393 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 394 | // Mark the file as a system header. |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 395 | HeaderInfo.MarkFileSystemHeader(TheLexer->getFileEntry()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 396 | |
| 397 | |
Chris Lattner | 6896a37 | 2009-06-15 05:02:34 +0000 | [diff] [blame] | 398 | PresumedLoc PLoc = SourceMgr.getPresumedLoc(SysHeaderTok.getLocation()); |
Douglas Gregor | cb7b1e1 | 2010-11-12 07:15:47 +0000 | [diff] [blame] | 399 | if (PLoc.isInvalid()) |
| 400 | return; |
| 401 | |
Jay Foad | 65aa688 | 2011-06-21 15:13:30 +0000 | [diff] [blame] | 402 | unsigned FilenameID = SourceMgr.getLineTableFilenameID(PLoc.getFilename()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 403 | |
Chris Lattner | 784c257 | 2011-05-22 22:10:16 +0000 | [diff] [blame] | 404 | // Notify the client, if desired, that we are in a new source file. |
| 405 | if (Callbacks) |
| 406 | Callbacks->FileChanged(SysHeaderTok.getLocation(), |
| 407 | PPCallbacks::SystemHeaderPragma, SrcMgr::C_System); |
| 408 | |
Chris Lattner | 6896a37 | 2009-06-15 05:02:34 +0000 | [diff] [blame] | 409 | // Emit a line marker. This will change any source locations from this point |
| 410 | // forward to realize they are in a system header. |
| 411 | // Create a line note with this information. |
| 412 | SourceMgr.AddLineNote(SysHeaderTok.getLocation(), PLoc.getLine(), FilenameID, |
| 413 | false, false, true, false); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 414 | } |
| 415 | |
James Dennett | b6e95b7 | 2012-06-17 03:26:26 +0000 | [diff] [blame] | 416 | /// HandlePragmaDependency - Handle \#pragma GCC dependency "foo" blah. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 417 | /// |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 418 | void Preprocessor::HandlePragmaDependency(Token &DependencyTok) { |
| 419 | Token FilenameTok; |
Ted Kremenek | 68a91d5 | 2008-11-18 01:12:54 +0000 | [diff] [blame] | 420 | CurPPLexer->LexIncludeFilename(FilenameTok); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 421 | |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 422 | // If the token kind is EOD, the error has already been diagnosed. |
| 423 | if (FilenameTok.is(tok::eod)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 424 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 425 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 426 | // Reserve a buffer to get the spelling. |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 427 | SmallString<128> FilenameBuffer; |
Douglas Gregor | 453091c | 2010-03-16 22:30:13 +0000 | [diff] [blame] | 428 | bool Invalid = false; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 429 | StringRef Filename = getSpelling(FilenameTok, FilenameBuffer, &Invalid); |
Douglas Gregor | 453091c | 2010-03-16 22:30:13 +0000 | [diff] [blame] | 430 | if (Invalid) |
| 431 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 432 | |
Chris Lattner | a139481 | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 433 | bool isAngled = |
| 434 | GetIncludeFilenameSpelling(FilenameTok.getLocation(), Filename); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 435 | // If GetIncludeFilenameSpelling set the start ptr to null, there was an |
| 436 | // error. |
Chris Lattner | a139481 | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 437 | if (Filename.empty()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 438 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 439 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 440 | // Search include directories for this file. |
| 441 | const DirectoryLookup *CurDir; |
Douglas Gregor | fba18aa | 2011-09-15 22:00:41 +0000 | [diff] [blame] | 442 | const FileEntry *File = LookupFile(Filename, isAngled, 0, CurDir, NULL, NULL, |
| 443 | NULL); |
Chris Lattner | 56b05c8 | 2008-11-18 08:02:48 +0000 | [diff] [blame] | 444 | if (File == 0) { |
Eli Friedman | f84139a | 2011-08-30 23:07:51 +0000 | [diff] [blame] | 445 | if (!SuppressIncludeNotFoundError) |
| 446 | Diag(FilenameTok, diag::err_pp_file_not_found) << Filename; |
Chris Lattner | 56b05c8 | 2008-11-18 08:02:48 +0000 | [diff] [blame] | 447 | return; |
| 448 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 449 | |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 450 | const FileEntry *CurFile = getCurrentFileLexer()->getFileEntry(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 451 | |
| 452 | // If this file is older than the file it depends on, emit a diagnostic. |
| 453 | if (CurFile && CurFile->getModificationTime() < File->getModificationTime()) { |
| 454 | // Lex tokens at the end of the message and include them in the message. |
| 455 | std::string Message; |
| 456 | Lex(DependencyTok); |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 457 | while (DependencyTok.isNot(tok::eod)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 458 | Message += getSpelling(DependencyTok) + " "; |
| 459 | Lex(DependencyTok); |
| 460 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 461 | |
Chris Lattner | 96de259 | 2010-09-05 23:16:09 +0000 | [diff] [blame] | 462 | // Remove the trailing ' ' if present. |
| 463 | if (!Message.empty()) |
| 464 | Message.erase(Message.end()-1); |
Chris Lattner | 56b05c8 | 2008-11-18 08:02:48 +0000 | [diff] [blame] | 465 | Diag(FilenameTok, diag::pp_out_of_date_dependency) << Message; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 466 | } |
| 467 | } |
| 468 | |
James Dennett | b6e95b7 | 2012-06-17 03:26:26 +0000 | [diff] [blame] | 469 | /// \brief Handle the microsoft \#pragma comment extension. |
| 470 | /// |
| 471 | /// The syntax is: |
| 472 | /// \code |
Dmitri Gribenko | e74dc19 | 2012-11-30 20:04:39 +0000 | [diff] [blame] | 473 | /// #pragma comment(linker, "foo") |
James Dennett | b6e95b7 | 2012-06-17 03:26:26 +0000 | [diff] [blame] | 474 | /// \endcode |
Chris Lattner | 636c5ef | 2009-01-16 08:21:25 +0000 | [diff] [blame] | 475 | /// 'linker' is one of five identifiers: compiler, exestr, lib, linker, user. |
| 476 | /// "foo" is a string, which is fully macro expanded, and permits string |
Gabor Greif | d7ee349 | 2009-03-17 11:39:38 +0000 | [diff] [blame] | 477 | /// concatenation, embedded escape characters etc. See MSDN for more details. |
Chris Lattner | 636c5ef | 2009-01-16 08:21:25 +0000 | [diff] [blame] | 478 | void Preprocessor::HandlePragmaComment(Token &Tok) { |
| 479 | SourceLocation CommentLoc = Tok.getLocation(); |
| 480 | Lex(Tok); |
| 481 | if (Tok.isNot(tok::l_paren)) { |
| 482 | Diag(CommentLoc, diag::err_pragma_comment_malformed); |
| 483 | return; |
| 484 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 485 | |
Chris Lattner | 636c5ef | 2009-01-16 08:21:25 +0000 | [diff] [blame] | 486 | // Read the identifier. |
| 487 | Lex(Tok); |
| 488 | if (Tok.isNot(tok::identifier)) { |
| 489 | Diag(CommentLoc, diag::err_pragma_comment_malformed); |
| 490 | return; |
| 491 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 492 | |
Chris Lattner | 636c5ef | 2009-01-16 08:21:25 +0000 | [diff] [blame] | 493 | // Verify that this is one of the 5 whitelisted options. |
| 494 | // FIXME: warn that 'exestr' is deprecated. |
| 495 | const IdentifierInfo *II = Tok.getIdentifierInfo(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 496 | if (!II->isStr("compiler") && !II->isStr("exestr") && !II->isStr("lib") && |
Chris Lattner | 636c5ef | 2009-01-16 08:21:25 +0000 | [diff] [blame] | 497 | !II->isStr("linker") && !II->isStr("user")) { |
| 498 | Diag(Tok.getLocation(), diag::err_pragma_comment_unknown_kind); |
| 499 | return; |
| 500 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 501 | |
Chris Lattner | a9d9145 | 2009-01-16 18:59:23 +0000 | [diff] [blame] | 502 | // Read the optional string if present. |
Chris Lattner | 636c5ef | 2009-01-16 08:21:25 +0000 | [diff] [blame] | 503 | Lex(Tok); |
Chris Lattner | a9d9145 | 2009-01-16 18:59:23 +0000 | [diff] [blame] | 504 | std::string ArgumentString; |
Andy Gibbs | 02a1768 | 2012-11-17 19:15:38 +0000 | [diff] [blame] | 505 | if (Tok.is(tok::comma) && !LexStringLiteral(Tok, ArgumentString, |
Andy Gibbs | 97f8461 | 2012-11-17 19:16:52 +0000 | [diff] [blame] | 506 | "pragma comment", |
Andy Gibbs | 02a1768 | 2012-11-17 19:15:38 +0000 | [diff] [blame] | 507 | /*MacroExpansion=*/true)) |
| 508 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 509 | |
Chris Lattner | a9d9145 | 2009-01-16 18:59:23 +0000 | [diff] [blame] | 510 | // FIXME: If the kind is "compiler" warn if the string is present (it is |
| 511 | // ignored). |
| 512 | // FIXME: 'lib' requires a comment string. |
| 513 | // FIXME: 'linker' requires a comment string, and has a specific list of |
| 514 | // things that are allowable. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 515 | |
Chris Lattner | 636c5ef | 2009-01-16 08:21:25 +0000 | [diff] [blame] | 516 | if (Tok.isNot(tok::r_paren)) { |
| 517 | Diag(Tok.getLocation(), diag::err_pragma_comment_malformed); |
| 518 | return; |
| 519 | } |
Chris Lattner | a9d9145 | 2009-01-16 18:59:23 +0000 | [diff] [blame] | 520 | Lex(Tok); // eat the r_paren. |
Chris Lattner | 636c5ef | 2009-01-16 08:21:25 +0000 | [diff] [blame] | 521 | |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 522 | if (Tok.isNot(tok::eod)) { |
Chris Lattner | 636c5ef | 2009-01-16 08:21:25 +0000 | [diff] [blame] | 523 | Diag(Tok.getLocation(), diag::err_pragma_comment_malformed); |
| 524 | return; |
| 525 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 526 | |
Chris Lattner | a9d9145 | 2009-01-16 18:59:23 +0000 | [diff] [blame] | 527 | // If the pragma is lexically sound, notify any interested PPCallbacks. |
Chris Lattner | 172e336 | 2009-01-16 19:01:46 +0000 | [diff] [blame] | 528 | if (Callbacks) |
| 529 | Callbacks->PragmaComment(CommentLoc, II, ArgumentString); |
Chris Lattner | 636c5ef | 2009-01-16 08:21:25 +0000 | [diff] [blame] | 530 | } |
| 531 | |
James Dennett | b6e95b7 | 2012-06-17 03:26:26 +0000 | [diff] [blame] | 532 | /// HandlePragmaMessage - Handle the microsoft and gcc \#pragma message |
Michael J. Spencer | 301669b | 2010-09-27 06:19:02 +0000 | [diff] [blame] | 533 | /// extension. The syntax is: |
James Dennett | b6e95b7 | 2012-06-17 03:26:26 +0000 | [diff] [blame] | 534 | /// \code |
Dmitri Gribenko | e74dc19 | 2012-11-30 20:04:39 +0000 | [diff] [blame] | 535 | /// #pragma message(string) |
James Dennett | b6e95b7 | 2012-06-17 03:26:26 +0000 | [diff] [blame] | 536 | /// \endcode |
Michael J. Spencer | 301669b | 2010-09-27 06:19:02 +0000 | [diff] [blame] | 537 | /// OR, in GCC mode: |
James Dennett | b6e95b7 | 2012-06-17 03:26:26 +0000 | [diff] [blame] | 538 | /// \code |
Dmitri Gribenko | e74dc19 | 2012-11-30 20:04:39 +0000 | [diff] [blame] | 539 | /// #pragma message string |
James Dennett | b6e95b7 | 2012-06-17 03:26:26 +0000 | [diff] [blame] | 540 | /// \endcode |
Michael J. Spencer | 301669b | 2010-09-27 06:19:02 +0000 | [diff] [blame] | 541 | /// string is a string, which is fully macro expanded, and permits string |
| 542 | /// concatenation, embedded escape characters, etc... See MSDN for more details. |
Chris Lattner | abfe094 | 2010-06-26 17:11:39 +0000 | [diff] [blame] | 543 | void Preprocessor::HandlePragmaMessage(Token &Tok) { |
| 544 | SourceLocation MessageLoc = Tok.getLocation(); |
| 545 | Lex(Tok); |
Michael J. Spencer | 301669b | 2010-09-27 06:19:02 +0000 | [diff] [blame] | 546 | bool ExpectClosingParen = false; |
Michael J. Spencer | d83fc54 | 2010-09-27 06:34:47 +0000 | [diff] [blame] | 547 | switch (Tok.getKind()) { |
Michael J. Spencer | 301669b | 2010-09-27 06:19:02 +0000 | [diff] [blame] | 548 | case tok::l_paren: |
| 549 | // We have a MSVC style pragma message. |
| 550 | ExpectClosingParen = true; |
| 551 | // Read the string. |
| 552 | Lex(Tok); |
| 553 | break; |
| 554 | case tok::string_literal: |
| 555 | // We have a GCC style pragma message, and we just read the string. |
| 556 | break; |
| 557 | default: |
Chris Lattner | abfe094 | 2010-06-26 17:11:39 +0000 | [diff] [blame] | 558 | Diag(MessageLoc, diag::err_pragma_message_malformed); |
| 559 | return; |
| 560 | } |
Chris Lattner | 636c5ef | 2009-01-16 08:21:25 +0000 | [diff] [blame] | 561 | |
Andy Gibbs | 02a1768 | 2012-11-17 19:15:38 +0000 | [diff] [blame] | 562 | std::string MessageString; |
Andy Gibbs | 97f8461 | 2012-11-17 19:16:52 +0000 | [diff] [blame] | 563 | if (!FinishLexStringLiteral(Tok, MessageString, "pragma message", |
| 564 | /*MacroExpansion=*/true)) |
Chris Lattner | abfe094 | 2010-06-26 17:11:39 +0000 | [diff] [blame] | 565 | return; |
Chris Lattner | abfe094 | 2010-06-26 17:11:39 +0000 | [diff] [blame] | 566 | |
Michael J. Spencer | 301669b | 2010-09-27 06:19:02 +0000 | [diff] [blame] | 567 | if (ExpectClosingParen) { |
| 568 | if (Tok.isNot(tok::r_paren)) { |
| 569 | Diag(Tok.getLocation(), diag::err_pragma_message_malformed); |
| 570 | return; |
| 571 | } |
| 572 | Lex(Tok); // eat the r_paren. |
Chris Lattner | abfe094 | 2010-06-26 17:11:39 +0000 | [diff] [blame] | 573 | } |
Chris Lattner | abfe094 | 2010-06-26 17:11:39 +0000 | [diff] [blame] | 574 | |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 575 | if (Tok.isNot(tok::eod)) { |
Chris Lattner | abfe094 | 2010-06-26 17:11:39 +0000 | [diff] [blame] | 576 | Diag(Tok.getLocation(), diag::err_pragma_message_malformed); |
| 577 | return; |
| 578 | } |
| 579 | |
| 580 | // Output the message. |
| 581 | Diag(MessageLoc, diag::warn_pragma_message) << MessageString; |
| 582 | |
| 583 | // If the pragma is lexically sound, notify any interested PPCallbacks. |
| 584 | if (Callbacks) |
| 585 | Callbacks->PragmaMessage(MessageLoc, MessageString); |
| 586 | } |
Chris Lattner | 636c5ef | 2009-01-16 08:21:25 +0000 | [diff] [blame] | 587 | |
Chris Lattner | f47724b | 2010-08-17 15:55:45 +0000 | [diff] [blame] | 588 | /// ParsePragmaPushOrPopMacro - Handle parsing of pragma push_macro/pop_macro. |
| 589 | /// Return the IdentifierInfo* associated with the macro to push or pop. |
| 590 | IdentifierInfo *Preprocessor::ParsePragmaPushOrPopMacro(Token &Tok) { |
| 591 | // Remember the pragma token location. |
| 592 | Token PragmaTok = Tok; |
| 593 | |
| 594 | // Read the '('. |
| 595 | Lex(Tok); |
| 596 | if (Tok.isNot(tok::l_paren)) { |
| 597 | Diag(PragmaTok.getLocation(), diag::err_pragma_push_pop_macro_malformed) |
| 598 | << getSpelling(PragmaTok); |
| 599 | return 0; |
| 600 | } |
| 601 | |
| 602 | // Read the macro name string. |
| 603 | Lex(Tok); |
| 604 | if (Tok.isNot(tok::string_literal)) { |
| 605 | Diag(PragmaTok.getLocation(), diag::err_pragma_push_pop_macro_malformed) |
| 606 | << getSpelling(PragmaTok); |
| 607 | return 0; |
| 608 | } |
| 609 | |
Richard Smith | 99831e4 | 2012-03-06 03:21:47 +0000 | [diff] [blame] | 610 | if (Tok.hasUDSuffix()) { |
| 611 | Diag(Tok, diag::err_invalid_string_udl); |
| 612 | return 0; |
| 613 | } |
| 614 | |
Chris Lattner | f47724b | 2010-08-17 15:55:45 +0000 | [diff] [blame] | 615 | // Remember the macro string. |
| 616 | std::string StrVal = getSpelling(Tok); |
| 617 | |
| 618 | // Read the ')'. |
| 619 | Lex(Tok); |
| 620 | if (Tok.isNot(tok::r_paren)) { |
| 621 | Diag(PragmaTok.getLocation(), diag::err_pragma_push_pop_macro_malformed) |
| 622 | << getSpelling(PragmaTok); |
| 623 | return 0; |
| 624 | } |
| 625 | |
| 626 | assert(StrVal[0] == '"' && StrVal[StrVal.size()-1] == '"' && |
| 627 | "Invalid string token!"); |
| 628 | |
| 629 | // Create a Token from the string. |
| 630 | Token MacroTok; |
| 631 | MacroTok.startToken(); |
Abramo Bagnara | c4bf2b9 | 2010-12-22 08:23:18 +0000 | [diff] [blame] | 632 | MacroTok.setKind(tok::raw_identifier); |
Dmitri Gribenko | 374b383 | 2012-09-24 21:07:17 +0000 | [diff] [blame] | 633 | CreateString(StringRef(&StrVal[1], StrVal.size() - 2), MacroTok); |
Chris Lattner | f47724b | 2010-08-17 15:55:45 +0000 | [diff] [blame] | 634 | |
| 635 | // Get the IdentifierInfo of MacroToPushTok. |
| 636 | return LookUpIdentifierInfo(MacroTok); |
| 637 | } |
| 638 | |
James Dennett | b6e95b7 | 2012-06-17 03:26:26 +0000 | [diff] [blame] | 639 | /// \brief Handle \#pragma push_macro. |
| 640 | /// |
Chris Lattner | f47724b | 2010-08-17 15:55:45 +0000 | [diff] [blame] | 641 | /// The syntax is: |
James Dennett | b6e95b7 | 2012-06-17 03:26:26 +0000 | [diff] [blame] | 642 | /// \code |
Dmitri Gribenko | e74dc19 | 2012-11-30 20:04:39 +0000 | [diff] [blame] | 643 | /// #pragma push_macro("macro") |
James Dennett | b6e95b7 | 2012-06-17 03:26:26 +0000 | [diff] [blame] | 644 | /// \endcode |
Chris Lattner | f47724b | 2010-08-17 15:55:45 +0000 | [diff] [blame] | 645 | void Preprocessor::HandlePragmaPushMacro(Token &PushMacroTok) { |
| 646 | // Parse the pragma directive and get the macro IdentifierInfo*. |
| 647 | IdentifierInfo *IdentInfo = ParsePragmaPushOrPopMacro(PushMacroTok); |
| 648 | if (!IdentInfo) return; |
| 649 | |
| 650 | // Get the MacroInfo associated with IdentInfo. |
| 651 | MacroInfo *MI = getMacroInfo(IdentInfo); |
| 652 | |
| 653 | MacroInfo *MacroCopyToPush = 0; |
| 654 | if (MI) { |
| 655 | // Make a clone of MI. |
| 656 | MacroCopyToPush = CloneMacroInfo(*MI); |
| 657 | |
| 658 | // Allow the original MacroInfo to be redefined later. |
| 659 | MI->setIsAllowRedefinitionsWithoutWarning(true); |
| 660 | } |
| 661 | |
| 662 | // Push the cloned MacroInfo so we can retrieve it later. |
| 663 | PragmaPushMacroInfo[IdentInfo].push_back(MacroCopyToPush); |
| 664 | } |
| 665 | |
James Dennett | b6e95b7 | 2012-06-17 03:26:26 +0000 | [diff] [blame] | 666 | /// \brief Handle \#pragma pop_macro. |
| 667 | /// |
Chris Lattner | f47724b | 2010-08-17 15:55:45 +0000 | [diff] [blame] | 668 | /// The syntax is: |
James Dennett | b6e95b7 | 2012-06-17 03:26:26 +0000 | [diff] [blame] | 669 | /// \code |
Chris Lattner | f47724b | 2010-08-17 15:55:45 +0000 | [diff] [blame] | 670 | /// #pragma pop_macro("macro") |
James Dennett | b6e95b7 | 2012-06-17 03:26:26 +0000 | [diff] [blame] | 671 | /// \endcode |
Chris Lattner | f47724b | 2010-08-17 15:55:45 +0000 | [diff] [blame] | 672 | void Preprocessor::HandlePragmaPopMacro(Token &PopMacroTok) { |
| 673 | SourceLocation MessageLoc = PopMacroTok.getLocation(); |
| 674 | |
| 675 | // Parse the pragma directive and get the macro IdentifierInfo*. |
| 676 | IdentifierInfo *IdentInfo = ParsePragmaPushOrPopMacro(PopMacroTok); |
| 677 | if (!IdentInfo) return; |
| 678 | |
| 679 | // Find the vector<MacroInfo*> associated with the macro. |
| 680 | llvm::DenseMap<IdentifierInfo*, std::vector<MacroInfo*> >::iterator iter = |
| 681 | PragmaPushMacroInfo.find(IdentInfo); |
| 682 | if (iter != PragmaPushMacroInfo.end()) { |
Alexander Kornienko | 8a64bb5 | 2012-08-29 00:20:03 +0000 | [diff] [blame] | 683 | // Forget the MacroInfo currently associated with IdentInfo. |
| 684 | if (MacroInfo *CurrentMI = getMacroInfo(IdentInfo)) { |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 685 | if (CurrentMI->isWarnIfUnused()) |
| 686 | WarnUnusedMacroLocs.erase(CurrentMI->getDefinitionLoc()); |
Douglas Gregor | a8235d6 | 2012-10-09 23:05:51 +0000 | [diff] [blame] | 687 | UndefineMacro(IdentInfo, CurrentMI, MessageLoc); |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 688 | } |
Chris Lattner | f47724b | 2010-08-17 15:55:45 +0000 | [diff] [blame] | 689 | |
| 690 | // Get the MacroInfo we want to reinstall. |
| 691 | MacroInfo *MacroToReInstall = iter->second.back(); |
| 692 | |
Alexander Kornienko | e40c423 | 2012-08-29 16:56:24 +0000 | [diff] [blame] | 693 | if (MacroToReInstall) { |
| 694 | // Reinstall the previously pushed macro. |
| 695 | setMacroInfo(IdentInfo, MacroToReInstall); |
| 696 | } else if (IdentInfo->hasMacroDefinition()) { |
| 697 | clearMacroInfo(IdentInfo); |
| 698 | } |
Chris Lattner | f47724b | 2010-08-17 15:55:45 +0000 | [diff] [blame] | 699 | |
| 700 | // Pop PragmaPushMacroInfo stack. |
| 701 | iter->second.pop_back(); |
| 702 | if (iter->second.size() == 0) |
| 703 | PragmaPushMacroInfo.erase(iter); |
| 704 | } else { |
| 705 | Diag(MessageLoc, diag::warn_pragma_pop_macro_no_push) |
| 706 | << IdentInfo->getName(); |
| 707 | } |
| 708 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 709 | |
Aaron Ballman | 4c55c54 | 2012-03-02 22:51:54 +0000 | [diff] [blame] | 710 | void Preprocessor::HandlePragmaIncludeAlias(Token &Tok) { |
| 711 | // We will either get a quoted filename or a bracketed filename, and we |
| 712 | // have to track which we got. The first filename is the source name, |
| 713 | // and the second name is the mapped filename. If the first is quoted, |
| 714 | // the second must be as well (cannot mix and match quotes and brackets). |
Aaron Ballman | 4c55c54 | 2012-03-02 22:51:54 +0000 | [diff] [blame] | 715 | |
| 716 | // Get the open paren |
| 717 | Lex(Tok); |
| 718 | if (Tok.isNot(tok::l_paren)) { |
| 719 | Diag(Tok, diag::warn_pragma_include_alias_expected) << "("; |
| 720 | return; |
| 721 | } |
| 722 | |
| 723 | // We expect either a quoted string literal, or a bracketed name |
| 724 | Token SourceFilenameTok; |
| 725 | CurPPLexer->LexIncludeFilename(SourceFilenameTok); |
| 726 | if (SourceFilenameTok.is(tok::eod)) { |
| 727 | // The diagnostic has already been handled |
| 728 | return; |
| 729 | } |
| 730 | |
| 731 | StringRef SourceFileName; |
| 732 | SmallString<128> FileNameBuffer; |
| 733 | if (SourceFilenameTok.is(tok::string_literal) || |
| 734 | SourceFilenameTok.is(tok::angle_string_literal)) { |
| 735 | SourceFileName = getSpelling(SourceFilenameTok, FileNameBuffer); |
| 736 | } else if (SourceFilenameTok.is(tok::less)) { |
| 737 | // This could be a path instead of just a name |
| 738 | FileNameBuffer.push_back('<'); |
| 739 | SourceLocation End; |
| 740 | if (ConcatenateIncludeName(FileNameBuffer, End)) |
| 741 | return; // Diagnostic already emitted |
| 742 | SourceFileName = FileNameBuffer.str(); |
| 743 | } else { |
| 744 | Diag(Tok, diag::warn_pragma_include_alias_expected_filename); |
| 745 | return; |
| 746 | } |
| 747 | FileNameBuffer.clear(); |
| 748 | |
| 749 | // Now we expect a comma, followed by another include name |
| 750 | Lex(Tok); |
| 751 | if (Tok.isNot(tok::comma)) { |
| 752 | Diag(Tok, diag::warn_pragma_include_alias_expected) << ","; |
| 753 | return; |
| 754 | } |
| 755 | |
| 756 | Token ReplaceFilenameTok; |
| 757 | CurPPLexer->LexIncludeFilename(ReplaceFilenameTok); |
| 758 | if (ReplaceFilenameTok.is(tok::eod)) { |
| 759 | // The diagnostic has already been handled |
| 760 | return; |
| 761 | } |
| 762 | |
| 763 | StringRef ReplaceFileName; |
| 764 | if (ReplaceFilenameTok.is(tok::string_literal) || |
| 765 | ReplaceFilenameTok.is(tok::angle_string_literal)) { |
| 766 | ReplaceFileName = getSpelling(ReplaceFilenameTok, FileNameBuffer); |
| 767 | } else if (ReplaceFilenameTok.is(tok::less)) { |
| 768 | // This could be a path instead of just a name |
| 769 | FileNameBuffer.push_back('<'); |
| 770 | SourceLocation End; |
| 771 | if (ConcatenateIncludeName(FileNameBuffer, End)) |
| 772 | return; // Diagnostic already emitted |
| 773 | ReplaceFileName = FileNameBuffer.str(); |
| 774 | } else { |
| 775 | Diag(Tok, diag::warn_pragma_include_alias_expected_filename); |
| 776 | return; |
| 777 | } |
| 778 | |
| 779 | // Finally, we expect the closing paren |
| 780 | Lex(Tok); |
| 781 | if (Tok.isNot(tok::r_paren)) { |
| 782 | Diag(Tok, diag::warn_pragma_include_alias_expected) << ")"; |
| 783 | return; |
| 784 | } |
| 785 | |
| 786 | // Now that we have the source and target filenames, we need to make sure |
| 787 | // they're both of the same type (angled vs non-angled) |
| 788 | StringRef OriginalSource = SourceFileName; |
| 789 | |
| 790 | bool SourceIsAngled = |
| 791 | GetIncludeFilenameSpelling(SourceFilenameTok.getLocation(), |
| 792 | SourceFileName); |
| 793 | bool ReplaceIsAngled = |
| 794 | GetIncludeFilenameSpelling(ReplaceFilenameTok.getLocation(), |
| 795 | ReplaceFileName); |
| 796 | if (!SourceFileName.empty() && !ReplaceFileName.empty() && |
| 797 | (SourceIsAngled != ReplaceIsAngled)) { |
| 798 | unsigned int DiagID; |
| 799 | if (SourceIsAngled) |
| 800 | DiagID = diag::warn_pragma_include_alias_mismatch_angle; |
| 801 | else |
| 802 | DiagID = diag::warn_pragma_include_alias_mismatch_quote; |
| 803 | |
| 804 | Diag(SourceFilenameTok.getLocation(), DiagID) |
| 805 | << SourceFileName |
| 806 | << ReplaceFileName; |
| 807 | |
| 808 | return; |
| 809 | } |
| 810 | |
| 811 | // Now we can let the include handler know about this mapping |
| 812 | getHeaderSearchInfo().AddIncludeAlias(OriginalSource, ReplaceFileName); |
| 813 | } |
| 814 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 815 | /// AddPragmaHandler - Add the specified pragma handler to the preprocessor. |
| 816 | /// If 'Namespace' is non-null, then it is a token required to exist on the |
| 817 | /// pragma line before the pragma string starts, e.g. "STDC" or "GCC". |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 818 | void Preprocessor::AddPragmaHandler(StringRef Namespace, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 819 | PragmaHandler *Handler) { |
| 820 | PragmaNamespace *InsertNS = PragmaHandlers; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 821 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 822 | // 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] | 823 | if (!Namespace.empty()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 824 | // If there is already a pragma handler with the name of this namespace, |
| 825 | // we either have an error (directive with the same name as a namespace) or |
| 826 | // we already have the namespace to insert into. |
Argyrios Kyrtzidis | 9b36c3f | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 827 | if (PragmaHandler *Existing = PragmaHandlers->FindHandler(Namespace)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 828 | InsertNS = Existing->getIfNamespace(); |
| 829 | assert(InsertNS != 0 && "Cannot have a pragma namespace and pragma" |
| 830 | " handler with the same name!"); |
| 831 | } else { |
| 832 | // Otherwise, this namespace doesn't exist yet, create and insert the |
| 833 | // handler for it. |
Argyrios Kyrtzidis | 9b36c3f | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 834 | InsertNS = new PragmaNamespace(Namespace); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 835 | PragmaHandlers->AddPragma(InsertNS); |
| 836 | } |
| 837 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 838 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 839 | // Check to make sure we don't already have a pragma for this identifier. |
| 840 | assert(!InsertNS->FindHandler(Handler->getName()) && |
| 841 | "Pragma handler already exists for this identifier!"); |
| 842 | InsertNS->AddPragma(Handler); |
| 843 | } |
| 844 | |
Daniel Dunbar | 4095080 | 2008-10-04 19:17:46 +0000 | [diff] [blame] | 845 | /// RemovePragmaHandler - Remove the specific pragma handler from the |
| 846 | /// preprocessor. If \arg Namespace is non-null, then it should be the |
| 847 | /// namespace that \arg Handler was added to. It is an error to remove |
| 848 | /// a handler that has not been registered. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 849 | void Preprocessor::RemovePragmaHandler(StringRef Namespace, |
Daniel Dunbar | 4095080 | 2008-10-04 19:17:46 +0000 | [diff] [blame] | 850 | PragmaHandler *Handler) { |
| 851 | PragmaNamespace *NS = PragmaHandlers; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 852 | |
Daniel Dunbar | 4095080 | 2008-10-04 19:17:46 +0000 | [diff] [blame] | 853 | // 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] | 854 | if (!Namespace.empty()) { |
| 855 | PragmaHandler *Existing = PragmaHandlers->FindHandler(Namespace); |
Daniel Dunbar | 4095080 | 2008-10-04 19:17:46 +0000 | [diff] [blame] | 856 | assert(Existing && "Namespace containing handler does not exist!"); |
| 857 | |
| 858 | NS = Existing->getIfNamespace(); |
| 859 | assert(NS && "Invalid namespace, registered as a regular pragma handler!"); |
| 860 | } |
| 861 | |
| 862 | NS->RemovePragmaHandler(Handler); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 863 | |
Daniel Dunbar | 4095080 | 2008-10-04 19:17:46 +0000 | [diff] [blame] | 864 | // If this is a non-default namespace and it is now empty, remove |
| 865 | // it. |
Argyrios Kyrtzidis | ce52bb3 | 2012-01-06 00:22:09 +0000 | [diff] [blame] | 866 | if (NS != PragmaHandlers && NS->IsEmpty()) { |
Daniel Dunbar | 4095080 | 2008-10-04 19:17:46 +0000 | [diff] [blame] | 867 | PragmaHandlers->RemovePragmaHandler(NS); |
Argyrios Kyrtzidis | ce52bb3 | 2012-01-06 00:22:09 +0000 | [diff] [blame] | 868 | delete NS; |
| 869 | } |
Daniel Dunbar | 4095080 | 2008-10-04 19:17:46 +0000 | [diff] [blame] | 870 | } |
| 871 | |
Peter Collingbourne | 9d3f5f7 | 2011-02-14 01:42:24 +0000 | [diff] [blame] | 872 | bool Preprocessor::LexOnOffSwitch(tok::OnOffSwitch &Result) { |
| 873 | Token Tok; |
| 874 | LexUnexpandedToken(Tok); |
| 875 | |
| 876 | if (Tok.isNot(tok::identifier)) { |
| 877 | Diag(Tok, diag::ext_on_off_switch_syntax); |
| 878 | return true; |
| 879 | } |
| 880 | IdentifierInfo *II = Tok.getIdentifierInfo(); |
| 881 | if (II->isStr("ON")) |
| 882 | Result = tok::OOS_ON; |
| 883 | else if (II->isStr("OFF")) |
| 884 | Result = tok::OOS_OFF; |
| 885 | else if (II->isStr("DEFAULT")) |
| 886 | Result = tok::OOS_DEFAULT; |
| 887 | else { |
| 888 | Diag(Tok, diag::ext_on_off_switch_syntax); |
| 889 | return true; |
| 890 | } |
| 891 | |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 892 | // Verify that this is followed by EOD. |
Peter Collingbourne | 9d3f5f7 | 2011-02-14 01:42:24 +0000 | [diff] [blame] | 893 | LexUnexpandedToken(Tok); |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 894 | if (Tok.isNot(tok::eod)) |
| 895 | Diag(Tok, diag::ext_pragma_syntax_eod); |
Peter Collingbourne | 9d3f5f7 | 2011-02-14 01:42:24 +0000 | [diff] [blame] | 896 | return false; |
| 897 | } |
| 898 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 899 | namespace { |
James Dennett | b6e95b7 | 2012-06-17 03:26:26 +0000 | [diff] [blame] | 900 | /// PragmaOnceHandler - "\#pragma once" marks the file as atomically included. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 901 | struct PragmaOnceHandler : public PragmaHandler { |
Argyrios Kyrtzidis | 9b36c3f | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 902 | PragmaOnceHandler() : PragmaHandler("once") {} |
Douglas Gregor | 80c60f7 | 2010-09-09 22:45:38 +0000 | [diff] [blame] | 903 | virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 904 | Token &OnceTok) { |
Chris Lattner | 35410d5 | 2009-04-14 05:07:49 +0000 | [diff] [blame] | 905 | PP.CheckEndOfDirective("pragma once"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 906 | PP.HandlePragmaOnce(OnceTok); |
| 907 | } |
| 908 | }; |
| 909 | |
James Dennett | b6e95b7 | 2012-06-17 03:26:26 +0000 | [diff] [blame] | 910 | /// PragmaMarkHandler - "\#pragma mark ..." is ignored by the compiler, and the |
Chris Lattner | 2243449 | 2007-12-19 19:38:36 +0000 | [diff] [blame] | 911 | /// rest of the line is not lexed. |
| 912 | struct PragmaMarkHandler : public PragmaHandler { |
Argyrios Kyrtzidis | 9b36c3f | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 913 | PragmaMarkHandler() : PragmaHandler("mark") {} |
Douglas Gregor | 80c60f7 | 2010-09-09 22:45:38 +0000 | [diff] [blame] | 914 | virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 915 | Token &MarkTok) { |
Chris Lattner | 2243449 | 2007-12-19 19:38:36 +0000 | [diff] [blame] | 916 | PP.HandlePragmaMark(); |
| 917 | } |
| 918 | }; |
| 919 | |
James Dennett | b6e95b7 | 2012-06-17 03:26:26 +0000 | [diff] [blame] | 920 | /// PragmaPoisonHandler - "\#pragma poison x" marks x as not usable. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 921 | struct PragmaPoisonHandler : public PragmaHandler { |
Argyrios Kyrtzidis | 9b36c3f | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 922 | PragmaPoisonHandler() : PragmaHandler("poison") {} |
Douglas Gregor | 80c60f7 | 2010-09-09 22:45:38 +0000 | [diff] [blame] | 923 | virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 924 | Token &PoisonTok) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 925 | PP.HandlePragmaPoison(PoisonTok); |
| 926 | } |
| 927 | }; |
| 928 | |
James Dennett | b6e95b7 | 2012-06-17 03:26:26 +0000 | [diff] [blame] | 929 | /// PragmaSystemHeaderHandler - "\#pragma system_header" marks the current file |
Chris Lattner | 2243449 | 2007-12-19 19:38:36 +0000 | [diff] [blame] | 930 | /// as a system header, which silences warnings in it. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 931 | struct PragmaSystemHeaderHandler : public PragmaHandler { |
Argyrios Kyrtzidis | 9b36c3f | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 932 | PragmaSystemHeaderHandler() : PragmaHandler("system_header") {} |
Douglas Gregor | 80c60f7 | 2010-09-09 22:45:38 +0000 | [diff] [blame] | 933 | virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 934 | Token &SHToken) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 935 | PP.HandlePragmaSystemHeader(SHToken); |
Chris Lattner | 35410d5 | 2009-04-14 05:07:49 +0000 | [diff] [blame] | 936 | PP.CheckEndOfDirective("pragma"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 937 | } |
| 938 | }; |
| 939 | struct PragmaDependencyHandler : public PragmaHandler { |
Argyrios Kyrtzidis | 9b36c3f | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 940 | PragmaDependencyHandler() : PragmaHandler("dependency") {} |
Douglas Gregor | 80c60f7 | 2010-09-09 22:45:38 +0000 | [diff] [blame] | 941 | virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 942 | Token &DepToken) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 943 | PP.HandlePragmaDependency(DepToken); |
| 944 | } |
| 945 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 946 | |
Daniel Dunbar | abf7b72 | 2010-07-28 15:40:33 +0000 | [diff] [blame] | 947 | struct PragmaDebugHandler : public PragmaHandler { |
| 948 | PragmaDebugHandler() : PragmaHandler("__debug") {} |
Douglas Gregor | 80c60f7 | 2010-09-09 22:45:38 +0000 | [diff] [blame] | 949 | virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 950 | Token &DepToken) { |
Daniel Dunbar | abf7b72 | 2010-07-28 15:40:33 +0000 | [diff] [blame] | 951 | Token Tok; |
| 952 | PP.LexUnexpandedToken(Tok); |
| 953 | if (Tok.isNot(tok::identifier)) { |
Douglas Gregor | 6493a4d | 2010-08-30 15:15:34 +0000 | [diff] [blame] | 954 | PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid); |
Daniel Dunbar | abf7b72 | 2010-07-28 15:40:33 +0000 | [diff] [blame] | 955 | return; |
| 956 | } |
| 957 | IdentifierInfo *II = Tok.getIdentifierInfo(); |
| 958 | |
Daniel Dunbar | 5505413 | 2010-08-17 22:32:48 +0000 | [diff] [blame] | 959 | if (II->isStr("assert")) { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 960 | llvm_unreachable("This is an assertion!"); |
Daniel Dunbar | abf7b72 | 2010-07-28 15:40:33 +0000 | [diff] [blame] | 961 | } else if (II->isStr("crash")) { |
David Blaikie | 377da4c | 2012-08-21 18:56:49 +0000 | [diff] [blame] | 962 | LLVM_BUILTIN_TRAP; |
David Blaikie | e75d9cf | 2012-06-29 22:03:56 +0000 | [diff] [blame] | 963 | } else if (II->isStr("parser_crash")) { |
| 964 | Token Crasher; |
| 965 | Crasher.setKind(tok::annot_pragma_parser_crash); |
| 966 | PP.EnterToken(Crasher); |
Daniel Dunbar | 5505413 | 2010-08-17 22:32:48 +0000 | [diff] [blame] | 967 | } else if (II->isStr("llvm_fatal_error")) { |
| 968 | llvm::report_fatal_error("#pragma clang __debug llvm_fatal_error"); |
| 969 | } else if (II->isStr("llvm_unreachable")) { |
| 970 | llvm_unreachable("#pragma clang __debug llvm_unreachable"); |
| 971 | } else if (II->isStr("overflow_stack")) { |
| 972 | DebugOverflowStack(); |
Daniel Dunbar | ff759a6 | 2010-08-18 23:09:23 +0000 | [diff] [blame] | 973 | } else if (II->isStr("handle_crash")) { |
| 974 | llvm::CrashRecoveryContext *CRC =llvm::CrashRecoveryContext::GetCurrent(); |
| 975 | if (CRC) |
| 976 | CRC->HandleCrash(); |
Daniel Dunbar | 5505413 | 2010-08-17 22:32:48 +0000 | [diff] [blame] | 977 | } else { |
| 978 | PP.Diag(Tok, diag::warn_pragma_debug_unexpected_command) |
| 979 | << II->getName(); |
Daniel Dunbar | abf7b72 | 2010-07-28 15:40:33 +0000 | [diff] [blame] | 980 | } |
| 981 | } |
| 982 | |
Francois Pichet | 1066c6c | 2011-05-25 16:15:03 +0000 | [diff] [blame] | 983 | // Disable MSVC warning about runtime stack overflow. |
| 984 | #ifdef _MSC_VER |
| 985 | #pragma warning(disable : 4717) |
| 986 | #endif |
Daniel Dunbar | abf7b72 | 2010-07-28 15:40:33 +0000 | [diff] [blame] | 987 | void DebugOverflowStack() { |
| 988 | DebugOverflowStack(); |
| 989 | } |
Francois Pichet | 1066c6c | 2011-05-25 16:15:03 +0000 | [diff] [blame] | 990 | #ifdef _MSC_VER |
| 991 | #pragma warning(default : 4717) |
| 992 | #endif |
| 993 | |
Daniel Dunbar | abf7b72 | 2010-07-28 15:40:33 +0000 | [diff] [blame] | 994 | }; |
| 995 | |
James Dennett | b6e95b7 | 2012-06-17 03:26:26 +0000 | [diff] [blame] | 996 | /// PragmaDiagnosticHandler - e.g. '\#pragma GCC diagnostic ignored "-Wformat"' |
Chris Lattner | edaf877 | 2009-04-19 23:16:58 +0000 | [diff] [blame] | 997 | struct PragmaDiagnosticHandler : public PragmaHandler { |
Douglas Gregor | c09ce12 | 2011-06-22 19:41:48 +0000 | [diff] [blame] | 998 | private: |
| 999 | const char *Namespace; |
Chris Lattner | 04ae2df | 2009-07-12 21:18:45 +0000 | [diff] [blame] | 1000 | public: |
Douglas Gregor | c09ce12 | 2011-06-22 19:41:48 +0000 | [diff] [blame] | 1001 | explicit PragmaDiagnosticHandler(const char *NS) : |
| 1002 | PragmaHandler("diagnostic"), Namespace(NS) {} |
Douglas Gregor | 80c60f7 | 2010-09-09 22:45:38 +0000 | [diff] [blame] | 1003 | virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 1004 | Token &DiagToken) { |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 1005 | SourceLocation DiagLoc = DiagToken.getLocation(); |
Chris Lattner | edaf877 | 2009-04-19 23:16:58 +0000 | [diff] [blame] | 1006 | Token Tok; |
| 1007 | PP.LexUnexpandedToken(Tok); |
| 1008 | if (Tok.isNot(tok::identifier)) { |
Douglas Gregor | 6493a4d | 2010-08-30 15:15:34 +0000 | [diff] [blame] | 1009 | PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid); |
Chris Lattner | edaf877 | 2009-04-19 23:16:58 +0000 | [diff] [blame] | 1010 | return; |
| 1011 | } |
| 1012 | IdentifierInfo *II = Tok.getIdentifierInfo(); |
Douglas Gregor | c09ce12 | 2011-06-22 19:41:48 +0000 | [diff] [blame] | 1013 | PPCallbacks *Callbacks = PP.getPPCallbacks(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1014 | |
Chris Lattner | edaf877 | 2009-04-19 23:16:58 +0000 | [diff] [blame] | 1015 | diag::Mapping Map; |
| 1016 | if (II->isStr("warning")) |
| 1017 | Map = diag::MAP_WARNING; |
| 1018 | else if (II->isStr("error")) |
| 1019 | Map = diag::MAP_ERROR; |
| 1020 | else if (II->isStr("ignored")) |
| 1021 | Map = diag::MAP_IGNORE; |
| 1022 | else if (II->isStr("fatal")) |
| 1023 | Map = diag::MAP_FATAL; |
Douglas Gregor | 6493a4d | 2010-08-30 15:15:34 +0000 | [diff] [blame] | 1024 | else if (II->isStr("pop")) { |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 1025 | if (!PP.getDiagnostics().popMappings(DiagLoc)) |
Douglas Gregor | 6493a4d | 2010-08-30 15:15:34 +0000 | [diff] [blame] | 1026 | PP.Diag(Tok, diag::warn_pragma_diagnostic_cannot_pop); |
Douglas Gregor | c09ce12 | 2011-06-22 19:41:48 +0000 | [diff] [blame] | 1027 | else if (Callbacks) |
| 1028 | Callbacks->PragmaDiagnosticPop(DiagLoc, Namespace); |
Douglas Gregor | 6493a4d | 2010-08-30 15:15:34 +0000 | [diff] [blame] | 1029 | return; |
| 1030 | } else if (II->isStr("push")) { |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 1031 | PP.getDiagnostics().pushMappings(DiagLoc); |
Douglas Gregor | c09ce12 | 2011-06-22 19:41:48 +0000 | [diff] [blame] | 1032 | if (Callbacks) |
| 1033 | Callbacks->PragmaDiagnosticPush(DiagLoc, Namespace); |
Chris Lattner | 04ae2df | 2009-07-12 21:18:45 +0000 | [diff] [blame] | 1034 | return; |
| 1035 | } else { |
Douglas Gregor | 6493a4d | 2010-08-30 15:15:34 +0000 | [diff] [blame] | 1036 | PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid); |
Chris Lattner | edaf877 | 2009-04-19 23:16:58 +0000 | [diff] [blame] | 1037 | return; |
| 1038 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1039 | |
Chris Lattner | edaf877 | 2009-04-19 23:16:58 +0000 | [diff] [blame] | 1040 | PP.LexUnexpandedToken(Tok); |
Andy Gibbs | 02a1768 | 2012-11-17 19:15:38 +0000 | [diff] [blame] | 1041 | SourceLocation StringLoc = Tok.getLocation(); |
Chris Lattner | edaf877 | 2009-04-19 23:16:58 +0000 | [diff] [blame] | 1042 | |
Andy Gibbs | 02a1768 | 2012-11-17 19:15:38 +0000 | [diff] [blame] | 1043 | std::string WarningName; |
Andy Gibbs | 97f8461 | 2012-11-17 19:16:52 +0000 | [diff] [blame] | 1044 | if (!PP.FinishLexStringLiteral(Tok, WarningName, "pragma diagnostic", |
| 1045 | /*MacroExpansion=*/false)) |
Chris Lattner | edaf877 | 2009-04-19 23:16:58 +0000 | [diff] [blame] | 1046 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1047 | |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 1048 | if (Tok.isNot(tok::eod)) { |
Chris Lattner | edaf877 | 2009-04-19 23:16:58 +0000 | [diff] [blame] | 1049 | PP.Diag(Tok.getLocation(), diag::warn_pragma_diagnostic_invalid_token); |
| 1050 | return; |
| 1051 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1052 | |
Chris Lattner | edaf877 | 2009-04-19 23:16:58 +0000 | [diff] [blame] | 1053 | if (WarningName.size() < 3 || WarningName[0] != '-' || |
| 1054 | WarningName[1] != 'W') { |
Andy Gibbs | 02a1768 | 2012-11-17 19:15:38 +0000 | [diff] [blame] | 1055 | PP.Diag(StringLoc, diag::warn_pragma_diagnostic_invalid_option); |
Chris Lattner | edaf877 | 2009-04-19 23:16:58 +0000 | [diff] [blame] | 1056 | return; |
| 1057 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1058 | |
Argyrios Kyrtzidis | 477aab6 | 2011-05-25 05:05:01 +0000 | [diff] [blame] | 1059 | if (PP.getDiagnostics().setDiagnosticGroupMapping(WarningName.substr(2), |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 1060 | Map, DiagLoc)) |
Andy Gibbs | 02a1768 | 2012-11-17 19:15:38 +0000 | [diff] [blame] | 1061 | PP.Diag(StringLoc, diag::warn_pragma_diagnostic_unknown_warning) |
| 1062 | << WarningName; |
Douglas Gregor | c09ce12 | 2011-06-22 19:41:48 +0000 | [diff] [blame] | 1063 | else if (Callbacks) |
| 1064 | Callbacks->PragmaDiagnostic(DiagLoc, Namespace, Map, WarningName); |
Chris Lattner | edaf877 | 2009-04-19 23:16:58 +0000 | [diff] [blame] | 1065 | } |
| 1066 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1067 | |
James Dennett | b6e95b7 | 2012-06-17 03:26:26 +0000 | [diff] [blame] | 1068 | /// PragmaCommentHandler - "\#pragma comment ...". |
Chris Lattner | 636c5ef | 2009-01-16 08:21:25 +0000 | [diff] [blame] | 1069 | struct PragmaCommentHandler : public PragmaHandler { |
Argyrios Kyrtzidis | 9b36c3f | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 1070 | PragmaCommentHandler() : PragmaHandler("comment") {} |
Douglas Gregor | 80c60f7 | 2010-09-09 22:45:38 +0000 | [diff] [blame] | 1071 | virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 1072 | Token &CommentTok) { |
Chris Lattner | 636c5ef | 2009-01-16 08:21:25 +0000 | [diff] [blame] | 1073 | PP.HandlePragmaComment(CommentTok); |
| 1074 | } |
| 1075 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1076 | |
James Dennett | b6e95b7 | 2012-06-17 03:26:26 +0000 | [diff] [blame] | 1077 | /// PragmaIncludeAliasHandler - "\#pragma include_alias("...")". |
Aaron Ballman | 4c55c54 | 2012-03-02 22:51:54 +0000 | [diff] [blame] | 1078 | struct PragmaIncludeAliasHandler : public PragmaHandler { |
| 1079 | PragmaIncludeAliasHandler() : PragmaHandler("include_alias") {} |
| 1080 | virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 1081 | Token &IncludeAliasTok) { |
| 1082 | PP.HandlePragmaIncludeAlias(IncludeAliasTok); |
| 1083 | } |
| 1084 | }; |
| 1085 | |
James Dennett | b6e95b7 | 2012-06-17 03:26:26 +0000 | [diff] [blame] | 1086 | /// PragmaMessageHandler - "\#pragma message("...")". |
Chris Lattner | abfe094 | 2010-06-26 17:11:39 +0000 | [diff] [blame] | 1087 | struct PragmaMessageHandler : public PragmaHandler { |
Argyrios Kyrtzidis | 9b36c3f | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 1088 | PragmaMessageHandler() : PragmaHandler("message") {} |
Douglas Gregor | 80c60f7 | 2010-09-09 22:45:38 +0000 | [diff] [blame] | 1089 | virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 1090 | Token &CommentTok) { |
Chris Lattner | abfe094 | 2010-06-26 17:11:39 +0000 | [diff] [blame] | 1091 | PP.HandlePragmaMessage(CommentTok); |
| 1092 | } |
| 1093 | }; |
| 1094 | |
James Dennett | b6e95b7 | 2012-06-17 03:26:26 +0000 | [diff] [blame] | 1095 | /// PragmaPushMacroHandler - "\#pragma push_macro" saves the value of the |
Chris Lattner | f47724b | 2010-08-17 15:55:45 +0000 | [diff] [blame] | 1096 | /// macro on the top of the stack. |
| 1097 | struct PragmaPushMacroHandler : public PragmaHandler { |
| 1098 | PragmaPushMacroHandler() : PragmaHandler("push_macro") {} |
Douglas Gregor | 80c60f7 | 2010-09-09 22:45:38 +0000 | [diff] [blame] | 1099 | virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 1100 | Token &PushMacroTok) { |
Chris Lattner | f47724b | 2010-08-17 15:55:45 +0000 | [diff] [blame] | 1101 | PP.HandlePragmaPushMacro(PushMacroTok); |
| 1102 | } |
| 1103 | }; |
| 1104 | |
| 1105 | |
James Dennett | b6e95b7 | 2012-06-17 03:26:26 +0000 | [diff] [blame] | 1106 | /// PragmaPopMacroHandler - "\#pragma pop_macro" sets the value of the |
Chris Lattner | f47724b | 2010-08-17 15:55:45 +0000 | [diff] [blame] | 1107 | /// macro to the value on the top of the stack. |
| 1108 | struct PragmaPopMacroHandler : public PragmaHandler { |
| 1109 | PragmaPopMacroHandler() : PragmaHandler("pop_macro") {} |
Douglas Gregor | 80c60f7 | 2010-09-09 22:45:38 +0000 | [diff] [blame] | 1110 | virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 1111 | Token &PopMacroTok) { |
Chris Lattner | f47724b | 2010-08-17 15:55:45 +0000 | [diff] [blame] | 1112 | PP.HandlePragmaPopMacro(PopMacroTok); |
| 1113 | } |
| 1114 | }; |
| 1115 | |
Chris Lattner | 062f232 | 2009-04-19 21:20:35 +0000 | [diff] [blame] | 1116 | // Pragma STDC implementations. |
Chris Lattner | 6c5cf4a | 2009-04-19 21:50:08 +0000 | [diff] [blame] | 1117 | |
James Dennett | b6e95b7 | 2012-06-17 03:26:26 +0000 | [diff] [blame] | 1118 | /// PragmaSTDC_FENV_ACCESSHandler - "\#pragma STDC FENV_ACCESS ...". |
Chris Lattner | 062f232 | 2009-04-19 21:20:35 +0000 | [diff] [blame] | 1119 | struct PragmaSTDC_FENV_ACCESSHandler : public PragmaHandler { |
Argyrios Kyrtzidis | 9b36c3f | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 1120 | PragmaSTDC_FENV_ACCESSHandler() : PragmaHandler("FENV_ACCESS") {} |
Douglas Gregor | 80c60f7 | 2010-09-09 22:45:38 +0000 | [diff] [blame] | 1121 | virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 1122 | Token &Tok) { |
Peter Collingbourne | 9d3f5f7 | 2011-02-14 01:42:24 +0000 | [diff] [blame] | 1123 | tok::OnOffSwitch OOS; |
| 1124 | if (PP.LexOnOffSwitch(OOS)) |
| 1125 | return; |
| 1126 | if (OOS == tok::OOS_ON) |
Chris Lattner | 4d8aac3 | 2009-04-19 21:55:32 +0000 | [diff] [blame] | 1127 | PP.Diag(Tok, diag::warn_stdc_fenv_access_not_supported); |
Chris Lattner | 062f232 | 2009-04-19 21:20:35 +0000 | [diff] [blame] | 1128 | } |
| 1129 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1130 | |
James Dennett | b6e95b7 | 2012-06-17 03:26:26 +0000 | [diff] [blame] | 1131 | /// PragmaSTDC_CX_LIMITED_RANGEHandler - "\#pragma STDC CX_LIMITED_RANGE ...". |
Chris Lattner | 062f232 | 2009-04-19 21:20:35 +0000 | [diff] [blame] | 1132 | struct PragmaSTDC_CX_LIMITED_RANGEHandler : public PragmaHandler { |
Argyrios Kyrtzidis | 9b36c3f | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 1133 | PragmaSTDC_CX_LIMITED_RANGEHandler() |
| 1134 | : PragmaHandler("CX_LIMITED_RANGE") {} |
Douglas Gregor | 80c60f7 | 2010-09-09 22:45:38 +0000 | [diff] [blame] | 1135 | virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 1136 | Token &Tok) { |
Peter Collingbourne | 9d3f5f7 | 2011-02-14 01:42:24 +0000 | [diff] [blame] | 1137 | tok::OnOffSwitch OOS; |
| 1138 | PP.LexOnOffSwitch(OOS); |
Chris Lattner | 062f232 | 2009-04-19 21:20:35 +0000 | [diff] [blame] | 1139 | } |
| 1140 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1141 | |
James Dennett | b6e95b7 | 2012-06-17 03:26:26 +0000 | [diff] [blame] | 1142 | /// PragmaSTDC_UnknownHandler - "\#pragma STDC ...". |
Chris Lattner | 062f232 | 2009-04-19 21:20:35 +0000 | [diff] [blame] | 1143 | struct PragmaSTDC_UnknownHandler : public PragmaHandler { |
Argyrios Kyrtzidis | 9b36c3f | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 1144 | PragmaSTDC_UnknownHandler() {} |
Douglas Gregor | 80c60f7 | 2010-09-09 22:45:38 +0000 | [diff] [blame] | 1145 | virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 1146 | Token &UnknownTok) { |
Chris Lattner | 6c5cf4a | 2009-04-19 21:50:08 +0000 | [diff] [blame] | 1147 | // C99 6.10.6p2, unknown forms are not allowed. |
Chris Lattner | f545be5 | 2009-04-19 21:25:37 +0000 | [diff] [blame] | 1148 | PP.Diag(UnknownTok, diag::ext_stdc_pragma_ignored); |
Chris Lattner | 062f232 | 2009-04-19 21:20:35 +0000 | [diff] [blame] | 1149 | } |
| 1150 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1151 | |
John McCall | 8dfac0b | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 1152 | /// PragmaARCCFCodeAuditedHandler - |
James Dennett | b6e95b7 | 2012-06-17 03:26:26 +0000 | [diff] [blame] | 1153 | /// \#pragma clang arc_cf_code_audited begin/end |
John McCall | 8dfac0b | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 1154 | struct PragmaARCCFCodeAuditedHandler : public PragmaHandler { |
| 1155 | PragmaARCCFCodeAuditedHandler() : PragmaHandler("arc_cf_code_audited") {} |
| 1156 | virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 1157 | Token &NameTok) { |
| 1158 | SourceLocation Loc = NameTok.getLocation(); |
| 1159 | bool IsBegin; |
| 1160 | |
| 1161 | Token Tok; |
| 1162 | |
| 1163 | // Lex the 'begin' or 'end'. |
| 1164 | PP.LexUnexpandedToken(Tok); |
| 1165 | const IdentifierInfo *BeginEnd = Tok.getIdentifierInfo(); |
| 1166 | if (BeginEnd && BeginEnd->isStr("begin")) { |
| 1167 | IsBegin = true; |
| 1168 | } else if (BeginEnd && BeginEnd->isStr("end")) { |
| 1169 | IsBegin = false; |
| 1170 | } else { |
| 1171 | PP.Diag(Tok.getLocation(), diag::err_pp_arc_cf_code_audited_syntax); |
| 1172 | return; |
| 1173 | } |
| 1174 | |
| 1175 | // Verify that this is followed by EOD. |
| 1176 | PP.LexUnexpandedToken(Tok); |
| 1177 | if (Tok.isNot(tok::eod)) |
| 1178 | PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma"; |
| 1179 | |
| 1180 | // The start location of the active audit. |
| 1181 | SourceLocation BeginLoc = PP.getPragmaARCCFCodeAuditedLoc(); |
| 1182 | |
| 1183 | // The start location we want after processing this. |
| 1184 | SourceLocation NewLoc; |
| 1185 | |
| 1186 | if (IsBegin) { |
| 1187 | // Complain about attempts to re-enter an audit. |
| 1188 | if (BeginLoc.isValid()) { |
| 1189 | PP.Diag(Loc, diag::err_pp_double_begin_of_arc_cf_code_audited); |
| 1190 | PP.Diag(BeginLoc, diag::note_pragma_entered_here); |
| 1191 | } |
| 1192 | NewLoc = Loc; |
| 1193 | } else { |
| 1194 | // Complain about attempts to leave an audit that doesn't exist. |
| 1195 | if (!BeginLoc.isValid()) { |
| 1196 | PP.Diag(Loc, diag::err_pp_unmatched_end_of_arc_cf_code_audited); |
| 1197 | return; |
| 1198 | } |
| 1199 | NewLoc = SourceLocation(); |
| 1200 | } |
| 1201 | |
| 1202 | PP.setPragmaARCCFCodeAuditedLoc(NewLoc); |
| 1203 | } |
| 1204 | }; |
| 1205 | |
Aaron Ballman | fafd101 | 2012-11-30 19:52:30 +0000 | [diff] [blame] | 1206 | /// \brief Handle "\#pragma region [...]" |
| 1207 | /// |
| 1208 | /// The syntax is |
| 1209 | /// \code |
Dmitri Gribenko | e74dc19 | 2012-11-30 20:04:39 +0000 | [diff] [blame] | 1210 | /// #pragma region [optional name] |
| 1211 | /// #pragma endregion [optional comment] |
Aaron Ballman | fafd101 | 2012-11-30 19:52:30 +0000 | [diff] [blame] | 1212 | /// \endcode |
| 1213 | /// |
| 1214 | /// \note This is |
| 1215 | /// <a href="http://msdn.microsoft.com/en-us/library/b6xkz944(v=vs.80).aspx">editor-only</a> |
| 1216 | /// pragma, just skipped by compiler. |
| 1217 | struct PragmaRegionHandler : public PragmaHandler { |
| 1218 | PragmaRegionHandler(const char *pragma) : PragmaHandler(pragma) { } |
| 1219 | |
| 1220 | virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 1221 | Token &NameTok) { |
| 1222 | // #pragma region: endregion matches can be verified |
| 1223 | // __pragma(region): no sense, but ignored by msvc |
| 1224 | // _Pragma is not valid for MSVC, but there isn't any point |
| 1225 | // to handle a _Pragma differently. |
| 1226 | } |
| 1227 | }; |
| 1228 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1229 | } // end anonymous namespace |
| 1230 | |
| 1231 | |
| 1232 | /// RegisterBuiltinPragmas - Install the standard preprocessor pragmas: |
James Dennett | b6e95b7 | 2012-06-17 03:26:26 +0000 | [diff] [blame] | 1233 | /// \#pragma GCC poison/system_header/dependency and \#pragma once. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1234 | void Preprocessor::RegisterBuiltinPragmas() { |
Argyrios Kyrtzidis | 9b36c3f | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 1235 | AddPragmaHandler(new PragmaOnceHandler()); |
| 1236 | AddPragmaHandler(new PragmaMarkHandler()); |
Chris Lattner | f47724b | 2010-08-17 15:55:45 +0000 | [diff] [blame] | 1237 | AddPragmaHandler(new PragmaPushMacroHandler()); |
| 1238 | AddPragmaHandler(new PragmaPopMacroHandler()); |
Michael J. Spencer | 301669b | 2010-09-27 06:19:02 +0000 | [diff] [blame] | 1239 | AddPragmaHandler(new PragmaMessageHandler()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1240 | |
Chris Lattner | e8fa06e | 2009-05-12 18:21:11 +0000 | [diff] [blame] | 1241 | // #pragma GCC ... |
Argyrios Kyrtzidis | 9b36c3f | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 1242 | AddPragmaHandler("GCC", new PragmaPoisonHandler()); |
| 1243 | AddPragmaHandler("GCC", new PragmaSystemHeaderHandler()); |
| 1244 | AddPragmaHandler("GCC", new PragmaDependencyHandler()); |
Douglas Gregor | c09ce12 | 2011-06-22 19:41:48 +0000 | [diff] [blame] | 1245 | AddPragmaHandler("GCC", new PragmaDiagnosticHandler("GCC")); |
Chris Lattner | e8fa06e | 2009-05-12 18:21:11 +0000 | [diff] [blame] | 1246 | // #pragma clang ... |
Argyrios Kyrtzidis | 9b36c3f | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 1247 | AddPragmaHandler("clang", new PragmaPoisonHandler()); |
| 1248 | AddPragmaHandler("clang", new PragmaSystemHeaderHandler()); |
Daniel Dunbar | abf7b72 | 2010-07-28 15:40:33 +0000 | [diff] [blame] | 1249 | AddPragmaHandler("clang", new PragmaDebugHandler()); |
Argyrios Kyrtzidis | 9b36c3f | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 1250 | AddPragmaHandler("clang", new PragmaDependencyHandler()); |
Douglas Gregor | c09ce12 | 2011-06-22 19:41:48 +0000 | [diff] [blame] | 1251 | AddPragmaHandler("clang", new PragmaDiagnosticHandler("clang")); |
John McCall | 8dfac0b | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 1252 | AddPragmaHandler("clang", new PragmaARCCFCodeAuditedHandler()); |
Chris Lattner | e8fa06e | 2009-05-12 18:21:11 +0000 | [diff] [blame] | 1253 | |
Argyrios Kyrtzidis | 9b36c3f | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 1254 | AddPragmaHandler("STDC", new PragmaSTDC_FENV_ACCESSHandler()); |
| 1255 | AddPragmaHandler("STDC", new PragmaSTDC_CX_LIMITED_RANGEHandler()); |
Chris Lattner | 062f232 | 2009-04-19 21:20:35 +0000 | [diff] [blame] | 1256 | AddPragmaHandler("STDC", new PragmaSTDC_UnknownHandler()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1257 | |
Chris Lattner | 636c5ef | 2009-01-16 08:21:25 +0000 | [diff] [blame] | 1258 | // MS extensions. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1259 | if (LangOpts.MicrosoftExt) { |
Argyrios Kyrtzidis | 9b36c3f | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 1260 | AddPragmaHandler(new PragmaCommentHandler()); |
Aaron Ballman | 4c55c54 | 2012-03-02 22:51:54 +0000 | [diff] [blame] | 1261 | AddPragmaHandler(new PragmaIncludeAliasHandler()); |
Aaron Ballman | fafd101 | 2012-11-30 19:52:30 +0000 | [diff] [blame] | 1262 | AddPragmaHandler(new PragmaRegionHandler("region")); |
| 1263 | AddPragmaHandler(new PragmaRegionHandler("endregion")); |
Chris Lattner | abfe094 | 2010-06-26 17:11:39 +0000 | [diff] [blame] | 1264 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1265 | } |