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" |
Reid Kleckner | 881dff3 | 2013-09-13 22:00:30 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/STLExtras.h" |
| 24 | #include "llvm/ADT/StringSwitch.h" |
Alexander Musman | 6b080fc | 2015-05-25 11:21:20 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/StringExtras.h" |
Daniel Dunbar | 211a787 | 2010-08-18 23:09:23 +0000 | [diff] [blame] | 26 | #include "llvm/Support/CrashRecoveryContext.h" |
Daniel Dunbar | f2cf329 | 2010-08-17 22:32:48 +0000 | [diff] [blame] | 27 | #include "llvm/Support/ErrorHandling.h" |
Douglas Gregor | c6d5edd | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 28 | #include <algorithm> |
Chris Lattner | b876183 | 2006-06-24 21:31:03 +0000 | [diff] [blame] | 29 | using namespace clang; |
| 30 | |
Reid Kleckner | 881dff3 | 2013-09-13 22:00:30 +0000 | [diff] [blame] | 31 | #include "llvm/Support/raw_ostream.h" |
| 32 | |
Chris Lattner | b876183 | 2006-06-24 21:31:03 +0000 | [diff] [blame] | 33 | // Out-of-line destructor to provide a home for the class. |
Angel Garcia Gomez | 637d1e6 | 2015-10-20 13:23:58 +0000 | [diff] [blame] | 34 | PragmaHandler::~PragmaHandler() { |
| 35 | } |
Chris Lattner | b876183 | 2006-06-24 21:31:03 +0000 | [diff] [blame] | 36 | |
Chris Lattner | 2e15530 | 2006-07-03 05:34:41 +0000 | [diff] [blame] | 37 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | d839e77 | 2010-06-11 20:10:12 +0000 | [diff] [blame] | 38 | // EmptyPragmaHandler Implementation. |
| 39 | //===----------------------------------------------------------------------===// |
| 40 | |
Hans Wennborg | 7357bbc | 2015-10-12 20:47:58 +0000 | [diff] [blame] | 41 | EmptyPragmaHandler::EmptyPragmaHandler(StringRef Name) : PragmaHandler(Name) {} |
Daniel Dunbar | d839e77 | 2010-06-11 20:10:12 +0000 | [diff] [blame] | 42 | |
Douglas Gregor | c7d6576 | 2010-09-09 22:45:38 +0000 | [diff] [blame] | 43 | void EmptyPragmaHandler::HandlePragma(Preprocessor &PP, |
| 44 | PragmaIntroducerKind Introducer, |
| 45 | Token &FirstToken) {} |
Daniel Dunbar | d839e77 | 2010-06-11 20:10:12 +0000 | [diff] [blame] | 46 | |
| 47 | //===----------------------------------------------------------------------===// |
Chris Lattner | 2e15530 | 2006-07-03 05:34:41 +0000 | [diff] [blame] | 48 | // PragmaNamespace Implementation. |
| 49 | //===----------------------------------------------------------------------===// |
| 50 | |
Chris Lattner | 2e15530 | 2006-07-03 05:34:41 +0000 | [diff] [blame] | 51 | PragmaNamespace::~PragmaNamespace() { |
Reid Kleckner | 588c937 | 2014-02-19 23:44:52 +0000 | [diff] [blame] | 52 | llvm::DeleteContainerSeconds(Handlers); |
Chris Lattner | 2e15530 | 2006-07-03 05:34:41 +0000 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | /// FindHandler - Check to see if there is already a handler for the |
| 56 | /// specified name. If not, return the handler for the null identifier if it |
| 57 | /// exists, otherwise return null. If IgnoreNull is true (the default) then |
| 58 | /// the null handler isn't returned on failure to match. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 59 | PragmaHandler *PragmaNamespace::FindHandler(StringRef Name, |
Chris Lattner | 2e15530 | 2006-07-03 05:34:41 +0000 | [diff] [blame] | 60 | bool IgnoreNull) const { |
Argyrios Kyrtzidis | 36745fd | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 61 | if (PragmaHandler *Handler = Handlers.lookup(Name)) |
| 62 | return Handler; |
Craig Topper | d2d442c | 2014-05-17 23:10:59 +0000 | [diff] [blame] | 63 | return IgnoreNull ? nullptr : Handlers.lookup(StringRef()); |
Argyrios Kyrtzidis | 36745fd | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 64 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 65 | |
Argyrios Kyrtzidis | 36745fd | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 66 | void PragmaNamespace::AddPragma(PragmaHandler *Handler) { |
| 67 | assert(!Handlers.lookup(Handler->getName()) && |
| 68 | "A handler with this name is already registered in this namespace"); |
David Blaikie | 13156b6 | 2014-11-19 03:06:06 +0000 | [diff] [blame] | 69 | Handlers[Handler->getName()] = Handler; |
Chris Lattner | 2e15530 | 2006-07-03 05:34:41 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Daniel Dunbar | 4059653 | 2008-10-04 19:17:46 +0000 | [diff] [blame] | 72 | void PragmaNamespace::RemovePragmaHandler(PragmaHandler *Handler) { |
Argyrios Kyrtzidis | 36745fd | 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 | 4059653 | 2008-10-04 19:17:46 +0000 | [diff] [blame] | 76 | } |
| 77 | |
Douglas Gregor | c7d6576 | 2010-09-09 22:45:38 +0000 | [diff] [blame] | 78 | void PragmaNamespace::HandlePragma(Preprocessor &PP, |
| 79 | PragmaIntroducerKind Introducer, |
| 80 | Token &Tok) { |
Chris Lattner | b876183 | 2006-06-24 21:31:03 +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 | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 84 | |
Chris Lattner | b876183 | 2006-06-24 21:31:03 +0000 | [diff] [blame] | 85 | // 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] | 86 | PragmaHandler *Handler |
| 87 | = FindHandler(Tok.getIdentifierInfo() ? Tok.getIdentifierInfo()->getName() |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 88 | : StringRef(), |
Argyrios Kyrtzidis | 36745fd | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 89 | /*IgnoreNull=*/false); |
Craig Topper | d2d442c | 2014-05-17 23:10:59 +0000 | [diff] [blame] | 90 | if (!Handler) { |
Chris Lattner | 21656f2 | 2009-04-19 21:10:26 +0000 | [diff] [blame] | 91 | PP.Diag(Tok, diag::warn_pragma_ignored); |
| 92 | return; |
| 93 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 94 | |
Chris Lattner | b876183 | 2006-06-24 21:31:03 +0000 | [diff] [blame] | 95 | // Otherwise, pass it down. |
Douglas Gregor | c7d6576 | 2010-09-09 22:45:38 +0000 | [diff] [blame] | 96 | Handler->HandlePragma(PP, Introducer, Tok); |
Chris Lattner | b876183 | 2006-06-24 21:31:03 +0000 | [diff] [blame] | 97 | } |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 98 | |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 99 | //===----------------------------------------------------------------------===// |
| 100 | // Preprocessor Pragma Directive Handling. |
| 101 | //===----------------------------------------------------------------------===// |
| 102 | |
James Dennett | 18a6d79 | 2012-06-17 03:26:26 +0000 | [diff] [blame] | 103 | /// HandlePragmaDirective - The "\#pragma" directive has been parsed. Lex the |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 104 | /// rest of the pragma, passing it to the registered pragma handlers. |
Enea Zaffanella | 5afb04a | 2013-07-20 20:09:11 +0000 | [diff] [blame] | 105 | void Preprocessor::HandlePragmaDirective(SourceLocation IntroducerLoc, |
| 106 | PragmaIntroducerKind Introducer) { |
| 107 | if (Callbacks) |
| 108 | Callbacks->PragmaDirective(IntroducerLoc, Introducer); |
| 109 | |
Jordan Rose | de1a292 | 2012-06-08 18:06:21 +0000 | [diff] [blame] | 110 | if (!PragmasEnabled) |
| 111 | return; |
| 112 | |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 113 | ++NumPragma; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 114 | |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 115 | // Invoke the first level of pragma handlers which reads the namespace id. |
Chris Lattner | 146762e | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 116 | Token Tok; |
Enea Zaffanella | 5afb04a | 2013-07-20 20:09:11 +0000 | [diff] [blame] | 117 | PragmaHandlers->HandlePragma(*this, Introducer, Tok); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 118 | |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 119 | // 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] | 120 | if ((CurTokenLexer && CurTokenLexer->isParsingPreprocessorDirective()) |
| 121 | || (CurPPLexer && CurPPLexer->ParsingPreprocessorDirective)) |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 122 | DiscardUntilEndOfDirective(); |
| 123 | } |
| 124 | |
Argyrios Kyrtzidis | f1b64c6 | 2012-04-03 16:47:40 +0000 | [diff] [blame] | 125 | namespace { |
| 126 | /// \brief Helper class for \see Preprocessor::Handle_Pragma. |
| 127 | class LexingFor_PragmaRAII { |
| 128 | Preprocessor &PP; |
| 129 | bool InMacroArgPreExpansion; |
| 130 | bool Failed; |
| 131 | Token &OutTok; |
| 132 | Token PragmaTok; |
| 133 | |
| 134 | public: |
| 135 | LexingFor_PragmaRAII(Preprocessor &PP, bool InMacroArgPreExpansion, |
| 136 | Token &Tok) |
| 137 | : PP(PP), InMacroArgPreExpansion(InMacroArgPreExpansion), |
| 138 | Failed(false), OutTok(Tok) { |
| 139 | if (InMacroArgPreExpansion) { |
| 140 | PragmaTok = OutTok; |
| 141 | PP.EnableBacktrackAtThisPos(); |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | ~LexingFor_PragmaRAII() { |
| 146 | if (InMacroArgPreExpansion) { |
| 147 | if (Failed) { |
| 148 | PP.CommitBacktrackedTokens(); |
| 149 | } else { |
| 150 | PP.Backtrack(); |
| 151 | OutTok = PragmaTok; |
| 152 | } |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | void failed() { |
| 157 | Failed = true; |
| 158 | } |
| 159 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 160 | } |
Argyrios Kyrtzidis | f1b64c6 | 2012-04-03 16:47:40 +0000 | [diff] [blame] | 161 | |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 162 | /// Handle_Pragma - Read a _Pragma directive, slice it up, process it, then |
| 163 | /// return the first token after the directive. The _Pragma token has just |
| 164 | /// been read into 'Tok'. |
Chris Lattner | 146762e | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 165 | void Preprocessor::Handle_Pragma(Token &Tok) { |
Argyrios Kyrtzidis | f1b64c6 | 2012-04-03 16:47:40 +0000 | [diff] [blame] | 166 | |
| 167 | // This works differently if we are pre-expanding a macro argument. |
| 168 | // In that case we don't actually "activate" the pragma now, we only lex it |
| 169 | // until we are sure it is lexically correct and then we backtrack so that |
| 170 | // we activate the pragma whenever we encounter the tokens again in the token |
| 171 | // stream. This ensures that we will activate it in the correct location |
| 172 | // or that we will ignore it if it never enters the token stream, e.g: |
| 173 | // |
| 174 | // #define EMPTY(x) |
| 175 | // #define INACTIVE(x) EMPTY(x) |
| 176 | // INACTIVE(_Pragma("clang diagnostic ignored \"-Wconversion\"")) |
| 177 | |
| 178 | LexingFor_PragmaRAII _PragmaLexing(*this, InMacroArgPreExpansion, Tok); |
| 179 | |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 180 | // Remember the pragma token location. |
| 181 | SourceLocation PragmaLoc = Tok.getLocation(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 182 | |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 183 | // Read the '('. |
| 184 | Lex(Tok); |
Chris Lattner | 907dfe9 | 2008-11-18 07:59:24 +0000 | [diff] [blame] | 185 | if (Tok.isNot(tok::l_paren)) { |
| 186 | Diag(PragmaLoc, diag::err__Pragma_malformed); |
Argyrios Kyrtzidis | f1b64c6 | 2012-04-03 16:47:40 +0000 | [diff] [blame] | 187 | return _PragmaLexing.failed(); |
Chris Lattner | 907dfe9 | 2008-11-18 07:59:24 +0000 | [diff] [blame] | 188 | } |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 189 | |
| 190 | // Read the '"..."'. |
| 191 | Lex(Tok); |
Richard Smith | c98bb4e | 2013-03-09 23:30:15 +0000 | [diff] [blame] | 192 | if (!tok::isStringLiteral(Tok.getKind())) { |
Chris Lattner | 907dfe9 | 2008-11-18 07:59:24 +0000 | [diff] [blame] | 193 | Diag(PragmaLoc, diag::err__Pragma_malformed); |
Hubert Tong | 0deb694 | 2015-07-30 21:30:00 +0000 | [diff] [blame] | 194 | // Skip bad tokens, and the ')', if present. |
Reid Kleckner | 53e6a5d | 2014-08-14 19:47:06 +0000 | [diff] [blame] | 195 | if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::eof)) |
Richard Smith | d67aea2 | 2012-03-06 03:21:47 +0000 | [diff] [blame] | 196 | Lex(Tok); |
Hubert Tong | 0deb694 | 2015-07-30 21:30:00 +0000 | [diff] [blame] | 197 | while (Tok.isNot(tok::r_paren) && |
| 198 | !Tok.isAtStartOfLine() && |
| 199 | Tok.isNot(tok::eof)) |
| 200 | Lex(Tok); |
Richard Smith | d67aea2 | 2012-03-06 03:21:47 +0000 | [diff] [blame] | 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(); |
Richard Smith | d67aea2 | 2012-03-06 03:21:47 +0000 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | if (Tok.hasUDSuffix()) { |
| 207 | Diag(Tok, diag::err_invalid_string_udl); |
| 208 | // Skip this token, and the ')', if present. |
| 209 | Lex(Tok); |
| 210 | if (Tok.is(tok::r_paren)) |
| 211 | Lex(Tok); |
Argyrios Kyrtzidis | f1b64c6 | 2012-04-03 16:47:40 +0000 | [diff] [blame] | 212 | return _PragmaLexing.failed(); |
Chris Lattner | 907dfe9 | 2008-11-18 07:59:24 +0000 | [diff] [blame] | 213 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 214 | |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 215 | // Remember the string. |
Argyrios Kyrtzidis | f1b64c6 | 2012-04-03 16:47:40 +0000 | [diff] [blame] | 216 | Token StrTok = Tok; |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 217 | |
| 218 | // Read the ')'. |
| 219 | Lex(Tok); |
Chris Lattner | 907dfe9 | 2008-11-18 07:59:24 +0000 | [diff] [blame] | 220 | if (Tok.isNot(tok::r_paren)) { |
| 221 | Diag(PragmaLoc, diag::err__Pragma_malformed); |
Argyrios Kyrtzidis | f1b64c6 | 2012-04-03 16:47:40 +0000 | [diff] [blame] | 222 | return _PragmaLexing.failed(); |
Chris Lattner | 907dfe9 | 2008-11-18 07:59:24 +0000 | [diff] [blame] | 223 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 224 | |
Argyrios Kyrtzidis | f1b64c6 | 2012-04-03 16:47:40 +0000 | [diff] [blame] | 225 | if (InMacroArgPreExpansion) |
| 226 | return; |
| 227 | |
Chris Lattner | 9dc9c20 | 2009-02-15 20:52:18 +0000 | [diff] [blame] | 228 | SourceLocation RParenLoc = Tok.getLocation(); |
Argyrios Kyrtzidis | f1b64c6 | 2012-04-03 16:47:40 +0000 | [diff] [blame] | 229 | std::string StrVal = getSpelling(StrTok); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 230 | |
Richard Smith | c98bb4e | 2013-03-09 23:30:15 +0000 | [diff] [blame] | 231 | // The _Pragma is lexically sound. Destringize according to C11 6.10.9.1: |
| 232 | // "The string literal is destringized by deleting any encoding prefix, |
Chris Lattner | 262d4e3 | 2009-01-16 18:59:23 +0000 | [diff] [blame] | 233 | // deleting the leading and trailing double-quotes, replacing each escape |
| 234 | // sequence \" by a double-quote, and replacing each escape sequence \\ by a |
| 235 | // single backslash." |
Richard Smith | c98bb4e | 2013-03-09 23:30:15 +0000 | [diff] [blame] | 236 | if (StrVal[0] == 'L' || StrVal[0] == 'U' || |
| 237 | (StrVal[0] == 'u' && StrVal[1] != '8')) |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 238 | StrVal.erase(StrVal.begin()); |
Richard Smith | c98bb4e | 2013-03-09 23:30:15 +0000 | [diff] [blame] | 239 | else if (StrVal[0] == 'u') |
| 240 | StrVal.erase(StrVal.begin(), StrVal.begin() + 2); |
| 241 | |
| 242 | if (StrVal[0] == 'R') { |
| 243 | // FIXME: C++11 does not specify how to handle raw-string-literals here. |
| 244 | // We strip off the 'R', the quotes, the d-char-sequences, and the parens. |
| 245 | assert(StrVal[1] == '"' && StrVal[StrVal.size() - 1] == '"' && |
| 246 | "Invalid raw string token!"); |
| 247 | |
| 248 | // Measure the length of the d-char-sequence. |
| 249 | unsigned NumDChars = 0; |
| 250 | while (StrVal[2 + NumDChars] != '(') { |
| 251 | assert(NumDChars < (StrVal.size() - 5) / 2 && |
| 252 | "Invalid raw string token!"); |
| 253 | ++NumDChars; |
| 254 | } |
| 255 | assert(StrVal[StrVal.size() - 2 - NumDChars] == ')'); |
| 256 | |
| 257 | // Remove 'R " d-char-sequence' and 'd-char-sequence "'. We'll replace the |
| 258 | // parens below. |
| 259 | StrVal.erase(0, 2 + NumDChars); |
| 260 | StrVal.erase(StrVal.size() - 1 - NumDChars); |
| 261 | } else { |
| 262 | assert(StrVal[0] == '"' && StrVal[StrVal.size()-1] == '"' && |
| 263 | "Invalid string token!"); |
| 264 | |
| 265 | // Remove escaped quotes and escapes. |
Benjamin Kramer | c2f5f29 | 2013-05-04 10:37:20 +0000 | [diff] [blame] | 266 | unsigned ResultPos = 1; |
Reid Kleckner | 95e036c | 2013-09-25 16:42:48 +0000 | [diff] [blame] | 267 | for (unsigned i = 1, e = StrVal.size() - 1; i != e; ++i) { |
| 268 | // Skip escapes. \\ -> '\' and \" -> '"'. |
| 269 | if (StrVal[i] == '\\' && i + 1 < e && |
| 270 | (StrVal[i + 1] == '\\' || StrVal[i + 1] == '"')) |
| 271 | ++i; |
| 272 | StrVal[ResultPos++] = StrVal[i]; |
Richard Smith | c98bb4e | 2013-03-09 23:30:15 +0000 | [diff] [blame] | 273 | } |
Reid Kleckner | 95e036c | 2013-09-25 16:42:48 +0000 | [diff] [blame] | 274 | StrVal.erase(StrVal.begin() + ResultPos, StrVal.end() - 1); |
Richard Smith | c98bb4e | 2013-03-09 23:30:15 +0000 | [diff] [blame] | 275 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 276 | |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 277 | // Remove the front quote, replacing it with a space, so that the pragma |
| 278 | // contents appear to have a space before them. |
| 279 | StrVal[0] = ' '; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 280 | |
Chris Lattner | fa217bd | 2009-03-08 08:08:45 +0000 | [diff] [blame] | 281 | // Replace the terminating quote with a \n. |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 282 | StrVal[StrVal.size()-1] = '\n'; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 283 | |
Peter Collingbourne | f29ce97 | 2011-02-22 13:49:06 +0000 | [diff] [blame] | 284 | // Plop the string (including the newline and trailing null) into a buffer |
| 285 | // where we can lex it. |
| 286 | Token TmpTok; |
| 287 | TmpTok.startToken(); |
Dmitri Gribenko | b8e9e75 | 2012-09-24 21:07:17 +0000 | [diff] [blame] | 288 | CreateString(StrVal, TmpTok); |
Peter Collingbourne | f29ce97 | 2011-02-22 13:49:06 +0000 | [diff] [blame] | 289 | SourceLocation TokLoc = TmpTok.getLocation(); |
| 290 | |
| 291 | // Make and enter a lexer object so that we lex and expand the tokens just |
| 292 | // like any others. |
| 293 | Lexer *TL = Lexer::Create_PragmaLexer(TokLoc, PragmaLoc, RParenLoc, |
| 294 | StrVal.size(), *this); |
| 295 | |
Craig Topper | d2d442c | 2014-05-17 23:10:59 +0000 | [diff] [blame] | 296 | EnterSourceFileWithLexer(TL, nullptr); |
Peter Collingbourne | f29ce97 | 2011-02-22 13:49:06 +0000 | [diff] [blame] | 297 | |
| 298 | // With everything set up, lex this as a #pragma directive. |
Enea Zaffanella | 5afb04a | 2013-07-20 20:09:11 +0000 | [diff] [blame] | 299 | HandlePragmaDirective(PragmaLoc, PIK__Pragma); |
John McCall | 89e925d | 2010-08-28 22:34:47 +0000 | [diff] [blame] | 300 | |
| 301 | // Finally, return whatever came after the pragma directive. |
| 302 | return Lex(Tok); |
| 303 | } |
| 304 | |
| 305 | /// HandleMicrosoft__pragma - Like Handle_Pragma except the pragma text |
| 306 | /// is not enclosed within a string literal. |
| 307 | void Preprocessor::HandleMicrosoft__pragma(Token &Tok) { |
| 308 | // Remember the pragma token location. |
| 309 | SourceLocation PragmaLoc = Tok.getLocation(); |
| 310 | |
| 311 | // Read the '('. |
| 312 | Lex(Tok); |
| 313 | if (Tok.isNot(tok::l_paren)) { |
| 314 | Diag(PragmaLoc, diag::err__Pragma_malformed); |
| 315 | return; |
| 316 | } |
| 317 | |
Peter Collingbourne | f29ce97 | 2011-02-22 13:49:06 +0000 | [diff] [blame] | 318 | // Get the tokens enclosed within the __pragma(), as well as the final ')'. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 319 | SmallVector<Token, 32> PragmaToks; |
John McCall | 89e925d | 2010-08-28 22:34:47 +0000 | [diff] [blame] | 320 | int NumParens = 0; |
| 321 | Lex(Tok); |
| 322 | while (Tok.isNot(tok::eof)) { |
Peter Collingbourne | f29ce97 | 2011-02-22 13:49:06 +0000 | [diff] [blame] | 323 | PragmaToks.push_back(Tok); |
John McCall | 89e925d | 2010-08-28 22:34:47 +0000 | [diff] [blame] | 324 | if (Tok.is(tok::l_paren)) |
| 325 | NumParens++; |
| 326 | else if (Tok.is(tok::r_paren) && NumParens-- == 0) |
| 327 | break; |
John McCall | 89e925d | 2010-08-28 22:34:47 +0000 | [diff] [blame] | 328 | Lex(Tok); |
| 329 | } |
| 330 | |
John McCall | 49039d4 | 2010-08-29 01:09:54 +0000 | [diff] [blame] | 331 | if (Tok.is(tok::eof)) { |
| 332 | Diag(PragmaLoc, diag::err_unterminated___pragma); |
| 333 | return; |
| 334 | } |
| 335 | |
Peter Collingbourne | f29ce97 | 2011-02-22 13:49:06 +0000 | [diff] [blame] | 336 | PragmaToks.front().setFlag(Token::LeadingSpace); |
John McCall | 89e925d | 2010-08-28 22:34:47 +0000 | [diff] [blame] | 337 | |
Peter Collingbourne | 2f1e36b | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 338 | // Replace the ')' with an EOD to mark the end of the pragma. |
| 339 | PragmaToks.back().setKind(tok::eod); |
Peter Collingbourne | f29ce97 | 2011-02-22 13:49:06 +0000 | [diff] [blame] | 340 | |
| 341 | Token *TokArray = new Token[PragmaToks.size()]; |
| 342 | std::copy(PragmaToks.begin(), PragmaToks.end(), TokArray); |
| 343 | |
| 344 | // Push the tokens onto the stack. |
| 345 | EnterTokenStream(TokArray, PragmaToks.size(), true, true); |
| 346 | |
| 347 | // With everything set up, lex this as a #pragma directive. |
Enea Zaffanella | 5afb04a | 2013-07-20 20:09:11 +0000 | [diff] [blame] | 348 | HandlePragmaDirective(PragmaLoc, PIK___pragma); |
John McCall | 89e925d | 2010-08-28 22:34:47 +0000 | [diff] [blame] | 349 | |
| 350 | // Finally, return whatever came after the pragma directive. |
| 351 | return Lex(Tok); |
| 352 | } |
| 353 | |
James Dennett | 18a6d79 | 2012-06-17 03:26:26 +0000 | [diff] [blame] | 354 | /// HandlePragmaOnce - Handle \#pragma once. OnceTok is the 'once'. |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 355 | /// |
Chris Lattner | 146762e | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 356 | void Preprocessor::HandlePragmaOnce(Token &OnceTok) { |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 357 | if (isInPrimaryFile()) { |
| 358 | Diag(OnceTok, diag::pp_pragma_once_in_main_file); |
| 359 | return; |
| 360 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 361 | |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 362 | // 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] | 363 | // Mark the file as a once-only file now. |
Chris Lattner | d32480d | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 364 | HeaderInfo.MarkFileIncludeOnce(getCurrentFileLexer()->getFileEntry()); |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 365 | } |
| 366 | |
Chris Lattner | c238331 | 2007-12-19 19:38:36 +0000 | [diff] [blame] | 367 | void Preprocessor::HandlePragmaMark() { |
Ted Kremenek | 76c3441 | 2008-11-19 22:21:33 +0000 | [diff] [blame] | 368 | assert(CurPPLexer && "No current lexer?"); |
Chris Lattner | d9efb6e | 2009-06-15 05:02:34 +0000 | [diff] [blame] | 369 | if (CurLexer) |
| 370 | CurLexer->ReadToEndOfLine(); |
| 371 | else |
| 372 | CurPTHLexer->DiscardToEndOfLine(); |
Chris Lattner | c238331 | 2007-12-19 19:38:36 +0000 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | |
James Dennett | 18a6d79 | 2012-06-17 03:26:26 +0000 | [diff] [blame] | 376 | /// HandlePragmaPoison - Handle \#pragma GCC poison. PoisonTok is the 'poison'. |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 377 | /// |
Chris Lattner | 146762e | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 378 | void Preprocessor::HandlePragmaPoison(Token &PoisonTok) { |
| 379 | Token Tok; |
Chris Lattner | 538d7f3 | 2006-07-20 04:31:52 +0000 | [diff] [blame] | 380 | |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 381 | while (1) { |
| 382 | // Read the next token to poison. While doing this, pretend that we are |
| 383 | // skipping while reading the identifier to poison. |
| 384 | // This avoids errors on code like: |
| 385 | // #pragma GCC poison X |
| 386 | // #pragma GCC poison X |
Ted Kremenek | 551c82a | 2008-11-18 01:12:54 +0000 | [diff] [blame] | 387 | if (CurPPLexer) CurPPLexer->LexingRawMode = true; |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 388 | LexUnexpandedToken(Tok); |
Ted Kremenek | 551c82a | 2008-11-18 01:12:54 +0000 | [diff] [blame] | 389 | if (CurPPLexer) CurPPLexer->LexingRawMode = false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 390 | |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 391 | // If we reached the end of line, we're done. |
Peter Collingbourne | 2f1e36b | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 392 | if (Tok.is(tok::eod)) return; |
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 | // Can only poison identifiers. |
Abramo Bagnara | ea4f7c7 | 2010-12-22 08:23:18 +0000 | [diff] [blame] | 395 | if (Tok.isNot(tok::raw_identifier)) { |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 396 | Diag(Tok, diag::err_pp_invalid_poison); |
| 397 | return; |
| 398 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 399 | |
Chris Lattner | cefc768 | 2006-07-08 08:28:12 +0000 | [diff] [blame] | 400 | // Look up the identifier info for the token. We disabled identifier lookup |
| 401 | // by saying we're skipping contents, so we need to do this manually. |
| 402 | IdentifierInfo *II = LookUpIdentifierInfo(Tok); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 403 | |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 404 | // Already poisoned. |
| 405 | if (II->isPoisoned()) continue; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 406 | |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 407 | // If this is a macro identifier, emit a warning. |
Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 408 | if (isMacroDefined(II)) |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 409 | Diag(Tok, diag::pp_poisoning_existing_macro); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 410 | |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 411 | // Finally, poison it! |
| 412 | II->setIsPoisoned(); |
Douglas Gregor | 935bc7a2 | 2011-10-27 09:33:13 +0000 | [diff] [blame] | 413 | if (II->isFromAST()) |
| 414 | II->setChangedSinceDeserialization(); |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 415 | } |
| 416 | } |
| 417 | |
James Dennett | 18a6d79 | 2012-06-17 03:26:26 +0000 | [diff] [blame] | 418 | /// HandlePragmaSystemHeader - Implement \#pragma GCC system_header. We know |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 419 | /// that the whole directive has been parsed. |
Chris Lattner | 146762e | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 420 | void Preprocessor::HandlePragmaSystemHeader(Token &SysHeaderTok) { |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 421 | if (isInPrimaryFile()) { |
| 422 | Diag(SysHeaderTok, diag::pp_pragma_sysheader_in_main_file); |
| 423 | return; |
| 424 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 425 | |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 426 | // 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] | 427 | PreprocessorLexer *TheLexer = getCurrentFileLexer(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 428 | |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 429 | // Mark the file as a system header. |
Chris Lattner | d32480d | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 430 | HeaderInfo.MarkFileSystemHeader(TheLexer->getFileEntry()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 431 | |
| 432 | |
Chris Lattner | d9efb6e | 2009-06-15 05:02:34 +0000 | [diff] [blame] | 433 | PresumedLoc PLoc = SourceMgr.getPresumedLoc(SysHeaderTok.getLocation()); |
Douglas Gregor | 453b012 | 2010-11-12 07:15:47 +0000 | [diff] [blame] | 434 | if (PLoc.isInvalid()) |
| 435 | return; |
| 436 | |
Jay Foad | 9a6b098 | 2011-06-21 15:13:30 +0000 | [diff] [blame] | 437 | unsigned FilenameID = SourceMgr.getLineTableFilenameID(PLoc.getFilename()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 438 | |
Chris Lattner | 3bdc767 | 2011-05-22 22:10:16 +0000 | [diff] [blame] | 439 | // Notify the client, if desired, that we are in a new source file. |
| 440 | if (Callbacks) |
| 441 | Callbacks->FileChanged(SysHeaderTok.getLocation(), |
| 442 | PPCallbacks::SystemHeaderPragma, SrcMgr::C_System); |
| 443 | |
Chris Lattner | d9efb6e | 2009-06-15 05:02:34 +0000 | [diff] [blame] | 444 | // Emit a line marker. This will change any source locations from this point |
| 445 | // forward to realize they are in a system header. |
| 446 | // Create a line note with this information. |
Jordan Rose | 111c4a6 | 2013-04-17 19:09:18 +0000 | [diff] [blame] | 447 | SourceMgr.AddLineNote(SysHeaderTok.getLocation(), PLoc.getLine()+1, |
| 448 | FilenameID, /*IsEntry=*/false, /*IsExit=*/false, |
| 449 | /*IsSystem=*/true, /*IsExternC=*/false); |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 450 | } |
| 451 | |
James Dennett | 18a6d79 | 2012-06-17 03:26:26 +0000 | [diff] [blame] | 452 | /// HandlePragmaDependency - Handle \#pragma GCC dependency "foo" blah. |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 453 | /// |
Chris Lattner | 146762e | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 454 | void Preprocessor::HandlePragmaDependency(Token &DependencyTok) { |
| 455 | Token FilenameTok; |
Ted Kremenek | 551c82a | 2008-11-18 01:12:54 +0000 | [diff] [blame] | 456 | CurPPLexer->LexIncludeFilename(FilenameTok); |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 457 | |
Peter Collingbourne | 2f1e36b | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 458 | // If the token kind is EOD, the error has already been diagnosed. |
| 459 | if (FilenameTok.is(tok::eod)) |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 460 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 461 | |
Chris Lattner | c07ba1f | 2006-10-30 05:58:32 +0000 | [diff] [blame] | 462 | // Reserve a buffer to get the spelling. |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 463 | SmallString<128> FilenameBuffer; |
Douglas Gregor | dc970f0 | 2010-03-16 22:30:13 +0000 | [diff] [blame] | 464 | bool Invalid = false; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 465 | StringRef Filename = getSpelling(FilenameTok, FilenameBuffer, &Invalid); |
Douglas Gregor | dc970f0 | 2010-03-16 22:30:13 +0000 | [diff] [blame] | 466 | if (Invalid) |
| 467 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 468 | |
Chris Lattner | d081f8c | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 469 | bool isAngled = |
| 470 | GetIncludeFilenameSpelling(FilenameTok.getLocation(), Filename); |
Chris Lattner | c07ba1f | 2006-10-30 05:58:32 +0000 | [diff] [blame] | 471 | // If GetIncludeFilenameSpelling set the start ptr to null, there was an |
| 472 | // error. |
Chris Lattner | d081f8c | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 473 | if (Filename.empty()) |
Chris Lattner | c07ba1f | 2006-10-30 05:58:32 +0000 | [diff] [blame] | 474 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 475 | |
Chris Lattner | 59a9ebd | 2006-10-18 05:34:33 +0000 | [diff] [blame] | 476 | // Search include directories for this file. |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 477 | const DirectoryLookup *CurDir; |
Richard Smith | 25d5075 | 2014-10-20 00:15:49 +0000 | [diff] [blame] | 478 | const FileEntry *File = |
| 479 | LookupFile(FilenameTok.getLocation(), Filename, isAngled, nullptr, |
| 480 | nullptr, CurDir, nullptr, nullptr, nullptr); |
Craig Topper | d2d442c | 2014-05-17 23:10:59 +0000 | [diff] [blame] | 481 | if (!File) { |
Eli Friedman | 3781a36 | 2011-08-30 23:07:51 +0000 | [diff] [blame] | 482 | if (!SuppressIncludeNotFoundError) |
| 483 | Diag(FilenameTok, diag::err_pp_file_not_found) << Filename; |
Chris Lattner | 97b8e84 | 2008-11-18 08:02:48 +0000 | [diff] [blame] | 484 | return; |
| 485 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 486 | |
Chris Lattner | d32480d | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 487 | const FileEntry *CurFile = getCurrentFileLexer()->getFileEntry(); |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 488 | |
| 489 | // If this file is older than the file it depends on, emit a diagnostic. |
| 490 | if (CurFile && CurFile->getModificationTime() < File->getModificationTime()) { |
| 491 | // Lex tokens at the end of the message and include them in the message. |
| 492 | std::string Message; |
| 493 | Lex(DependencyTok); |
Peter Collingbourne | 2f1e36b | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 494 | while (DependencyTok.isNot(tok::eod)) { |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 495 | Message += getSpelling(DependencyTok) + " "; |
| 496 | Lex(DependencyTok); |
| 497 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 498 | |
Chris Lattner | f0b0497 | 2010-09-05 23:16:09 +0000 | [diff] [blame] | 499 | // Remove the trailing ' ' if present. |
| 500 | if (!Message.empty()) |
| 501 | Message.erase(Message.end()-1); |
Chris Lattner | 97b8e84 | 2008-11-18 08:02:48 +0000 | [diff] [blame] | 502 | Diag(FilenameTok, diag::pp_out_of_date_dependency) << Message; |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 503 | } |
| 504 | } |
| 505 | |
Reid Kleckner | 002562a | 2013-05-06 21:02:12 +0000 | [diff] [blame] | 506 | /// ParsePragmaPushOrPopMacro - Handle parsing of pragma push_macro/pop_macro. |
Chris Lattner | c0a585d | 2010-08-17 15:55:45 +0000 | [diff] [blame] | 507 | /// Return the IdentifierInfo* associated with the macro to push or pop. |
| 508 | IdentifierInfo *Preprocessor::ParsePragmaPushOrPopMacro(Token &Tok) { |
| 509 | // Remember the pragma token location. |
| 510 | Token PragmaTok = Tok; |
| 511 | |
| 512 | // Read the '('. |
| 513 | Lex(Tok); |
| 514 | if (Tok.isNot(tok::l_paren)) { |
| 515 | Diag(PragmaTok.getLocation(), diag::err_pragma_push_pop_macro_malformed) |
| 516 | << getSpelling(PragmaTok); |
Craig Topper | d2d442c | 2014-05-17 23:10:59 +0000 | [diff] [blame] | 517 | return nullptr; |
Chris Lattner | c0a585d | 2010-08-17 15:55:45 +0000 | [diff] [blame] | 518 | } |
| 519 | |
| 520 | // Read the macro name string. |
| 521 | Lex(Tok); |
| 522 | if (Tok.isNot(tok::string_literal)) { |
| 523 | Diag(PragmaTok.getLocation(), diag::err_pragma_push_pop_macro_malformed) |
| 524 | << getSpelling(PragmaTok); |
Craig Topper | d2d442c | 2014-05-17 23:10:59 +0000 | [diff] [blame] | 525 | return nullptr; |
Chris Lattner | c0a585d | 2010-08-17 15:55:45 +0000 | [diff] [blame] | 526 | } |
| 527 | |
Richard Smith | d67aea2 | 2012-03-06 03:21:47 +0000 | [diff] [blame] | 528 | if (Tok.hasUDSuffix()) { |
| 529 | Diag(Tok, diag::err_invalid_string_udl); |
Craig Topper | d2d442c | 2014-05-17 23:10:59 +0000 | [diff] [blame] | 530 | return nullptr; |
Richard Smith | d67aea2 | 2012-03-06 03:21:47 +0000 | [diff] [blame] | 531 | } |
| 532 | |
Chris Lattner | c0a585d | 2010-08-17 15:55:45 +0000 | [diff] [blame] | 533 | // Remember the macro string. |
| 534 | std::string StrVal = getSpelling(Tok); |
| 535 | |
| 536 | // Read the ')'. |
| 537 | Lex(Tok); |
| 538 | if (Tok.isNot(tok::r_paren)) { |
| 539 | Diag(PragmaTok.getLocation(), diag::err_pragma_push_pop_macro_malformed) |
| 540 | << getSpelling(PragmaTok); |
Craig Topper | d2d442c | 2014-05-17 23:10:59 +0000 | [diff] [blame] | 541 | return nullptr; |
Chris Lattner | c0a585d | 2010-08-17 15:55:45 +0000 | [diff] [blame] | 542 | } |
| 543 | |
| 544 | assert(StrVal[0] == '"' && StrVal[StrVal.size()-1] == '"' && |
| 545 | "Invalid string token!"); |
| 546 | |
| 547 | // Create a Token from the string. |
| 548 | Token MacroTok; |
| 549 | MacroTok.startToken(); |
Abramo Bagnara | ea4f7c7 | 2010-12-22 08:23:18 +0000 | [diff] [blame] | 550 | MacroTok.setKind(tok::raw_identifier); |
Dmitri Gribenko | b8e9e75 | 2012-09-24 21:07:17 +0000 | [diff] [blame] | 551 | CreateString(StringRef(&StrVal[1], StrVal.size() - 2), MacroTok); |
Chris Lattner | c0a585d | 2010-08-17 15:55:45 +0000 | [diff] [blame] | 552 | |
| 553 | // Get the IdentifierInfo of MacroToPushTok. |
| 554 | return LookUpIdentifierInfo(MacroTok); |
| 555 | } |
| 556 | |
James Dennett | 18a6d79 | 2012-06-17 03:26:26 +0000 | [diff] [blame] | 557 | /// \brief Handle \#pragma push_macro. |
| 558 | /// |
Chris Lattner | c0a585d | 2010-08-17 15:55:45 +0000 | [diff] [blame] | 559 | /// The syntax is: |
James Dennett | 18a6d79 | 2012-06-17 03:26:26 +0000 | [diff] [blame] | 560 | /// \code |
Dmitri Gribenko | 9ebd161 | 2012-11-30 20:04:39 +0000 | [diff] [blame] | 561 | /// #pragma push_macro("macro") |
James Dennett | 18a6d79 | 2012-06-17 03:26:26 +0000 | [diff] [blame] | 562 | /// \endcode |
Chris Lattner | c0a585d | 2010-08-17 15:55:45 +0000 | [diff] [blame] | 563 | void Preprocessor::HandlePragmaPushMacro(Token &PushMacroTok) { |
| 564 | // Parse the pragma directive and get the macro IdentifierInfo*. |
| 565 | IdentifierInfo *IdentInfo = ParsePragmaPushOrPopMacro(PushMacroTok); |
| 566 | if (!IdentInfo) return; |
| 567 | |
| 568 | // Get the MacroInfo associated with IdentInfo. |
| 569 | MacroInfo *MI = getMacroInfo(IdentInfo); |
| 570 | |
Chris Lattner | c0a585d | 2010-08-17 15:55:45 +0000 | [diff] [blame] | 571 | if (MI) { |
Chris Lattner | c0a585d | 2010-08-17 15:55:45 +0000 | [diff] [blame] | 572 | // Allow the original MacroInfo to be redefined later. |
| 573 | MI->setIsAllowRedefinitionsWithoutWarning(true); |
| 574 | } |
| 575 | |
| 576 | // Push the cloned MacroInfo so we can retrieve it later. |
Argyrios Kyrtzidis | 09c9e81 | 2013-02-20 00:54:57 +0000 | [diff] [blame] | 577 | PragmaPushMacroInfo[IdentInfo].push_back(MI); |
Chris Lattner | c0a585d | 2010-08-17 15:55:45 +0000 | [diff] [blame] | 578 | } |
| 579 | |
James Dennett | 18a6d79 | 2012-06-17 03:26:26 +0000 | [diff] [blame] | 580 | /// \brief Handle \#pragma pop_macro. |
| 581 | /// |
Chris Lattner | c0a585d | 2010-08-17 15:55:45 +0000 | [diff] [blame] | 582 | /// The syntax is: |
James Dennett | 18a6d79 | 2012-06-17 03:26:26 +0000 | [diff] [blame] | 583 | /// \code |
Chris Lattner | c0a585d | 2010-08-17 15:55:45 +0000 | [diff] [blame] | 584 | /// #pragma pop_macro("macro") |
James Dennett | 18a6d79 | 2012-06-17 03:26:26 +0000 | [diff] [blame] | 585 | /// \endcode |
Chris Lattner | c0a585d | 2010-08-17 15:55:45 +0000 | [diff] [blame] | 586 | void Preprocessor::HandlePragmaPopMacro(Token &PopMacroTok) { |
| 587 | SourceLocation MessageLoc = PopMacroTok.getLocation(); |
| 588 | |
| 589 | // Parse the pragma directive and get the macro IdentifierInfo*. |
| 590 | IdentifierInfo *IdentInfo = ParsePragmaPushOrPopMacro(PopMacroTok); |
| 591 | if (!IdentInfo) return; |
| 592 | |
| 593 | // Find the vector<MacroInfo*> associated with the macro. |
| 594 | llvm::DenseMap<IdentifierInfo*, std::vector<MacroInfo*> >::iterator iter = |
| 595 | PragmaPushMacroInfo.find(IdentInfo); |
| 596 | if (iter != PragmaPushMacroInfo.end()) { |
Alexander Kornienko | 8b3f623 | 2012-08-29 00:20:03 +0000 | [diff] [blame] | 597 | // Forget the MacroInfo currently associated with IdentInfo. |
Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 598 | if (MacroInfo *MI = getMacroInfo(IdentInfo)) { |
Argyrios Kyrtzidis | b6210df | 2013-03-26 17:17:01 +0000 | [diff] [blame] | 599 | if (MI->isWarnIfUnused()) |
| 600 | WarnUnusedMacroLocs.erase(MI->getDefinitionLoc()); |
| 601 | appendMacroDirective(IdentInfo, AllocateUndefMacroDirective(MessageLoc)); |
Argyrios Kyrtzidis | 1cb0de1 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 602 | } |
Chris Lattner | c0a585d | 2010-08-17 15:55:45 +0000 | [diff] [blame] | 603 | |
| 604 | // Get the MacroInfo we want to reinstall. |
| 605 | MacroInfo *MacroToReInstall = iter->second.back(); |
| 606 | |
Richard Smith | 713369b | 2015-04-23 20:40:50 +0000 | [diff] [blame] | 607 | if (MacroToReInstall) |
Alexander Kornienko | c0b4928 | 2012-08-29 16:56:24 +0000 | [diff] [blame] | 608 | // Reinstall the previously pushed macro. |
Richard Smith | 713369b | 2015-04-23 20:40:50 +0000 | [diff] [blame] | 609 | appendDefMacroDirective(IdentInfo, MacroToReInstall, MessageLoc); |
Chris Lattner | c0a585d | 2010-08-17 15:55:45 +0000 | [diff] [blame] | 610 | |
| 611 | // Pop PragmaPushMacroInfo stack. |
| 612 | iter->second.pop_back(); |
| 613 | if (iter->second.size() == 0) |
| 614 | PragmaPushMacroInfo.erase(iter); |
| 615 | } else { |
| 616 | Diag(MessageLoc, diag::warn_pragma_pop_macro_no_push) |
| 617 | << IdentInfo->getName(); |
| 618 | } |
| 619 | } |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 620 | |
Aaron Ballman | 611306e | 2012-03-02 22:51:54 +0000 | [diff] [blame] | 621 | void Preprocessor::HandlePragmaIncludeAlias(Token &Tok) { |
| 622 | // We will either get a quoted filename or a bracketed filename, and we |
| 623 | // have to track which we got. The first filename is the source name, |
| 624 | // and the second name is the mapped filename. If the first is quoted, |
| 625 | // 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] | 626 | |
| 627 | // Get the open paren |
| 628 | Lex(Tok); |
| 629 | if (Tok.isNot(tok::l_paren)) { |
| 630 | Diag(Tok, diag::warn_pragma_include_alias_expected) << "("; |
| 631 | return; |
| 632 | } |
| 633 | |
| 634 | // We expect either a quoted string literal, or a bracketed name |
| 635 | Token SourceFilenameTok; |
| 636 | CurPPLexer->LexIncludeFilename(SourceFilenameTok); |
| 637 | if (SourceFilenameTok.is(tok::eod)) { |
| 638 | // The diagnostic has already been handled |
| 639 | return; |
| 640 | } |
| 641 | |
| 642 | StringRef SourceFileName; |
| 643 | SmallString<128> FileNameBuffer; |
| 644 | if (SourceFilenameTok.is(tok::string_literal) || |
| 645 | SourceFilenameTok.is(tok::angle_string_literal)) { |
| 646 | SourceFileName = getSpelling(SourceFilenameTok, FileNameBuffer); |
| 647 | } else if (SourceFilenameTok.is(tok::less)) { |
| 648 | // This could be a path instead of just a name |
| 649 | FileNameBuffer.push_back('<'); |
| 650 | SourceLocation End; |
| 651 | if (ConcatenateIncludeName(FileNameBuffer, End)) |
| 652 | return; // Diagnostic already emitted |
Yaron Keren | 92e1b62 | 2015-03-18 10:17:07 +0000 | [diff] [blame] | 653 | SourceFileName = FileNameBuffer; |
Aaron Ballman | 611306e | 2012-03-02 22:51:54 +0000 | [diff] [blame] | 654 | } else { |
| 655 | Diag(Tok, diag::warn_pragma_include_alias_expected_filename); |
| 656 | return; |
| 657 | } |
| 658 | FileNameBuffer.clear(); |
| 659 | |
| 660 | // Now we expect a comma, followed by another include name |
| 661 | Lex(Tok); |
| 662 | if (Tok.isNot(tok::comma)) { |
| 663 | Diag(Tok, diag::warn_pragma_include_alias_expected) << ","; |
| 664 | return; |
| 665 | } |
| 666 | |
| 667 | Token ReplaceFilenameTok; |
| 668 | CurPPLexer->LexIncludeFilename(ReplaceFilenameTok); |
| 669 | if (ReplaceFilenameTok.is(tok::eod)) { |
| 670 | // The diagnostic has already been handled |
| 671 | return; |
| 672 | } |
| 673 | |
| 674 | StringRef ReplaceFileName; |
| 675 | if (ReplaceFilenameTok.is(tok::string_literal) || |
| 676 | ReplaceFilenameTok.is(tok::angle_string_literal)) { |
| 677 | ReplaceFileName = getSpelling(ReplaceFilenameTok, FileNameBuffer); |
| 678 | } else if (ReplaceFilenameTok.is(tok::less)) { |
| 679 | // This could be a path instead of just a name |
| 680 | FileNameBuffer.push_back('<'); |
| 681 | SourceLocation End; |
| 682 | if (ConcatenateIncludeName(FileNameBuffer, End)) |
| 683 | return; // Diagnostic already emitted |
Yaron Keren | 92e1b62 | 2015-03-18 10:17:07 +0000 | [diff] [blame] | 684 | ReplaceFileName = FileNameBuffer; |
Aaron Ballman | 611306e | 2012-03-02 22:51:54 +0000 | [diff] [blame] | 685 | } else { |
| 686 | Diag(Tok, diag::warn_pragma_include_alias_expected_filename); |
| 687 | return; |
| 688 | } |
| 689 | |
| 690 | // Finally, we expect the closing paren |
| 691 | Lex(Tok); |
| 692 | if (Tok.isNot(tok::r_paren)) { |
| 693 | Diag(Tok, diag::warn_pragma_include_alias_expected) << ")"; |
| 694 | return; |
| 695 | } |
| 696 | |
| 697 | // Now that we have the source and target filenames, we need to make sure |
| 698 | // they're both of the same type (angled vs non-angled) |
| 699 | StringRef OriginalSource = SourceFileName; |
| 700 | |
| 701 | bool SourceIsAngled = |
| 702 | GetIncludeFilenameSpelling(SourceFilenameTok.getLocation(), |
| 703 | SourceFileName); |
| 704 | bool ReplaceIsAngled = |
| 705 | GetIncludeFilenameSpelling(ReplaceFilenameTok.getLocation(), |
| 706 | ReplaceFileName); |
| 707 | if (!SourceFileName.empty() && !ReplaceFileName.empty() && |
| 708 | (SourceIsAngled != ReplaceIsAngled)) { |
| 709 | unsigned int DiagID; |
| 710 | if (SourceIsAngled) |
| 711 | DiagID = diag::warn_pragma_include_alias_mismatch_angle; |
| 712 | else |
| 713 | DiagID = diag::warn_pragma_include_alias_mismatch_quote; |
| 714 | |
| 715 | Diag(SourceFilenameTok.getLocation(), DiagID) |
| 716 | << SourceFileName |
| 717 | << ReplaceFileName; |
| 718 | |
| 719 | return; |
| 720 | } |
| 721 | |
| 722 | // Now we can let the include handler know about this mapping |
| 723 | getHeaderSearchInfo().AddIncludeAlias(OriginalSource, ReplaceFileName); |
| 724 | } |
| 725 | |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 726 | /// AddPragmaHandler - Add the specified pragma handler to the preprocessor. |
| 727 | /// If 'Namespace' is non-null, then it is a token required to exist on the |
| 728 | /// pragma line before the pragma string starts, e.g. "STDC" or "GCC". |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 729 | void Preprocessor::AddPragmaHandler(StringRef Namespace, |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 730 | PragmaHandler *Handler) { |
Craig Topper | be25030 | 2014-09-12 05:19:24 +0000 | [diff] [blame] | 731 | PragmaNamespace *InsertNS = PragmaHandlers.get(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 732 | |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 733 | // 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] | 734 | if (!Namespace.empty()) { |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 735 | // If there is already a pragma handler with the name of this namespace, |
| 736 | // we either have an error (directive with the same name as a namespace) or |
| 737 | // we already have the namespace to insert into. |
Argyrios Kyrtzidis | 36745fd | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 738 | if (PragmaHandler *Existing = PragmaHandlers->FindHandler(Namespace)) { |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 739 | InsertNS = Existing->getIfNamespace(); |
Craig Topper | d2d442c | 2014-05-17 23:10:59 +0000 | [diff] [blame] | 740 | assert(InsertNS != nullptr && "Cannot have a pragma namespace and pragma" |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 741 | " handler with the same name!"); |
| 742 | } else { |
| 743 | // Otherwise, this namespace doesn't exist yet, create and insert the |
| 744 | // handler for it. |
Argyrios Kyrtzidis | 36745fd | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 745 | InsertNS = new PragmaNamespace(Namespace); |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 746 | PragmaHandlers->AddPragma(InsertNS); |
| 747 | } |
| 748 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 749 | |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 750 | // Check to make sure we don't already have a pragma for this identifier. |
| 751 | assert(!InsertNS->FindHandler(Handler->getName()) && |
| 752 | "Pragma handler already exists for this identifier!"); |
| 753 | InsertNS->AddPragma(Handler); |
| 754 | } |
| 755 | |
Daniel Dunbar | 4059653 | 2008-10-04 19:17:46 +0000 | [diff] [blame] | 756 | /// RemovePragmaHandler - Remove the specific pragma handler from the |
| 757 | /// preprocessor. If \arg Namespace is non-null, then it should be the |
| 758 | /// namespace that \arg Handler was added to. It is an error to remove |
| 759 | /// a handler that has not been registered. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 760 | void Preprocessor::RemovePragmaHandler(StringRef Namespace, |
Daniel Dunbar | 4059653 | 2008-10-04 19:17:46 +0000 | [diff] [blame] | 761 | PragmaHandler *Handler) { |
Craig Topper | be25030 | 2014-09-12 05:19:24 +0000 | [diff] [blame] | 762 | PragmaNamespace *NS = PragmaHandlers.get(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 763 | |
Daniel Dunbar | 4059653 | 2008-10-04 19:17:46 +0000 | [diff] [blame] | 764 | // 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] | 765 | if (!Namespace.empty()) { |
| 766 | PragmaHandler *Existing = PragmaHandlers->FindHandler(Namespace); |
Daniel Dunbar | 4059653 | 2008-10-04 19:17:46 +0000 | [diff] [blame] | 767 | assert(Existing && "Namespace containing handler does not exist!"); |
| 768 | |
| 769 | NS = Existing->getIfNamespace(); |
| 770 | assert(NS && "Invalid namespace, registered as a regular pragma handler!"); |
| 771 | } |
| 772 | |
| 773 | NS->RemovePragmaHandler(Handler); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 774 | |
Craig Topper | be25030 | 2014-09-12 05:19:24 +0000 | [diff] [blame] | 775 | // If this is a non-default namespace and it is now empty, remove it. |
| 776 | if (NS != PragmaHandlers.get() && NS->IsEmpty()) { |
Daniel Dunbar | 4059653 | 2008-10-04 19:17:46 +0000 | [diff] [blame] | 777 | PragmaHandlers->RemovePragmaHandler(NS); |
Argyrios Kyrtzidis | 7ce7526 | 2012-01-06 00:22:09 +0000 | [diff] [blame] | 778 | delete NS; |
| 779 | } |
Daniel Dunbar | 4059653 | 2008-10-04 19:17:46 +0000 | [diff] [blame] | 780 | } |
| 781 | |
Peter Collingbourne | 3bffa52 | 2011-02-14 01:42:24 +0000 | [diff] [blame] | 782 | bool Preprocessor::LexOnOffSwitch(tok::OnOffSwitch &Result) { |
| 783 | Token Tok; |
| 784 | LexUnexpandedToken(Tok); |
| 785 | |
| 786 | if (Tok.isNot(tok::identifier)) { |
| 787 | Diag(Tok, diag::ext_on_off_switch_syntax); |
| 788 | return true; |
| 789 | } |
| 790 | IdentifierInfo *II = Tok.getIdentifierInfo(); |
| 791 | if (II->isStr("ON")) |
| 792 | Result = tok::OOS_ON; |
| 793 | else if (II->isStr("OFF")) |
| 794 | Result = tok::OOS_OFF; |
| 795 | else if (II->isStr("DEFAULT")) |
| 796 | Result = tok::OOS_DEFAULT; |
| 797 | else { |
| 798 | Diag(Tok, diag::ext_on_off_switch_syntax); |
| 799 | return true; |
| 800 | } |
| 801 | |
Peter Collingbourne | 2f1e36b | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 802 | // Verify that this is followed by EOD. |
Peter Collingbourne | 3bffa52 | 2011-02-14 01:42:24 +0000 | [diff] [blame] | 803 | LexUnexpandedToken(Tok); |
Peter Collingbourne | 2f1e36b | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 804 | if (Tok.isNot(tok::eod)) |
| 805 | Diag(Tok, diag::ext_pragma_syntax_eod); |
Peter Collingbourne | 3bffa52 | 2011-02-14 01:42:24 +0000 | [diff] [blame] | 806 | return false; |
| 807 | } |
| 808 | |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 809 | namespace { |
James Dennett | 18a6d79 | 2012-06-17 03:26:26 +0000 | [diff] [blame] | 810 | /// PragmaOnceHandler - "\#pragma once" marks the file as atomically included. |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 811 | struct PragmaOnceHandler : public PragmaHandler { |
Argyrios Kyrtzidis | 36745fd | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 812 | PragmaOnceHandler() : PragmaHandler("once") {} |
Craig Topper | 9140dd2 | 2014-03-11 06:50:42 +0000 | [diff] [blame] | 813 | void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 814 | Token &OnceTok) override { |
Chris Lattner | ce2ab6f | 2009-04-14 05:07:49 +0000 | [diff] [blame] | 815 | PP.CheckEndOfDirective("pragma once"); |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 816 | PP.HandlePragmaOnce(OnceTok); |
| 817 | } |
| 818 | }; |
| 819 | |
James Dennett | 18a6d79 | 2012-06-17 03:26:26 +0000 | [diff] [blame] | 820 | /// PragmaMarkHandler - "\#pragma mark ..." is ignored by the compiler, and the |
Chris Lattner | c238331 | 2007-12-19 19:38:36 +0000 | [diff] [blame] | 821 | /// rest of the line is not lexed. |
| 822 | struct PragmaMarkHandler : public PragmaHandler { |
Argyrios Kyrtzidis | 36745fd | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 823 | PragmaMarkHandler() : PragmaHandler("mark") {} |
Craig Topper | 9140dd2 | 2014-03-11 06:50:42 +0000 | [diff] [blame] | 824 | void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 825 | Token &MarkTok) override { |
Chris Lattner | c238331 | 2007-12-19 19:38:36 +0000 | [diff] [blame] | 826 | PP.HandlePragmaMark(); |
| 827 | } |
| 828 | }; |
| 829 | |
James Dennett | 18a6d79 | 2012-06-17 03:26:26 +0000 | [diff] [blame] | 830 | /// PragmaPoisonHandler - "\#pragma poison x" marks x as not usable. |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 831 | struct PragmaPoisonHandler : public PragmaHandler { |
Argyrios Kyrtzidis | 36745fd | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 832 | PragmaPoisonHandler() : PragmaHandler("poison") {} |
Craig Topper | 9140dd2 | 2014-03-11 06:50:42 +0000 | [diff] [blame] | 833 | void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 834 | Token &PoisonTok) override { |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 835 | PP.HandlePragmaPoison(PoisonTok); |
| 836 | } |
| 837 | }; |
| 838 | |
James Dennett | 18a6d79 | 2012-06-17 03:26:26 +0000 | [diff] [blame] | 839 | /// PragmaSystemHeaderHandler - "\#pragma system_header" marks the current file |
Chris Lattner | c238331 | 2007-12-19 19:38:36 +0000 | [diff] [blame] | 840 | /// as a system header, which silences warnings in it. |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 841 | struct PragmaSystemHeaderHandler : public PragmaHandler { |
Argyrios Kyrtzidis | 36745fd | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 842 | PragmaSystemHeaderHandler() : PragmaHandler("system_header") {} |
Craig Topper | 9140dd2 | 2014-03-11 06:50:42 +0000 | [diff] [blame] | 843 | void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 844 | Token &SHToken) override { |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 845 | PP.HandlePragmaSystemHeader(SHToken); |
Chris Lattner | ce2ab6f | 2009-04-14 05:07:49 +0000 | [diff] [blame] | 846 | PP.CheckEndOfDirective("pragma"); |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 847 | } |
| 848 | }; |
| 849 | struct PragmaDependencyHandler : public PragmaHandler { |
Argyrios Kyrtzidis | 36745fd | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 850 | PragmaDependencyHandler() : PragmaHandler("dependency") {} |
Craig Topper | 9140dd2 | 2014-03-11 06:50:42 +0000 | [diff] [blame] | 851 | void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 852 | Token &DepToken) override { |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 853 | PP.HandlePragmaDependency(DepToken); |
| 854 | } |
| 855 | }; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 856 | |
Daniel Dunbar | b8068c3 | 2010-07-28 15:40:33 +0000 | [diff] [blame] | 857 | struct PragmaDebugHandler : public PragmaHandler { |
| 858 | PragmaDebugHandler() : PragmaHandler("__debug") {} |
Craig Topper | 9140dd2 | 2014-03-11 06:50:42 +0000 | [diff] [blame] | 859 | void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 860 | Token &DepToken) override { |
Daniel Dunbar | b8068c3 | 2010-07-28 15:40:33 +0000 | [diff] [blame] | 861 | Token Tok; |
| 862 | PP.LexUnexpandedToken(Tok); |
| 863 | if (Tok.isNot(tok::identifier)) { |
Douglas Gregor | 3cc2648 | 2010-08-30 15:15:34 +0000 | [diff] [blame] | 864 | PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid); |
Daniel Dunbar | b8068c3 | 2010-07-28 15:40:33 +0000 | [diff] [blame] | 865 | return; |
| 866 | } |
| 867 | IdentifierInfo *II = Tok.getIdentifierInfo(); |
| 868 | |
Daniel Dunbar | f2cf329 | 2010-08-17 22:32:48 +0000 | [diff] [blame] | 869 | if (II->isStr("assert")) { |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 870 | llvm_unreachable("This is an assertion!"); |
Daniel Dunbar | b8068c3 | 2010-07-28 15:40:33 +0000 | [diff] [blame] | 871 | } else if (II->isStr("crash")) { |
David Blaikie | 5bd4c2a | 2012-08-21 18:56:49 +0000 | [diff] [blame] | 872 | LLVM_BUILTIN_TRAP; |
David Blaikie | 5d577a2 | 2012-06-29 22:03:56 +0000 | [diff] [blame] | 873 | } else if (II->isStr("parser_crash")) { |
| 874 | Token Crasher; |
Benjamin Kramer | 3162f29 | 2015-03-08 19:28:24 +0000 | [diff] [blame] | 875 | Crasher.startToken(); |
David Blaikie | 5d577a2 | 2012-06-29 22:03:56 +0000 | [diff] [blame] | 876 | Crasher.setKind(tok::annot_pragma_parser_crash); |
Benjamin Kramer | 3162f29 | 2015-03-08 19:28:24 +0000 | [diff] [blame] | 877 | Crasher.setAnnotationRange(SourceRange(Tok.getLocation())); |
David Blaikie | 5d577a2 | 2012-06-29 22:03:56 +0000 | [diff] [blame] | 878 | PP.EnterToken(Crasher); |
Richard Smith | ba3a4f9 | 2016-01-12 21:59:26 +0000 | [diff] [blame] | 879 | } else if (II->isStr("dump")) { |
| 880 | Token Identifier; |
| 881 | PP.LexUnexpandedToken(Identifier); |
| 882 | if (auto *DumpII = Identifier.getIdentifierInfo()) { |
| 883 | Token DumpAnnot; |
| 884 | DumpAnnot.startToken(); |
| 885 | DumpAnnot.setKind(tok::annot_pragma_dump); |
| 886 | DumpAnnot.setAnnotationRange( |
| 887 | SourceRange(Tok.getLocation(), Identifier.getLocation())); |
| 888 | DumpAnnot.setAnnotationValue(DumpII); |
| 889 | PP.DiscardUntilEndOfDirective(); |
| 890 | PP.EnterToken(DumpAnnot); |
| 891 | } else { |
| 892 | PP.Diag(Identifier, diag::warn_pragma_debug_missing_argument) |
| 893 | << II->getName(); |
| 894 | } |
Daniel Dunbar | f2cf329 | 2010-08-17 22:32:48 +0000 | [diff] [blame] | 895 | } else if (II->isStr("llvm_fatal_error")) { |
| 896 | llvm::report_fatal_error("#pragma clang __debug llvm_fatal_error"); |
| 897 | } else if (II->isStr("llvm_unreachable")) { |
| 898 | llvm_unreachable("#pragma clang __debug llvm_unreachable"); |
Richard Smith | 3ffa61d | 2015-04-30 23:10:40 +0000 | [diff] [blame] | 899 | } else if (II->isStr("macro")) { |
| 900 | Token MacroName; |
| 901 | PP.LexUnexpandedToken(MacroName); |
| 902 | auto *MacroII = MacroName.getIdentifierInfo(); |
| 903 | if (MacroII) |
| 904 | PP.dumpMacroInfo(MacroII); |
| 905 | else |
Richard Smith | ba3a4f9 | 2016-01-12 21:59:26 +0000 | [diff] [blame] | 906 | PP.Diag(MacroName, diag::warn_pragma_debug_missing_argument) |
| 907 | << II->getName(); |
Daniel Dunbar | f2cf329 | 2010-08-17 22:32:48 +0000 | [diff] [blame] | 908 | } else if (II->isStr("overflow_stack")) { |
| 909 | DebugOverflowStack(); |
Daniel Dunbar | 211a787 | 2010-08-18 23:09:23 +0000 | [diff] [blame] | 910 | } else if (II->isStr("handle_crash")) { |
| 911 | llvm::CrashRecoveryContext *CRC =llvm::CrashRecoveryContext::GetCurrent(); |
| 912 | if (CRC) |
| 913 | CRC->HandleCrash(); |
Tareq A. Siraj | 0de0dd4 | 2013-04-16 18:41:26 +0000 | [diff] [blame] | 914 | } else if (II->isStr("captured")) { |
| 915 | HandleCaptured(PP); |
Daniel Dunbar | f2cf329 | 2010-08-17 22:32:48 +0000 | [diff] [blame] | 916 | } else { |
| 917 | PP.Diag(Tok, diag::warn_pragma_debug_unexpected_command) |
| 918 | << II->getName(); |
Daniel Dunbar | b8068c3 | 2010-07-28 15:40:33 +0000 | [diff] [blame] | 919 | } |
Tareq A. Siraj | 0de0dd4 | 2013-04-16 18:41:26 +0000 | [diff] [blame] | 920 | |
| 921 | PPCallbacks *Callbacks = PP.getPPCallbacks(); |
| 922 | if (Callbacks) |
| 923 | Callbacks->PragmaDebug(Tok.getLocation(), II->getName()); |
| 924 | } |
| 925 | |
| 926 | void HandleCaptured(Preprocessor &PP) { |
| 927 | // Skip if emitting preprocessed output. |
| 928 | if (PP.isPreprocessedOutput()) |
| 929 | return; |
| 930 | |
| 931 | Token Tok; |
| 932 | PP.LexUnexpandedToken(Tok); |
| 933 | |
| 934 | if (Tok.isNot(tok::eod)) { |
| 935 | PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol) |
| 936 | << "pragma clang __debug captured"; |
| 937 | return; |
| 938 | } |
| 939 | |
| 940 | SourceLocation NameLoc = Tok.getLocation(); |
David Blaikie | 2eabcc9 | 2016-02-09 18:52:09 +0000 | [diff] [blame] | 941 | MutableArrayRef<Token> Toks( |
| 942 | PP.getPreprocessorAllocator().Allocate<Token>(1), 1); |
| 943 | Toks[0].startToken(); |
| 944 | Toks[0].setKind(tok::annot_pragma_captured); |
| 945 | Toks[0].setLocation(NameLoc); |
Tareq A. Siraj | 0de0dd4 | 2013-04-16 18:41:26 +0000 | [diff] [blame] | 946 | |
David Blaikie | 2eabcc9 | 2016-02-09 18:52:09 +0000 | [diff] [blame] | 947 | PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true); |
Daniel Dunbar | b8068c3 | 2010-07-28 15:40:33 +0000 | [diff] [blame] | 948 | } |
| 949 | |
Francois Pichet | 2e11f5d | 2011-05-25 16:15:03 +0000 | [diff] [blame] | 950 | // Disable MSVC warning about runtime stack overflow. |
| 951 | #ifdef _MSC_VER |
| 952 | #pragma warning(disable : 4717) |
| 953 | #endif |
Richard Trieu | 0732beb | 2013-12-21 01:04:02 +0000 | [diff] [blame] | 954 | static void DebugOverflowStack() { |
| 955 | void (*volatile Self)() = DebugOverflowStack; |
| 956 | Self(); |
Daniel Dunbar | b8068c3 | 2010-07-28 15:40:33 +0000 | [diff] [blame] | 957 | } |
Francois Pichet | 2e11f5d | 2011-05-25 16:15:03 +0000 | [diff] [blame] | 958 | #ifdef _MSC_VER |
| 959 | #pragma warning(default : 4717) |
| 960 | #endif |
| 961 | |
Daniel Dunbar | b8068c3 | 2010-07-28 15:40:33 +0000 | [diff] [blame] | 962 | }; |
| 963 | |
James Dennett | 18a6d79 | 2012-06-17 03:26:26 +0000 | [diff] [blame] | 964 | /// PragmaDiagnosticHandler - e.g. '\#pragma GCC diagnostic ignored "-Wformat"' |
Chris Lattner | 504af11 | 2009-04-19 23:16:58 +0000 | [diff] [blame] | 965 | struct PragmaDiagnosticHandler : public PragmaHandler { |
Douglas Gregor | 3bde9b1 | 2011-06-22 19:41:48 +0000 | [diff] [blame] | 966 | private: |
| 967 | const char *Namespace; |
Chris Lattner | fb42a18 | 2009-07-12 21:18:45 +0000 | [diff] [blame] | 968 | public: |
Douglas Gregor | 3bde9b1 | 2011-06-22 19:41:48 +0000 | [diff] [blame] | 969 | explicit PragmaDiagnosticHandler(const char *NS) : |
| 970 | PragmaHandler("diagnostic"), Namespace(NS) {} |
Craig Topper | 9140dd2 | 2014-03-11 06:50:42 +0000 | [diff] [blame] | 971 | void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 972 | Token &DiagToken) override { |
Argyrios Kyrtzidis | 1cb0de1 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 973 | SourceLocation DiagLoc = DiagToken.getLocation(); |
Chris Lattner | 504af11 | 2009-04-19 23:16:58 +0000 | [diff] [blame] | 974 | Token Tok; |
| 975 | PP.LexUnexpandedToken(Tok); |
| 976 | if (Tok.isNot(tok::identifier)) { |
Douglas Gregor | 3cc2648 | 2010-08-30 15:15:34 +0000 | [diff] [blame] | 977 | PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid); |
Chris Lattner | 504af11 | 2009-04-19 23:16:58 +0000 | [diff] [blame] | 978 | return; |
| 979 | } |
| 980 | IdentifierInfo *II = Tok.getIdentifierInfo(); |
Douglas Gregor | 3bde9b1 | 2011-06-22 19:41:48 +0000 | [diff] [blame] | 981 | PPCallbacks *Callbacks = PP.getPPCallbacks(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 982 | |
Alp Toker | 46df1c0 | 2014-06-12 10:15:20 +0000 | [diff] [blame] | 983 | if (II->isStr("pop")) { |
Argyrios Kyrtzidis | 1cb0de1 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 984 | if (!PP.getDiagnostics().popMappings(DiagLoc)) |
Douglas Gregor | 3cc2648 | 2010-08-30 15:15:34 +0000 | [diff] [blame] | 985 | PP.Diag(Tok, diag::warn_pragma_diagnostic_cannot_pop); |
Douglas Gregor | 3bde9b1 | 2011-06-22 19:41:48 +0000 | [diff] [blame] | 986 | else if (Callbacks) |
| 987 | Callbacks->PragmaDiagnosticPop(DiagLoc, Namespace); |
Douglas Gregor | 3cc2648 | 2010-08-30 15:15:34 +0000 | [diff] [blame] | 988 | return; |
| 989 | } else if (II->isStr("push")) { |
Argyrios Kyrtzidis | 1cb0de1 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 990 | PP.getDiagnostics().pushMappings(DiagLoc); |
Douglas Gregor | 3bde9b1 | 2011-06-22 19:41:48 +0000 | [diff] [blame] | 991 | if (Callbacks) |
| 992 | Callbacks->PragmaDiagnosticPush(DiagLoc, Namespace); |
Chris Lattner | fb42a18 | 2009-07-12 21:18:45 +0000 | [diff] [blame] | 993 | return; |
Alp Toker | 46df1c0 | 2014-06-12 10:15:20 +0000 | [diff] [blame] | 994 | } |
| 995 | |
| 996 | diag::Severity SV = llvm::StringSwitch<diag::Severity>(II->getName()) |
| 997 | .Case("ignored", diag::Severity::Ignored) |
| 998 | .Case("warning", diag::Severity::Warning) |
| 999 | .Case("error", diag::Severity::Error) |
| 1000 | .Case("fatal", diag::Severity::Fatal) |
| 1001 | .Default(diag::Severity()); |
| 1002 | |
| 1003 | if (SV == diag::Severity()) { |
Douglas Gregor | 3cc2648 | 2010-08-30 15:15:34 +0000 | [diff] [blame] | 1004 | PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid); |
Chris Lattner | 504af11 | 2009-04-19 23:16:58 +0000 | [diff] [blame] | 1005 | return; |
| 1006 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1007 | |
Chris Lattner | 504af11 | 2009-04-19 23:16:58 +0000 | [diff] [blame] | 1008 | PP.LexUnexpandedToken(Tok); |
Andy Gibbs | 58905d2 | 2012-11-17 19:15:38 +0000 | [diff] [blame] | 1009 | SourceLocation StringLoc = Tok.getLocation(); |
Chris Lattner | 504af11 | 2009-04-19 23:16:58 +0000 | [diff] [blame] | 1010 | |
Andy Gibbs | 58905d2 | 2012-11-17 19:15:38 +0000 | [diff] [blame] | 1011 | std::string WarningName; |
Andy Gibbs | a8df57a | 2012-11-17 19:16:52 +0000 | [diff] [blame] | 1012 | if (!PP.FinishLexStringLiteral(Tok, WarningName, "pragma diagnostic", |
| 1013 | /*MacroExpansion=*/false)) |
Chris Lattner | 504af11 | 2009-04-19 23:16:58 +0000 | [diff] [blame] | 1014 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1015 | |
Peter Collingbourne | 2f1e36b | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 1016 | if (Tok.isNot(tok::eod)) { |
Chris Lattner | 504af11 | 2009-04-19 23:16:58 +0000 | [diff] [blame] | 1017 | PP.Diag(Tok.getLocation(), diag::warn_pragma_diagnostic_invalid_token); |
| 1018 | return; |
| 1019 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1020 | |
Chris Lattner | 504af11 | 2009-04-19 23:16:58 +0000 | [diff] [blame] | 1021 | if (WarningName.size() < 3 || WarningName[0] != '-' || |
Richard Smith | 3be1cb2 | 2014-08-07 00:24:21 +0000 | [diff] [blame] | 1022 | (WarningName[1] != 'W' && WarningName[1] != 'R')) { |
Andy Gibbs | 58905d2 | 2012-11-17 19:15:38 +0000 | [diff] [blame] | 1023 | PP.Diag(StringLoc, diag::warn_pragma_diagnostic_invalid_option); |
Chris Lattner | 504af11 | 2009-04-19 23:16:58 +0000 | [diff] [blame] | 1024 | return; |
| 1025 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1026 | |
Sunil Srivastava | 5239de7 | 2016-02-13 01:44:05 +0000 | [diff] [blame] | 1027 | diag::Flavor Flavor = WarningName[1] == 'W' ? diag::Flavor::WarningOrError |
| 1028 | : diag::Flavor::Remark; |
Benjamin Kramer | 2193e23 | 2016-02-13 13:42:41 +0000 | [diff] [blame^] | 1029 | StringRef Group = StringRef(WarningName).substr(2); |
Sunil Srivastava | 5239de7 | 2016-02-13 01:44:05 +0000 | [diff] [blame] | 1030 | bool unknownDiag = false; |
| 1031 | if (Group == "everything") { |
| 1032 | // Special handling for pragma clang diagnostic ... "-Weverything". |
| 1033 | // There is no formal group named "everything", so there has to be a |
| 1034 | // special case for it. |
| 1035 | PP.getDiagnostics().setSeverityForAll(Flavor, SV, DiagLoc); |
| 1036 | } else |
| 1037 | unknownDiag = PP.getDiagnostics().setSeverityForGroup(Flavor, Group, SV, |
| 1038 | DiagLoc); |
| 1039 | if (unknownDiag) |
Andy Gibbs | 58905d2 | 2012-11-17 19:15:38 +0000 | [diff] [blame] | 1040 | PP.Diag(StringLoc, diag::warn_pragma_diagnostic_unknown_warning) |
| 1041 | << WarningName; |
Douglas Gregor | 3bde9b1 | 2011-06-22 19:41:48 +0000 | [diff] [blame] | 1042 | else if (Callbacks) |
Alp Toker | 46df1c0 | 2014-06-12 10:15:20 +0000 | [diff] [blame] | 1043 | Callbacks->PragmaDiagnostic(DiagLoc, Namespace, SV, WarningName); |
Chris Lattner | 504af11 | 2009-04-19 23:16:58 +0000 | [diff] [blame] | 1044 | } |
| 1045 | }; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1046 | |
Reid Kleckner | 881dff3 | 2013-09-13 22:00:30 +0000 | [diff] [blame] | 1047 | /// "\#pragma warning(...)". MSVC's diagnostics do not map cleanly to clang's |
| 1048 | /// diagnostics, so we don't really implement this pragma. We parse it and |
| 1049 | /// ignore it to avoid -Wunknown-pragma warnings. |
| 1050 | struct PragmaWarningHandler : public PragmaHandler { |
| 1051 | PragmaWarningHandler() : PragmaHandler("warning") {} |
| 1052 | |
Craig Topper | 9140dd2 | 2014-03-11 06:50:42 +0000 | [diff] [blame] | 1053 | void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 1054 | Token &Tok) override { |
Reid Kleckner | 881dff3 | 2013-09-13 22:00:30 +0000 | [diff] [blame] | 1055 | // Parse things like: |
| 1056 | // warning(push, 1) |
| 1057 | // warning(pop) |
John Thompson | 4762b23 | 2013-11-16 00:16:03 +0000 | [diff] [blame] | 1058 | // warning(disable : 1 2 3 ; error : 4 5 6 ; suppress : 7 8 9) |
Reid Kleckner | 881dff3 | 2013-09-13 22:00:30 +0000 | [diff] [blame] | 1059 | SourceLocation DiagLoc = Tok.getLocation(); |
| 1060 | PPCallbacks *Callbacks = PP.getPPCallbacks(); |
| 1061 | |
| 1062 | PP.Lex(Tok); |
| 1063 | if (Tok.isNot(tok::l_paren)) { |
| 1064 | PP.Diag(Tok, diag::warn_pragma_warning_expected) << "("; |
| 1065 | return; |
| 1066 | } |
| 1067 | |
| 1068 | PP.Lex(Tok); |
| 1069 | IdentifierInfo *II = Tok.getIdentifierInfo(); |
Reid Kleckner | 881dff3 | 2013-09-13 22:00:30 +0000 | [diff] [blame] | 1070 | |
Alexander Musman | 6b080fc | 2015-05-25 11:21:20 +0000 | [diff] [blame] | 1071 | if (II && II->isStr("push")) { |
Reid Kleckner | 881dff3 | 2013-09-13 22:00:30 +0000 | [diff] [blame] | 1072 | // #pragma warning( push[ ,n ] ) |
Reid Kleckner | 4d18510 | 2013-10-02 15:19:23 +0000 | [diff] [blame] | 1073 | int Level = -1; |
Reid Kleckner | 881dff3 | 2013-09-13 22:00:30 +0000 | [diff] [blame] | 1074 | PP.Lex(Tok); |
| 1075 | if (Tok.is(tok::comma)) { |
| 1076 | PP.Lex(Tok); |
Reid Kleckner | c0dca6d | 2014-02-12 23:50:26 +0000 | [diff] [blame] | 1077 | uint64_t Value; |
| 1078 | if (Tok.is(tok::numeric_constant) && |
| 1079 | PP.parseSimpleIntegerLiteral(Tok, Value)) |
| 1080 | Level = int(Value); |
Reid Kleckner | 4d18510 | 2013-10-02 15:19:23 +0000 | [diff] [blame] | 1081 | if (Level < 0 || Level > 4) { |
Reid Kleckner | 881dff3 | 2013-09-13 22:00:30 +0000 | [diff] [blame] | 1082 | PP.Diag(Tok, diag::warn_pragma_warning_push_level); |
| 1083 | return; |
| 1084 | } |
| 1085 | } |
| 1086 | if (Callbacks) |
| 1087 | Callbacks->PragmaWarningPush(DiagLoc, Level); |
Alexander Musman | 6b080fc | 2015-05-25 11:21:20 +0000 | [diff] [blame] | 1088 | } else if (II && II->isStr("pop")) { |
Reid Kleckner | 881dff3 | 2013-09-13 22:00:30 +0000 | [diff] [blame] | 1089 | // #pragma warning( pop ) |
| 1090 | PP.Lex(Tok); |
| 1091 | if (Callbacks) |
| 1092 | Callbacks->PragmaWarningPop(DiagLoc); |
| 1093 | } else { |
| 1094 | // #pragma warning( warning-specifier : warning-number-list |
| 1095 | // [; warning-specifier : warning-number-list...] ) |
| 1096 | while (true) { |
| 1097 | II = Tok.getIdentifierInfo(); |
Alexander Musman | 6b080fc | 2015-05-25 11:21:20 +0000 | [diff] [blame] | 1098 | if (!II && !Tok.is(tok::numeric_constant)) { |
Reid Kleckner | 881dff3 | 2013-09-13 22:00:30 +0000 | [diff] [blame] | 1099 | PP.Diag(Tok, diag::warn_pragma_warning_spec_invalid); |
| 1100 | return; |
| 1101 | } |
| 1102 | |
| 1103 | // Figure out which warning specifier this is. |
Alexander Musman | 6b080fc | 2015-05-25 11:21:20 +0000 | [diff] [blame] | 1104 | bool SpecifierValid; |
| 1105 | StringRef Specifier; |
| 1106 | llvm::SmallString<1> SpecifierBuf; |
| 1107 | if (II) { |
| 1108 | Specifier = II->getName(); |
| 1109 | SpecifierValid = llvm::StringSwitch<bool>(Specifier) |
| 1110 | .Cases("default", "disable", "error", "once", |
| 1111 | "suppress", true) |
| 1112 | .Default(false); |
| 1113 | // If we read a correct specifier, snatch next token (that should be |
| 1114 | // ":", checked later). |
| 1115 | if (SpecifierValid) |
| 1116 | PP.Lex(Tok); |
| 1117 | } else { |
| 1118 | // Token is a numeric constant. It should be either 1, 2, 3 or 4. |
| 1119 | uint64_t Value; |
| 1120 | Specifier = PP.getSpelling(Tok, SpecifierBuf); |
| 1121 | if (PP.parseSimpleIntegerLiteral(Tok, Value)) { |
| 1122 | SpecifierValid = (Value >= 1) && (Value <= 4); |
| 1123 | } else |
| 1124 | SpecifierValid = false; |
| 1125 | // Next token already snatched by parseSimpleIntegerLiteral. |
| 1126 | } |
| 1127 | |
Reid Kleckner | 881dff3 | 2013-09-13 22:00:30 +0000 | [diff] [blame] | 1128 | if (!SpecifierValid) { |
| 1129 | PP.Diag(Tok, diag::warn_pragma_warning_spec_invalid); |
| 1130 | return; |
| 1131 | } |
Reid Kleckner | 881dff3 | 2013-09-13 22:00:30 +0000 | [diff] [blame] | 1132 | if (Tok.isNot(tok::colon)) { |
| 1133 | PP.Diag(Tok, diag::warn_pragma_warning_expected) << ":"; |
| 1134 | return; |
| 1135 | } |
| 1136 | |
| 1137 | // Collect the warning ids. |
| 1138 | SmallVector<int, 4> Ids; |
| 1139 | PP.Lex(Tok); |
| 1140 | while (Tok.is(tok::numeric_constant)) { |
Reid Kleckner | c0dca6d | 2014-02-12 23:50:26 +0000 | [diff] [blame] | 1141 | uint64_t Value; |
| 1142 | if (!PP.parseSimpleIntegerLiteral(Tok, Value) || Value == 0 || |
| 1143 | Value > INT_MAX) { |
Reid Kleckner | 881dff3 | 2013-09-13 22:00:30 +0000 | [diff] [blame] | 1144 | PP.Diag(Tok, diag::warn_pragma_warning_expected_number); |
| 1145 | return; |
| 1146 | } |
Reid Kleckner | c0dca6d | 2014-02-12 23:50:26 +0000 | [diff] [blame] | 1147 | Ids.push_back(int(Value)); |
Reid Kleckner | 881dff3 | 2013-09-13 22:00:30 +0000 | [diff] [blame] | 1148 | } |
| 1149 | if (Callbacks) |
| 1150 | Callbacks->PragmaWarning(DiagLoc, Specifier, Ids); |
| 1151 | |
| 1152 | // Parse the next specifier if there is a semicolon. |
| 1153 | if (Tok.isNot(tok::semi)) |
| 1154 | break; |
| 1155 | PP.Lex(Tok); |
| 1156 | } |
| 1157 | } |
| 1158 | |
| 1159 | if (Tok.isNot(tok::r_paren)) { |
| 1160 | PP.Diag(Tok, diag::warn_pragma_warning_expected) << ")"; |
| 1161 | return; |
| 1162 | } |
| 1163 | |
| 1164 | PP.Lex(Tok); |
| 1165 | if (Tok.isNot(tok::eod)) |
| 1166 | PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma warning"; |
| 1167 | } |
| 1168 | }; |
| 1169 | |
James Dennett | 18a6d79 | 2012-06-17 03:26:26 +0000 | [diff] [blame] | 1170 | /// PragmaIncludeAliasHandler - "\#pragma include_alias("...")". |
Aaron Ballman | 611306e | 2012-03-02 22:51:54 +0000 | [diff] [blame] | 1171 | struct PragmaIncludeAliasHandler : public PragmaHandler { |
| 1172 | PragmaIncludeAliasHandler() : PragmaHandler("include_alias") {} |
Craig Topper | 9140dd2 | 2014-03-11 06:50:42 +0000 | [diff] [blame] | 1173 | void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 1174 | Token &IncludeAliasTok) override { |
Reid Kleckner | 881dff3 | 2013-09-13 22:00:30 +0000 | [diff] [blame] | 1175 | PP.HandlePragmaIncludeAlias(IncludeAliasTok); |
Aaron Ballman | 611306e | 2012-03-02 22:51:54 +0000 | [diff] [blame] | 1176 | } |
| 1177 | }; |
| 1178 | |
Andy Gibbs | 9c2ccd6 | 2013-04-17 16:16:16 +0000 | [diff] [blame] | 1179 | /// PragmaMessageHandler - Handle the microsoft and gcc \#pragma message |
| 1180 | /// extension. The syntax is: |
| 1181 | /// \code |
| 1182 | /// #pragma message(string) |
| 1183 | /// \endcode |
| 1184 | /// OR, in GCC mode: |
| 1185 | /// \code |
| 1186 | /// #pragma message string |
| 1187 | /// \endcode |
| 1188 | /// string is a string, which is fully macro expanded, and permits string |
| 1189 | /// concatenation, embedded escape characters, etc... See MSDN for more details. |
| 1190 | /// Also handles \#pragma GCC warning and \#pragma GCC error which take the same |
| 1191 | /// form as \#pragma message. |
Chris Lattner | 30c924b | 2010-06-26 17:11:39 +0000 | [diff] [blame] | 1192 | struct PragmaMessageHandler : public PragmaHandler { |
Andy Gibbs | 9c2ccd6 | 2013-04-17 16:16:16 +0000 | [diff] [blame] | 1193 | private: |
| 1194 | const PPCallbacks::PragmaMessageKind Kind; |
| 1195 | const StringRef Namespace; |
| 1196 | |
| 1197 | static const char* PragmaKind(PPCallbacks::PragmaMessageKind Kind, |
| 1198 | bool PragmaNameOnly = false) { |
| 1199 | switch (Kind) { |
| 1200 | case PPCallbacks::PMK_Message: |
| 1201 | return PragmaNameOnly ? "message" : "pragma message"; |
| 1202 | case PPCallbacks::PMK_Warning: |
| 1203 | return PragmaNameOnly ? "warning" : "pragma warning"; |
| 1204 | case PPCallbacks::PMK_Error: |
| 1205 | return PragmaNameOnly ? "error" : "pragma error"; |
| 1206 | } |
| 1207 | llvm_unreachable("Unknown PragmaMessageKind!"); |
| 1208 | } |
| 1209 | |
| 1210 | public: |
| 1211 | PragmaMessageHandler(PPCallbacks::PragmaMessageKind Kind, |
| 1212 | StringRef Namespace = StringRef()) |
| 1213 | : PragmaHandler(PragmaKind(Kind, true)), Kind(Kind), Namespace(Namespace) {} |
| 1214 | |
Craig Topper | 9140dd2 | 2014-03-11 06:50:42 +0000 | [diff] [blame] | 1215 | void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 1216 | Token &Tok) override { |
Andy Gibbs | 9c2ccd6 | 2013-04-17 16:16:16 +0000 | [diff] [blame] | 1217 | SourceLocation MessageLoc = Tok.getLocation(); |
| 1218 | PP.Lex(Tok); |
| 1219 | bool ExpectClosingParen = false; |
| 1220 | switch (Tok.getKind()) { |
| 1221 | case tok::l_paren: |
| 1222 | // We have a MSVC style pragma message. |
| 1223 | ExpectClosingParen = true; |
| 1224 | // Read the string. |
| 1225 | PP.Lex(Tok); |
| 1226 | break; |
| 1227 | case tok::string_literal: |
| 1228 | // We have a GCC style pragma message, and we just read the string. |
| 1229 | break; |
| 1230 | default: |
| 1231 | PP.Diag(MessageLoc, diag::err_pragma_message_malformed) << Kind; |
| 1232 | return; |
| 1233 | } |
| 1234 | |
| 1235 | std::string MessageString; |
| 1236 | if (!PP.FinishLexStringLiteral(Tok, MessageString, PragmaKind(Kind), |
| 1237 | /*MacroExpansion=*/true)) |
| 1238 | return; |
| 1239 | |
| 1240 | if (ExpectClosingParen) { |
| 1241 | if (Tok.isNot(tok::r_paren)) { |
| 1242 | PP.Diag(Tok.getLocation(), diag::err_pragma_message_malformed) << Kind; |
| 1243 | return; |
| 1244 | } |
| 1245 | PP.Lex(Tok); // eat the r_paren. |
| 1246 | } |
| 1247 | |
| 1248 | if (Tok.isNot(tok::eod)) { |
| 1249 | PP.Diag(Tok.getLocation(), diag::err_pragma_message_malformed) << Kind; |
| 1250 | return; |
| 1251 | } |
| 1252 | |
| 1253 | // Output the message. |
| 1254 | PP.Diag(MessageLoc, (Kind == PPCallbacks::PMK_Error) |
| 1255 | ? diag::err_pragma_message |
| 1256 | : diag::warn_pragma_message) << MessageString; |
| 1257 | |
| 1258 | // If the pragma is lexically sound, notify any interested PPCallbacks. |
| 1259 | if (PPCallbacks *Callbacks = PP.getPPCallbacks()) |
| 1260 | Callbacks->PragmaMessage(MessageLoc, Namespace, Kind, MessageString); |
Chris Lattner | 30c924b | 2010-06-26 17:11:39 +0000 | [diff] [blame] | 1261 | } |
| 1262 | }; |
| 1263 | |
James Dennett | 18a6d79 | 2012-06-17 03:26:26 +0000 | [diff] [blame] | 1264 | /// PragmaPushMacroHandler - "\#pragma push_macro" saves the value of the |
Chris Lattner | c0a585d | 2010-08-17 15:55:45 +0000 | [diff] [blame] | 1265 | /// macro on the top of the stack. |
| 1266 | struct PragmaPushMacroHandler : public PragmaHandler { |
| 1267 | PragmaPushMacroHandler() : PragmaHandler("push_macro") {} |
Craig Topper | 9140dd2 | 2014-03-11 06:50:42 +0000 | [diff] [blame] | 1268 | void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 1269 | Token &PushMacroTok) override { |
Chris Lattner | c0a585d | 2010-08-17 15:55:45 +0000 | [diff] [blame] | 1270 | PP.HandlePragmaPushMacro(PushMacroTok); |
| 1271 | } |
| 1272 | }; |
| 1273 | |
| 1274 | |
James Dennett | 18a6d79 | 2012-06-17 03:26:26 +0000 | [diff] [blame] | 1275 | /// PragmaPopMacroHandler - "\#pragma pop_macro" sets the value of the |
Chris Lattner | c0a585d | 2010-08-17 15:55:45 +0000 | [diff] [blame] | 1276 | /// macro to the value on the top of the stack. |
| 1277 | struct PragmaPopMacroHandler : public PragmaHandler { |
| 1278 | PragmaPopMacroHandler() : PragmaHandler("pop_macro") {} |
Craig Topper | 9140dd2 | 2014-03-11 06:50:42 +0000 | [diff] [blame] | 1279 | void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 1280 | Token &PopMacroTok) override { |
Chris Lattner | c0a585d | 2010-08-17 15:55:45 +0000 | [diff] [blame] | 1281 | PP.HandlePragmaPopMacro(PopMacroTok); |
| 1282 | } |
| 1283 | }; |
| 1284 | |
Chris Lattner | 958ee04 | 2009-04-19 21:20:35 +0000 | [diff] [blame] | 1285 | // Pragma STDC implementations. |
Chris Lattner | 02ef4e3 | 2009-04-19 21:50:08 +0000 | [diff] [blame] | 1286 | |
James Dennett | 18a6d79 | 2012-06-17 03:26:26 +0000 | [diff] [blame] | 1287 | /// PragmaSTDC_FENV_ACCESSHandler - "\#pragma STDC FENV_ACCESS ...". |
Chris Lattner | 958ee04 | 2009-04-19 21:20:35 +0000 | [diff] [blame] | 1288 | struct PragmaSTDC_FENV_ACCESSHandler : public PragmaHandler { |
Argyrios Kyrtzidis | 36745fd | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 1289 | PragmaSTDC_FENV_ACCESSHandler() : PragmaHandler("FENV_ACCESS") {} |
Craig Topper | 9140dd2 | 2014-03-11 06:50:42 +0000 | [diff] [blame] | 1290 | void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 1291 | Token &Tok) override { |
Peter Collingbourne | 3bffa52 | 2011-02-14 01:42:24 +0000 | [diff] [blame] | 1292 | tok::OnOffSwitch OOS; |
| 1293 | if (PP.LexOnOffSwitch(OOS)) |
| 1294 | return; |
| 1295 | if (OOS == tok::OOS_ON) |
Chris Lattner | df22268 | 2009-04-19 21:55:32 +0000 | [diff] [blame] | 1296 | PP.Diag(Tok, diag::warn_stdc_fenv_access_not_supported); |
Chris Lattner | 958ee04 | 2009-04-19 21:20:35 +0000 | [diff] [blame] | 1297 | } |
| 1298 | }; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1299 | |
James Dennett | 18a6d79 | 2012-06-17 03:26:26 +0000 | [diff] [blame] | 1300 | /// PragmaSTDC_CX_LIMITED_RANGEHandler - "\#pragma STDC CX_LIMITED_RANGE ...". |
Chris Lattner | 958ee04 | 2009-04-19 21:20:35 +0000 | [diff] [blame] | 1301 | struct PragmaSTDC_CX_LIMITED_RANGEHandler : public PragmaHandler { |
Argyrios Kyrtzidis | 36745fd | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 1302 | PragmaSTDC_CX_LIMITED_RANGEHandler() |
| 1303 | : PragmaHandler("CX_LIMITED_RANGE") {} |
Craig Topper | 9140dd2 | 2014-03-11 06:50:42 +0000 | [diff] [blame] | 1304 | void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 1305 | Token &Tok) override { |
Peter Collingbourne | 3bffa52 | 2011-02-14 01:42:24 +0000 | [diff] [blame] | 1306 | tok::OnOffSwitch OOS; |
| 1307 | PP.LexOnOffSwitch(OOS); |
Chris Lattner | 958ee04 | 2009-04-19 21:20:35 +0000 | [diff] [blame] | 1308 | } |
| 1309 | }; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1310 | |
James Dennett | 18a6d79 | 2012-06-17 03:26:26 +0000 | [diff] [blame] | 1311 | /// PragmaSTDC_UnknownHandler - "\#pragma STDC ...". |
Chris Lattner | 958ee04 | 2009-04-19 21:20:35 +0000 | [diff] [blame] | 1312 | struct PragmaSTDC_UnknownHandler : public PragmaHandler { |
Argyrios Kyrtzidis | 36745fd | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 1313 | PragmaSTDC_UnknownHandler() {} |
Craig Topper | 9140dd2 | 2014-03-11 06:50:42 +0000 | [diff] [blame] | 1314 | void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 1315 | Token &UnknownTok) override { |
Chris Lattner | 02ef4e3 | 2009-04-19 21:50:08 +0000 | [diff] [blame] | 1316 | // C99 6.10.6p2, unknown forms are not allowed. |
Chris Lattner | a0b1f76 | 2009-04-19 21:25:37 +0000 | [diff] [blame] | 1317 | PP.Diag(UnknownTok, diag::ext_stdc_pragma_ignored); |
Chris Lattner | 958ee04 | 2009-04-19 21:20:35 +0000 | [diff] [blame] | 1318 | } |
| 1319 | }; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1320 | |
John McCall | 32f5fe1 | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 1321 | /// PragmaARCCFCodeAuditedHandler - |
James Dennett | 18a6d79 | 2012-06-17 03:26:26 +0000 | [diff] [blame] | 1322 | /// \#pragma clang arc_cf_code_audited begin/end |
John McCall | 32f5fe1 | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 1323 | struct PragmaARCCFCodeAuditedHandler : public PragmaHandler { |
| 1324 | PragmaARCCFCodeAuditedHandler() : PragmaHandler("arc_cf_code_audited") {} |
Craig Topper | 9140dd2 | 2014-03-11 06:50:42 +0000 | [diff] [blame] | 1325 | void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 1326 | Token &NameTok) override { |
John McCall | 32f5fe1 | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 1327 | SourceLocation Loc = NameTok.getLocation(); |
| 1328 | bool IsBegin; |
| 1329 | |
| 1330 | Token Tok; |
| 1331 | |
| 1332 | // Lex the 'begin' or 'end'. |
| 1333 | PP.LexUnexpandedToken(Tok); |
| 1334 | const IdentifierInfo *BeginEnd = Tok.getIdentifierInfo(); |
| 1335 | if (BeginEnd && BeginEnd->isStr("begin")) { |
| 1336 | IsBegin = true; |
| 1337 | } else if (BeginEnd && BeginEnd->isStr("end")) { |
| 1338 | IsBegin = false; |
| 1339 | } else { |
| 1340 | PP.Diag(Tok.getLocation(), diag::err_pp_arc_cf_code_audited_syntax); |
| 1341 | return; |
| 1342 | } |
| 1343 | |
| 1344 | // Verify that this is followed by EOD. |
| 1345 | PP.LexUnexpandedToken(Tok); |
| 1346 | if (Tok.isNot(tok::eod)) |
| 1347 | PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma"; |
| 1348 | |
| 1349 | // The start location of the active audit. |
| 1350 | SourceLocation BeginLoc = PP.getPragmaARCCFCodeAuditedLoc(); |
| 1351 | |
| 1352 | // The start location we want after processing this. |
| 1353 | SourceLocation NewLoc; |
| 1354 | |
| 1355 | if (IsBegin) { |
| 1356 | // Complain about attempts to re-enter an audit. |
| 1357 | if (BeginLoc.isValid()) { |
| 1358 | PP.Diag(Loc, diag::err_pp_double_begin_of_arc_cf_code_audited); |
| 1359 | PP.Diag(BeginLoc, diag::note_pragma_entered_here); |
| 1360 | } |
| 1361 | NewLoc = Loc; |
| 1362 | } else { |
| 1363 | // Complain about attempts to leave an audit that doesn't exist. |
| 1364 | if (!BeginLoc.isValid()) { |
| 1365 | PP.Diag(Loc, diag::err_pp_unmatched_end_of_arc_cf_code_audited); |
| 1366 | return; |
| 1367 | } |
| 1368 | NewLoc = SourceLocation(); |
| 1369 | } |
| 1370 | |
| 1371 | PP.setPragmaARCCFCodeAuditedLoc(NewLoc); |
| 1372 | } |
| 1373 | }; |
| 1374 | |
Douglas Gregor | 2a20bd1 | 2015-06-19 18:25:57 +0000 | [diff] [blame] | 1375 | /// PragmaAssumeNonNullHandler - |
| 1376 | /// \#pragma clang assume_nonnull begin/end |
| 1377 | struct PragmaAssumeNonNullHandler : public PragmaHandler { |
| 1378 | PragmaAssumeNonNullHandler() : PragmaHandler("assume_nonnull") {} |
| 1379 | void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 1380 | Token &NameTok) override { |
| 1381 | SourceLocation Loc = NameTok.getLocation(); |
| 1382 | bool IsBegin; |
| 1383 | |
| 1384 | Token Tok; |
| 1385 | |
| 1386 | // Lex the 'begin' or 'end'. |
| 1387 | PP.LexUnexpandedToken(Tok); |
| 1388 | const IdentifierInfo *BeginEnd = Tok.getIdentifierInfo(); |
| 1389 | if (BeginEnd && BeginEnd->isStr("begin")) { |
| 1390 | IsBegin = true; |
| 1391 | } else if (BeginEnd && BeginEnd->isStr("end")) { |
| 1392 | IsBegin = false; |
| 1393 | } else { |
| 1394 | PP.Diag(Tok.getLocation(), diag::err_pp_assume_nonnull_syntax); |
| 1395 | return; |
| 1396 | } |
| 1397 | |
| 1398 | // Verify that this is followed by EOD. |
| 1399 | PP.LexUnexpandedToken(Tok); |
| 1400 | if (Tok.isNot(tok::eod)) |
| 1401 | PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma"; |
| 1402 | |
| 1403 | // The start location of the active audit. |
| 1404 | SourceLocation BeginLoc = PP.getPragmaAssumeNonNullLoc(); |
| 1405 | |
| 1406 | // The start location we want after processing this. |
| 1407 | SourceLocation NewLoc; |
| 1408 | |
| 1409 | if (IsBegin) { |
| 1410 | // Complain about attempts to re-enter an audit. |
| 1411 | if (BeginLoc.isValid()) { |
| 1412 | PP.Diag(Loc, diag::err_pp_double_begin_of_assume_nonnull); |
| 1413 | PP.Diag(BeginLoc, diag::note_pragma_entered_here); |
| 1414 | } |
| 1415 | NewLoc = Loc; |
| 1416 | } else { |
| 1417 | // Complain about attempts to leave an audit that doesn't exist. |
| 1418 | if (!BeginLoc.isValid()) { |
| 1419 | PP.Diag(Loc, diag::err_pp_unmatched_end_of_assume_nonnull); |
| 1420 | return; |
| 1421 | } |
| 1422 | NewLoc = SourceLocation(); |
| 1423 | } |
| 1424 | |
| 1425 | PP.setPragmaAssumeNonNullLoc(NewLoc); |
| 1426 | } |
| 1427 | }; |
| 1428 | |
David Majnemer | 7aa8c2f | 2013-06-30 08:18:16 +0000 | [diff] [blame] | 1429 | /// \brief Handle "\#pragma region [...]" |
| 1430 | /// |
| 1431 | /// The syntax is |
| 1432 | /// \code |
| 1433 | /// #pragma region [optional name] |
| 1434 | /// #pragma endregion [optional comment] |
| 1435 | /// \endcode |
| 1436 | /// |
| 1437 | /// \note This is |
| 1438 | /// <a href="http://msdn.microsoft.com/en-us/library/b6xkz944(v=vs.80).aspx">editor-only</a> |
| 1439 | /// pragma, just skipped by compiler. |
| 1440 | struct PragmaRegionHandler : public PragmaHandler { |
| 1441 | PragmaRegionHandler(const char *pragma) : PragmaHandler(pragma) { } |
Aaron Ballman | 406ea51 | 2012-11-30 19:52:30 +0000 | [diff] [blame] | 1442 | |
Craig Topper | 9140dd2 | 2014-03-11 06:50:42 +0000 | [diff] [blame] | 1443 | void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 1444 | Token &NameTok) override { |
David Majnemer | 7aa8c2f | 2013-06-30 08:18:16 +0000 | [diff] [blame] | 1445 | // #pragma region: endregion matches can be verified |
| 1446 | // __pragma(region): no sense, but ignored by msvc |
| 1447 | // _Pragma is not valid for MSVC, but there isn't any point |
| 1448 | // to handle a _Pragma differently. |
| 1449 | } |
| 1450 | }; |
Aaron Ballman | 406ea51 | 2012-11-30 19:52:30 +0000 | [diff] [blame] | 1451 | |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 1452 | } // end anonymous namespace |
| 1453 | |
| 1454 | |
| 1455 | /// RegisterBuiltinPragmas - Install the standard preprocessor pragmas: |
James Dennett | 18a6d79 | 2012-06-17 03:26:26 +0000 | [diff] [blame] | 1456 | /// \#pragma GCC poison/system_header/dependency and \#pragma once. |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 1457 | void Preprocessor::RegisterBuiltinPragmas() { |
Argyrios Kyrtzidis | 36745fd | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 1458 | AddPragmaHandler(new PragmaOnceHandler()); |
| 1459 | AddPragmaHandler(new PragmaMarkHandler()); |
Chris Lattner | c0a585d | 2010-08-17 15:55:45 +0000 | [diff] [blame] | 1460 | AddPragmaHandler(new PragmaPushMacroHandler()); |
| 1461 | AddPragmaHandler(new PragmaPopMacroHandler()); |
Andy Gibbs | 9c2ccd6 | 2013-04-17 16:16:16 +0000 | [diff] [blame] | 1462 | AddPragmaHandler(new PragmaMessageHandler(PPCallbacks::PMK_Message)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1463 | |
Chris Lattner | b61448d | 2009-05-12 18:21:11 +0000 | [diff] [blame] | 1464 | // #pragma GCC ... |
Argyrios Kyrtzidis | 36745fd | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 1465 | AddPragmaHandler("GCC", new PragmaPoisonHandler()); |
| 1466 | AddPragmaHandler("GCC", new PragmaSystemHeaderHandler()); |
| 1467 | AddPragmaHandler("GCC", new PragmaDependencyHandler()); |
Douglas Gregor | 3bde9b1 | 2011-06-22 19:41:48 +0000 | [diff] [blame] | 1468 | AddPragmaHandler("GCC", new PragmaDiagnosticHandler("GCC")); |
Andy Gibbs | 9c2ccd6 | 2013-04-17 16:16:16 +0000 | [diff] [blame] | 1469 | AddPragmaHandler("GCC", new PragmaMessageHandler(PPCallbacks::PMK_Warning, |
| 1470 | "GCC")); |
| 1471 | AddPragmaHandler("GCC", new PragmaMessageHandler(PPCallbacks::PMK_Error, |
| 1472 | "GCC")); |
Chris Lattner | b61448d | 2009-05-12 18:21:11 +0000 | [diff] [blame] | 1473 | // #pragma clang ... |
Argyrios Kyrtzidis | 36745fd | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 1474 | AddPragmaHandler("clang", new PragmaPoisonHandler()); |
| 1475 | AddPragmaHandler("clang", new PragmaSystemHeaderHandler()); |
Daniel Dunbar | b8068c3 | 2010-07-28 15:40:33 +0000 | [diff] [blame] | 1476 | AddPragmaHandler("clang", new PragmaDebugHandler()); |
Argyrios Kyrtzidis | 36745fd | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 1477 | AddPragmaHandler("clang", new PragmaDependencyHandler()); |
Douglas Gregor | 3bde9b1 | 2011-06-22 19:41:48 +0000 | [diff] [blame] | 1478 | AddPragmaHandler("clang", new PragmaDiagnosticHandler("clang")); |
John McCall | 32f5fe1 | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 1479 | AddPragmaHandler("clang", new PragmaARCCFCodeAuditedHandler()); |
Douglas Gregor | 2a20bd1 | 2015-06-19 18:25:57 +0000 | [diff] [blame] | 1480 | AddPragmaHandler("clang", new PragmaAssumeNonNullHandler()); |
Chris Lattner | b61448d | 2009-05-12 18:21:11 +0000 | [diff] [blame] | 1481 | |
Argyrios Kyrtzidis | 36745fd | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 1482 | AddPragmaHandler("STDC", new PragmaSTDC_FENV_ACCESSHandler()); |
| 1483 | AddPragmaHandler("STDC", new PragmaSTDC_CX_LIMITED_RANGEHandler()); |
Chris Lattner | 958ee04 | 2009-04-19 21:20:35 +0000 | [diff] [blame] | 1484 | AddPragmaHandler("STDC", new PragmaSTDC_UnknownHandler()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1485 | |
Chris Lattner | 2ff698d | 2009-01-16 08:21:25 +0000 | [diff] [blame] | 1486 | // MS extensions. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1487 | if (LangOpts.MicrosoftExt) { |
Reid Kleckner | 881dff3 | 2013-09-13 22:00:30 +0000 | [diff] [blame] | 1488 | AddPragmaHandler(new PragmaWarningHandler()); |
Aaron Ballman | 611306e | 2012-03-02 22:51:54 +0000 | [diff] [blame] | 1489 | AddPragmaHandler(new PragmaIncludeAliasHandler()); |
Aaron Ballman | 406ea51 | 2012-11-30 19:52:30 +0000 | [diff] [blame] | 1490 | AddPragmaHandler(new PragmaRegionHandler("region")); |
| 1491 | AddPragmaHandler(new PragmaRegionHandler("endregion")); |
Chris Lattner | 30c924b | 2010-06-26 17:11:39 +0000 | [diff] [blame] | 1492 | } |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 1493 | } |
Lubos Lunak | 576a041 | 2014-05-01 12:54:03 +0000 | [diff] [blame] | 1494 | |
| 1495 | /// Ignore all pragmas, useful for modes such as -Eonly which would otherwise |
| 1496 | /// warn about those pragmas being unknown. |
| 1497 | void Preprocessor::IgnorePragmas() { |
| 1498 | AddPragmaHandler(new EmptyPragmaHandler()); |
| 1499 | // Also ignore all pragmas in all namespaces created |
| 1500 | // in Preprocessor::RegisterBuiltinPragmas(). |
| 1501 | AddPragmaHandler("GCC", new EmptyPragmaHandler()); |
| 1502 | AddPragmaHandler("clang", new EmptyPragmaHandler()); |
| 1503 | if (PragmaHandler *NS = PragmaHandlers->FindHandler("STDC")) { |
| 1504 | // Preprocessor::RegisterBuiltinPragmas() already registers |
| 1505 | // PragmaSTDC_UnknownHandler as the empty handler, so remove it first, |
| 1506 | // otherwise there will be an assert about a duplicate handler. |
| 1507 | PragmaNamespace *STDCNamespace = NS->getIfNamespace(); |
| 1508 | assert(STDCNamespace && |
| 1509 | "Invalid namespace, registered as a regular pragma handler!"); |
| 1510 | if (PragmaHandler *Existing = STDCNamespace->FindHandler("", false)) { |
| 1511 | RemovePragmaHandler("STDC", Existing); |
Chandler Carruth | 4d9c3df | 2014-05-02 21:44:48 +0000 | [diff] [blame] | 1512 | delete Existing; |
Lubos Lunak | 576a041 | 2014-05-01 12:54:03 +0000 | [diff] [blame] | 1513 | } |
| 1514 | } |
| 1515 | AddPragmaHandler("STDC", new EmptyPragmaHandler()); |
| 1516 | } |