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