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