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