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