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