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; |
Richard Smith | b9b0510 | 2019-03-19 01:51:19 +0000 | [diff] [blame^] | 485 | if (LexHeaderName(FilenameTok, /*AllowConcatenation*/false)) |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 486 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 487 | |
Richard Smith | b9b0510 | 2019-03-19 01:51:19 +0000 | [diff] [blame^] | 488 | // If the next token wasn't a header-name, diagnose the error. |
| 489 | if (!FilenameTok.isOneOf(tok::angle_string_literal, tok::string_literal)) { |
| 490 | Diag(FilenameTok.getLocation(), diag::err_pp_expects_filename); |
| 491 | return; |
| 492 | } |
| 493 | |
Chris Lattner | c07ba1f | 2006-10-30 05:58:32 +0000 | [diff] [blame] | 494 | // Reserve a buffer to get the spelling. |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 495 | SmallString<128> FilenameBuffer; |
Douglas Gregor | dc970f0 | 2010-03-16 22:30:13 +0000 | [diff] [blame] | 496 | bool Invalid = false; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 497 | StringRef Filename = getSpelling(FilenameTok, FilenameBuffer, &Invalid); |
Douglas Gregor | dc970f0 | 2010-03-16 22:30:13 +0000 | [diff] [blame] | 498 | if (Invalid) |
| 499 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 500 | |
Chris Lattner | d081f8c | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 501 | bool isAngled = |
| 502 | GetIncludeFilenameSpelling(FilenameTok.getLocation(), Filename); |
Chris Lattner | c07ba1f | 2006-10-30 05:58:32 +0000 | [diff] [blame] | 503 | // If GetIncludeFilenameSpelling set the start ptr to null, there was an |
| 504 | // error. |
Chris Lattner | d081f8c | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 505 | if (Filename.empty()) |
Chris Lattner | c07ba1f | 2006-10-30 05:58:32 +0000 | [diff] [blame] | 506 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 507 | |
Chris Lattner | 59a9ebd | 2006-10-18 05:34:33 +0000 | [diff] [blame] | 508 | // Search include directories for this file. |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 509 | const DirectoryLookup *CurDir; |
Richard Smith | 25d5075 | 2014-10-20 00:15:49 +0000 | [diff] [blame] | 510 | const FileEntry *File = |
| 511 | LookupFile(FilenameTok.getLocation(), Filename, isAngled, nullptr, |
Volodymyr Sapsai | 421380a | 2019-02-05 22:34:55 +0000 | [diff] [blame] | 512 | nullptr, CurDir, nullptr, nullptr, nullptr, nullptr, nullptr); |
Craig Topper | d2d442c | 2014-05-17 23:10:59 +0000 | [diff] [blame] | 513 | if (!File) { |
Eli Friedman | 3781a36 | 2011-08-30 23:07:51 +0000 | [diff] [blame] | 514 | if (!SuppressIncludeNotFoundError) |
| 515 | Diag(FilenameTok, diag::err_pp_file_not_found) << Filename; |
Chris Lattner | 97b8e84 | 2008-11-18 08:02:48 +0000 | [diff] [blame] | 516 | return; |
| 517 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 518 | |
Chris Lattner | d32480d | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 519 | const FileEntry *CurFile = getCurrentFileLexer()->getFileEntry(); |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 520 | |
| 521 | // If this file is older than the file it depends on, emit a diagnostic. |
| 522 | if (CurFile && CurFile->getModificationTime() < File->getModificationTime()) { |
| 523 | // Lex tokens at the end of the message and include them in the message. |
| 524 | std::string Message; |
| 525 | Lex(DependencyTok); |
Peter Collingbourne | 2f1e36b | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 526 | while (DependencyTok.isNot(tok::eod)) { |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 527 | Message += getSpelling(DependencyTok) + " "; |
| 528 | Lex(DependencyTok); |
| 529 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 530 | |
Chris Lattner | f0b0497 | 2010-09-05 23:16:09 +0000 | [diff] [blame] | 531 | // Remove the trailing ' ' if present. |
| 532 | if (!Message.empty()) |
| 533 | Message.erase(Message.end()-1); |
Chris Lattner | 97b8e84 | 2008-11-18 08:02:48 +0000 | [diff] [blame] | 534 | Diag(FilenameTok, diag::pp_out_of_date_dependency) << Message; |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 535 | } |
| 536 | } |
| 537 | |
Reid Kleckner | 002562a | 2013-05-06 21:02:12 +0000 | [diff] [blame] | 538 | /// ParsePragmaPushOrPopMacro - Handle parsing of pragma push_macro/pop_macro. |
Chris Lattner | c0a585d | 2010-08-17 15:55:45 +0000 | [diff] [blame] | 539 | /// Return the IdentifierInfo* associated with the macro to push or pop. |
| 540 | IdentifierInfo *Preprocessor::ParsePragmaPushOrPopMacro(Token &Tok) { |
| 541 | // Remember the pragma token location. |
| 542 | Token PragmaTok = Tok; |
| 543 | |
| 544 | // Read the '('. |
| 545 | Lex(Tok); |
| 546 | if (Tok.isNot(tok::l_paren)) { |
| 547 | Diag(PragmaTok.getLocation(), diag::err_pragma_push_pop_macro_malformed) |
| 548 | << getSpelling(PragmaTok); |
Craig Topper | d2d442c | 2014-05-17 23:10:59 +0000 | [diff] [blame] | 549 | return nullptr; |
Chris Lattner | c0a585d | 2010-08-17 15:55:45 +0000 | [diff] [blame] | 550 | } |
| 551 | |
| 552 | // Read the macro name string. |
| 553 | Lex(Tok); |
| 554 | if (Tok.isNot(tok::string_literal)) { |
| 555 | Diag(PragmaTok.getLocation(), diag::err_pragma_push_pop_macro_malformed) |
| 556 | << getSpelling(PragmaTok); |
Craig Topper | d2d442c | 2014-05-17 23:10:59 +0000 | [diff] [blame] | 557 | return nullptr; |
Chris Lattner | c0a585d | 2010-08-17 15:55:45 +0000 | [diff] [blame] | 558 | } |
| 559 | |
Richard Smith | d67aea2 | 2012-03-06 03:21:47 +0000 | [diff] [blame] | 560 | if (Tok.hasUDSuffix()) { |
| 561 | Diag(Tok, diag::err_invalid_string_udl); |
Craig Topper | d2d442c | 2014-05-17 23:10:59 +0000 | [diff] [blame] | 562 | return nullptr; |
Richard Smith | d67aea2 | 2012-03-06 03:21:47 +0000 | [diff] [blame] | 563 | } |
| 564 | |
Chris Lattner | c0a585d | 2010-08-17 15:55:45 +0000 | [diff] [blame] | 565 | // Remember the macro string. |
| 566 | std::string StrVal = getSpelling(Tok); |
| 567 | |
| 568 | // Read the ')'. |
| 569 | Lex(Tok); |
| 570 | if (Tok.isNot(tok::r_paren)) { |
| 571 | Diag(PragmaTok.getLocation(), diag::err_pragma_push_pop_macro_malformed) |
| 572 | << getSpelling(PragmaTok); |
Craig Topper | d2d442c | 2014-05-17 23:10:59 +0000 | [diff] [blame] | 573 | return nullptr; |
Chris Lattner | c0a585d | 2010-08-17 15:55:45 +0000 | [diff] [blame] | 574 | } |
| 575 | |
| 576 | assert(StrVal[0] == '"' && StrVal[StrVal.size()-1] == '"' && |
| 577 | "Invalid string token!"); |
| 578 | |
| 579 | // Create a Token from the string. |
| 580 | Token MacroTok; |
| 581 | MacroTok.startToken(); |
Abramo Bagnara | ea4f7c7 | 2010-12-22 08:23:18 +0000 | [diff] [blame] | 582 | MacroTok.setKind(tok::raw_identifier); |
Dmitri Gribenko | b8e9e75 | 2012-09-24 21:07:17 +0000 | [diff] [blame] | 583 | CreateString(StringRef(&StrVal[1], StrVal.size() - 2), MacroTok); |
Chris Lattner | c0a585d | 2010-08-17 15:55:45 +0000 | [diff] [blame] | 584 | |
| 585 | // Get the IdentifierInfo of MacroToPushTok. |
| 586 | return LookUpIdentifierInfo(MacroTok); |
| 587 | } |
| 588 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 589 | /// Handle \#pragma push_macro. |
James Dennett | 18a6d79 | 2012-06-17 03:26:26 +0000 | [diff] [blame] | 590 | /// |
Chris Lattner | c0a585d | 2010-08-17 15:55:45 +0000 | [diff] [blame] | 591 | /// The syntax is: |
James Dennett | 18a6d79 | 2012-06-17 03:26:26 +0000 | [diff] [blame] | 592 | /// \code |
Dmitri Gribenko | 9ebd161 | 2012-11-30 20:04:39 +0000 | [diff] [blame] | 593 | /// #pragma push_macro("macro") |
James Dennett | 18a6d79 | 2012-06-17 03:26:26 +0000 | [diff] [blame] | 594 | /// \endcode |
Chris Lattner | c0a585d | 2010-08-17 15:55:45 +0000 | [diff] [blame] | 595 | void Preprocessor::HandlePragmaPushMacro(Token &PushMacroTok) { |
| 596 | // Parse the pragma directive and get the macro IdentifierInfo*. |
| 597 | IdentifierInfo *IdentInfo = ParsePragmaPushOrPopMacro(PushMacroTok); |
| 598 | if (!IdentInfo) return; |
| 599 | |
| 600 | // Get the MacroInfo associated with IdentInfo. |
| 601 | MacroInfo *MI = getMacroInfo(IdentInfo); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 602 | |
Chris Lattner | c0a585d | 2010-08-17 15:55:45 +0000 | [diff] [blame] | 603 | if (MI) { |
Chris Lattner | c0a585d | 2010-08-17 15:55:45 +0000 | [diff] [blame] | 604 | // Allow the original MacroInfo to be redefined later. |
| 605 | MI->setIsAllowRedefinitionsWithoutWarning(true); |
| 606 | } |
| 607 | |
| 608 | // Push the cloned MacroInfo so we can retrieve it later. |
Argyrios Kyrtzidis | 09c9e81 | 2013-02-20 00:54:57 +0000 | [diff] [blame] | 609 | PragmaPushMacroInfo[IdentInfo].push_back(MI); |
Chris Lattner | c0a585d | 2010-08-17 15:55:45 +0000 | [diff] [blame] | 610 | } |
| 611 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 612 | /// Handle \#pragma pop_macro. |
James Dennett | 18a6d79 | 2012-06-17 03:26:26 +0000 | [diff] [blame] | 613 | /// |
Chris Lattner | c0a585d | 2010-08-17 15:55:45 +0000 | [diff] [blame] | 614 | /// The syntax is: |
James Dennett | 18a6d79 | 2012-06-17 03:26:26 +0000 | [diff] [blame] | 615 | /// \code |
Chris Lattner | c0a585d | 2010-08-17 15:55:45 +0000 | [diff] [blame] | 616 | /// #pragma pop_macro("macro") |
James Dennett | 18a6d79 | 2012-06-17 03:26:26 +0000 | [diff] [blame] | 617 | /// \endcode |
Chris Lattner | c0a585d | 2010-08-17 15:55:45 +0000 | [diff] [blame] | 618 | void Preprocessor::HandlePragmaPopMacro(Token &PopMacroTok) { |
| 619 | SourceLocation MessageLoc = PopMacroTok.getLocation(); |
| 620 | |
| 621 | // Parse the pragma directive and get the macro IdentifierInfo*. |
| 622 | IdentifierInfo *IdentInfo = ParsePragmaPushOrPopMacro(PopMacroTok); |
| 623 | if (!IdentInfo) return; |
| 624 | |
| 625 | // Find the vector<MacroInfo*> associated with the macro. |
Eugene Zelenko | cb96ac6 | 2017-12-08 22:39:26 +0000 | [diff] [blame] | 626 | llvm::DenseMap<IdentifierInfo *, std::vector<MacroInfo *>>::iterator iter = |
Chris Lattner | c0a585d | 2010-08-17 15:55:45 +0000 | [diff] [blame] | 627 | PragmaPushMacroInfo.find(IdentInfo); |
| 628 | if (iter != PragmaPushMacroInfo.end()) { |
Alexander Kornienko | 8b3f623 | 2012-08-29 00:20:03 +0000 | [diff] [blame] | 629 | // Forget the MacroInfo currently associated with IdentInfo. |
Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 630 | if (MacroInfo *MI = getMacroInfo(IdentInfo)) { |
Argyrios Kyrtzidis | b6210df | 2013-03-26 17:17:01 +0000 | [diff] [blame] | 631 | if (MI->isWarnIfUnused()) |
| 632 | WarnUnusedMacroLocs.erase(MI->getDefinitionLoc()); |
| 633 | appendMacroDirective(IdentInfo, AllocateUndefMacroDirective(MessageLoc)); |
Argyrios Kyrtzidis | 1cb0de1 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 634 | } |
Chris Lattner | c0a585d | 2010-08-17 15:55:45 +0000 | [diff] [blame] | 635 | |
| 636 | // Get the MacroInfo we want to reinstall. |
| 637 | MacroInfo *MacroToReInstall = iter->second.back(); |
| 638 | |
Richard Smith | 713369b | 2015-04-23 20:40:50 +0000 | [diff] [blame] | 639 | if (MacroToReInstall) |
Alexander Kornienko | c0b4928 | 2012-08-29 16:56:24 +0000 | [diff] [blame] | 640 | // Reinstall the previously pushed macro. |
Richard Smith | 713369b | 2015-04-23 20:40:50 +0000 | [diff] [blame] | 641 | appendDefMacroDirective(IdentInfo, MacroToReInstall, MessageLoc); |
Chris Lattner | c0a585d | 2010-08-17 15:55:45 +0000 | [diff] [blame] | 642 | |
| 643 | // Pop PragmaPushMacroInfo stack. |
| 644 | iter->second.pop_back(); |
Eugene Zelenko | e95e7d5 | 2016-09-07 21:53:17 +0000 | [diff] [blame] | 645 | if (iter->second.empty()) |
Chris Lattner | c0a585d | 2010-08-17 15:55:45 +0000 | [diff] [blame] | 646 | PragmaPushMacroInfo.erase(iter); |
| 647 | } else { |
| 648 | Diag(MessageLoc, diag::warn_pragma_pop_macro_no_push) |
| 649 | << IdentInfo->getName(); |
| 650 | } |
| 651 | } |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 652 | |
Aaron Ballman | 611306e | 2012-03-02 22:51:54 +0000 | [diff] [blame] | 653 | void Preprocessor::HandlePragmaIncludeAlias(Token &Tok) { |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 654 | // 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] | 655 | // have to track which we got. The first filename is the source name, |
| 656 | // and the second name is the mapped filename. If the first is quoted, |
| 657 | // 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] | 658 | |
| 659 | // Get the open paren |
| 660 | Lex(Tok); |
| 661 | if (Tok.isNot(tok::l_paren)) { |
| 662 | Diag(Tok, diag::warn_pragma_include_alias_expected) << "("; |
| 663 | return; |
| 664 | } |
| 665 | |
| 666 | // We expect either a quoted string literal, or a bracketed name |
| 667 | Token SourceFilenameTok; |
Richard Smith | b9b0510 | 2019-03-19 01:51:19 +0000 | [diff] [blame^] | 668 | if (LexHeaderName(SourceFilenameTok)) |
Aaron Ballman | 611306e | 2012-03-02 22:51:54 +0000 | [diff] [blame] | 669 | return; |
Aaron Ballman | 611306e | 2012-03-02 22:51:54 +0000 | [diff] [blame] | 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); |
Aaron Ballman | 611306e | 2012-03-02 22:51:54 +0000 | [diff] [blame] | 676 | } else { |
| 677 | Diag(Tok, diag::warn_pragma_include_alias_expected_filename); |
| 678 | return; |
| 679 | } |
| 680 | FileNameBuffer.clear(); |
| 681 | |
| 682 | // Now we expect a comma, followed by another include name |
| 683 | Lex(Tok); |
| 684 | if (Tok.isNot(tok::comma)) { |
| 685 | Diag(Tok, diag::warn_pragma_include_alias_expected) << ","; |
| 686 | return; |
| 687 | } |
| 688 | |
| 689 | Token ReplaceFilenameTok; |
Richard Smith | b9b0510 | 2019-03-19 01:51:19 +0000 | [diff] [blame^] | 690 | if (LexHeaderName(ReplaceFilenameTok)) |
Aaron Ballman | 611306e | 2012-03-02 22:51:54 +0000 | [diff] [blame] | 691 | return; |
Aaron Ballman | 611306e | 2012-03-02 22:51:54 +0000 | [diff] [blame] | 692 | |
| 693 | StringRef ReplaceFileName; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 694 | if (ReplaceFilenameTok.is(tok::string_literal) || |
Aaron Ballman | 611306e | 2012-03-02 22:51:54 +0000 | [diff] [blame] | 695 | ReplaceFilenameTok.is(tok::angle_string_literal)) { |
| 696 | ReplaceFileName = getSpelling(ReplaceFilenameTok, FileNameBuffer); |
Aaron Ballman | 611306e | 2012-03-02 22:51:54 +0000 | [diff] [blame] | 697 | } else { |
| 698 | Diag(Tok, diag::warn_pragma_include_alias_expected_filename); |
| 699 | return; |
| 700 | } |
| 701 | |
| 702 | // Finally, we expect the closing paren |
| 703 | Lex(Tok); |
| 704 | if (Tok.isNot(tok::r_paren)) { |
| 705 | Diag(Tok, diag::warn_pragma_include_alias_expected) << ")"; |
| 706 | return; |
| 707 | } |
| 708 | |
| 709 | // Now that we have the source and target filenames, we need to make sure |
| 710 | // they're both of the same type (angled vs non-angled) |
| 711 | StringRef OriginalSource = SourceFileName; |
| 712 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 713 | bool SourceIsAngled = |
| 714 | GetIncludeFilenameSpelling(SourceFilenameTok.getLocation(), |
Aaron Ballman | 611306e | 2012-03-02 22:51:54 +0000 | [diff] [blame] | 715 | SourceFileName); |
| 716 | bool ReplaceIsAngled = |
| 717 | GetIncludeFilenameSpelling(ReplaceFilenameTok.getLocation(), |
| 718 | ReplaceFileName); |
| 719 | if (!SourceFileName.empty() && !ReplaceFileName.empty() && |
| 720 | (SourceIsAngled != ReplaceIsAngled)) { |
| 721 | unsigned int DiagID; |
| 722 | if (SourceIsAngled) |
| 723 | DiagID = diag::warn_pragma_include_alias_mismatch_angle; |
| 724 | else |
| 725 | DiagID = diag::warn_pragma_include_alias_mismatch_quote; |
| 726 | |
| 727 | Diag(SourceFilenameTok.getLocation(), DiagID) |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 728 | << SourceFileName |
Aaron Ballman | 611306e | 2012-03-02 22:51:54 +0000 | [diff] [blame] | 729 | << ReplaceFileName; |
| 730 | |
| 731 | return; |
| 732 | } |
| 733 | |
| 734 | // Now we can let the include handler know about this mapping |
| 735 | getHeaderSearchInfo().AddIncludeAlias(OriginalSource, ReplaceFileName); |
| 736 | } |
| 737 | |
Richard Smith | 9565c75b | 2017-06-19 23:09:36 +0000 | [diff] [blame] | 738 | // Lex a component of a module name: either an identifier or a string literal; |
| 739 | // for components that can be expressed both ways, the two forms are equivalent. |
| 740 | static bool LexModuleNameComponent( |
| 741 | Preprocessor &PP, Token &Tok, |
| 742 | std::pair<IdentifierInfo *, SourceLocation> &ModuleNameComponent, |
| 743 | bool First) { |
| 744 | PP.LexUnexpandedToken(Tok); |
| 745 | if (Tok.is(tok::string_literal) && !Tok.hasUDSuffix()) { |
| 746 | StringLiteralParser Literal(Tok, PP); |
| 747 | if (Literal.hadError) |
| 748 | return true; |
| 749 | ModuleNameComponent = std::make_pair( |
| 750 | PP.getIdentifierInfo(Literal.GetString()), Tok.getLocation()); |
| 751 | } else if (!Tok.isAnnotation() && Tok.getIdentifierInfo()) { |
| 752 | ModuleNameComponent = |
| 753 | std::make_pair(Tok.getIdentifierInfo(), Tok.getLocation()); |
| 754 | } else { |
| 755 | PP.Diag(Tok.getLocation(), diag::err_pp_expected_module_name) << First; |
| 756 | return true; |
| 757 | } |
| 758 | return false; |
| 759 | } |
| 760 | |
| 761 | static bool LexModuleName( |
| 762 | Preprocessor &PP, Token &Tok, |
| 763 | llvm::SmallVectorImpl<std::pair<IdentifierInfo *, SourceLocation>> |
| 764 | &ModuleName) { |
| 765 | while (true) { |
| 766 | std::pair<IdentifierInfo*, SourceLocation> NameComponent; |
| 767 | if (LexModuleNameComponent(PP, Tok, NameComponent, ModuleName.empty())) |
| 768 | return true; |
| 769 | ModuleName.push_back(NameComponent); |
| 770 | |
| 771 | PP.LexUnexpandedToken(Tok); |
| 772 | if (Tok.isNot(tok::period)) |
| 773 | return false; |
| 774 | } |
| 775 | } |
| 776 | |
Richard Smith | 5d2ed48 | 2017-06-09 19:22:32 +0000 | [diff] [blame] | 777 | void Preprocessor::HandlePragmaModuleBuild(Token &Tok) { |
| 778 | SourceLocation Loc = Tok.getLocation(); |
| 779 | |
Richard Smith | 9565c75b | 2017-06-19 23:09:36 +0000 | [diff] [blame] | 780 | std::pair<IdentifierInfo *, SourceLocation> ModuleNameLoc; |
| 781 | if (LexModuleNameComponent(*this, Tok, ModuleNameLoc, true)) |
Richard Smith | 5d2ed48 | 2017-06-09 19:22:32 +0000 | [diff] [blame] | 782 | return; |
Richard Smith | 9565c75b | 2017-06-19 23:09:36 +0000 | [diff] [blame] | 783 | IdentifierInfo *ModuleName = ModuleNameLoc.first; |
Richard Smith | 5d2ed48 | 2017-06-09 19:22:32 +0000 | [diff] [blame] | 784 | |
| 785 | LexUnexpandedToken(Tok); |
| 786 | if (Tok.isNot(tok::eod)) { |
| 787 | Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma"; |
| 788 | DiscardUntilEndOfDirective(); |
| 789 | } |
| 790 | |
Richard Smith | 5d2ed48 | 2017-06-09 19:22:32 +0000 | [diff] [blame] | 791 | CurLexer->LexingRawMode = true; |
| 792 | |
| 793 | auto TryConsumeIdentifier = [&](StringRef Ident) -> bool { |
| 794 | if (Tok.getKind() != tok::raw_identifier || |
| 795 | Tok.getRawIdentifier() != Ident) |
| 796 | return false; |
| 797 | CurLexer->Lex(Tok); |
| 798 | return true; |
| 799 | }; |
| 800 | |
| 801 | // Scan forward looking for the end of the module. |
| 802 | const char *Start = CurLexer->getBufferLocation(); |
| 803 | const char *End = nullptr; |
| 804 | unsigned NestingLevel = 1; |
| 805 | while (true) { |
| 806 | End = CurLexer->getBufferLocation(); |
| 807 | CurLexer->Lex(Tok); |
| 808 | |
| 809 | if (Tok.is(tok::eof)) { |
| 810 | Diag(Loc, diag::err_pp_module_build_missing_end); |
| 811 | break; |
| 812 | } |
| 813 | |
| 814 | if (Tok.isNot(tok::hash) || !Tok.isAtStartOfLine()) { |
| 815 | // Token was part of module; keep going. |
| 816 | continue; |
| 817 | } |
| 818 | |
| 819 | // We hit something directive-shaped; check to see if this is the end |
| 820 | // of the module build. |
| 821 | CurLexer->ParsingPreprocessorDirective = true; |
| 822 | CurLexer->Lex(Tok); |
| 823 | if (TryConsumeIdentifier("pragma") && TryConsumeIdentifier("clang") && |
| 824 | TryConsumeIdentifier("module")) { |
| 825 | if (TryConsumeIdentifier("build")) |
| 826 | // #pragma clang module build -> entering a nested module build. |
| 827 | ++NestingLevel; |
| 828 | else if (TryConsumeIdentifier("endbuild")) { |
| 829 | // #pragma clang module endbuild -> leaving a module build. |
| 830 | if (--NestingLevel == 0) |
| 831 | break; |
| 832 | } |
| 833 | // We should either be looking at the EOD or more of the current directive |
| 834 | // preceding the EOD. Either way we can ignore this token and keep going. |
| 835 | assert(Tok.getKind() != tok::eof && "missing EOD before EOF"); |
| 836 | } |
| 837 | } |
| 838 | |
| 839 | CurLexer->LexingRawMode = false; |
| 840 | |
| 841 | // Load the extracted text as a preprocessed module. |
| 842 | assert(CurLexer->getBuffer().begin() <= Start && |
| 843 | Start <= CurLexer->getBuffer().end() && |
| 844 | CurLexer->getBuffer().begin() <= End && |
| 845 | End <= CurLexer->getBuffer().end() && |
| 846 | "module source range not contained within same file buffer"); |
| 847 | TheModuleLoader.loadModuleFromSource(Loc, ModuleName->getName(), |
| 848 | StringRef(Start, End - Start)); |
| 849 | } |
| 850 | |
Mike Rice | 58df1af | 2018-09-11 17:10:44 +0000 | [diff] [blame] | 851 | void Preprocessor::HandlePragmaHdrstop(Token &Tok) { |
| 852 | Lex(Tok); |
| 853 | if (Tok.is(tok::l_paren)) { |
| 854 | Diag(Tok.getLocation(), diag::warn_pp_hdrstop_filename_ignored); |
| 855 | |
| 856 | std::string FileName; |
| 857 | if (!LexStringLiteral(Tok, FileName, "pragma hdrstop", false)) |
| 858 | return; |
| 859 | |
| 860 | if (Tok.isNot(tok::r_paren)) { |
| 861 | Diag(Tok, diag::err_expected) << tok::r_paren; |
| 862 | return; |
| 863 | } |
| 864 | Lex(Tok); |
| 865 | } |
| 866 | if (Tok.isNot(tok::eod)) |
| 867 | Diag(Tok.getLocation(), diag::ext_pp_extra_tokens_at_eol) |
| 868 | << "pragma hdrstop"; |
| 869 | |
| 870 | if (creatingPCHWithPragmaHdrStop() && |
| 871 | SourceMgr.isInMainFile(Tok.getLocation())) { |
| 872 | assert(CurLexer && "no lexer for #pragma hdrstop processing"); |
| 873 | Token &Result = Tok; |
| 874 | Result.startToken(); |
| 875 | CurLexer->FormTokenWithChars(Result, CurLexer->BufferEnd, tok::eof); |
| 876 | CurLexer->cutOffLexing(); |
| 877 | } |
| 878 | if (usingPCHWithPragmaHdrStop()) |
| 879 | SkippingUntilPragmaHdrStop = false; |
| 880 | } |
| 881 | |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 882 | /// AddPragmaHandler - Add the specified pragma handler to the preprocessor. |
| 883 | /// If 'Namespace' is non-null, then it is a token required to exist on the |
| 884 | /// pragma line before the pragma string starts, e.g. "STDC" or "GCC". |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 885 | void Preprocessor::AddPragmaHandler(StringRef Namespace, |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 886 | PragmaHandler *Handler) { |
Craig Topper | be25030 | 2014-09-12 05:19:24 +0000 | [diff] [blame] | 887 | PragmaNamespace *InsertNS = PragmaHandlers.get(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 888 | |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 889 | // 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] | 890 | if (!Namespace.empty()) { |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 891 | // If there is already a pragma handler with the name of this namespace, |
| 892 | // we either have an error (directive with the same name as a namespace) or |
| 893 | // we already have the namespace to insert into. |
Argyrios Kyrtzidis | 36745fd | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 894 | if (PragmaHandler *Existing = PragmaHandlers->FindHandler(Namespace)) { |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 895 | InsertNS = Existing->getIfNamespace(); |
Craig Topper | d2d442c | 2014-05-17 23:10:59 +0000 | [diff] [blame] | 896 | assert(InsertNS != nullptr && "Cannot have a pragma namespace and pragma" |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 897 | " handler with the same name!"); |
| 898 | } else { |
| 899 | // Otherwise, this namespace doesn't exist yet, create and insert the |
| 900 | // handler for it. |
Argyrios Kyrtzidis | 36745fd | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 901 | InsertNS = new PragmaNamespace(Namespace); |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 902 | PragmaHandlers->AddPragma(InsertNS); |
| 903 | } |
| 904 | } |
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 | // Check to make sure we don't already have a pragma for this identifier. |
| 907 | assert(!InsertNS->FindHandler(Handler->getName()) && |
| 908 | "Pragma handler already exists for this identifier!"); |
| 909 | InsertNS->AddPragma(Handler); |
| 910 | } |
| 911 | |
Daniel Dunbar | 4059653 | 2008-10-04 19:17:46 +0000 | [diff] [blame] | 912 | /// RemovePragmaHandler - Remove the specific pragma handler from the |
| 913 | /// preprocessor. If \arg Namespace is non-null, then it should be the |
| 914 | /// namespace that \arg Handler was added to. It is an error to remove |
| 915 | /// a handler that has not been registered. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 916 | void Preprocessor::RemovePragmaHandler(StringRef Namespace, |
Daniel Dunbar | 4059653 | 2008-10-04 19:17:46 +0000 | [diff] [blame] | 917 | PragmaHandler *Handler) { |
Craig Topper | be25030 | 2014-09-12 05:19:24 +0000 | [diff] [blame] | 918 | PragmaNamespace *NS = PragmaHandlers.get(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 919 | |
Daniel Dunbar | 4059653 | 2008-10-04 19:17:46 +0000 | [diff] [blame] | 920 | // 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] | 921 | if (!Namespace.empty()) { |
| 922 | PragmaHandler *Existing = PragmaHandlers->FindHandler(Namespace); |
Daniel Dunbar | 4059653 | 2008-10-04 19:17:46 +0000 | [diff] [blame] | 923 | assert(Existing && "Namespace containing handler does not exist!"); |
| 924 | |
| 925 | NS = Existing->getIfNamespace(); |
| 926 | assert(NS && "Invalid namespace, registered as a regular pragma handler!"); |
| 927 | } |
| 928 | |
| 929 | NS->RemovePragmaHandler(Handler); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 930 | |
Craig Topper | be25030 | 2014-09-12 05:19:24 +0000 | [diff] [blame] | 931 | // If this is a non-default namespace and it is now empty, remove it. |
| 932 | if (NS != PragmaHandlers.get() && NS->IsEmpty()) { |
Daniel Dunbar | 4059653 | 2008-10-04 19:17:46 +0000 | [diff] [blame] | 933 | PragmaHandlers->RemovePragmaHandler(NS); |
Argyrios Kyrtzidis | 7ce7526 | 2012-01-06 00:22:09 +0000 | [diff] [blame] | 934 | delete NS; |
| 935 | } |
Daniel Dunbar | 4059653 | 2008-10-04 19:17:46 +0000 | [diff] [blame] | 936 | } |
| 937 | |
Peter Collingbourne | 3bffa52 | 2011-02-14 01:42:24 +0000 | [diff] [blame] | 938 | bool Preprocessor::LexOnOffSwitch(tok::OnOffSwitch &Result) { |
| 939 | Token Tok; |
| 940 | LexUnexpandedToken(Tok); |
| 941 | |
| 942 | if (Tok.isNot(tok::identifier)) { |
| 943 | Diag(Tok, diag::ext_on_off_switch_syntax); |
| 944 | return true; |
| 945 | } |
| 946 | IdentifierInfo *II = Tok.getIdentifierInfo(); |
| 947 | if (II->isStr("ON")) |
| 948 | Result = tok::OOS_ON; |
| 949 | else if (II->isStr("OFF")) |
| 950 | Result = tok::OOS_OFF; |
| 951 | else if (II->isStr("DEFAULT")) |
| 952 | Result = tok::OOS_DEFAULT; |
| 953 | else { |
| 954 | Diag(Tok, diag::ext_on_off_switch_syntax); |
| 955 | return true; |
| 956 | } |
| 957 | |
Peter Collingbourne | 2f1e36b | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 958 | // Verify that this is followed by EOD. |
Peter Collingbourne | 3bffa52 | 2011-02-14 01:42:24 +0000 | [diff] [blame] | 959 | LexUnexpandedToken(Tok); |
Peter Collingbourne | 2f1e36b | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 960 | if (Tok.isNot(tok::eod)) |
| 961 | Diag(Tok, diag::ext_pragma_syntax_eod); |
Peter Collingbourne | 3bffa52 | 2011-02-14 01:42:24 +0000 | [diff] [blame] | 962 | return false; |
| 963 | } |
| 964 | |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 965 | namespace { |
Eugene Zelenko | e95e7d5 | 2016-09-07 21:53:17 +0000 | [diff] [blame] | 966 | |
James Dennett | 18a6d79 | 2012-06-17 03:26:26 +0000 | [diff] [blame] | 967 | /// PragmaOnceHandler - "\#pragma once" marks the file as atomically included. |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 968 | struct PragmaOnceHandler : public PragmaHandler { |
Argyrios Kyrtzidis | 36745fd | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 969 | PragmaOnceHandler() : PragmaHandler("once") {} |
Eugene Zelenko | cb96ac6 | 2017-12-08 22:39:26 +0000 | [diff] [blame] | 970 | |
Craig Topper | 9140dd2 | 2014-03-11 06:50:42 +0000 | [diff] [blame] | 971 | void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 972 | Token &OnceTok) override { |
Chris Lattner | ce2ab6f | 2009-04-14 05:07:49 +0000 | [diff] [blame] | 973 | PP.CheckEndOfDirective("pragma once"); |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 974 | PP.HandlePragmaOnce(OnceTok); |
| 975 | } |
| 976 | }; |
| 977 | |
James Dennett | 18a6d79 | 2012-06-17 03:26:26 +0000 | [diff] [blame] | 978 | /// PragmaMarkHandler - "\#pragma mark ..." is ignored by the compiler, and the |
Chris Lattner | c238331 | 2007-12-19 19:38:36 +0000 | [diff] [blame] | 979 | /// rest of the line is not lexed. |
| 980 | struct PragmaMarkHandler : public PragmaHandler { |
Argyrios Kyrtzidis | 36745fd | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 981 | PragmaMarkHandler() : PragmaHandler("mark") {} |
Eugene Zelenko | e95e7d5 | 2016-09-07 21:53:17 +0000 | [diff] [blame] | 982 | |
Craig Topper | 9140dd2 | 2014-03-11 06:50:42 +0000 | [diff] [blame] | 983 | void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 984 | Token &MarkTok) override { |
Chris Lattner | c238331 | 2007-12-19 19:38:36 +0000 | [diff] [blame] | 985 | PP.HandlePragmaMark(); |
| 986 | } |
| 987 | }; |
| 988 | |
James Dennett | 18a6d79 | 2012-06-17 03:26:26 +0000 | [diff] [blame] | 989 | /// PragmaPoisonHandler - "\#pragma poison x" marks x as not usable. |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 990 | struct PragmaPoisonHandler : public PragmaHandler { |
Argyrios Kyrtzidis | 36745fd | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 991 | PragmaPoisonHandler() : PragmaHandler("poison") {} |
Eugene Zelenko | e95e7d5 | 2016-09-07 21:53:17 +0000 | [diff] [blame] | 992 | |
Craig Topper | 9140dd2 | 2014-03-11 06:50:42 +0000 | [diff] [blame] | 993 | void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 994 | Token &PoisonTok) override { |
Erik Verbruggen | 851ce0e | 2016-10-26 11:46:10 +0000 | [diff] [blame] | 995 | PP.HandlePragmaPoison(); |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 996 | } |
| 997 | }; |
| 998 | |
James Dennett | 18a6d79 | 2012-06-17 03:26:26 +0000 | [diff] [blame] | 999 | /// PragmaSystemHeaderHandler - "\#pragma system_header" marks the current file |
Chris Lattner | c238331 | 2007-12-19 19:38:36 +0000 | [diff] [blame] | 1000 | /// as a system header, which silences warnings in it. |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 1001 | struct PragmaSystemHeaderHandler : public PragmaHandler { |
Argyrios Kyrtzidis | 36745fd | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 1002 | PragmaSystemHeaderHandler() : PragmaHandler("system_header") {} |
Eugene Zelenko | e95e7d5 | 2016-09-07 21:53:17 +0000 | [diff] [blame] | 1003 | |
Craig Topper | 9140dd2 | 2014-03-11 06:50:42 +0000 | [diff] [blame] | 1004 | void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 1005 | Token &SHToken) override { |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 1006 | PP.HandlePragmaSystemHeader(SHToken); |
Chris Lattner | ce2ab6f | 2009-04-14 05:07:49 +0000 | [diff] [blame] | 1007 | PP.CheckEndOfDirective("pragma"); |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 1008 | } |
| 1009 | }; |
Eugene Zelenko | e95e7d5 | 2016-09-07 21:53:17 +0000 | [diff] [blame] | 1010 | |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 1011 | struct PragmaDependencyHandler : public PragmaHandler { |
Argyrios Kyrtzidis | 36745fd | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 1012 | PragmaDependencyHandler() : PragmaHandler("dependency") {} |
Eugene Zelenko | e95e7d5 | 2016-09-07 21:53:17 +0000 | [diff] [blame] | 1013 | |
Craig Topper | 9140dd2 | 2014-03-11 06:50:42 +0000 | [diff] [blame] | 1014 | void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 1015 | Token &DepToken) override { |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 1016 | PP.HandlePragmaDependency(DepToken); |
| 1017 | } |
| 1018 | }; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1019 | |
Daniel Dunbar | b8068c3 | 2010-07-28 15:40:33 +0000 | [diff] [blame] | 1020 | struct PragmaDebugHandler : public PragmaHandler { |
| 1021 | PragmaDebugHandler() : PragmaHandler("__debug") {} |
Eugene Zelenko | e95e7d5 | 2016-09-07 21:53:17 +0000 | [diff] [blame] | 1022 | |
Craig Topper | 9140dd2 | 2014-03-11 06:50:42 +0000 | [diff] [blame] | 1023 | void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 1024 | Token &DepToken) override { |
Daniel Dunbar | b8068c3 | 2010-07-28 15:40:33 +0000 | [diff] [blame] | 1025 | Token Tok; |
| 1026 | PP.LexUnexpandedToken(Tok); |
| 1027 | if (Tok.isNot(tok::identifier)) { |
Douglas Gregor | 3cc2648 | 2010-08-30 15:15:34 +0000 | [diff] [blame] | 1028 | PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid); |
Daniel Dunbar | b8068c3 | 2010-07-28 15:40:33 +0000 | [diff] [blame] | 1029 | return; |
| 1030 | } |
| 1031 | IdentifierInfo *II = Tok.getIdentifierInfo(); |
| 1032 | |
Daniel Dunbar | f2cf329 | 2010-08-17 22:32:48 +0000 | [diff] [blame] | 1033 | if (II->isStr("assert")) { |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 1034 | llvm_unreachable("This is an assertion!"); |
Daniel Dunbar | b8068c3 | 2010-07-28 15:40:33 +0000 | [diff] [blame] | 1035 | } else if (II->isStr("crash")) { |
David Blaikie | 5bd4c2a | 2012-08-21 18:56:49 +0000 | [diff] [blame] | 1036 | LLVM_BUILTIN_TRAP; |
David Blaikie | 5d577a2 | 2012-06-29 22:03:56 +0000 | [diff] [blame] | 1037 | } else if (II->isStr("parser_crash")) { |
| 1038 | Token Crasher; |
Benjamin Kramer | 3162f29 | 2015-03-08 19:28:24 +0000 | [diff] [blame] | 1039 | Crasher.startToken(); |
David Blaikie | 5d577a2 | 2012-06-29 22:03:56 +0000 | [diff] [blame] | 1040 | Crasher.setKind(tok::annot_pragma_parser_crash); |
Benjamin Kramer | 3162f29 | 2015-03-08 19:28:24 +0000 | [diff] [blame] | 1041 | Crasher.setAnnotationRange(SourceRange(Tok.getLocation())); |
David Blaikie | 5d577a2 | 2012-06-29 22:03:56 +0000 | [diff] [blame] | 1042 | PP.EnterToken(Crasher); |
Richard Smith | ba3a4f9 | 2016-01-12 21:59:26 +0000 | [diff] [blame] | 1043 | } else if (II->isStr("dump")) { |
| 1044 | Token Identifier; |
| 1045 | PP.LexUnexpandedToken(Identifier); |
| 1046 | if (auto *DumpII = Identifier.getIdentifierInfo()) { |
| 1047 | Token DumpAnnot; |
| 1048 | DumpAnnot.startToken(); |
| 1049 | DumpAnnot.setKind(tok::annot_pragma_dump); |
| 1050 | DumpAnnot.setAnnotationRange( |
| 1051 | SourceRange(Tok.getLocation(), Identifier.getLocation())); |
| 1052 | DumpAnnot.setAnnotationValue(DumpII); |
| 1053 | PP.DiscardUntilEndOfDirective(); |
| 1054 | PP.EnterToken(DumpAnnot); |
| 1055 | } else { |
| 1056 | PP.Diag(Identifier, diag::warn_pragma_debug_missing_argument) |
| 1057 | << II->getName(); |
| 1058 | } |
Richard Smith | 6c2b5a8 | 2018-02-09 01:15:13 +0000 | [diff] [blame] | 1059 | } else if (II->isStr("diag_mapping")) { |
| 1060 | Token DiagName; |
| 1061 | PP.LexUnexpandedToken(DiagName); |
| 1062 | if (DiagName.is(tok::eod)) |
| 1063 | PP.getDiagnostics().dump(); |
| 1064 | else if (DiagName.is(tok::string_literal) && !DiagName.hasUDSuffix()) { |
| 1065 | StringLiteralParser Literal(DiagName, PP); |
| 1066 | if (Literal.hadError) |
| 1067 | return; |
| 1068 | PP.getDiagnostics().dump(Literal.GetString()); |
| 1069 | } else { |
| 1070 | PP.Diag(DiagName, diag::warn_pragma_debug_missing_argument) |
| 1071 | << II->getName(); |
| 1072 | } |
Daniel Dunbar | f2cf329 | 2010-08-17 22:32:48 +0000 | [diff] [blame] | 1073 | } else if (II->isStr("llvm_fatal_error")) { |
| 1074 | llvm::report_fatal_error("#pragma clang __debug llvm_fatal_error"); |
| 1075 | } else if (II->isStr("llvm_unreachable")) { |
| 1076 | llvm_unreachable("#pragma clang __debug llvm_unreachable"); |
Richard Smith | 3ffa61d | 2015-04-30 23:10:40 +0000 | [diff] [blame] | 1077 | } else if (II->isStr("macro")) { |
| 1078 | Token MacroName; |
| 1079 | PP.LexUnexpandedToken(MacroName); |
| 1080 | auto *MacroII = MacroName.getIdentifierInfo(); |
| 1081 | if (MacroII) |
| 1082 | PP.dumpMacroInfo(MacroII); |
| 1083 | else |
Richard Smith | ba3a4f9 | 2016-01-12 21:59:26 +0000 | [diff] [blame] | 1084 | PP.Diag(MacroName, diag::warn_pragma_debug_missing_argument) |
| 1085 | << II->getName(); |
Daniel Dunbar | f2cf329 | 2010-08-17 22:32:48 +0000 | [diff] [blame] | 1086 | } else if (II->isStr("overflow_stack")) { |
| 1087 | DebugOverflowStack(); |
Daniel Dunbar | 211a787 | 2010-08-18 23:09:23 +0000 | [diff] [blame] | 1088 | } else if (II->isStr("handle_crash")) { |
| 1089 | llvm::CrashRecoveryContext *CRC =llvm::CrashRecoveryContext::GetCurrent(); |
| 1090 | if (CRC) |
| 1091 | CRC->HandleCrash(); |
Tareq A. Siraj | 0de0dd4 | 2013-04-16 18:41:26 +0000 | [diff] [blame] | 1092 | } else if (II->isStr("captured")) { |
| 1093 | HandleCaptured(PP); |
Daniel Dunbar | f2cf329 | 2010-08-17 22:32:48 +0000 | [diff] [blame] | 1094 | } else { |
| 1095 | PP.Diag(Tok, diag::warn_pragma_debug_unexpected_command) |
| 1096 | << II->getName(); |
Daniel Dunbar | b8068c3 | 2010-07-28 15:40:33 +0000 | [diff] [blame] | 1097 | } |
Tareq A. Siraj | 0de0dd4 | 2013-04-16 18:41:26 +0000 | [diff] [blame] | 1098 | |
| 1099 | PPCallbacks *Callbacks = PP.getPPCallbacks(); |
| 1100 | if (Callbacks) |
| 1101 | Callbacks->PragmaDebug(Tok.getLocation(), II->getName()); |
| 1102 | } |
| 1103 | |
| 1104 | void HandleCaptured(Preprocessor &PP) { |
Tareq A. Siraj | 0de0dd4 | 2013-04-16 18:41:26 +0000 | [diff] [blame] | 1105 | Token Tok; |
| 1106 | PP.LexUnexpandedToken(Tok); |
| 1107 | |
| 1108 | if (Tok.isNot(tok::eod)) { |
| 1109 | PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol) |
| 1110 | << "pragma clang __debug captured"; |
| 1111 | return; |
| 1112 | } |
| 1113 | |
| 1114 | SourceLocation NameLoc = Tok.getLocation(); |
David Blaikie | 2eabcc9 | 2016-02-09 18:52:09 +0000 | [diff] [blame] | 1115 | MutableArrayRef<Token> Toks( |
| 1116 | PP.getPreprocessorAllocator().Allocate<Token>(1), 1); |
| 1117 | Toks[0].startToken(); |
| 1118 | Toks[0].setKind(tok::annot_pragma_captured); |
| 1119 | Toks[0].setLocation(NameLoc); |
Tareq A. Siraj | 0de0dd4 | 2013-04-16 18:41:26 +0000 | [diff] [blame] | 1120 | |
David Blaikie | 2eabcc9 | 2016-02-09 18:52:09 +0000 | [diff] [blame] | 1121 | PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true); |
Daniel Dunbar | b8068c3 | 2010-07-28 15:40:33 +0000 | [diff] [blame] | 1122 | } |
| 1123 | |
Francois Pichet | 2e11f5d | 2011-05-25 16:15:03 +0000 | [diff] [blame] | 1124 | // Disable MSVC warning about runtime stack overflow. |
| 1125 | #ifdef _MSC_VER |
| 1126 | #pragma warning(disable : 4717) |
| 1127 | #endif |
Matthias Braun | 285f88d | 2017-04-24 18:41:00 +0000 | [diff] [blame] | 1128 | static void DebugOverflowStack(void (*P)() = nullptr) { |
| 1129 | void (*volatile Self)(void(*P)()) = DebugOverflowStack; |
| 1130 | Self(reinterpret_cast<void(*)()>(Self)); |
Daniel Dunbar | b8068c3 | 2010-07-28 15:40:33 +0000 | [diff] [blame] | 1131 | } |
Francois Pichet | 2e11f5d | 2011-05-25 16:15:03 +0000 | [diff] [blame] | 1132 | #ifdef _MSC_VER |
| 1133 | #pragma warning(default : 4717) |
| 1134 | #endif |
Daniel Dunbar | b8068c3 | 2010-07-28 15:40:33 +0000 | [diff] [blame] | 1135 | }; |
| 1136 | |
James Dennett | 18a6d79 | 2012-06-17 03:26:26 +0000 | [diff] [blame] | 1137 | /// PragmaDiagnosticHandler - e.g. '\#pragma GCC diagnostic ignored "-Wformat"' |
Chris Lattner | 504af11 | 2009-04-19 23:16:58 +0000 | [diff] [blame] | 1138 | struct PragmaDiagnosticHandler : public PragmaHandler { |
Douglas Gregor | 3bde9b1 | 2011-06-22 19:41:48 +0000 | [diff] [blame] | 1139 | private: |
| 1140 | const char *Namespace; |
Eugene Zelenko | e95e7d5 | 2016-09-07 21:53:17 +0000 | [diff] [blame] | 1141 | |
Chris Lattner | fb42a18 | 2009-07-12 21:18:45 +0000 | [diff] [blame] | 1142 | public: |
Eugene Zelenko | cb96ac6 | 2017-12-08 22:39:26 +0000 | [diff] [blame] | 1143 | explicit PragmaDiagnosticHandler(const char *NS) |
| 1144 | : PragmaHandler("diagnostic"), Namespace(NS) {} |
Eugene Zelenko | e95e7d5 | 2016-09-07 21:53:17 +0000 | [diff] [blame] | 1145 | |
Craig Topper | 9140dd2 | 2014-03-11 06:50:42 +0000 | [diff] [blame] | 1146 | void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 1147 | Token &DiagToken) override { |
Argyrios Kyrtzidis | 1cb0de1 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 1148 | SourceLocation DiagLoc = DiagToken.getLocation(); |
Chris Lattner | 504af11 | 2009-04-19 23:16:58 +0000 | [diff] [blame] | 1149 | Token Tok; |
| 1150 | PP.LexUnexpandedToken(Tok); |
| 1151 | if (Tok.isNot(tok::identifier)) { |
Douglas Gregor | 3cc2648 | 2010-08-30 15:15:34 +0000 | [diff] [blame] | 1152 | PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid); |
Chris Lattner | 504af11 | 2009-04-19 23:16:58 +0000 | [diff] [blame] | 1153 | return; |
| 1154 | } |
| 1155 | IdentifierInfo *II = Tok.getIdentifierInfo(); |
Douglas Gregor | 3bde9b1 | 2011-06-22 19:41:48 +0000 | [diff] [blame] | 1156 | PPCallbacks *Callbacks = PP.getPPCallbacks(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1157 | |
Alp Toker | 46df1c0 | 2014-06-12 10:15:20 +0000 | [diff] [blame] | 1158 | if (II->isStr("pop")) { |
Argyrios Kyrtzidis | 1cb0de1 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 1159 | if (!PP.getDiagnostics().popMappings(DiagLoc)) |
Douglas Gregor | 3cc2648 | 2010-08-30 15:15:34 +0000 | [diff] [blame] | 1160 | PP.Diag(Tok, diag::warn_pragma_diagnostic_cannot_pop); |
Douglas Gregor | 3bde9b1 | 2011-06-22 19:41:48 +0000 | [diff] [blame] | 1161 | else if (Callbacks) |
| 1162 | Callbacks->PragmaDiagnosticPop(DiagLoc, Namespace); |
Douglas Gregor | 3cc2648 | 2010-08-30 15:15:34 +0000 | [diff] [blame] | 1163 | return; |
| 1164 | } else if (II->isStr("push")) { |
Argyrios Kyrtzidis | 1cb0de1 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 1165 | PP.getDiagnostics().pushMappings(DiagLoc); |
Douglas Gregor | 3bde9b1 | 2011-06-22 19:41:48 +0000 | [diff] [blame] | 1166 | if (Callbacks) |
| 1167 | Callbacks->PragmaDiagnosticPush(DiagLoc, Namespace); |
Chris Lattner | fb42a18 | 2009-07-12 21:18:45 +0000 | [diff] [blame] | 1168 | return; |
Alp Toker | 46df1c0 | 2014-06-12 10:15:20 +0000 | [diff] [blame] | 1169 | } |
| 1170 | |
| 1171 | diag::Severity SV = llvm::StringSwitch<diag::Severity>(II->getName()) |
| 1172 | .Case("ignored", diag::Severity::Ignored) |
| 1173 | .Case("warning", diag::Severity::Warning) |
| 1174 | .Case("error", diag::Severity::Error) |
| 1175 | .Case("fatal", diag::Severity::Fatal) |
| 1176 | .Default(diag::Severity()); |
| 1177 | |
| 1178 | if (SV == diag::Severity()) { |
Douglas Gregor | 3cc2648 | 2010-08-30 15:15:34 +0000 | [diff] [blame] | 1179 | PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid); |
Chris Lattner | 504af11 | 2009-04-19 23:16:58 +0000 | [diff] [blame] | 1180 | return; |
| 1181 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1182 | |
Chris Lattner | 504af11 | 2009-04-19 23:16:58 +0000 | [diff] [blame] | 1183 | PP.LexUnexpandedToken(Tok); |
Andy Gibbs | 58905d2 | 2012-11-17 19:15:38 +0000 | [diff] [blame] | 1184 | SourceLocation StringLoc = Tok.getLocation(); |
Chris Lattner | 504af11 | 2009-04-19 23:16:58 +0000 | [diff] [blame] | 1185 | |
Andy Gibbs | 58905d2 | 2012-11-17 19:15:38 +0000 | [diff] [blame] | 1186 | std::string WarningName; |
Andy Gibbs | a8df57a | 2012-11-17 19:16:52 +0000 | [diff] [blame] | 1187 | if (!PP.FinishLexStringLiteral(Tok, WarningName, "pragma diagnostic", |
| 1188 | /*MacroExpansion=*/false)) |
Chris Lattner | 504af11 | 2009-04-19 23:16:58 +0000 | [diff] [blame] | 1189 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1190 | |
Peter Collingbourne | 2f1e36b | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 1191 | if (Tok.isNot(tok::eod)) { |
Chris Lattner | 504af11 | 2009-04-19 23:16:58 +0000 | [diff] [blame] | 1192 | PP.Diag(Tok.getLocation(), diag::warn_pragma_diagnostic_invalid_token); |
| 1193 | return; |
| 1194 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1195 | |
Chris Lattner | 504af11 | 2009-04-19 23:16:58 +0000 | [diff] [blame] | 1196 | if (WarningName.size() < 3 || WarningName[0] != '-' || |
Richard Smith | 3be1cb2 | 2014-08-07 00:24:21 +0000 | [diff] [blame] | 1197 | (WarningName[1] != 'W' && WarningName[1] != 'R')) { |
Andy Gibbs | 58905d2 | 2012-11-17 19:15:38 +0000 | [diff] [blame] | 1198 | PP.Diag(StringLoc, diag::warn_pragma_diagnostic_invalid_option); |
Chris Lattner | 504af11 | 2009-04-19 23:16:58 +0000 | [diff] [blame] | 1199 | return; |
| 1200 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1201 | |
Sunil Srivastava | 5239de7 | 2016-02-13 01:44:05 +0000 | [diff] [blame] | 1202 | diag::Flavor Flavor = WarningName[1] == 'W' ? diag::Flavor::WarningOrError |
| 1203 | : diag::Flavor::Remark; |
Benjamin Kramer | 2193e23 | 2016-02-13 13:42:41 +0000 | [diff] [blame] | 1204 | StringRef Group = StringRef(WarningName).substr(2); |
Sunil Srivastava | 5239de7 | 2016-02-13 01:44:05 +0000 | [diff] [blame] | 1205 | bool unknownDiag = false; |
| 1206 | if (Group == "everything") { |
| 1207 | // Special handling for pragma clang diagnostic ... "-Weverything". |
| 1208 | // There is no formal group named "everything", so there has to be a |
| 1209 | // special case for it. |
| 1210 | PP.getDiagnostics().setSeverityForAll(Flavor, SV, DiagLoc); |
| 1211 | } else |
| 1212 | unknownDiag = PP.getDiagnostics().setSeverityForGroup(Flavor, Group, SV, |
| 1213 | DiagLoc); |
| 1214 | if (unknownDiag) |
Andy Gibbs | 58905d2 | 2012-11-17 19:15:38 +0000 | [diff] [blame] | 1215 | PP.Diag(StringLoc, diag::warn_pragma_diagnostic_unknown_warning) |
| 1216 | << WarningName; |
Douglas Gregor | 3bde9b1 | 2011-06-22 19:41:48 +0000 | [diff] [blame] | 1217 | else if (Callbacks) |
Alp Toker | 46df1c0 | 2014-06-12 10:15:20 +0000 | [diff] [blame] | 1218 | Callbacks->PragmaDiagnostic(DiagLoc, Namespace, SV, WarningName); |
Chris Lattner | 504af11 | 2009-04-19 23:16:58 +0000 | [diff] [blame] | 1219 | } |
| 1220 | }; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1221 | |
Mike Rice | 58df1af | 2018-09-11 17:10:44 +0000 | [diff] [blame] | 1222 | /// "\#pragma hdrstop [<header-name-string>]" |
| 1223 | struct PragmaHdrstopHandler : public PragmaHandler { |
| 1224 | PragmaHdrstopHandler() : PragmaHandler("hdrstop") {} |
| 1225 | void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 1226 | Token &DepToken) override { |
| 1227 | PP.HandlePragmaHdrstop(DepToken); |
| 1228 | } |
| 1229 | }; |
| 1230 | |
Reid Kleckner | 881dff3 | 2013-09-13 22:00:30 +0000 | [diff] [blame] | 1231 | /// "\#pragma warning(...)". MSVC's diagnostics do not map cleanly to clang's |
| 1232 | /// diagnostics, so we don't really implement this pragma. We parse it and |
| 1233 | /// ignore it to avoid -Wunknown-pragma warnings. |
| 1234 | struct PragmaWarningHandler : public PragmaHandler { |
| 1235 | PragmaWarningHandler() : PragmaHandler("warning") {} |
| 1236 | |
Craig Topper | 9140dd2 | 2014-03-11 06:50:42 +0000 | [diff] [blame] | 1237 | void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 1238 | Token &Tok) override { |
Reid Kleckner | 881dff3 | 2013-09-13 22:00:30 +0000 | [diff] [blame] | 1239 | // Parse things like: |
| 1240 | // warning(push, 1) |
| 1241 | // warning(pop) |
John Thompson | 4762b23 | 2013-11-16 00:16:03 +0000 | [diff] [blame] | 1242 | // 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] | 1243 | SourceLocation DiagLoc = Tok.getLocation(); |
| 1244 | PPCallbacks *Callbacks = PP.getPPCallbacks(); |
| 1245 | |
| 1246 | PP.Lex(Tok); |
| 1247 | if (Tok.isNot(tok::l_paren)) { |
| 1248 | PP.Diag(Tok, diag::warn_pragma_warning_expected) << "("; |
| 1249 | return; |
| 1250 | } |
| 1251 | |
| 1252 | PP.Lex(Tok); |
| 1253 | IdentifierInfo *II = Tok.getIdentifierInfo(); |
Reid Kleckner | 881dff3 | 2013-09-13 22:00:30 +0000 | [diff] [blame] | 1254 | |
Alexander Musman | 6b080fc | 2015-05-25 11:21:20 +0000 | [diff] [blame] | 1255 | if (II && II->isStr("push")) { |
Reid Kleckner | 881dff3 | 2013-09-13 22:00:30 +0000 | [diff] [blame] | 1256 | // #pragma warning( push[ ,n ] ) |
Reid Kleckner | 4d18510 | 2013-10-02 15:19:23 +0000 | [diff] [blame] | 1257 | int Level = -1; |
Reid Kleckner | 881dff3 | 2013-09-13 22:00:30 +0000 | [diff] [blame] | 1258 | PP.Lex(Tok); |
| 1259 | if (Tok.is(tok::comma)) { |
| 1260 | PP.Lex(Tok); |
Reid Kleckner | c0dca6d | 2014-02-12 23:50:26 +0000 | [diff] [blame] | 1261 | uint64_t Value; |
| 1262 | if (Tok.is(tok::numeric_constant) && |
| 1263 | PP.parseSimpleIntegerLiteral(Tok, Value)) |
| 1264 | Level = int(Value); |
Reid Kleckner | 4d18510 | 2013-10-02 15:19:23 +0000 | [diff] [blame] | 1265 | if (Level < 0 || Level > 4) { |
Reid Kleckner | 881dff3 | 2013-09-13 22:00:30 +0000 | [diff] [blame] | 1266 | PP.Diag(Tok, diag::warn_pragma_warning_push_level); |
| 1267 | return; |
| 1268 | } |
| 1269 | } |
| 1270 | if (Callbacks) |
| 1271 | Callbacks->PragmaWarningPush(DiagLoc, Level); |
Alexander Musman | 6b080fc | 2015-05-25 11:21:20 +0000 | [diff] [blame] | 1272 | } else if (II && II->isStr("pop")) { |
Reid Kleckner | 881dff3 | 2013-09-13 22:00:30 +0000 | [diff] [blame] | 1273 | // #pragma warning( pop ) |
| 1274 | PP.Lex(Tok); |
| 1275 | if (Callbacks) |
| 1276 | Callbacks->PragmaWarningPop(DiagLoc); |
| 1277 | } else { |
| 1278 | // #pragma warning( warning-specifier : warning-number-list |
| 1279 | // [; warning-specifier : warning-number-list...] ) |
| 1280 | while (true) { |
| 1281 | II = Tok.getIdentifierInfo(); |
Alexander Musman | 6b080fc | 2015-05-25 11:21:20 +0000 | [diff] [blame] | 1282 | if (!II && !Tok.is(tok::numeric_constant)) { |
Reid Kleckner | 881dff3 | 2013-09-13 22:00:30 +0000 | [diff] [blame] | 1283 | PP.Diag(Tok, diag::warn_pragma_warning_spec_invalid); |
| 1284 | return; |
| 1285 | } |
| 1286 | |
| 1287 | // Figure out which warning specifier this is. |
Alexander Musman | 6b080fc | 2015-05-25 11:21:20 +0000 | [diff] [blame] | 1288 | bool SpecifierValid; |
| 1289 | StringRef Specifier; |
| 1290 | llvm::SmallString<1> SpecifierBuf; |
| 1291 | if (II) { |
| 1292 | Specifier = II->getName(); |
| 1293 | SpecifierValid = llvm::StringSwitch<bool>(Specifier) |
| 1294 | .Cases("default", "disable", "error", "once", |
| 1295 | "suppress", true) |
| 1296 | .Default(false); |
| 1297 | // If we read a correct specifier, snatch next token (that should be |
| 1298 | // ":", checked later). |
| 1299 | if (SpecifierValid) |
| 1300 | PP.Lex(Tok); |
| 1301 | } else { |
| 1302 | // Token is a numeric constant. It should be either 1, 2, 3 or 4. |
| 1303 | uint64_t Value; |
| 1304 | Specifier = PP.getSpelling(Tok, SpecifierBuf); |
| 1305 | if (PP.parseSimpleIntegerLiteral(Tok, Value)) { |
| 1306 | SpecifierValid = (Value >= 1) && (Value <= 4); |
| 1307 | } else |
| 1308 | SpecifierValid = false; |
| 1309 | // Next token already snatched by parseSimpleIntegerLiteral. |
| 1310 | } |
| 1311 | |
Reid Kleckner | 881dff3 | 2013-09-13 22:00:30 +0000 | [diff] [blame] | 1312 | if (!SpecifierValid) { |
| 1313 | PP.Diag(Tok, diag::warn_pragma_warning_spec_invalid); |
| 1314 | return; |
| 1315 | } |
Reid Kleckner | 881dff3 | 2013-09-13 22:00:30 +0000 | [diff] [blame] | 1316 | if (Tok.isNot(tok::colon)) { |
| 1317 | PP.Diag(Tok, diag::warn_pragma_warning_expected) << ":"; |
| 1318 | return; |
| 1319 | } |
| 1320 | |
| 1321 | // Collect the warning ids. |
| 1322 | SmallVector<int, 4> Ids; |
| 1323 | PP.Lex(Tok); |
| 1324 | while (Tok.is(tok::numeric_constant)) { |
Reid Kleckner | c0dca6d | 2014-02-12 23:50:26 +0000 | [diff] [blame] | 1325 | uint64_t Value; |
| 1326 | if (!PP.parseSimpleIntegerLiteral(Tok, Value) || Value == 0 || |
Eugene Zelenko | e95e7d5 | 2016-09-07 21:53:17 +0000 | [diff] [blame] | 1327 | Value > std::numeric_limits<int>::max()) { |
Reid Kleckner | 881dff3 | 2013-09-13 22:00:30 +0000 | [diff] [blame] | 1328 | PP.Diag(Tok, diag::warn_pragma_warning_expected_number); |
| 1329 | return; |
| 1330 | } |
Reid Kleckner | c0dca6d | 2014-02-12 23:50:26 +0000 | [diff] [blame] | 1331 | Ids.push_back(int(Value)); |
Reid Kleckner | 881dff3 | 2013-09-13 22:00:30 +0000 | [diff] [blame] | 1332 | } |
| 1333 | if (Callbacks) |
| 1334 | Callbacks->PragmaWarning(DiagLoc, Specifier, Ids); |
| 1335 | |
| 1336 | // Parse the next specifier if there is a semicolon. |
| 1337 | if (Tok.isNot(tok::semi)) |
| 1338 | break; |
| 1339 | PP.Lex(Tok); |
| 1340 | } |
| 1341 | } |
| 1342 | |
| 1343 | if (Tok.isNot(tok::r_paren)) { |
| 1344 | PP.Diag(Tok, diag::warn_pragma_warning_expected) << ")"; |
| 1345 | return; |
| 1346 | } |
| 1347 | |
| 1348 | PP.Lex(Tok); |
| 1349 | if (Tok.isNot(tok::eod)) |
| 1350 | PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma warning"; |
| 1351 | } |
| 1352 | }; |
| 1353 | |
Reid Kleckner | 0f56b22 | 2019-03-14 18:12:17 +0000 | [diff] [blame] | 1354 | /// "\#pragma execution_character_set(...)". MSVC supports this pragma only |
| 1355 | /// for "UTF-8". We parse it and ignore it if UTF-8 is provided and warn |
| 1356 | /// otherwise to avoid -Wunknown-pragma warnings. |
| 1357 | struct PragmaExecCharsetHandler : public PragmaHandler { |
| 1358 | PragmaExecCharsetHandler() : PragmaHandler("execution_character_set") {} |
| 1359 | |
| 1360 | void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 1361 | Token &Tok) override { |
| 1362 | // Parse things like: |
| 1363 | // execution_character_set(push, "UTF-8") |
| 1364 | // execution_character_set(pop) |
| 1365 | SourceLocation DiagLoc = Tok.getLocation(); |
| 1366 | PPCallbacks *Callbacks = PP.getPPCallbacks(); |
| 1367 | |
| 1368 | PP.Lex(Tok); |
| 1369 | if (Tok.isNot(tok::l_paren)) { |
| 1370 | PP.Diag(Tok, diag::warn_pragma_exec_charset_expected) << "("; |
| 1371 | return; |
| 1372 | } |
| 1373 | |
| 1374 | PP.Lex(Tok); |
| 1375 | IdentifierInfo *II = Tok.getIdentifierInfo(); |
| 1376 | |
| 1377 | if (II && II->isStr("push")) { |
| 1378 | // #pragma execution_character_set( push[ , string ] ) |
| 1379 | PP.Lex(Tok); |
| 1380 | if (Tok.is(tok::comma)) { |
| 1381 | PP.Lex(Tok); |
| 1382 | |
| 1383 | std::string ExecCharset; |
| 1384 | if (!PP.FinishLexStringLiteral(Tok, ExecCharset, |
| 1385 | "pragma execution_character_set", |
| 1386 | /*MacroExpansion=*/false)) |
| 1387 | return; |
| 1388 | |
| 1389 | // MSVC supports either of these, but nothing else. |
| 1390 | if (ExecCharset != "UTF-8" && ExecCharset != "utf-8") { |
| 1391 | PP.Diag(Tok, diag::warn_pragma_exec_charset_push_invalid) << ExecCharset; |
| 1392 | return; |
| 1393 | } |
| 1394 | } |
| 1395 | if (Callbacks) |
| 1396 | Callbacks->PragmaExecCharsetPush(DiagLoc, "UTF-8"); |
| 1397 | } else if (II && II->isStr("pop")) { |
| 1398 | // #pragma execution_character_set( pop ) |
| 1399 | PP.Lex(Tok); |
| 1400 | if (Callbacks) |
| 1401 | Callbacks->PragmaExecCharsetPop(DiagLoc); |
| 1402 | } else { |
| 1403 | PP.Diag(Tok, diag::warn_pragma_exec_charset_spec_invalid); |
| 1404 | return; |
| 1405 | } |
| 1406 | |
| 1407 | if (Tok.isNot(tok::r_paren)) { |
| 1408 | PP.Diag(Tok, diag::warn_pragma_exec_charset_expected) << ")"; |
| 1409 | return; |
| 1410 | } |
| 1411 | |
| 1412 | PP.Lex(Tok); |
| 1413 | if (Tok.isNot(tok::eod)) |
| 1414 | PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma execution_character_set"; |
| 1415 | } |
| 1416 | }; |
| 1417 | |
James Dennett | 18a6d79 | 2012-06-17 03:26:26 +0000 | [diff] [blame] | 1418 | /// PragmaIncludeAliasHandler - "\#pragma include_alias("...")". |
Aaron Ballman | 611306e | 2012-03-02 22:51:54 +0000 | [diff] [blame] | 1419 | struct PragmaIncludeAliasHandler : public PragmaHandler { |
| 1420 | PragmaIncludeAliasHandler() : PragmaHandler("include_alias") {} |
Eugene Zelenko | cb96ac6 | 2017-12-08 22:39:26 +0000 | [diff] [blame] | 1421 | |
Craig Topper | 9140dd2 | 2014-03-11 06:50:42 +0000 | [diff] [blame] | 1422 | void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 1423 | Token &IncludeAliasTok) override { |
Reid Kleckner | 881dff3 | 2013-09-13 22:00:30 +0000 | [diff] [blame] | 1424 | PP.HandlePragmaIncludeAlias(IncludeAliasTok); |
Aaron Ballman | 611306e | 2012-03-02 22:51:54 +0000 | [diff] [blame] | 1425 | } |
| 1426 | }; |
| 1427 | |
Andy Gibbs | 9c2ccd6 | 2013-04-17 16:16:16 +0000 | [diff] [blame] | 1428 | /// PragmaMessageHandler - Handle the microsoft and gcc \#pragma message |
| 1429 | /// extension. The syntax is: |
| 1430 | /// \code |
| 1431 | /// #pragma message(string) |
| 1432 | /// \endcode |
| 1433 | /// OR, in GCC mode: |
| 1434 | /// \code |
| 1435 | /// #pragma message string |
| 1436 | /// \endcode |
| 1437 | /// string is a string, which is fully macro expanded, and permits string |
| 1438 | /// concatenation, embedded escape characters, etc... See MSDN for more details. |
| 1439 | /// Also handles \#pragma GCC warning and \#pragma GCC error which take the same |
| 1440 | /// form as \#pragma message. |
Chris Lattner | 30c924b | 2010-06-26 17:11:39 +0000 | [diff] [blame] | 1441 | struct PragmaMessageHandler : public PragmaHandler { |
Andy Gibbs | 9c2ccd6 | 2013-04-17 16:16:16 +0000 | [diff] [blame] | 1442 | private: |
| 1443 | const PPCallbacks::PragmaMessageKind Kind; |
| 1444 | const StringRef Namespace; |
| 1445 | |
| 1446 | static const char* PragmaKind(PPCallbacks::PragmaMessageKind Kind, |
| 1447 | bool PragmaNameOnly = false) { |
| 1448 | switch (Kind) { |
| 1449 | case PPCallbacks::PMK_Message: |
| 1450 | return PragmaNameOnly ? "message" : "pragma message"; |
| 1451 | case PPCallbacks::PMK_Warning: |
| 1452 | return PragmaNameOnly ? "warning" : "pragma warning"; |
| 1453 | case PPCallbacks::PMK_Error: |
| 1454 | return PragmaNameOnly ? "error" : "pragma error"; |
| 1455 | } |
| 1456 | llvm_unreachable("Unknown PragmaMessageKind!"); |
| 1457 | } |
| 1458 | |
| 1459 | public: |
| 1460 | PragmaMessageHandler(PPCallbacks::PragmaMessageKind Kind, |
| 1461 | StringRef Namespace = StringRef()) |
Eugene Zelenko | cb96ac6 | 2017-12-08 22:39:26 +0000 | [diff] [blame] | 1462 | : PragmaHandler(PragmaKind(Kind, true)), Kind(Kind), |
| 1463 | Namespace(Namespace) {} |
Andy Gibbs | 9c2ccd6 | 2013-04-17 16:16:16 +0000 | [diff] [blame] | 1464 | |
Craig Topper | 9140dd2 | 2014-03-11 06:50:42 +0000 | [diff] [blame] | 1465 | void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 1466 | Token &Tok) override { |
Andy Gibbs | 9c2ccd6 | 2013-04-17 16:16:16 +0000 | [diff] [blame] | 1467 | SourceLocation MessageLoc = Tok.getLocation(); |
| 1468 | PP.Lex(Tok); |
| 1469 | bool ExpectClosingParen = false; |
| 1470 | switch (Tok.getKind()) { |
| 1471 | case tok::l_paren: |
| 1472 | // We have a MSVC style pragma message. |
| 1473 | ExpectClosingParen = true; |
| 1474 | // Read the string. |
| 1475 | PP.Lex(Tok); |
| 1476 | break; |
| 1477 | case tok::string_literal: |
| 1478 | // We have a GCC style pragma message, and we just read the string. |
| 1479 | break; |
| 1480 | default: |
| 1481 | PP.Diag(MessageLoc, diag::err_pragma_message_malformed) << Kind; |
| 1482 | return; |
| 1483 | } |
| 1484 | |
| 1485 | std::string MessageString; |
| 1486 | if (!PP.FinishLexStringLiteral(Tok, MessageString, PragmaKind(Kind), |
| 1487 | /*MacroExpansion=*/true)) |
| 1488 | return; |
| 1489 | |
| 1490 | if (ExpectClosingParen) { |
| 1491 | if (Tok.isNot(tok::r_paren)) { |
| 1492 | PP.Diag(Tok.getLocation(), diag::err_pragma_message_malformed) << Kind; |
| 1493 | return; |
| 1494 | } |
| 1495 | PP.Lex(Tok); // eat the r_paren. |
| 1496 | } |
| 1497 | |
| 1498 | if (Tok.isNot(tok::eod)) { |
| 1499 | PP.Diag(Tok.getLocation(), diag::err_pragma_message_malformed) << Kind; |
| 1500 | return; |
| 1501 | } |
| 1502 | |
| 1503 | // Output the message. |
| 1504 | PP.Diag(MessageLoc, (Kind == PPCallbacks::PMK_Error) |
| 1505 | ? diag::err_pragma_message |
| 1506 | : diag::warn_pragma_message) << MessageString; |
| 1507 | |
| 1508 | // If the pragma is lexically sound, notify any interested PPCallbacks. |
| 1509 | if (PPCallbacks *Callbacks = PP.getPPCallbacks()) |
| 1510 | Callbacks->PragmaMessage(MessageLoc, Namespace, Kind, MessageString); |
Chris Lattner | 30c924b | 2010-06-26 17:11:39 +0000 | [diff] [blame] | 1511 | } |
| 1512 | }; |
| 1513 | |
Richard Smith | c51c38b | 2017-04-29 00:34:47 +0000 | [diff] [blame] | 1514 | /// Handle the clang \#pragma module import extension. The syntax is: |
| 1515 | /// \code |
| 1516 | /// #pragma clang module import some.module.name |
| 1517 | /// \endcode |
| 1518 | struct PragmaModuleImportHandler : public PragmaHandler { |
| 1519 | PragmaModuleImportHandler() : PragmaHandler("import") {} |
| 1520 | |
| 1521 | void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
Richard Smith | d138630 | 2017-05-04 00:29:54 +0000 | [diff] [blame] | 1522 | Token &Tok) override { |
| 1523 | SourceLocation ImportLoc = Tok.getLocation(); |
| 1524 | |
| 1525 | // Read the module name. |
| 1526 | llvm::SmallVector<std::pair<IdentifierInfo *, SourceLocation>, 8> |
| 1527 | ModuleName; |
| 1528 | if (LexModuleName(PP, Tok, ModuleName)) |
| 1529 | return; |
| 1530 | |
| 1531 | if (Tok.isNot(tok::eod)) |
| 1532 | PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma"; |
| 1533 | |
| 1534 | // If we have a non-empty module path, load the named module. |
| 1535 | Module *Imported = |
| 1536 | PP.getModuleLoader().loadModule(ImportLoc, ModuleName, Module::Hidden, |
| 1537 | /*IsIncludeDirective=*/false); |
| 1538 | if (!Imported) |
| 1539 | return; |
| 1540 | |
| 1541 | PP.makeModuleVisible(Imported, ImportLoc); |
| 1542 | PP.EnterAnnotationToken(SourceRange(ImportLoc, ModuleName.back().second), |
| 1543 | tok::annot_module_include, Imported); |
| 1544 | if (auto *CB = PP.getPPCallbacks()) |
| 1545 | CB->moduleImport(ImportLoc, ModuleName, Imported); |
| 1546 | } |
| 1547 | }; |
| 1548 | |
| 1549 | /// Handle the clang \#pragma module begin extension. The syntax is: |
| 1550 | /// \code |
| 1551 | /// #pragma clang module begin some.module.name |
| 1552 | /// ... |
| 1553 | /// #pragma clang module end |
| 1554 | /// \endcode |
| 1555 | struct PragmaModuleBeginHandler : public PragmaHandler { |
| 1556 | PragmaModuleBeginHandler() : PragmaHandler("begin") {} |
| 1557 | |
| 1558 | void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 1559 | Token &Tok) override { |
| 1560 | SourceLocation BeginLoc = Tok.getLocation(); |
| 1561 | |
| 1562 | // Read the module name. |
| 1563 | llvm::SmallVector<std::pair<IdentifierInfo *, SourceLocation>, 8> |
| 1564 | ModuleName; |
| 1565 | if (LexModuleName(PP, Tok, ModuleName)) |
| 1566 | return; |
| 1567 | |
| 1568 | if (Tok.isNot(tok::eod)) |
| 1569 | PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma"; |
| 1570 | |
| 1571 | // We can only enter submodules of the current module. |
| 1572 | StringRef Current = PP.getLangOpts().CurrentModule; |
| 1573 | if (ModuleName.front().first->getName() != Current) { |
| 1574 | PP.Diag(ModuleName.front().second, diag::err_pp_module_begin_wrong_module) |
| 1575 | << ModuleName.front().first << (ModuleName.size() > 1) |
| 1576 | << Current.empty() << Current; |
| 1577 | return; |
| 1578 | } |
| 1579 | |
| 1580 | // Find the module we're entering. We require that a module map for it |
| 1581 | // be loaded or implicitly loadable. |
| 1582 | // FIXME: We could create the submodule here. We'd need to know whether |
| 1583 | // it's supposed to be explicit, but not much else. |
Richard Smith | 9565c75b | 2017-06-19 23:09:36 +0000 | [diff] [blame] | 1584 | Module *M = PP.getHeaderSearchInfo().lookupModule(Current); |
Richard Smith | d138630 | 2017-05-04 00:29:54 +0000 | [diff] [blame] | 1585 | if (!M) { |
| 1586 | PP.Diag(ModuleName.front().second, |
| 1587 | diag::err_pp_module_begin_no_module_map) << Current; |
| 1588 | return; |
| 1589 | } |
| 1590 | for (unsigned I = 1; I != ModuleName.size(); ++I) { |
| 1591 | auto *NewM = M->findSubmodule(ModuleName[I].first->getName()); |
| 1592 | if (!NewM) { |
| 1593 | PP.Diag(ModuleName[I].second, diag::err_pp_module_begin_no_submodule) |
| 1594 | << M->getFullModuleName() << ModuleName[I].first; |
| 1595 | return; |
| 1596 | } |
| 1597 | M = NewM; |
| 1598 | } |
| 1599 | |
Richard Smith | 51d09c5 | 2017-05-30 05:22:59 +0000 | [diff] [blame] | 1600 | // 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] | 1601 | if (Preprocessor::checkModuleIsAvailable( |
| 1602 | PP.getLangOpts(), PP.getTargetInfo(), PP.getDiagnostics(), M)) { |
Richard Smith | 51d09c5 | 2017-05-30 05:22:59 +0000 | [diff] [blame] | 1603 | PP.Diag(BeginLoc, diag::note_pp_module_begin_here) |
| 1604 | << M->getTopLevelModuleName(); |
| 1605 | return; |
| 1606 | } |
| 1607 | |
Richard Smith | d138630 | 2017-05-04 00:29:54 +0000 | [diff] [blame] | 1608 | // Enter the scope of the submodule. |
| 1609 | PP.EnterSubmodule(M, BeginLoc, /*ForPragma*/true); |
| 1610 | PP.EnterAnnotationToken(SourceRange(BeginLoc, ModuleName.back().second), |
| 1611 | tok::annot_module_begin, M); |
| 1612 | } |
| 1613 | }; |
| 1614 | |
| 1615 | /// Handle the clang \#pragma module end extension. |
| 1616 | struct PragmaModuleEndHandler : public PragmaHandler { |
| 1617 | PragmaModuleEndHandler() : PragmaHandler("end") {} |
| 1618 | |
| 1619 | void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 1620 | Token &Tok) override { |
| 1621 | SourceLocation Loc = Tok.getLocation(); |
| 1622 | |
| 1623 | PP.LexUnexpandedToken(Tok); |
| 1624 | if (Tok.isNot(tok::eod)) |
| 1625 | PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma"; |
| 1626 | |
| 1627 | Module *M = PP.LeaveSubmodule(/*ForPragma*/true); |
| 1628 | if (M) |
| 1629 | PP.EnterAnnotationToken(SourceRange(Loc), tok::annot_module_end, M); |
| 1630 | else |
| 1631 | PP.Diag(Loc, diag::err_pp_module_end_without_module_begin); |
Richard Smith | c51c38b | 2017-04-29 00:34:47 +0000 | [diff] [blame] | 1632 | } |
| 1633 | }; |
| 1634 | |
Richard Smith | 5d2ed48 | 2017-06-09 19:22:32 +0000 | [diff] [blame] | 1635 | /// Handle the clang \#pragma module build extension. |
| 1636 | struct PragmaModuleBuildHandler : public PragmaHandler { |
| 1637 | PragmaModuleBuildHandler() : PragmaHandler("build") {} |
| 1638 | |
| 1639 | void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 1640 | Token &Tok) override { |
| 1641 | PP.HandlePragmaModuleBuild(Tok); |
| 1642 | } |
| 1643 | }; |
| 1644 | |
| 1645 | /// Handle the clang \#pragma module load extension. |
| 1646 | struct PragmaModuleLoadHandler : public PragmaHandler { |
| 1647 | PragmaModuleLoadHandler() : PragmaHandler("load") {} |
| 1648 | |
| 1649 | void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 1650 | Token &Tok) override { |
| 1651 | SourceLocation Loc = Tok.getLocation(); |
| 1652 | |
| 1653 | // Read the module name. |
| 1654 | llvm::SmallVector<std::pair<IdentifierInfo *, SourceLocation>, 8> |
| 1655 | ModuleName; |
| 1656 | if (LexModuleName(PP, Tok, ModuleName)) |
| 1657 | return; |
| 1658 | |
| 1659 | if (Tok.isNot(tok::eod)) |
| 1660 | PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma"; |
| 1661 | |
| 1662 | // Load the module, don't make it visible. |
| 1663 | PP.getModuleLoader().loadModule(Loc, ModuleName, Module::Hidden, |
| 1664 | /*IsIncludeDirective=*/false); |
| 1665 | } |
| 1666 | }; |
| 1667 | |
James Dennett | 18a6d79 | 2012-06-17 03:26:26 +0000 | [diff] [blame] | 1668 | /// PragmaPushMacroHandler - "\#pragma push_macro" saves the value of the |
Chris Lattner | c0a585d | 2010-08-17 15:55:45 +0000 | [diff] [blame] | 1669 | /// macro on the top of the stack. |
| 1670 | struct PragmaPushMacroHandler : public PragmaHandler { |
| 1671 | PragmaPushMacroHandler() : PragmaHandler("push_macro") {} |
Eugene Zelenko | e95e7d5 | 2016-09-07 21:53:17 +0000 | [diff] [blame] | 1672 | |
Craig Topper | 9140dd2 | 2014-03-11 06:50:42 +0000 | [diff] [blame] | 1673 | void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 1674 | Token &PushMacroTok) override { |
Chris Lattner | c0a585d | 2010-08-17 15:55:45 +0000 | [diff] [blame] | 1675 | PP.HandlePragmaPushMacro(PushMacroTok); |
| 1676 | } |
| 1677 | }; |
| 1678 | |
James Dennett | 18a6d79 | 2012-06-17 03:26:26 +0000 | [diff] [blame] | 1679 | /// PragmaPopMacroHandler - "\#pragma pop_macro" sets the value of the |
Chris Lattner | c0a585d | 2010-08-17 15:55:45 +0000 | [diff] [blame] | 1680 | /// macro to the value on the top of the stack. |
| 1681 | struct PragmaPopMacroHandler : public PragmaHandler { |
| 1682 | PragmaPopMacroHandler() : PragmaHandler("pop_macro") {} |
Eugene Zelenko | e95e7d5 | 2016-09-07 21:53:17 +0000 | [diff] [blame] | 1683 | |
Craig Topper | 9140dd2 | 2014-03-11 06:50:42 +0000 | [diff] [blame] | 1684 | void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 1685 | Token &PopMacroTok) override { |
Chris Lattner | c0a585d | 2010-08-17 15:55:45 +0000 | [diff] [blame] | 1686 | PP.HandlePragmaPopMacro(PopMacroTok); |
| 1687 | } |
| 1688 | }; |
| 1689 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1690 | /// PragmaARCCFCodeAuditedHandler - |
James Dennett | 18a6d79 | 2012-06-17 03:26:26 +0000 | [diff] [blame] | 1691 | /// \#pragma clang arc_cf_code_audited begin/end |
John McCall | 32f5fe1 | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 1692 | struct PragmaARCCFCodeAuditedHandler : public PragmaHandler { |
| 1693 | PragmaARCCFCodeAuditedHandler() : PragmaHandler("arc_cf_code_audited") {} |
Eugene Zelenko | e95e7d5 | 2016-09-07 21:53:17 +0000 | [diff] [blame] | 1694 | |
Craig Topper | 9140dd2 | 2014-03-11 06:50:42 +0000 | [diff] [blame] | 1695 | void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 1696 | Token &NameTok) override { |
John McCall | 32f5fe1 | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 1697 | SourceLocation Loc = NameTok.getLocation(); |
| 1698 | bool IsBegin; |
| 1699 | |
| 1700 | Token Tok; |
| 1701 | |
| 1702 | // Lex the 'begin' or 'end'. |
| 1703 | PP.LexUnexpandedToken(Tok); |
| 1704 | const IdentifierInfo *BeginEnd = Tok.getIdentifierInfo(); |
| 1705 | if (BeginEnd && BeginEnd->isStr("begin")) { |
| 1706 | IsBegin = true; |
| 1707 | } else if (BeginEnd && BeginEnd->isStr("end")) { |
| 1708 | IsBegin = false; |
| 1709 | } else { |
| 1710 | PP.Diag(Tok.getLocation(), diag::err_pp_arc_cf_code_audited_syntax); |
| 1711 | return; |
| 1712 | } |
| 1713 | |
| 1714 | // Verify that this is followed by EOD. |
| 1715 | PP.LexUnexpandedToken(Tok); |
| 1716 | if (Tok.isNot(tok::eod)) |
| 1717 | PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma"; |
| 1718 | |
| 1719 | // The start location of the active audit. |
| 1720 | SourceLocation BeginLoc = PP.getPragmaARCCFCodeAuditedLoc(); |
| 1721 | |
| 1722 | // The start location we want after processing this. |
| 1723 | SourceLocation NewLoc; |
| 1724 | |
| 1725 | if (IsBegin) { |
| 1726 | // Complain about attempts to re-enter an audit. |
| 1727 | if (BeginLoc.isValid()) { |
| 1728 | PP.Diag(Loc, diag::err_pp_double_begin_of_arc_cf_code_audited); |
| 1729 | PP.Diag(BeginLoc, diag::note_pragma_entered_here); |
| 1730 | } |
| 1731 | NewLoc = Loc; |
| 1732 | } else { |
| 1733 | // Complain about attempts to leave an audit that doesn't exist. |
| 1734 | if (!BeginLoc.isValid()) { |
| 1735 | PP.Diag(Loc, diag::err_pp_unmatched_end_of_arc_cf_code_audited); |
| 1736 | return; |
| 1737 | } |
| 1738 | NewLoc = SourceLocation(); |
| 1739 | } |
| 1740 | |
| 1741 | PP.setPragmaARCCFCodeAuditedLoc(NewLoc); |
| 1742 | } |
| 1743 | }; |
| 1744 | |
Douglas Gregor | 2a20bd1 | 2015-06-19 18:25:57 +0000 | [diff] [blame] | 1745 | /// PragmaAssumeNonNullHandler - |
| 1746 | /// \#pragma clang assume_nonnull begin/end |
| 1747 | struct PragmaAssumeNonNullHandler : public PragmaHandler { |
| 1748 | PragmaAssumeNonNullHandler() : PragmaHandler("assume_nonnull") {} |
Eugene Zelenko | e95e7d5 | 2016-09-07 21:53:17 +0000 | [diff] [blame] | 1749 | |
Douglas Gregor | 2a20bd1 | 2015-06-19 18:25:57 +0000 | [diff] [blame] | 1750 | void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 1751 | Token &NameTok) override { |
| 1752 | SourceLocation Loc = NameTok.getLocation(); |
| 1753 | bool IsBegin; |
| 1754 | |
| 1755 | Token Tok; |
| 1756 | |
| 1757 | // Lex the 'begin' or 'end'. |
| 1758 | PP.LexUnexpandedToken(Tok); |
| 1759 | const IdentifierInfo *BeginEnd = Tok.getIdentifierInfo(); |
| 1760 | if (BeginEnd && BeginEnd->isStr("begin")) { |
| 1761 | IsBegin = true; |
| 1762 | } else if (BeginEnd && BeginEnd->isStr("end")) { |
| 1763 | IsBegin = false; |
| 1764 | } else { |
| 1765 | PP.Diag(Tok.getLocation(), diag::err_pp_assume_nonnull_syntax); |
| 1766 | return; |
| 1767 | } |
| 1768 | |
| 1769 | // Verify that this is followed by EOD. |
| 1770 | PP.LexUnexpandedToken(Tok); |
| 1771 | if (Tok.isNot(tok::eod)) |
| 1772 | PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma"; |
| 1773 | |
| 1774 | // The start location of the active audit. |
| 1775 | SourceLocation BeginLoc = PP.getPragmaAssumeNonNullLoc(); |
| 1776 | |
| 1777 | // The start location we want after processing this. |
| 1778 | SourceLocation NewLoc; |
Eli Friedman | 16fee08 | 2017-09-27 23:29:37 +0000 | [diff] [blame] | 1779 | PPCallbacks *Callbacks = PP.getPPCallbacks(); |
Douglas Gregor | 2a20bd1 | 2015-06-19 18:25:57 +0000 | [diff] [blame] | 1780 | |
| 1781 | if (IsBegin) { |
| 1782 | // Complain about attempts to re-enter an audit. |
| 1783 | if (BeginLoc.isValid()) { |
| 1784 | PP.Diag(Loc, diag::err_pp_double_begin_of_assume_nonnull); |
| 1785 | PP.Diag(BeginLoc, diag::note_pragma_entered_here); |
| 1786 | } |
| 1787 | NewLoc = Loc; |
Eli Friedman | 16fee08 | 2017-09-27 23:29:37 +0000 | [diff] [blame] | 1788 | if (Callbacks) |
| 1789 | Callbacks->PragmaAssumeNonNullBegin(NewLoc); |
Douglas Gregor | 2a20bd1 | 2015-06-19 18:25:57 +0000 | [diff] [blame] | 1790 | } else { |
| 1791 | // Complain about attempts to leave an audit that doesn't exist. |
| 1792 | if (!BeginLoc.isValid()) { |
| 1793 | PP.Diag(Loc, diag::err_pp_unmatched_end_of_assume_nonnull); |
| 1794 | return; |
| 1795 | } |
| 1796 | NewLoc = SourceLocation(); |
Eli Friedman | 16fee08 | 2017-09-27 23:29:37 +0000 | [diff] [blame] | 1797 | if (Callbacks) |
| 1798 | Callbacks->PragmaAssumeNonNullEnd(NewLoc); |
Douglas Gregor | 2a20bd1 | 2015-06-19 18:25:57 +0000 | [diff] [blame] | 1799 | } |
| 1800 | |
| 1801 | PP.setPragmaAssumeNonNullLoc(NewLoc); |
| 1802 | } |
| 1803 | }; |
| 1804 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1805 | /// Handle "\#pragma region [...]" |
David Majnemer | 7aa8c2f | 2013-06-30 08:18:16 +0000 | [diff] [blame] | 1806 | /// |
| 1807 | /// The syntax is |
| 1808 | /// \code |
| 1809 | /// #pragma region [optional name] |
| 1810 | /// #pragma endregion [optional comment] |
| 1811 | /// \endcode |
| 1812 | /// |
| 1813 | /// \note This is |
| 1814 | /// <a href="http://msdn.microsoft.com/en-us/library/b6xkz944(v=vs.80).aspx">editor-only</a> |
| 1815 | /// pragma, just skipped by compiler. |
| 1816 | struct PragmaRegionHandler : public PragmaHandler { |
Eugene Zelenko | cb96ac6 | 2017-12-08 22:39:26 +0000 | [diff] [blame] | 1817 | PragmaRegionHandler(const char *pragma) : PragmaHandler(pragma) {} |
Aaron Ballman | 406ea51 | 2012-11-30 19:52:30 +0000 | [diff] [blame] | 1818 | |
Craig Topper | 9140dd2 | 2014-03-11 06:50:42 +0000 | [diff] [blame] | 1819 | void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 1820 | Token &NameTok) override { |
David Majnemer | 7aa8c2f | 2013-06-30 08:18:16 +0000 | [diff] [blame] | 1821 | // #pragma region: endregion matches can be verified |
| 1822 | // __pragma(region): no sense, but ignored by msvc |
| 1823 | // _Pragma is not valid for MSVC, but there isn't any point |
| 1824 | // to handle a _Pragma differently. |
| 1825 | } |
| 1826 | }; |
Aaron Ballman | 406ea51 | 2012-11-30 19:52:30 +0000 | [diff] [blame] | 1827 | |
Eugene Zelenko | cb96ac6 | 2017-12-08 22:39:26 +0000 | [diff] [blame] | 1828 | } // namespace |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 1829 | |
| 1830 | /// RegisterBuiltinPragmas - Install the standard preprocessor pragmas: |
James Dennett | 18a6d79 | 2012-06-17 03:26:26 +0000 | [diff] [blame] | 1831 | /// \#pragma GCC poison/system_header/dependency and \#pragma once. |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 1832 | void Preprocessor::RegisterBuiltinPragmas() { |
Argyrios Kyrtzidis | 36745fd | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 1833 | AddPragmaHandler(new PragmaOnceHandler()); |
| 1834 | AddPragmaHandler(new PragmaMarkHandler()); |
Chris Lattner | c0a585d | 2010-08-17 15:55:45 +0000 | [diff] [blame] | 1835 | AddPragmaHandler(new PragmaPushMacroHandler()); |
| 1836 | AddPragmaHandler(new PragmaPopMacroHandler()); |
Andy Gibbs | 9c2ccd6 | 2013-04-17 16:16:16 +0000 | [diff] [blame] | 1837 | AddPragmaHandler(new PragmaMessageHandler(PPCallbacks::PMK_Message)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1838 | |
Chris Lattner | b61448d | 2009-05-12 18:21:11 +0000 | [diff] [blame] | 1839 | // #pragma GCC ... |
Argyrios Kyrtzidis | 36745fd | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 1840 | AddPragmaHandler("GCC", new PragmaPoisonHandler()); |
| 1841 | AddPragmaHandler("GCC", new PragmaSystemHeaderHandler()); |
| 1842 | AddPragmaHandler("GCC", new PragmaDependencyHandler()); |
Douglas Gregor | 3bde9b1 | 2011-06-22 19:41:48 +0000 | [diff] [blame] | 1843 | AddPragmaHandler("GCC", new PragmaDiagnosticHandler("GCC")); |
Andy Gibbs | 9c2ccd6 | 2013-04-17 16:16:16 +0000 | [diff] [blame] | 1844 | AddPragmaHandler("GCC", new PragmaMessageHandler(PPCallbacks::PMK_Warning, |
| 1845 | "GCC")); |
| 1846 | AddPragmaHandler("GCC", new PragmaMessageHandler(PPCallbacks::PMK_Error, |
| 1847 | "GCC")); |
Chris Lattner | b61448d | 2009-05-12 18:21:11 +0000 | [diff] [blame] | 1848 | // #pragma clang ... |
Argyrios Kyrtzidis | 36745fd | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 1849 | AddPragmaHandler("clang", new PragmaPoisonHandler()); |
| 1850 | AddPragmaHandler("clang", new PragmaSystemHeaderHandler()); |
Daniel Dunbar | b8068c3 | 2010-07-28 15:40:33 +0000 | [diff] [blame] | 1851 | AddPragmaHandler("clang", new PragmaDebugHandler()); |
Argyrios Kyrtzidis | 36745fd | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 1852 | AddPragmaHandler("clang", new PragmaDependencyHandler()); |
Douglas Gregor | 3bde9b1 | 2011-06-22 19:41:48 +0000 | [diff] [blame] | 1853 | AddPragmaHandler("clang", new PragmaDiagnosticHandler("clang")); |
John McCall | 32f5fe1 | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 1854 | AddPragmaHandler("clang", new PragmaARCCFCodeAuditedHandler()); |
Douglas Gregor | 2a20bd1 | 2015-06-19 18:25:57 +0000 | [diff] [blame] | 1855 | AddPragmaHandler("clang", new PragmaAssumeNonNullHandler()); |
Chris Lattner | b61448d | 2009-05-12 18:21:11 +0000 | [diff] [blame] | 1856 | |
Richard Smith | c51c38b | 2017-04-29 00:34:47 +0000 | [diff] [blame] | 1857 | // #pragma clang module ... |
| 1858 | auto *ModuleHandler = new PragmaNamespace("module"); |
| 1859 | AddPragmaHandler("clang", ModuleHandler); |
| 1860 | ModuleHandler->AddPragma(new PragmaModuleImportHandler()); |
Richard Smith | d138630 | 2017-05-04 00:29:54 +0000 | [diff] [blame] | 1861 | ModuleHandler->AddPragma(new PragmaModuleBeginHandler()); |
| 1862 | ModuleHandler->AddPragma(new PragmaModuleEndHandler()); |
Richard Smith | 5d2ed48 | 2017-06-09 19:22:32 +0000 | [diff] [blame] | 1863 | ModuleHandler->AddPragma(new PragmaModuleBuildHandler()); |
| 1864 | ModuleHandler->AddPragma(new PragmaModuleLoadHandler()); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1865 | |
Matt Davis | 1edb905 | 2018-01-27 00:25:29 +0000 | [diff] [blame] | 1866 | // Add region pragmas. |
| 1867 | AddPragmaHandler(new PragmaRegionHandler("region")); |
| 1868 | AddPragmaHandler(new PragmaRegionHandler("endregion")); |
Richard Smith | c51c38b | 2017-04-29 00:34:47 +0000 | [diff] [blame] | 1869 | |
Chris Lattner | 2ff698d | 2009-01-16 08:21:25 +0000 | [diff] [blame] | 1870 | // MS extensions. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1871 | if (LangOpts.MicrosoftExt) { |
Reid Kleckner | 881dff3 | 2013-09-13 22:00:30 +0000 | [diff] [blame] | 1872 | AddPragmaHandler(new PragmaWarningHandler()); |
Reid Kleckner | 0f56b22 | 2019-03-14 18:12:17 +0000 | [diff] [blame] | 1873 | AddPragmaHandler(new PragmaExecCharsetHandler()); |
Aaron Ballman | 611306e | 2012-03-02 22:51:54 +0000 | [diff] [blame] | 1874 | AddPragmaHandler(new PragmaIncludeAliasHandler()); |
Mike Rice | 58df1af | 2018-09-11 17:10:44 +0000 | [diff] [blame] | 1875 | AddPragmaHandler(new PragmaHdrstopHandler()); |
Chris Lattner | 30c924b | 2010-06-26 17:11:39 +0000 | [diff] [blame] | 1876 | } |
John Brawn | 8e62db3 | 2016-04-04 14:22:58 +0000 | [diff] [blame] | 1877 | |
| 1878 | // Pragmas added by plugins |
| 1879 | for (PragmaHandlerRegistry::iterator it = PragmaHandlerRegistry::begin(), |
| 1880 | ie = PragmaHandlerRegistry::end(); |
| 1881 | it != ie; ++it) { |
| 1882 | AddPragmaHandler(it->instantiate().release()); |
| 1883 | } |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 1884 | } |
Lubos Lunak | 576a041 | 2014-05-01 12:54:03 +0000 | [diff] [blame] | 1885 | |
| 1886 | /// Ignore all pragmas, useful for modes such as -Eonly which would otherwise |
| 1887 | /// warn about those pragmas being unknown. |
| 1888 | void Preprocessor::IgnorePragmas() { |
| 1889 | AddPragmaHandler(new EmptyPragmaHandler()); |
| 1890 | // Also ignore all pragmas in all namespaces created |
| 1891 | // in Preprocessor::RegisterBuiltinPragmas(). |
| 1892 | AddPragmaHandler("GCC", new EmptyPragmaHandler()); |
| 1893 | AddPragmaHandler("clang", new EmptyPragmaHandler()); |
Lubos Lunak | 576a041 | 2014-05-01 12:54:03 +0000 | [diff] [blame] | 1894 | } |