Daniel Dunbar | 921b968 | 2008-10-04 19:21:03 +0000 | [diff] [blame] | 1 | //===--- ParsePragma.cpp - Language specific pragma parsing ---------------===// |
| 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 |
Daniel Dunbar | 921b968 | 2008-10-04 19:21:03 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file implements the language specific #pragma handlers. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
Hans Wennborg | 899ded9 | 2014-10-16 20:52:46 +0000 | [diff] [blame] | 13 | #include "clang/AST/ASTContext.h" |
Nico Weber | 6622029 | 2016-03-02 17:28:48 +0000 | [diff] [blame] | 14 | #include "clang/Basic/PragmaKinds.h" |
David Majnemer | ad2986e | 2014-08-14 06:35:08 +0000 | [diff] [blame] | 15 | #include "clang/Basic/TargetInfo.h" |
Eli Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 16 | #include "clang/Lex/Preprocessor.h" |
Richard Trieu | 0614cff | 2018-11-28 04:36:31 +0000 | [diff] [blame] | 17 | #include "clang/Parse/LoopHint.h" |
Eli Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 18 | #include "clang/Parse/ParseDiagnostic.h" |
| 19 | #include "clang/Parse/Parser.h" |
Vassil Vassilev | 11ad339 | 2017-03-23 15:11:07 +0000 | [diff] [blame] | 20 | #include "clang/Parse/RAIIObjectsForParser.h" |
Eli Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 21 | #include "clang/Sema/Scope.h" |
| 22 | #include "llvm/ADT/StringSwitch.h" |
| 23 | using namespace clang; |
Daniel Dunbar | 921b968 | 2008-10-04 19:21:03 +0000 | [diff] [blame] | 24 | |
Reid Kleckner | 5b08646 | 2014-02-20 22:52:09 +0000 | [diff] [blame] | 25 | namespace { |
| 26 | |
| 27 | struct PragmaAlignHandler : public PragmaHandler { |
| 28 | explicit PragmaAlignHandler() : PragmaHandler("align") {} |
Craig Topper | 2b07f02 | 2014-03-12 05:09:18 +0000 | [diff] [blame] | 29 | void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 30 | Token &FirstToken) override; |
Reid Kleckner | 5b08646 | 2014-02-20 22:52:09 +0000 | [diff] [blame] | 31 | }; |
| 32 | |
| 33 | struct PragmaGCCVisibilityHandler : public PragmaHandler { |
| 34 | explicit PragmaGCCVisibilityHandler() : PragmaHandler("visibility") {} |
Craig Topper | 2b07f02 | 2014-03-12 05:09:18 +0000 | [diff] [blame] | 35 | void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 36 | Token &FirstToken) override; |
Reid Kleckner | 5b08646 | 2014-02-20 22:52:09 +0000 | [diff] [blame] | 37 | }; |
| 38 | |
| 39 | struct PragmaOptionsHandler : public PragmaHandler { |
| 40 | explicit PragmaOptionsHandler() : PragmaHandler("options") {} |
Craig Topper | 2b07f02 | 2014-03-12 05:09:18 +0000 | [diff] [blame] | 41 | void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 42 | Token &FirstToken) override; |
Reid Kleckner | 5b08646 | 2014-02-20 22:52:09 +0000 | [diff] [blame] | 43 | }; |
| 44 | |
| 45 | struct PragmaPackHandler : public PragmaHandler { |
| 46 | explicit PragmaPackHandler() : PragmaHandler("pack") {} |
Craig Topper | 2b07f02 | 2014-03-12 05:09:18 +0000 | [diff] [blame] | 47 | void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 48 | Token &FirstToken) override; |
Reid Kleckner | 5b08646 | 2014-02-20 22:52:09 +0000 | [diff] [blame] | 49 | }; |
| 50 | |
Javed Absar | 2a67c9e | 2017-06-05 10:11:57 +0000 | [diff] [blame] | 51 | struct PragmaClangSectionHandler : public PragmaHandler { |
| 52 | explicit PragmaClangSectionHandler(Sema &S) |
| 53 | : PragmaHandler("section"), Actions(S) {} |
| 54 | void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 55 | Token &FirstToken) override; |
| 56 | private: |
| 57 | Sema &Actions; |
| 58 | }; |
| 59 | |
Reid Kleckner | 5b08646 | 2014-02-20 22:52:09 +0000 | [diff] [blame] | 60 | struct PragmaMSStructHandler : public PragmaHandler { |
| 61 | explicit PragmaMSStructHandler() : PragmaHandler("ms_struct") {} |
Craig Topper | 2b07f02 | 2014-03-12 05:09:18 +0000 | [diff] [blame] | 62 | void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 63 | Token &FirstToken) override; |
Reid Kleckner | 5b08646 | 2014-02-20 22:52:09 +0000 | [diff] [blame] | 64 | }; |
| 65 | |
| 66 | struct PragmaUnusedHandler : public PragmaHandler { |
| 67 | PragmaUnusedHandler() : PragmaHandler("unused") {} |
Craig Topper | 2b07f02 | 2014-03-12 05:09:18 +0000 | [diff] [blame] | 68 | void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 69 | Token &FirstToken) override; |
Reid Kleckner | 5b08646 | 2014-02-20 22:52:09 +0000 | [diff] [blame] | 70 | }; |
| 71 | |
| 72 | struct PragmaWeakHandler : public PragmaHandler { |
| 73 | explicit PragmaWeakHandler() : PragmaHandler("weak") {} |
Craig Topper | 2b07f02 | 2014-03-12 05:09:18 +0000 | [diff] [blame] | 74 | void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 75 | Token &FirstToken) override; |
Reid Kleckner | 5b08646 | 2014-02-20 22:52:09 +0000 | [diff] [blame] | 76 | }; |
| 77 | |
| 78 | struct PragmaRedefineExtnameHandler : public PragmaHandler { |
| 79 | explicit PragmaRedefineExtnameHandler() : PragmaHandler("redefine_extname") {} |
Craig Topper | 2b07f02 | 2014-03-12 05:09:18 +0000 | [diff] [blame] | 80 | void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 81 | Token &FirstToken) override; |
Reid Kleckner | 5b08646 | 2014-02-20 22:52:09 +0000 | [diff] [blame] | 82 | }; |
| 83 | |
| 84 | struct PragmaOpenCLExtensionHandler : public PragmaHandler { |
| 85 | PragmaOpenCLExtensionHandler() : PragmaHandler("EXTENSION") {} |
Craig Topper | 2b07f02 | 2014-03-12 05:09:18 +0000 | [diff] [blame] | 86 | void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 87 | Token &FirstToken) override; |
Reid Kleckner | 5b08646 | 2014-02-20 22:52:09 +0000 | [diff] [blame] | 88 | }; |
| 89 | |
| 90 | |
| 91 | struct PragmaFPContractHandler : public PragmaHandler { |
| 92 | PragmaFPContractHandler() : PragmaHandler("FP_CONTRACT") {} |
Craig Topper | 2b07f02 | 2014-03-12 05:09:18 +0000 | [diff] [blame] | 93 | void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 94 | Token &FirstToken) override; |
Reid Kleckner | 5b08646 | 2014-02-20 22:52:09 +0000 | [diff] [blame] | 95 | }; |
| 96 | |
Steven Wu | b96a3a4 | 2018-01-05 22:45:03 +0000 | [diff] [blame] | 97 | // Pragma STDC implementations. |
| 98 | |
| 99 | /// PragmaSTDC_FENV_ACCESSHandler - "\#pragma STDC FENV_ACCESS ...". |
| 100 | struct PragmaSTDC_FENV_ACCESSHandler : public PragmaHandler { |
| 101 | PragmaSTDC_FENV_ACCESSHandler() : PragmaHandler("FENV_ACCESS") {} |
| 102 | |
| 103 | void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 104 | Token &Tok) override { |
| 105 | tok::OnOffSwitch OOS; |
| 106 | if (PP.LexOnOffSwitch(OOS)) |
| 107 | return; |
Kevin P. Neal | 2c0bc8b | 2018-08-14 17:06:56 +0000 | [diff] [blame] | 108 | if (OOS == tok::OOS_ON) { |
Steven Wu | b96a3a4 | 2018-01-05 22:45:03 +0000 | [diff] [blame] | 109 | PP.Diag(Tok, diag::warn_stdc_fenv_access_not_supported); |
Kevin P. Neal | 2c0bc8b | 2018-08-14 17:06:56 +0000 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | MutableArrayRef<Token> Toks(PP.getPreprocessorAllocator().Allocate<Token>(1), |
| 113 | 1); |
| 114 | Toks[0].startToken(); |
| 115 | Toks[0].setKind(tok::annot_pragma_fenv_access); |
| 116 | Toks[0].setLocation(Tok.getLocation()); |
| 117 | Toks[0].setAnnotationEndLoc(Tok.getLocation()); |
| 118 | Toks[0].setAnnotationValue(reinterpret_cast<void*>( |
| 119 | static_cast<uintptr_t>(OOS))); |
| 120 | PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true); |
Steven Wu | b96a3a4 | 2018-01-05 22:45:03 +0000 | [diff] [blame] | 121 | } |
| 122 | }; |
| 123 | |
| 124 | /// PragmaSTDC_CX_LIMITED_RANGEHandler - "\#pragma STDC CX_LIMITED_RANGE ...". |
| 125 | struct PragmaSTDC_CX_LIMITED_RANGEHandler : public PragmaHandler { |
| 126 | PragmaSTDC_CX_LIMITED_RANGEHandler() : PragmaHandler("CX_LIMITED_RANGE") {} |
| 127 | |
| 128 | void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 129 | Token &Tok) override { |
| 130 | tok::OnOffSwitch OOS; |
| 131 | PP.LexOnOffSwitch(OOS); |
| 132 | } |
| 133 | }; |
| 134 | |
| 135 | /// PragmaSTDC_UnknownHandler - "\#pragma STDC ...". |
| 136 | struct PragmaSTDC_UnknownHandler : public PragmaHandler { |
| 137 | PragmaSTDC_UnknownHandler() = default; |
| 138 | |
| 139 | void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 140 | Token &UnknownTok) override { |
| 141 | // C99 6.10.6p2, unknown forms are not allowed. |
| 142 | PP.Diag(UnknownTok, diag::ext_stdc_pragma_ignored); |
| 143 | } |
| 144 | }; |
| 145 | |
Adam Nemet | 60d3264 | 2017-04-04 21:18:36 +0000 | [diff] [blame] | 146 | struct PragmaFPHandler : public PragmaHandler { |
| 147 | PragmaFPHandler() : PragmaHandler("fp") {} |
| 148 | void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 149 | Token &FirstToken) override; |
| 150 | }; |
| 151 | |
Reid Kleckner | 5b08646 | 2014-02-20 22:52:09 +0000 | [diff] [blame] | 152 | struct PragmaNoOpenMPHandler : public PragmaHandler { |
| 153 | PragmaNoOpenMPHandler() : PragmaHandler("omp") { } |
Craig Topper | 2b07f02 | 2014-03-12 05:09:18 +0000 | [diff] [blame] | 154 | void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 155 | Token &FirstToken) override; |
Reid Kleckner | 5b08646 | 2014-02-20 22:52:09 +0000 | [diff] [blame] | 156 | }; |
| 157 | |
| 158 | struct PragmaOpenMPHandler : public PragmaHandler { |
| 159 | PragmaOpenMPHandler() : PragmaHandler("omp") { } |
Craig Topper | 2b07f02 | 2014-03-12 05:09:18 +0000 | [diff] [blame] | 160 | void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 161 | Token &FirstToken) override; |
Reid Kleckner | 5b08646 | 2014-02-20 22:52:09 +0000 | [diff] [blame] | 162 | }; |
| 163 | |
| 164 | /// PragmaCommentHandler - "\#pragma comment ...". |
| 165 | struct PragmaCommentHandler : public PragmaHandler { |
| 166 | PragmaCommentHandler(Sema &Actions) |
| 167 | : PragmaHandler("comment"), Actions(Actions) {} |
Craig Topper | 2b07f02 | 2014-03-12 05:09:18 +0000 | [diff] [blame] | 168 | void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 169 | Token &FirstToken) override; |
Reid Kleckner | 5b08646 | 2014-02-20 22:52:09 +0000 | [diff] [blame] | 170 | private: |
| 171 | Sema &Actions; |
| 172 | }; |
| 173 | |
| 174 | struct PragmaDetectMismatchHandler : public PragmaHandler { |
| 175 | PragmaDetectMismatchHandler(Sema &Actions) |
| 176 | : PragmaHandler("detect_mismatch"), Actions(Actions) {} |
Craig Topper | 2b07f02 | 2014-03-12 05:09:18 +0000 | [diff] [blame] | 177 | void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 178 | Token &FirstToken) override; |
Reid Kleckner | 5b08646 | 2014-02-20 22:52:09 +0000 | [diff] [blame] | 179 | private: |
| 180 | Sema &Actions; |
| 181 | }; |
| 182 | |
| 183 | struct PragmaMSPointersToMembers : public PragmaHandler { |
| 184 | explicit PragmaMSPointersToMembers() : PragmaHandler("pointers_to_members") {} |
Craig Topper | 2b07f02 | 2014-03-12 05:09:18 +0000 | [diff] [blame] | 185 | void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 186 | Token &FirstToken) override; |
Reid Kleckner | 5b08646 | 2014-02-20 22:52:09 +0000 | [diff] [blame] | 187 | }; |
| 188 | |
| 189 | struct PragmaMSVtorDisp : public PragmaHandler { |
| 190 | explicit PragmaMSVtorDisp() : PragmaHandler("vtordisp") {} |
Craig Topper | 2b07f02 | 2014-03-12 05:09:18 +0000 | [diff] [blame] | 191 | void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 192 | Token &FirstToken) override; |
Reid Kleckner | 5b08646 | 2014-02-20 22:52:09 +0000 | [diff] [blame] | 193 | }; |
| 194 | |
Warren Hunt | c3b1896 | 2014-04-08 22:30:47 +0000 | [diff] [blame] | 195 | struct PragmaMSPragma : public PragmaHandler { |
| 196 | explicit PragmaMSPragma(const char *name) : PragmaHandler(name) {} |
Reid Kleckner | d3923aa | 2014-04-03 19:04:24 +0000 | [diff] [blame] | 197 | void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 198 | Token &FirstToken) override; |
| 199 | }; |
| 200 | |
Dario Domizioli | 13a0a38 | 2014-05-23 12:13:25 +0000 | [diff] [blame] | 201 | /// PragmaOptimizeHandler - "\#pragma clang optimize on/off". |
| 202 | struct PragmaOptimizeHandler : public PragmaHandler { |
| 203 | PragmaOptimizeHandler(Sema &S) |
| 204 | : PragmaHandler("optimize"), Actions(S) {} |
| 205 | void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 206 | Token &FirstToken) override; |
| 207 | private: |
Eli Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 208 | Sema &Actions; |
| 209 | }; |
| 210 | |
| 211 | struct PragmaLoopHintHandler : public PragmaHandler { |
| 212 | PragmaLoopHintHandler() : PragmaHandler("loop") {} |
| 213 | void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 214 | Token &FirstToken) override; |
| 215 | }; |
| 216 | |
Mark Heffernan | bd26f5e | 2014-07-21 18:08:34 +0000 | [diff] [blame] | 217 | struct PragmaUnrollHintHandler : public PragmaHandler { |
| 218 | PragmaUnrollHintHandler(const char *name) : PragmaHandler(name) {} |
| 219 | void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 220 | Token &FirstToken) override; |
| 221 | }; |
| 222 | |
Hans Wennborg | 7357bbc | 2015-10-12 20:47:58 +0000 | [diff] [blame] | 223 | struct PragmaMSRuntimeChecksHandler : public EmptyPragmaHandler { |
| 224 | PragmaMSRuntimeChecksHandler() : EmptyPragmaHandler("runtime_checks") {} |
| 225 | }; |
| 226 | |
Reid Kleckner | 3f1ec62 | 2016-09-07 16:38:32 +0000 | [diff] [blame] | 227 | struct PragmaMSIntrinsicHandler : public PragmaHandler { |
| 228 | PragmaMSIntrinsicHandler() : PragmaHandler("intrinsic") {} |
| 229 | void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 230 | Token &FirstToken) override; |
| 231 | }; |
| 232 | |
Hans Wennborg | 1bbe00e | 2018-03-20 08:53:11 +0000 | [diff] [blame] | 233 | struct PragmaMSOptimizeHandler : public PragmaHandler { |
| 234 | PragmaMSOptimizeHandler() : PragmaHandler("optimize") {} |
| 235 | void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 236 | Token &FirstToken) override; |
| 237 | }; |
| 238 | |
Justin Lebar | 67a78a6 | 2016-10-08 22:15:58 +0000 | [diff] [blame] | 239 | struct PragmaForceCUDAHostDeviceHandler : public PragmaHandler { |
| 240 | PragmaForceCUDAHostDeviceHandler(Sema &Actions) |
| 241 | : PragmaHandler("force_cuda_host_device"), Actions(Actions) {} |
| 242 | void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 243 | Token &FirstToken) override; |
| 244 | |
| 245 | private: |
| 246 | Sema &Actions; |
| 247 | }; |
| 248 | |
Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 249 | /// PragmaAttributeHandler - "\#pragma clang attribute ...". |
| 250 | struct PragmaAttributeHandler : public PragmaHandler { |
| 251 | PragmaAttributeHandler(AttributeFactory &AttrFactory) |
| 252 | : PragmaHandler("attribute"), AttributesForPragmaAttribute(AttrFactory) {} |
| 253 | void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 254 | Token &FirstToken) override; |
| 255 | |
| 256 | /// A pool of attributes that were parsed in \#pragma clang attribute. |
| 257 | ParsedAttributes AttributesForPragmaAttribute; |
| 258 | }; |
| 259 | |
Eli Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 260 | } // end namespace |
| 261 | |
| 262 | void Parser::initializePragmaHandlers() { |
David Blaikie | e0cfc04 | 2018-11-15 03:04:23 +0000 | [diff] [blame] | 263 | AlignHandler = llvm::make_unique<PragmaAlignHandler>(); |
Reid Kleckner | 5b08646 | 2014-02-20 22:52:09 +0000 | [diff] [blame] | 264 | PP.AddPragmaHandler(AlignHandler.get()); |
| 265 | |
David Blaikie | e0cfc04 | 2018-11-15 03:04:23 +0000 | [diff] [blame] | 266 | GCCVisibilityHandler = llvm::make_unique<PragmaGCCVisibilityHandler>(); |
Reid Kleckner | 5b08646 | 2014-02-20 22:52:09 +0000 | [diff] [blame] | 267 | PP.AddPragmaHandler("GCC", GCCVisibilityHandler.get()); |
| 268 | |
David Blaikie | e0cfc04 | 2018-11-15 03:04:23 +0000 | [diff] [blame] | 269 | OptionsHandler = llvm::make_unique<PragmaOptionsHandler>(); |
Reid Kleckner | 5b08646 | 2014-02-20 22:52:09 +0000 | [diff] [blame] | 270 | PP.AddPragmaHandler(OptionsHandler.get()); |
| 271 | |
David Blaikie | e0cfc04 | 2018-11-15 03:04:23 +0000 | [diff] [blame] | 272 | PackHandler = llvm::make_unique<PragmaPackHandler>(); |
Reid Kleckner | 5b08646 | 2014-02-20 22:52:09 +0000 | [diff] [blame] | 273 | PP.AddPragmaHandler(PackHandler.get()); |
| 274 | |
David Blaikie | e0cfc04 | 2018-11-15 03:04:23 +0000 | [diff] [blame] | 275 | MSStructHandler = llvm::make_unique<PragmaMSStructHandler>(); |
Reid Kleckner | 5b08646 | 2014-02-20 22:52:09 +0000 | [diff] [blame] | 276 | PP.AddPragmaHandler(MSStructHandler.get()); |
| 277 | |
David Blaikie | e0cfc04 | 2018-11-15 03:04:23 +0000 | [diff] [blame] | 278 | UnusedHandler = llvm::make_unique<PragmaUnusedHandler>(); |
Reid Kleckner | 5b08646 | 2014-02-20 22:52:09 +0000 | [diff] [blame] | 279 | PP.AddPragmaHandler(UnusedHandler.get()); |
| 280 | |
David Blaikie | e0cfc04 | 2018-11-15 03:04:23 +0000 | [diff] [blame] | 281 | WeakHandler = llvm::make_unique<PragmaWeakHandler>(); |
Reid Kleckner | 5b08646 | 2014-02-20 22:52:09 +0000 | [diff] [blame] | 282 | PP.AddPragmaHandler(WeakHandler.get()); |
| 283 | |
David Blaikie | e0cfc04 | 2018-11-15 03:04:23 +0000 | [diff] [blame] | 284 | RedefineExtnameHandler = llvm::make_unique<PragmaRedefineExtnameHandler>(); |
Reid Kleckner | 5b08646 | 2014-02-20 22:52:09 +0000 | [diff] [blame] | 285 | PP.AddPragmaHandler(RedefineExtnameHandler.get()); |
| 286 | |
David Blaikie | e0cfc04 | 2018-11-15 03:04:23 +0000 | [diff] [blame] | 287 | FPContractHandler = llvm::make_unique<PragmaFPContractHandler>(); |
Reid Kleckner | 5b08646 | 2014-02-20 22:52:09 +0000 | [diff] [blame] | 288 | PP.AddPragmaHandler("STDC", FPContractHandler.get()); |
| 289 | |
David Blaikie | e0cfc04 | 2018-11-15 03:04:23 +0000 | [diff] [blame] | 290 | STDCFENVHandler = llvm::make_unique<PragmaSTDC_FENV_ACCESSHandler>(); |
Steven Wu | b96a3a4 | 2018-01-05 22:45:03 +0000 | [diff] [blame] | 291 | PP.AddPragmaHandler("STDC", STDCFENVHandler.get()); |
| 292 | |
David Blaikie | e0cfc04 | 2018-11-15 03:04:23 +0000 | [diff] [blame] | 293 | STDCCXLIMITHandler = llvm::make_unique<PragmaSTDC_CX_LIMITED_RANGEHandler>(); |
Steven Wu | b96a3a4 | 2018-01-05 22:45:03 +0000 | [diff] [blame] | 294 | PP.AddPragmaHandler("STDC", STDCCXLIMITHandler.get()); |
| 295 | |
David Blaikie | e0cfc04 | 2018-11-15 03:04:23 +0000 | [diff] [blame] | 296 | STDCUnknownHandler = llvm::make_unique<PragmaSTDC_UnknownHandler>(); |
Steven Wu | b96a3a4 | 2018-01-05 22:45:03 +0000 | [diff] [blame] | 297 | PP.AddPragmaHandler("STDC", STDCUnknownHandler.get()); |
| 298 | |
David Blaikie | e0cfc04 | 2018-11-15 03:04:23 +0000 | [diff] [blame] | 299 | PCSectionHandler = llvm::make_unique<PragmaClangSectionHandler>(Actions); |
Javed Absar | 2a67c9e | 2017-06-05 10:11:57 +0000 | [diff] [blame] | 300 | PP.AddPragmaHandler("clang", PCSectionHandler.get()); |
| 301 | |
Reid Kleckner | 5b08646 | 2014-02-20 22:52:09 +0000 | [diff] [blame] | 302 | if (getLangOpts().OpenCL) { |
David Blaikie | e0cfc04 | 2018-11-15 03:04:23 +0000 | [diff] [blame] | 303 | OpenCLExtensionHandler = llvm::make_unique<PragmaOpenCLExtensionHandler>(); |
Reid Kleckner | 5b08646 | 2014-02-20 22:52:09 +0000 | [diff] [blame] | 304 | PP.AddPragmaHandler("OPENCL", OpenCLExtensionHandler.get()); |
| 305 | |
| 306 | PP.AddPragmaHandler("OPENCL", FPContractHandler.get()); |
| 307 | } |
| 308 | if (getLangOpts().OpenMP) |
David Blaikie | e0cfc04 | 2018-11-15 03:04:23 +0000 | [diff] [blame] | 309 | OpenMPHandler = llvm::make_unique<PragmaOpenMPHandler>(); |
Reid Kleckner | 5b08646 | 2014-02-20 22:52:09 +0000 | [diff] [blame] | 310 | else |
David Blaikie | e0cfc04 | 2018-11-15 03:04:23 +0000 | [diff] [blame] | 311 | OpenMPHandler = llvm::make_unique<PragmaNoOpenMPHandler>(); |
Reid Kleckner | 5b08646 | 2014-02-20 22:52:09 +0000 | [diff] [blame] | 312 | PP.AddPragmaHandler(OpenMPHandler.get()); |
| 313 | |
Saleem Abdulrasool | fd4db53 | 2018-02-07 01:46:46 +0000 | [diff] [blame] | 314 | if (getLangOpts().MicrosoftExt || |
| 315 | getTargetInfo().getTriple().isOSBinFormatELF()) { |
David Blaikie | e0cfc04 | 2018-11-15 03:04:23 +0000 | [diff] [blame] | 316 | MSCommentHandler = llvm::make_unique<PragmaCommentHandler>(Actions); |
Reid Kleckner | 5b08646 | 2014-02-20 22:52:09 +0000 | [diff] [blame] | 317 | PP.AddPragmaHandler(MSCommentHandler.get()); |
Yunzhong Gao | 99efc03 | 2015-03-23 20:41:42 +0000 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | if (getLangOpts().MicrosoftExt) { |
David Blaikie | e0cfc04 | 2018-11-15 03:04:23 +0000 | [diff] [blame] | 321 | MSDetectMismatchHandler = |
| 322 | llvm::make_unique<PragmaDetectMismatchHandler>(Actions); |
Reid Kleckner | 5b08646 | 2014-02-20 22:52:09 +0000 | [diff] [blame] | 323 | PP.AddPragmaHandler(MSDetectMismatchHandler.get()); |
David Blaikie | e0cfc04 | 2018-11-15 03:04:23 +0000 | [diff] [blame] | 324 | MSPointersToMembers = llvm::make_unique<PragmaMSPointersToMembers>(); |
Reid Kleckner | 5b08646 | 2014-02-20 22:52:09 +0000 | [diff] [blame] | 325 | PP.AddPragmaHandler(MSPointersToMembers.get()); |
David Blaikie | e0cfc04 | 2018-11-15 03:04:23 +0000 | [diff] [blame] | 326 | MSVtorDisp = llvm::make_unique<PragmaMSVtorDisp>(); |
Reid Kleckner | 5b08646 | 2014-02-20 22:52:09 +0000 | [diff] [blame] | 327 | PP.AddPragmaHandler(MSVtorDisp.get()); |
David Blaikie | e0cfc04 | 2018-11-15 03:04:23 +0000 | [diff] [blame] | 328 | MSInitSeg = llvm::make_unique<PragmaMSPragma>("init_seg"); |
Reid Kleckner | d3923aa | 2014-04-03 19:04:24 +0000 | [diff] [blame] | 329 | PP.AddPragmaHandler(MSInitSeg.get()); |
David Blaikie | e0cfc04 | 2018-11-15 03:04:23 +0000 | [diff] [blame] | 330 | MSDataSeg = llvm::make_unique<PragmaMSPragma>("data_seg"); |
Warren Hunt | c3b1896 | 2014-04-08 22:30:47 +0000 | [diff] [blame] | 331 | PP.AddPragmaHandler(MSDataSeg.get()); |
David Blaikie | e0cfc04 | 2018-11-15 03:04:23 +0000 | [diff] [blame] | 332 | MSBSSSeg = llvm::make_unique<PragmaMSPragma>("bss_seg"); |
Warren Hunt | c3b1896 | 2014-04-08 22:30:47 +0000 | [diff] [blame] | 333 | PP.AddPragmaHandler(MSBSSSeg.get()); |
David Blaikie | e0cfc04 | 2018-11-15 03:04:23 +0000 | [diff] [blame] | 334 | MSConstSeg = llvm::make_unique<PragmaMSPragma>("const_seg"); |
Warren Hunt | c3b1896 | 2014-04-08 22:30:47 +0000 | [diff] [blame] | 335 | PP.AddPragmaHandler(MSConstSeg.get()); |
David Blaikie | e0cfc04 | 2018-11-15 03:04:23 +0000 | [diff] [blame] | 336 | MSCodeSeg = llvm::make_unique<PragmaMSPragma>("code_seg"); |
Warren Hunt | c3b1896 | 2014-04-08 22:30:47 +0000 | [diff] [blame] | 337 | PP.AddPragmaHandler(MSCodeSeg.get()); |
David Blaikie | e0cfc04 | 2018-11-15 03:04:23 +0000 | [diff] [blame] | 338 | MSSection = llvm::make_unique<PragmaMSPragma>("section"); |
Warren Hunt | c3b1896 | 2014-04-08 22:30:47 +0000 | [diff] [blame] | 339 | PP.AddPragmaHandler(MSSection.get()); |
David Blaikie | e0cfc04 | 2018-11-15 03:04:23 +0000 | [diff] [blame] | 340 | MSRuntimeChecks = llvm::make_unique<PragmaMSRuntimeChecksHandler>(); |
Hans Wennborg | 7357bbc | 2015-10-12 20:47:58 +0000 | [diff] [blame] | 341 | PP.AddPragmaHandler(MSRuntimeChecks.get()); |
David Blaikie | e0cfc04 | 2018-11-15 03:04:23 +0000 | [diff] [blame] | 342 | MSIntrinsic = llvm::make_unique<PragmaMSIntrinsicHandler>(); |
Reid Kleckner | 3f1ec62 | 2016-09-07 16:38:32 +0000 | [diff] [blame] | 343 | PP.AddPragmaHandler(MSIntrinsic.get()); |
David Blaikie | e0cfc04 | 2018-11-15 03:04:23 +0000 | [diff] [blame] | 344 | MSOptimize = llvm::make_unique<PragmaMSOptimizeHandler>(); |
Hans Wennborg | 1bbe00e | 2018-03-20 08:53:11 +0000 | [diff] [blame] | 345 | PP.AddPragmaHandler(MSOptimize.get()); |
Reid Kleckner | 5b08646 | 2014-02-20 22:52:09 +0000 | [diff] [blame] | 346 | } |
Eli Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 347 | |
Justin Lebar | 67a78a6 | 2016-10-08 22:15:58 +0000 | [diff] [blame] | 348 | if (getLangOpts().CUDA) { |
David Blaikie | e0cfc04 | 2018-11-15 03:04:23 +0000 | [diff] [blame] | 349 | CUDAForceHostDeviceHandler = |
| 350 | llvm::make_unique<PragmaForceCUDAHostDeviceHandler>(Actions); |
Justin Lebar | 67a78a6 | 2016-10-08 22:15:58 +0000 | [diff] [blame] | 351 | PP.AddPragmaHandler("clang", CUDAForceHostDeviceHandler.get()); |
| 352 | } |
| 353 | |
David Blaikie | e0cfc04 | 2018-11-15 03:04:23 +0000 | [diff] [blame] | 354 | OptimizeHandler = llvm::make_unique<PragmaOptimizeHandler>(Actions); |
Eli Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 355 | PP.AddPragmaHandler("clang", OptimizeHandler.get()); |
| 356 | |
David Blaikie | e0cfc04 | 2018-11-15 03:04:23 +0000 | [diff] [blame] | 357 | LoopHintHandler = llvm::make_unique<PragmaLoopHintHandler>(); |
Eli Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 358 | PP.AddPragmaHandler("clang", LoopHintHandler.get()); |
Mark Heffernan | bd26f5e | 2014-07-21 18:08:34 +0000 | [diff] [blame] | 359 | |
David Blaikie | e0cfc04 | 2018-11-15 03:04:23 +0000 | [diff] [blame] | 360 | UnrollHintHandler = llvm::make_unique<PragmaUnrollHintHandler>("unroll"); |
Mark Heffernan | bd26f5e | 2014-07-21 18:08:34 +0000 | [diff] [blame] | 361 | PP.AddPragmaHandler(UnrollHintHandler.get()); |
Mark Heffernan | c888e41 | 2014-07-24 18:09:38 +0000 | [diff] [blame] | 362 | |
David Blaikie | e0cfc04 | 2018-11-15 03:04:23 +0000 | [diff] [blame] | 363 | NoUnrollHintHandler = llvm::make_unique<PragmaUnrollHintHandler>("nounroll"); |
Mark Heffernan | c888e41 | 2014-07-24 18:09:38 +0000 | [diff] [blame] | 364 | PP.AddPragmaHandler(NoUnrollHintHandler.get()); |
Adam Nemet | 60d3264 | 2017-04-04 21:18:36 +0000 | [diff] [blame] | 365 | |
David Blaikie | e0cfc04 | 2018-11-15 03:04:23 +0000 | [diff] [blame] | 366 | UnrollAndJamHintHandler = |
| 367 | llvm::make_unique<PragmaUnrollHintHandler>("unroll_and_jam"); |
David Green | c8e3924 | 2018-08-01 14:36:12 +0000 | [diff] [blame] | 368 | PP.AddPragmaHandler(UnrollAndJamHintHandler.get()); |
| 369 | |
David Blaikie | e0cfc04 | 2018-11-15 03:04:23 +0000 | [diff] [blame] | 370 | NoUnrollAndJamHintHandler = |
| 371 | llvm::make_unique<PragmaUnrollHintHandler>("nounroll_and_jam"); |
David Green | c8e3924 | 2018-08-01 14:36:12 +0000 | [diff] [blame] | 372 | PP.AddPragmaHandler(NoUnrollAndJamHintHandler.get()); |
| 373 | |
David Blaikie | e0cfc04 | 2018-11-15 03:04:23 +0000 | [diff] [blame] | 374 | FPHandler = llvm::make_unique<PragmaFPHandler>(); |
Adam Nemet | 60d3264 | 2017-04-04 21:18:36 +0000 | [diff] [blame] | 375 | PP.AddPragmaHandler("clang", FPHandler.get()); |
Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 376 | |
David Blaikie | e0cfc04 | 2018-11-15 03:04:23 +0000 | [diff] [blame] | 377 | AttributePragmaHandler = |
| 378 | llvm::make_unique<PragmaAttributeHandler>(AttrFactory); |
Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 379 | PP.AddPragmaHandler("clang", AttributePragmaHandler.get()); |
Eli Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | void Parser::resetPragmaHandlers() { |
Reid Kleckner | 5b08646 | 2014-02-20 22:52:09 +0000 | [diff] [blame] | 383 | // Remove the pragma handlers we installed. |
| 384 | PP.RemovePragmaHandler(AlignHandler.get()); |
| 385 | AlignHandler.reset(); |
| 386 | PP.RemovePragmaHandler("GCC", GCCVisibilityHandler.get()); |
| 387 | GCCVisibilityHandler.reset(); |
| 388 | PP.RemovePragmaHandler(OptionsHandler.get()); |
| 389 | OptionsHandler.reset(); |
| 390 | PP.RemovePragmaHandler(PackHandler.get()); |
| 391 | PackHandler.reset(); |
| 392 | PP.RemovePragmaHandler(MSStructHandler.get()); |
| 393 | MSStructHandler.reset(); |
| 394 | PP.RemovePragmaHandler(UnusedHandler.get()); |
| 395 | UnusedHandler.reset(); |
| 396 | PP.RemovePragmaHandler(WeakHandler.get()); |
| 397 | WeakHandler.reset(); |
| 398 | PP.RemovePragmaHandler(RedefineExtnameHandler.get()); |
| 399 | RedefineExtnameHandler.reset(); |
| 400 | |
| 401 | if (getLangOpts().OpenCL) { |
| 402 | PP.RemovePragmaHandler("OPENCL", OpenCLExtensionHandler.get()); |
| 403 | OpenCLExtensionHandler.reset(); |
| 404 | PP.RemovePragmaHandler("OPENCL", FPContractHandler.get()); |
| 405 | } |
| 406 | PP.RemovePragmaHandler(OpenMPHandler.get()); |
| 407 | OpenMPHandler.reset(); |
| 408 | |
Saleem Abdulrasool | fd4db53 | 2018-02-07 01:46:46 +0000 | [diff] [blame] | 409 | if (getLangOpts().MicrosoftExt || |
| 410 | getTargetInfo().getTriple().isOSBinFormatELF()) { |
Reid Kleckner | 5b08646 | 2014-02-20 22:52:09 +0000 | [diff] [blame] | 411 | PP.RemovePragmaHandler(MSCommentHandler.get()); |
| 412 | MSCommentHandler.reset(); |
Yunzhong Gao | 99efc03 | 2015-03-23 20:41:42 +0000 | [diff] [blame] | 413 | } |
| 414 | |
Javed Absar | 2a67c9e | 2017-06-05 10:11:57 +0000 | [diff] [blame] | 415 | PP.RemovePragmaHandler("clang", PCSectionHandler.get()); |
| 416 | PCSectionHandler.reset(); |
| 417 | |
Yunzhong Gao | 99efc03 | 2015-03-23 20:41:42 +0000 | [diff] [blame] | 418 | if (getLangOpts().MicrosoftExt) { |
Reid Kleckner | 5b08646 | 2014-02-20 22:52:09 +0000 | [diff] [blame] | 419 | PP.RemovePragmaHandler(MSDetectMismatchHandler.get()); |
| 420 | MSDetectMismatchHandler.reset(); |
| 421 | PP.RemovePragmaHandler(MSPointersToMembers.get()); |
| 422 | MSPointersToMembers.reset(); |
| 423 | PP.RemovePragmaHandler(MSVtorDisp.get()); |
| 424 | MSVtorDisp.reset(); |
Reid Kleckner | d3923aa | 2014-04-03 19:04:24 +0000 | [diff] [blame] | 425 | PP.RemovePragmaHandler(MSInitSeg.get()); |
| 426 | MSInitSeg.reset(); |
Warren Hunt | c3b1896 | 2014-04-08 22:30:47 +0000 | [diff] [blame] | 427 | PP.RemovePragmaHandler(MSDataSeg.get()); |
| 428 | MSDataSeg.reset(); |
| 429 | PP.RemovePragmaHandler(MSBSSSeg.get()); |
| 430 | MSBSSSeg.reset(); |
| 431 | PP.RemovePragmaHandler(MSConstSeg.get()); |
| 432 | MSConstSeg.reset(); |
| 433 | PP.RemovePragmaHandler(MSCodeSeg.get()); |
| 434 | MSCodeSeg.reset(); |
| 435 | PP.RemovePragmaHandler(MSSection.get()); |
| 436 | MSSection.reset(); |
Hans Wennborg | 7357bbc | 2015-10-12 20:47:58 +0000 | [diff] [blame] | 437 | PP.RemovePragmaHandler(MSRuntimeChecks.get()); |
| 438 | MSRuntimeChecks.reset(); |
Reid Kleckner | 3f1ec62 | 2016-09-07 16:38:32 +0000 | [diff] [blame] | 439 | PP.RemovePragmaHandler(MSIntrinsic.get()); |
| 440 | MSIntrinsic.reset(); |
Hans Wennborg | 1bbe00e | 2018-03-20 08:53:11 +0000 | [diff] [blame] | 441 | PP.RemovePragmaHandler(MSOptimize.get()); |
| 442 | MSOptimize.reset(); |
Reid Kleckner | 5b08646 | 2014-02-20 22:52:09 +0000 | [diff] [blame] | 443 | } |
| 444 | |
Justin Lebar | 67a78a6 | 2016-10-08 22:15:58 +0000 | [diff] [blame] | 445 | if (getLangOpts().CUDA) { |
| 446 | PP.RemovePragmaHandler("clang", CUDAForceHostDeviceHandler.get()); |
| 447 | CUDAForceHostDeviceHandler.reset(); |
| 448 | } |
| 449 | |
Reid Kleckner | 5b08646 | 2014-02-20 22:52:09 +0000 | [diff] [blame] | 450 | PP.RemovePragmaHandler("STDC", FPContractHandler.get()); |
| 451 | FPContractHandler.reset(); |
Eli Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 452 | |
Steven Wu | b96a3a4 | 2018-01-05 22:45:03 +0000 | [diff] [blame] | 453 | PP.RemovePragmaHandler("STDC", STDCFENVHandler.get()); |
| 454 | STDCFENVHandler.reset(); |
| 455 | |
| 456 | PP.RemovePragmaHandler("STDC", STDCCXLIMITHandler.get()); |
| 457 | STDCCXLIMITHandler.reset(); |
| 458 | |
| 459 | PP.RemovePragmaHandler("STDC", STDCUnknownHandler.get()); |
| 460 | STDCUnknownHandler.reset(); |
| 461 | |
Eli Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 462 | PP.RemovePragmaHandler("clang", OptimizeHandler.get()); |
| 463 | OptimizeHandler.reset(); |
| 464 | |
| 465 | PP.RemovePragmaHandler("clang", LoopHintHandler.get()); |
| 466 | LoopHintHandler.reset(); |
Mark Heffernan | bd26f5e | 2014-07-21 18:08:34 +0000 | [diff] [blame] | 467 | |
| 468 | PP.RemovePragmaHandler(UnrollHintHandler.get()); |
| 469 | UnrollHintHandler.reset(); |
Mark Heffernan | c888e41 | 2014-07-24 18:09:38 +0000 | [diff] [blame] | 470 | |
| 471 | PP.RemovePragmaHandler(NoUnrollHintHandler.get()); |
| 472 | NoUnrollHintHandler.reset(); |
Adam Nemet | 60d3264 | 2017-04-04 21:18:36 +0000 | [diff] [blame] | 473 | |
David Green | c8e3924 | 2018-08-01 14:36:12 +0000 | [diff] [blame] | 474 | PP.RemovePragmaHandler(UnrollAndJamHintHandler.get()); |
| 475 | UnrollAndJamHintHandler.reset(); |
| 476 | |
| 477 | PP.RemovePragmaHandler(NoUnrollAndJamHintHandler.get()); |
| 478 | NoUnrollAndJamHintHandler.reset(); |
| 479 | |
Adam Nemet | 60d3264 | 2017-04-04 21:18:36 +0000 | [diff] [blame] | 480 | PP.RemovePragmaHandler("clang", FPHandler.get()); |
| 481 | FPHandler.reset(); |
Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 482 | |
| 483 | PP.RemovePragmaHandler("clang", AttributePragmaHandler.get()); |
| 484 | AttributePragmaHandler.reset(); |
Eli Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 485 | } |
| 486 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 487 | /// Handle the annotation token produced for #pragma unused(...) |
Argyrios Kyrtzidis | ee56962 | 2011-01-17 18:58:44 +0000 | [diff] [blame] | 488 | /// |
| 489 | /// Each annot_pragma_unused is followed by the argument token so e.g. |
| 490 | /// "#pragma unused(x,y)" becomes: |
| 491 | /// annot_pragma_unused 'x' annot_pragma_unused 'y' |
| 492 | void Parser::HandlePragmaUnused() { |
| 493 | assert(Tok.is(tok::annot_pragma_unused)); |
Richard Smith | af3b325 | 2017-05-18 19:21:48 +0000 | [diff] [blame] | 494 | SourceLocation UnusedLoc = ConsumeAnnotationToken(); |
Argyrios Kyrtzidis | ee56962 | 2011-01-17 18:58:44 +0000 | [diff] [blame] | 495 | Actions.ActOnPragmaUnused(Tok, getCurScope(), UnusedLoc); |
| 496 | ConsumeToken(); // The argument token. |
| 497 | } |
Eli Friedman | 570024a | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 498 | |
Rafael Espindola | 273fd77 | 2012-01-26 02:02:57 +0000 | [diff] [blame] | 499 | void Parser::HandlePragmaVisibility() { |
| 500 | assert(Tok.is(tok::annot_pragma_vis)); |
| 501 | const IdentifierInfo *VisType = |
| 502 | static_cast<IdentifierInfo *>(Tok.getAnnotationValue()); |
Richard Smith | af3b325 | 2017-05-18 19:21:48 +0000 | [diff] [blame] | 503 | SourceLocation VisLoc = ConsumeAnnotationToken(); |
Rafael Espindola | 273fd77 | 2012-01-26 02:02:57 +0000 | [diff] [blame] | 504 | Actions.ActOnPragmaVisibility(VisType, VisLoc); |
| 505 | } |
| 506 | |
Benjamin Kramer | e003ca2 | 2015-10-28 13:54:16 +0000 | [diff] [blame] | 507 | namespace { |
Eli Friedman | ec52f92 | 2012-02-23 23:47:16 +0000 | [diff] [blame] | 508 | struct PragmaPackInfo { |
Denis Zobnin | 10c4f45 | 2016-04-29 18:17:40 +0000 | [diff] [blame] | 509 | Sema::PragmaMsStackAction Action; |
| 510 | StringRef SlotLabel; |
Eli Friedman | 68be164 | 2012-10-04 02:36:51 +0000 | [diff] [blame] | 511 | Token Alignment; |
Eli Friedman | ec52f92 | 2012-02-23 23:47:16 +0000 | [diff] [blame] | 512 | }; |
Benjamin Kramer | e003ca2 | 2015-10-28 13:54:16 +0000 | [diff] [blame] | 513 | } // end anonymous namespace |
Eli Friedman | ec52f92 | 2012-02-23 23:47:16 +0000 | [diff] [blame] | 514 | |
| 515 | void Parser::HandlePragmaPack() { |
| 516 | assert(Tok.is(tok::annot_pragma_pack)); |
| 517 | PragmaPackInfo *Info = |
| 518 | static_cast<PragmaPackInfo *>(Tok.getAnnotationValue()); |
Alex Lorenz | 45b4014 | 2017-07-28 14:41:21 +0000 | [diff] [blame] | 519 | SourceLocation PragmaLoc = Tok.getLocation(); |
Eli Friedman | 68be164 | 2012-10-04 02:36:51 +0000 | [diff] [blame] | 520 | ExprResult Alignment; |
| 521 | if (Info->Alignment.is(tok::numeric_constant)) { |
| 522 | Alignment = Actions.ActOnNumericConstant(Info->Alignment); |
Alex Lorenz | 45b4014 | 2017-07-28 14:41:21 +0000 | [diff] [blame] | 523 | if (Alignment.isInvalid()) { |
| 524 | ConsumeAnnotationToken(); |
Eli Friedman | 68be164 | 2012-10-04 02:36:51 +0000 | [diff] [blame] | 525 | return; |
Alex Lorenz | 45b4014 | 2017-07-28 14:41:21 +0000 | [diff] [blame] | 526 | } |
Eli Friedman | 68be164 | 2012-10-04 02:36:51 +0000 | [diff] [blame] | 527 | } |
Denis Zobnin | 10c4f45 | 2016-04-29 18:17:40 +0000 | [diff] [blame] | 528 | Actions.ActOnPragmaPack(PragmaLoc, Info->Action, Info->SlotLabel, |
| 529 | Alignment.get()); |
Alex Lorenz | 45b4014 | 2017-07-28 14:41:21 +0000 | [diff] [blame] | 530 | // Consume the token after processing the pragma to enable pragma-specific |
| 531 | // #include warnings. |
| 532 | ConsumeAnnotationToken(); |
Eli Friedman | ec52f92 | 2012-02-23 23:47:16 +0000 | [diff] [blame] | 533 | } |
| 534 | |
Eli Friedman | 68be164 | 2012-10-04 02:36:51 +0000 | [diff] [blame] | 535 | void Parser::HandlePragmaMSStruct() { |
| 536 | assert(Tok.is(tok::annot_pragma_msstruct)); |
Nico Weber | 779355f | 2016-03-02 23:22:00 +0000 | [diff] [blame] | 537 | PragmaMSStructKind Kind = static_cast<PragmaMSStructKind>( |
| 538 | reinterpret_cast<uintptr_t>(Tok.getAnnotationValue())); |
Eli Friedman | 68be164 | 2012-10-04 02:36:51 +0000 | [diff] [blame] | 539 | Actions.ActOnPragmaMSStruct(Kind); |
Richard Smith | af3b325 | 2017-05-18 19:21:48 +0000 | [diff] [blame] | 540 | ConsumeAnnotationToken(); |
Eli Friedman | 68be164 | 2012-10-04 02:36:51 +0000 | [diff] [blame] | 541 | } |
| 542 | |
| 543 | void Parser::HandlePragmaAlign() { |
| 544 | assert(Tok.is(tok::annot_pragma_align)); |
| 545 | Sema::PragmaOptionsAlignKind Kind = |
| 546 | static_cast<Sema::PragmaOptionsAlignKind>( |
| 547 | reinterpret_cast<uintptr_t>(Tok.getAnnotationValue())); |
Alex Lorenz | 692821a | 2018-02-08 21:20:43 +0000 | [diff] [blame] | 548 | Actions.ActOnPragmaOptionsAlign(Kind, Tok.getLocation()); |
| 549 | // Consume the token after processing the pragma to enable pragma-specific |
| 550 | // #include warnings. |
| 551 | ConsumeAnnotationToken(); |
Eli Friedman | 68be164 | 2012-10-04 02:36:51 +0000 | [diff] [blame] | 552 | } |
| 553 | |
Richard Smith | ba3a4f9 | 2016-01-12 21:59:26 +0000 | [diff] [blame] | 554 | void Parser::HandlePragmaDump() { |
| 555 | assert(Tok.is(tok::annot_pragma_dump)); |
| 556 | IdentifierInfo *II = |
| 557 | reinterpret_cast<IdentifierInfo *>(Tok.getAnnotationValue()); |
| 558 | Actions.ActOnPragmaDump(getCurScope(), Tok.getLocation(), II); |
Richard Smith | af3b325 | 2017-05-18 19:21:48 +0000 | [diff] [blame] | 559 | ConsumeAnnotationToken(); |
Richard Smith | ba3a4f9 | 2016-01-12 21:59:26 +0000 | [diff] [blame] | 560 | } |
| 561 | |
Eli Friedman | 68be164 | 2012-10-04 02:36:51 +0000 | [diff] [blame] | 562 | void Parser::HandlePragmaWeak() { |
| 563 | assert(Tok.is(tok::annot_pragma_weak)); |
Richard Smith | af3b325 | 2017-05-18 19:21:48 +0000 | [diff] [blame] | 564 | SourceLocation PragmaLoc = ConsumeAnnotationToken(); |
Eli Friedman | 68be164 | 2012-10-04 02:36:51 +0000 | [diff] [blame] | 565 | Actions.ActOnPragmaWeakID(Tok.getIdentifierInfo(), PragmaLoc, |
| 566 | Tok.getLocation()); |
| 567 | ConsumeToken(); // The weak name. |
| 568 | } |
| 569 | |
| 570 | void Parser::HandlePragmaWeakAlias() { |
| 571 | assert(Tok.is(tok::annot_pragma_weakalias)); |
Richard Smith | af3b325 | 2017-05-18 19:21:48 +0000 | [diff] [blame] | 572 | SourceLocation PragmaLoc = ConsumeAnnotationToken(); |
Eli Friedman | 68be164 | 2012-10-04 02:36:51 +0000 | [diff] [blame] | 573 | IdentifierInfo *WeakName = Tok.getIdentifierInfo(); |
| 574 | SourceLocation WeakNameLoc = Tok.getLocation(); |
| 575 | ConsumeToken(); |
| 576 | IdentifierInfo *AliasName = Tok.getIdentifierInfo(); |
| 577 | SourceLocation AliasNameLoc = Tok.getLocation(); |
| 578 | ConsumeToken(); |
| 579 | Actions.ActOnPragmaWeakAlias(WeakName, AliasName, PragmaLoc, |
| 580 | WeakNameLoc, AliasNameLoc); |
| 581 | |
| 582 | } |
| 583 | |
| 584 | void Parser::HandlePragmaRedefineExtname() { |
| 585 | assert(Tok.is(tok::annot_pragma_redefine_extname)); |
Richard Smith | af3b325 | 2017-05-18 19:21:48 +0000 | [diff] [blame] | 586 | SourceLocation RedefLoc = ConsumeAnnotationToken(); |
Eli Friedman | 68be164 | 2012-10-04 02:36:51 +0000 | [diff] [blame] | 587 | IdentifierInfo *RedefName = Tok.getIdentifierInfo(); |
| 588 | SourceLocation RedefNameLoc = Tok.getLocation(); |
| 589 | ConsumeToken(); |
| 590 | IdentifierInfo *AliasName = Tok.getIdentifierInfo(); |
| 591 | SourceLocation AliasNameLoc = Tok.getLocation(); |
| 592 | ConsumeToken(); |
| 593 | Actions.ActOnPragmaRedefineExtname(RedefName, AliasName, RedefLoc, |
| 594 | RedefNameLoc, AliasNameLoc); |
| 595 | } |
| 596 | |
| 597 | void Parser::HandlePragmaFPContract() { |
| 598 | assert(Tok.is(tok::annot_pragma_fp_contract)); |
| 599 | tok::OnOffSwitch OOS = |
| 600 | static_cast<tok::OnOffSwitch>( |
| 601 | reinterpret_cast<uintptr_t>(Tok.getAnnotationValue())); |
Adam Nemet | 60d3264 | 2017-04-04 21:18:36 +0000 | [diff] [blame] | 602 | |
| 603 | LangOptions::FPContractModeKind FPC; |
| 604 | switch (OOS) { |
| 605 | case tok::OOS_ON: |
| 606 | FPC = LangOptions::FPC_On; |
| 607 | break; |
| 608 | case tok::OOS_OFF: |
| 609 | FPC = LangOptions::FPC_Off; |
| 610 | break; |
| 611 | case tok::OOS_DEFAULT: |
| 612 | FPC = getLangOpts().getDefaultFPContractMode(); |
| 613 | break; |
| 614 | } |
| 615 | |
| 616 | Actions.ActOnPragmaFPContract(FPC); |
Richard Smith | af3b325 | 2017-05-18 19:21:48 +0000 | [diff] [blame] | 617 | ConsumeAnnotationToken(); |
Eli Friedman | 68be164 | 2012-10-04 02:36:51 +0000 | [diff] [blame] | 618 | } |
| 619 | |
Kevin P. Neal | 2c0bc8b | 2018-08-14 17:06:56 +0000 | [diff] [blame] | 620 | void Parser::HandlePragmaFEnvAccess() { |
| 621 | assert(Tok.is(tok::annot_pragma_fenv_access)); |
| 622 | tok::OnOffSwitch OOS = |
| 623 | static_cast<tok::OnOffSwitch>( |
| 624 | reinterpret_cast<uintptr_t>(Tok.getAnnotationValue())); |
| 625 | |
| 626 | LangOptions::FEnvAccessModeKind FPC; |
| 627 | switch (OOS) { |
| 628 | case tok::OOS_ON: |
| 629 | FPC = LangOptions::FEA_On; |
| 630 | break; |
| 631 | case tok::OOS_OFF: |
| 632 | FPC = LangOptions::FEA_Off; |
| 633 | break; |
| 634 | case tok::OOS_DEFAULT: // FIXME: Add this cli option when it makes sense. |
| 635 | FPC = LangOptions::FEA_Off; |
| 636 | break; |
| 637 | } |
| 638 | |
| 639 | Actions.ActOnPragmaFEnvAccess(FPC); |
| 640 | ConsumeAnnotationToken(); |
| 641 | } |
| 642 | |
| 643 | |
Tareq A. Siraj | 0de0dd4 | 2013-04-16 18:41:26 +0000 | [diff] [blame] | 644 | StmtResult Parser::HandlePragmaCaptured() |
| 645 | { |
| 646 | assert(Tok.is(tok::annot_pragma_captured)); |
Richard Smith | af3b325 | 2017-05-18 19:21:48 +0000 | [diff] [blame] | 647 | ConsumeAnnotationToken(); |
Tareq A. Siraj | 0de0dd4 | 2013-04-16 18:41:26 +0000 | [diff] [blame] | 648 | |
| 649 | if (Tok.isNot(tok::l_brace)) { |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 650 | PP.Diag(Tok, diag::err_expected) << tok::l_brace; |
Tareq A. Siraj | 0de0dd4 | 2013-04-16 18:41:26 +0000 | [diff] [blame] | 651 | return StmtError(); |
| 652 | } |
| 653 | |
Tareq A. Siraj | 6dfa25a | 2013-04-16 19:37:38 +0000 | [diff] [blame] | 654 | SourceLocation Loc = Tok.getLocation(); |
| 655 | |
Momchil Velikov | 57c681f | 2017-08-10 15:43:06 +0000 | [diff] [blame] | 656 | ParseScope CapturedRegionScope(this, Scope::FnScope | Scope::DeclScope | |
| 657 | Scope::CompoundStmtScope); |
Ben Langmuir | 37943a7 | 2013-05-03 19:00:33 +0000 | [diff] [blame] | 658 | Actions.ActOnCapturedRegionStart(Loc, getCurScope(), CR_Default, |
| 659 | /*NumParams=*/1); |
Tareq A. Siraj | 6dfa25a | 2013-04-16 19:37:38 +0000 | [diff] [blame] | 660 | |
| 661 | StmtResult R = ParseCompoundStatement(); |
| 662 | CapturedRegionScope.Exit(); |
| 663 | |
| 664 | if (R.isInvalid()) { |
| 665 | Actions.ActOnCapturedRegionError(); |
| 666 | return StmtError(); |
| 667 | } |
| 668 | |
| 669 | return Actions.ActOnCapturedRegionEnd(R.get()); |
Tareq A. Siraj | 0de0dd4 | 2013-04-16 18:41:26 +0000 | [diff] [blame] | 670 | } |
| 671 | |
Eli Friedman | 68be164 | 2012-10-04 02:36:51 +0000 | [diff] [blame] | 672 | namespace { |
Yaxun Liu | 5b74665 | 2016-12-18 05:18:55 +0000 | [diff] [blame] | 673 | enum OpenCLExtState : char { |
| 674 | Disable, Enable, Begin, End |
| 675 | }; |
| 676 | typedef std::pair<const IdentifierInfo *, OpenCLExtState> OpenCLExtData; |
Eli Friedman | 68be164 | 2012-10-04 02:36:51 +0000 | [diff] [blame] | 677 | } |
| 678 | |
| 679 | void Parser::HandlePragmaOpenCLExtension() { |
| 680 | assert(Tok.is(tok::annot_pragma_opencl_extension)); |
Yaxun Liu | 5b74665 | 2016-12-18 05:18:55 +0000 | [diff] [blame] | 681 | OpenCLExtData *Data = static_cast<OpenCLExtData*>(Tok.getAnnotationValue()); |
| 682 | auto State = Data->second; |
| 683 | auto Ident = Data->first; |
Eli Friedman | 68be164 | 2012-10-04 02:36:51 +0000 | [diff] [blame] | 684 | SourceLocation NameLoc = Tok.getLocation(); |
Richard Smith | af3b325 | 2017-05-18 19:21:48 +0000 | [diff] [blame] | 685 | ConsumeAnnotationToken(); |
Eli Friedman | 68be164 | 2012-10-04 02:36:51 +0000 | [diff] [blame] | 686 | |
Yaxun Liu | 5b74665 | 2016-12-18 05:18:55 +0000 | [diff] [blame] | 687 | auto &Opt = Actions.getOpenCLOptions(); |
| 688 | auto Name = Ident->getName(); |
Eli Friedman | 68be164 | 2012-10-04 02:36:51 +0000 | [diff] [blame] | 689 | // OpenCL 1.1 9.1: "The all variant sets the behavior for all extensions, |
| 690 | // overriding all previously issued extension directives, but only if the |
| 691 | // behavior is set to disable." |
Yaxun Liu | 5b74665 | 2016-12-18 05:18:55 +0000 | [diff] [blame] | 692 | if (Name == "all") { |
Konstantin Zhuravlyov | de70a88 | 2017-01-06 16:14:41 +0000 | [diff] [blame] | 693 | if (State == Disable) { |
Yaxun Liu | 5b74665 | 2016-12-18 05:18:55 +0000 | [diff] [blame] | 694 | Opt.disableAll(); |
Konstantin Zhuravlyov | de70a88 | 2017-01-06 16:14:41 +0000 | [diff] [blame] | 695 | Opt.enableSupportedCore(getLangOpts().OpenCLVersion); |
| 696 | } else { |
Yaxun Liu | 5b74665 | 2016-12-18 05:18:55 +0000 | [diff] [blame] | 697 | PP.Diag(NameLoc, diag::warn_pragma_expected_predicate) << 1; |
Konstantin Zhuravlyov | de70a88 | 2017-01-06 16:14:41 +0000 | [diff] [blame] | 698 | } |
Yaxun Liu | 5b74665 | 2016-12-18 05:18:55 +0000 | [diff] [blame] | 699 | } else if (State == Begin) { |
| 700 | if (!Opt.isKnown(Name) || |
| 701 | !Opt.isSupported(Name, getLangOpts().OpenCLVersion)) { |
| 702 | Opt.support(Name); |
| 703 | } |
| 704 | Actions.setCurrentOpenCLExtension(Name); |
| 705 | } else if (State == End) { |
| 706 | if (Name != Actions.getCurrentOpenCLExtension()) |
| 707 | PP.Diag(NameLoc, diag::warn_pragma_begin_end_mismatch); |
| 708 | Actions.setCurrentOpenCLExtension(""); |
| 709 | } else if (!Opt.isKnown(Name)) |
| 710 | PP.Diag(NameLoc, diag::warn_pragma_unknown_extension) << Ident; |
| 711 | else if (Opt.isSupportedExtension(Name, getLangOpts().OpenCLVersion)) |
| 712 | Opt.enable(Name, State == Enable); |
| 713 | else if (Opt.isSupportedCore(Name, getLangOpts().OpenCLVersion)) |
| 714 | PP.Diag(NameLoc, diag::warn_pragma_extension_is_core) << Ident; |
| 715 | else |
| 716 | PP.Diag(NameLoc, diag::warn_pragma_unsupported_extension) << Ident; |
Eli Friedman | 68be164 | 2012-10-04 02:36:51 +0000 | [diff] [blame] | 717 | } |
| 718 | |
David Majnemer | 4bb0980 | 2014-02-10 19:50:15 +0000 | [diff] [blame] | 719 | void Parser::HandlePragmaMSPointersToMembers() { |
| 720 | assert(Tok.is(tok::annot_pragma_ms_pointers_to_members)); |
David Majnemer | 86c318f | 2014-02-11 21:05:00 +0000 | [diff] [blame] | 721 | LangOptions::PragmaMSPointersToMembersKind RepresentationMethod = |
| 722 | static_cast<LangOptions::PragmaMSPointersToMembersKind>( |
David Majnemer | 4bb0980 | 2014-02-10 19:50:15 +0000 | [diff] [blame] | 723 | reinterpret_cast<uintptr_t>(Tok.getAnnotationValue())); |
Richard Smith | af3b325 | 2017-05-18 19:21:48 +0000 | [diff] [blame] | 724 | SourceLocation PragmaLoc = ConsumeAnnotationToken(); |
David Majnemer | 4bb0980 | 2014-02-10 19:50:15 +0000 | [diff] [blame] | 725 | Actions.ActOnPragmaMSPointersToMembers(RepresentationMethod, PragmaLoc); |
| 726 | } |
Tareq A. Siraj | 0de0dd4 | 2013-04-16 18:41:26 +0000 | [diff] [blame] | 727 | |
Reid Kleckner | c0dca6d | 2014-02-12 23:50:26 +0000 | [diff] [blame] | 728 | void Parser::HandlePragmaMSVtorDisp() { |
| 729 | assert(Tok.is(tok::annot_pragma_ms_vtordisp)); |
| 730 | uintptr_t Value = reinterpret_cast<uintptr_t>(Tok.getAnnotationValue()); |
Denis Zobnin | 2290dac | 2016-04-29 11:27:00 +0000 | [diff] [blame] | 731 | Sema::PragmaMsStackAction Action = |
| 732 | static_cast<Sema::PragmaMsStackAction>((Value >> 16) & 0xFFFF); |
Reid Kleckner | c0dca6d | 2014-02-12 23:50:26 +0000 | [diff] [blame] | 733 | MSVtorDispAttr::Mode Mode = MSVtorDispAttr::Mode(Value & 0xFFFF); |
Richard Smith | af3b325 | 2017-05-18 19:21:48 +0000 | [diff] [blame] | 734 | SourceLocation PragmaLoc = ConsumeAnnotationToken(); |
Denis Zobnin | 2290dac | 2016-04-29 11:27:00 +0000 | [diff] [blame] | 735 | Actions.ActOnPragmaMSVtorDisp(Action, PragmaLoc, Mode); |
Reid Kleckner | c0dca6d | 2014-02-12 23:50:26 +0000 | [diff] [blame] | 736 | } |
Tareq A. Siraj | 0de0dd4 | 2013-04-16 18:41:26 +0000 | [diff] [blame] | 737 | |
Warren Hunt | c3b1896 | 2014-04-08 22:30:47 +0000 | [diff] [blame] | 738 | void Parser::HandlePragmaMSPragma() { |
| 739 | assert(Tok.is(tok::annot_pragma_ms_pragma)); |
| 740 | // Grab the tokens out of the annotation and enter them into the stream. |
David Blaikie | 2eabcc9 | 2016-02-09 18:52:09 +0000 | [diff] [blame] | 741 | auto TheTokens = |
| 742 | (std::pair<std::unique_ptr<Token[]>, size_t> *)Tok.getAnnotationValue(); |
| 743 | PP.EnterTokenStream(std::move(TheTokens->first), TheTokens->second, true); |
Richard Smith | af3b325 | 2017-05-18 19:21:48 +0000 | [diff] [blame] | 744 | SourceLocation PragmaLocation = ConsumeAnnotationToken(); |
Warren Hunt | c3b1896 | 2014-04-08 22:30:47 +0000 | [diff] [blame] | 745 | assert(Tok.isAnyIdentifier()); |
Reid Kleckner | 722b1df | 2014-07-18 00:13:16 +0000 | [diff] [blame] | 746 | StringRef PragmaName = Tok.getIdentifierInfo()->getName(); |
Warren Hunt | c3b1896 | 2014-04-08 22:30:47 +0000 | [diff] [blame] | 747 | PP.Lex(Tok); // pragma kind |
Reid Kleckner | 722b1df | 2014-07-18 00:13:16 +0000 | [diff] [blame] | 748 | |
Warren Hunt | c3b1896 | 2014-04-08 22:30:47 +0000 | [diff] [blame] | 749 | // Figure out which #pragma we're dealing with. The switch has no default |
| 750 | // because lex shouldn't emit the annotation token for unrecognized pragmas. |
Reid Kleckner | 722b1df | 2014-07-18 00:13:16 +0000 | [diff] [blame] | 751 | typedef bool (Parser::*PragmaHandler)(StringRef, SourceLocation); |
Warren Hunt | c3b1896 | 2014-04-08 22:30:47 +0000 | [diff] [blame] | 752 | PragmaHandler Handler = llvm::StringSwitch<PragmaHandler>(PragmaName) |
| 753 | .Case("data_seg", &Parser::HandlePragmaMSSegment) |
| 754 | .Case("bss_seg", &Parser::HandlePragmaMSSegment) |
| 755 | .Case("const_seg", &Parser::HandlePragmaMSSegment) |
| 756 | .Case("code_seg", &Parser::HandlePragmaMSSegment) |
| 757 | .Case("section", &Parser::HandlePragmaMSSection) |
| 758 | .Case("init_seg", &Parser::HandlePragmaMSInitSeg); |
Reid Kleckner | 722b1df | 2014-07-18 00:13:16 +0000 | [diff] [blame] | 759 | |
| 760 | if (!(this->*Handler)(PragmaName, PragmaLocation)) { |
| 761 | // Pragma handling failed, and has been diagnosed. Slurp up the tokens |
| 762 | // until eof (really end of line) to prevent follow-on errors. |
Warren Hunt | c3b1896 | 2014-04-08 22:30:47 +0000 | [diff] [blame] | 763 | while (Tok.isNot(tok::eof)) |
| 764 | PP.Lex(Tok); |
| 765 | PP.Lex(Tok); |
| 766 | } |
| 767 | } |
| 768 | |
Reid Kleckner | 722b1df | 2014-07-18 00:13:16 +0000 | [diff] [blame] | 769 | bool Parser::HandlePragmaMSSection(StringRef PragmaName, |
| 770 | SourceLocation PragmaLocation) { |
| 771 | if (Tok.isNot(tok::l_paren)) { |
| 772 | PP.Diag(PragmaLocation, diag::warn_pragma_expected_lparen) << PragmaName; |
| 773 | return false; |
| 774 | } |
Warren Hunt | c3b1896 | 2014-04-08 22:30:47 +0000 | [diff] [blame] | 775 | PP.Lex(Tok); // ( |
| 776 | // Parsing code for pragma section |
Reid Kleckner | 722b1df | 2014-07-18 00:13:16 +0000 | [diff] [blame] | 777 | if (Tok.isNot(tok::string_literal)) { |
| 778 | PP.Diag(PragmaLocation, diag::warn_pragma_expected_section_name) |
| 779 | << PragmaName; |
| 780 | return false; |
| 781 | } |
| 782 | ExprResult StringResult = ParseStringLiteralExpression(); |
| 783 | if (StringResult.isInvalid()) |
| 784 | return false; // Already diagnosed. |
| 785 | StringLiteral *SegmentName = cast<StringLiteral>(StringResult.get()); |
| 786 | if (SegmentName->getCharByteWidth() != 1) { |
| 787 | PP.Diag(PragmaLocation, diag::warn_pragma_expected_non_wide_string) |
| 788 | << PragmaName; |
| 789 | return false; |
| 790 | } |
David Majnemer | 48c28fa | 2014-10-22 21:08:43 +0000 | [diff] [blame] | 791 | int SectionFlags = ASTContext::PSF_Read; |
| 792 | bool SectionFlagsAreDefault = true; |
Warren Hunt | c3b1896 | 2014-04-08 22:30:47 +0000 | [diff] [blame] | 793 | while (Tok.is(tok::comma)) { |
| 794 | PP.Lex(Tok); // , |
David Majnemer | 48c28fa | 2014-10-22 21:08:43 +0000 | [diff] [blame] | 795 | // Ignore "long" and "short". |
| 796 | // They are undocumented, but widely used, section attributes which appear |
| 797 | // to do nothing. |
| 798 | if (Tok.is(tok::kw_long) || Tok.is(tok::kw_short)) { |
| 799 | PP.Lex(Tok); // long/short |
| 800 | continue; |
| 801 | } |
| 802 | |
Reid Kleckner | 722b1df | 2014-07-18 00:13:16 +0000 | [diff] [blame] | 803 | if (!Tok.isAnyIdentifier()) { |
| 804 | PP.Diag(PragmaLocation, diag::warn_pragma_expected_action_or_r_paren) |
| 805 | << PragmaName; |
| 806 | return false; |
| 807 | } |
Hans Wennborg | 899ded9 | 2014-10-16 20:52:46 +0000 | [diff] [blame] | 808 | ASTContext::PragmaSectionFlag Flag = |
| 809 | llvm::StringSwitch<ASTContext::PragmaSectionFlag>( |
Warren Hunt | c3b1896 | 2014-04-08 22:30:47 +0000 | [diff] [blame] | 810 | Tok.getIdentifierInfo()->getName()) |
Hans Wennborg | 899ded9 | 2014-10-16 20:52:46 +0000 | [diff] [blame] | 811 | .Case("read", ASTContext::PSF_Read) |
| 812 | .Case("write", ASTContext::PSF_Write) |
| 813 | .Case("execute", ASTContext::PSF_Execute) |
| 814 | .Case("shared", ASTContext::PSF_Invalid) |
| 815 | .Case("nopage", ASTContext::PSF_Invalid) |
| 816 | .Case("nocache", ASTContext::PSF_Invalid) |
| 817 | .Case("discard", ASTContext::PSF_Invalid) |
| 818 | .Case("remove", ASTContext::PSF_Invalid) |
| 819 | .Default(ASTContext::PSF_None); |
| 820 | if (Flag == ASTContext::PSF_None || Flag == ASTContext::PSF_Invalid) { |
| 821 | PP.Diag(PragmaLocation, Flag == ASTContext::PSF_None |
Reid Kleckner | 722b1df | 2014-07-18 00:13:16 +0000 | [diff] [blame] | 822 | ? diag::warn_pragma_invalid_specific_action |
| 823 | : diag::warn_pragma_unsupported_action) |
Warren Hunt | c3b1896 | 2014-04-08 22:30:47 +0000 | [diff] [blame] | 824 | << PragmaName << Tok.getIdentifierInfo()->getName(); |
Reid Kleckner | 722b1df | 2014-07-18 00:13:16 +0000 | [diff] [blame] | 825 | return false; |
Warren Hunt | c3b1896 | 2014-04-08 22:30:47 +0000 | [diff] [blame] | 826 | } |
| 827 | SectionFlags |= Flag; |
David Majnemer | 48c28fa | 2014-10-22 21:08:43 +0000 | [diff] [blame] | 828 | SectionFlagsAreDefault = false; |
Warren Hunt | c3b1896 | 2014-04-08 22:30:47 +0000 | [diff] [blame] | 829 | PP.Lex(Tok); // Identifier |
| 830 | } |
David Majnemer | 48c28fa | 2014-10-22 21:08:43 +0000 | [diff] [blame] | 831 | // If no section attributes are specified, the section will be marked as |
| 832 | // read/write. |
| 833 | if (SectionFlagsAreDefault) |
| 834 | SectionFlags |= ASTContext::PSF_Write; |
Reid Kleckner | 722b1df | 2014-07-18 00:13:16 +0000 | [diff] [blame] | 835 | if (Tok.isNot(tok::r_paren)) { |
| 836 | PP.Diag(PragmaLocation, diag::warn_pragma_expected_rparen) << PragmaName; |
| 837 | return false; |
| 838 | } |
Warren Hunt | c3b1896 | 2014-04-08 22:30:47 +0000 | [diff] [blame] | 839 | PP.Lex(Tok); // ) |
Reid Kleckner | 722b1df | 2014-07-18 00:13:16 +0000 | [diff] [blame] | 840 | if (Tok.isNot(tok::eof)) { |
| 841 | PP.Diag(PragmaLocation, diag::warn_pragma_extra_tokens_at_eol) |
| 842 | << PragmaName; |
| 843 | return false; |
| 844 | } |
Warren Hunt | c3b1896 | 2014-04-08 22:30:47 +0000 | [diff] [blame] | 845 | PP.Lex(Tok); // eof |
| 846 | Actions.ActOnPragmaMSSection(PragmaLocation, SectionFlags, SegmentName); |
Reid Kleckner | 722b1df | 2014-07-18 00:13:16 +0000 | [diff] [blame] | 847 | return true; |
Warren Hunt | c3b1896 | 2014-04-08 22:30:47 +0000 | [diff] [blame] | 848 | } |
| 849 | |
Reid Kleckner | 722b1df | 2014-07-18 00:13:16 +0000 | [diff] [blame] | 850 | bool Parser::HandlePragmaMSSegment(StringRef PragmaName, |
| 851 | SourceLocation PragmaLocation) { |
| 852 | if (Tok.isNot(tok::l_paren)) { |
| 853 | PP.Diag(PragmaLocation, diag::warn_pragma_expected_lparen) << PragmaName; |
| 854 | return false; |
| 855 | } |
Warren Hunt | c3b1896 | 2014-04-08 22:30:47 +0000 | [diff] [blame] | 856 | PP.Lex(Tok); // ( |
| 857 | Sema::PragmaMsStackAction Action = Sema::PSK_Reset; |
Reid Kleckner | 722b1df | 2014-07-18 00:13:16 +0000 | [diff] [blame] | 858 | StringRef SlotLabel; |
Warren Hunt | c3b1896 | 2014-04-08 22:30:47 +0000 | [diff] [blame] | 859 | if (Tok.isAnyIdentifier()) { |
Reid Kleckner | 722b1df | 2014-07-18 00:13:16 +0000 | [diff] [blame] | 860 | StringRef PushPop = Tok.getIdentifierInfo()->getName(); |
Warren Hunt | c3b1896 | 2014-04-08 22:30:47 +0000 | [diff] [blame] | 861 | if (PushPop == "push") |
| 862 | Action = Sema::PSK_Push; |
| 863 | else if (PushPop == "pop") |
| 864 | Action = Sema::PSK_Pop; |
Reid Kleckner | 722b1df | 2014-07-18 00:13:16 +0000 | [diff] [blame] | 865 | else { |
| 866 | PP.Diag(PragmaLocation, |
| 867 | diag::warn_pragma_expected_section_push_pop_or_name) |
| 868 | << PragmaName; |
| 869 | return false; |
| 870 | } |
Warren Hunt | c3b1896 | 2014-04-08 22:30:47 +0000 | [diff] [blame] | 871 | if (Action != Sema::PSK_Reset) { |
| 872 | PP.Lex(Tok); // push | pop |
| 873 | if (Tok.is(tok::comma)) { |
| 874 | PP.Lex(Tok); // , |
| 875 | // If we've got a comma, we either need a label or a string. |
| 876 | if (Tok.isAnyIdentifier()) { |
| 877 | SlotLabel = Tok.getIdentifierInfo()->getName(); |
| 878 | PP.Lex(Tok); // identifier |
| 879 | if (Tok.is(tok::comma)) |
| 880 | PP.Lex(Tok); |
Reid Kleckner | 722b1df | 2014-07-18 00:13:16 +0000 | [diff] [blame] | 881 | else if (Tok.isNot(tok::r_paren)) { |
| 882 | PP.Diag(PragmaLocation, diag::warn_pragma_expected_punc) |
| 883 | << PragmaName; |
| 884 | return false; |
| 885 | } |
Warren Hunt | c3b1896 | 2014-04-08 22:30:47 +0000 | [diff] [blame] | 886 | } |
Reid Kleckner | 722b1df | 2014-07-18 00:13:16 +0000 | [diff] [blame] | 887 | } else if (Tok.isNot(tok::r_paren)) { |
| 888 | PP.Diag(PragmaLocation, diag::warn_pragma_expected_punc) << PragmaName; |
| 889 | return false; |
| 890 | } |
Warren Hunt | c3b1896 | 2014-04-08 22:30:47 +0000 | [diff] [blame] | 891 | } |
| 892 | } |
| 893 | // Grab the string literal for our section name. |
| 894 | StringLiteral *SegmentName = nullptr; |
| 895 | if (Tok.isNot(tok::r_paren)) { |
Reid Kleckner | 722b1df | 2014-07-18 00:13:16 +0000 | [diff] [blame] | 896 | if (Tok.isNot(tok::string_literal)) { |
| 897 | unsigned DiagID = Action != Sema::PSK_Reset ? !SlotLabel.empty() ? |
Warren Hunt | c3b1896 | 2014-04-08 22:30:47 +0000 | [diff] [blame] | 898 | diag::warn_pragma_expected_section_name : |
| 899 | diag::warn_pragma_expected_section_label_or_name : |
| 900 | diag::warn_pragma_expected_section_push_pop_or_name; |
Reid Kleckner | 722b1df | 2014-07-18 00:13:16 +0000 | [diff] [blame] | 901 | PP.Diag(PragmaLocation, DiagID) << PragmaName; |
| 902 | return false; |
| 903 | } |
| 904 | ExprResult StringResult = ParseStringLiteralExpression(); |
| 905 | if (StringResult.isInvalid()) |
| 906 | return false; // Already diagnosed. |
| 907 | SegmentName = cast<StringLiteral>(StringResult.get()); |
| 908 | if (SegmentName->getCharByteWidth() != 1) { |
| 909 | PP.Diag(PragmaLocation, diag::warn_pragma_expected_non_wide_string) |
| 910 | << PragmaName; |
| 911 | return false; |
| 912 | } |
Warren Hunt | c3b1896 | 2014-04-08 22:30:47 +0000 | [diff] [blame] | 913 | // Setting section "" has no effect |
| 914 | if (SegmentName->getLength()) |
| 915 | Action = (Sema::PragmaMsStackAction)(Action | Sema::PSK_Set); |
| 916 | } |
Reid Kleckner | 722b1df | 2014-07-18 00:13:16 +0000 | [diff] [blame] | 917 | if (Tok.isNot(tok::r_paren)) { |
| 918 | PP.Diag(PragmaLocation, diag::warn_pragma_expected_rparen) << PragmaName; |
| 919 | return false; |
| 920 | } |
Warren Hunt | c3b1896 | 2014-04-08 22:30:47 +0000 | [diff] [blame] | 921 | PP.Lex(Tok); // ) |
Reid Kleckner | 722b1df | 2014-07-18 00:13:16 +0000 | [diff] [blame] | 922 | if (Tok.isNot(tok::eof)) { |
| 923 | PP.Diag(PragmaLocation, diag::warn_pragma_extra_tokens_at_eol) |
| 924 | << PragmaName; |
| 925 | return false; |
| 926 | } |
Warren Hunt | c3b1896 | 2014-04-08 22:30:47 +0000 | [diff] [blame] | 927 | PP.Lex(Tok); // eof |
| 928 | Actions.ActOnPragmaMSSeg(PragmaLocation, Action, SlotLabel, |
| 929 | SegmentName, PragmaName); |
Reid Kleckner | 722b1df | 2014-07-18 00:13:16 +0000 | [diff] [blame] | 930 | return true; |
Warren Hunt | c3b1896 | 2014-04-08 22:30:47 +0000 | [diff] [blame] | 931 | } |
| 932 | |
Reid Kleckner | 1a711b1 | 2014-07-22 00:53:05 +0000 | [diff] [blame] | 933 | // #pragma init_seg({ compiler | lib | user | "section-name" [, func-name]} ) |
Reid Kleckner | 722b1df | 2014-07-18 00:13:16 +0000 | [diff] [blame] | 934 | bool Parser::HandlePragmaMSInitSeg(StringRef PragmaName, |
| 935 | SourceLocation PragmaLocation) { |
David Majnemer | ad2986e | 2014-08-14 06:35:08 +0000 | [diff] [blame] | 936 | if (getTargetInfo().getTriple().getEnvironment() != llvm::Triple::MSVC) { |
| 937 | PP.Diag(PragmaLocation, diag::warn_pragma_init_seg_unsupported_target); |
| 938 | return false; |
| 939 | } |
| 940 | |
Reid Kleckner | 1a711b1 | 2014-07-22 00:53:05 +0000 | [diff] [blame] | 941 | if (ExpectAndConsume(tok::l_paren, diag::warn_pragma_expected_lparen, |
| 942 | PragmaName)) |
| 943 | return false; |
| 944 | |
| 945 | // Parse either the known section names or the string section name. |
| 946 | StringLiteral *SegmentName = nullptr; |
| 947 | if (Tok.isAnyIdentifier()) { |
| 948 | auto *II = Tok.getIdentifierInfo(); |
| 949 | StringRef Section = llvm::StringSwitch<StringRef>(II->getName()) |
| 950 | .Case("compiler", "\".CRT$XCC\"") |
| 951 | .Case("lib", "\".CRT$XCL\"") |
| 952 | .Case("user", "\".CRT$XCU\"") |
| 953 | .Default(""); |
| 954 | |
| 955 | if (!Section.empty()) { |
| 956 | // Pretend the user wrote the appropriate string literal here. |
| 957 | Token Toks[1]; |
| 958 | Toks[0].startToken(); |
| 959 | Toks[0].setKind(tok::string_literal); |
| 960 | Toks[0].setLocation(Tok.getLocation()); |
| 961 | Toks[0].setLiteralData(Section.data()); |
| 962 | Toks[0].setLength(Section.size()); |
| 963 | SegmentName = |
| 964 | cast<StringLiteral>(Actions.ActOnStringLiteral(Toks, nullptr).get()); |
| 965 | PP.Lex(Tok); |
| 966 | } |
| 967 | } else if (Tok.is(tok::string_literal)) { |
| 968 | ExprResult StringResult = ParseStringLiteralExpression(); |
| 969 | if (StringResult.isInvalid()) |
| 970 | return false; |
| 971 | SegmentName = cast<StringLiteral>(StringResult.get()); |
| 972 | if (SegmentName->getCharByteWidth() != 1) { |
| 973 | PP.Diag(PragmaLocation, diag::warn_pragma_expected_non_wide_string) |
| 974 | << PragmaName; |
| 975 | return false; |
| 976 | } |
| 977 | // FIXME: Add support for the '[, func-name]' part of the pragma. |
| 978 | } |
| 979 | |
| 980 | if (!SegmentName) { |
| 981 | PP.Diag(PragmaLocation, diag::warn_pragma_expected_init_seg) << PragmaName; |
| 982 | return false; |
| 983 | } |
| 984 | |
| 985 | if (ExpectAndConsume(tok::r_paren, diag::warn_pragma_expected_rparen, |
| 986 | PragmaName) || |
| 987 | ExpectAndConsume(tok::eof, diag::warn_pragma_extra_tokens_at_eol, |
| 988 | PragmaName)) |
| 989 | return false; |
| 990 | |
| 991 | Actions.ActOnPragmaMSInitSeg(PragmaLocation, SegmentName); |
| 992 | return true; |
Eli Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 993 | } |
| 994 | |
Benjamin Kramer | e003ca2 | 2015-10-28 13:54:16 +0000 | [diff] [blame] | 995 | namespace { |
Eli Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 996 | struct PragmaLoopHintInfo { |
Mark Heffernan | bd26f5e | 2014-07-21 18:08:34 +0000 | [diff] [blame] | 997 | Token PragmaName; |
Eli Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 998 | Token Option; |
Benjamin Kramer | fa7f855 | 2015-08-05 09:39:57 +0000 | [diff] [blame] | 999 | ArrayRef<Token> Toks; |
Eli Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 1000 | }; |
Benjamin Kramer | e003ca2 | 2015-10-28 13:54:16 +0000 | [diff] [blame] | 1001 | } // end anonymous namespace |
Eli Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 1002 | |
Tyler Nowicki | c724a83e | 2014-10-12 20:46:07 +0000 | [diff] [blame] | 1003 | static std::string PragmaLoopHintString(Token PragmaName, Token Option) { |
| 1004 | std::string PragmaString; |
| 1005 | if (PragmaName.getIdentifierInfo()->getName() == "loop") { |
| 1006 | PragmaString = "clang loop "; |
| 1007 | PragmaString += Option.getIdentifierInfo()->getName(); |
David Green | c8e3924 | 2018-08-01 14:36:12 +0000 | [diff] [blame] | 1008 | } else if (PragmaName.getIdentifierInfo()->getName() == "unroll_and_jam") { |
| 1009 | PragmaString = "unroll_and_jam"; |
Tyler Nowicki | c724a83e | 2014-10-12 20:46:07 +0000 | [diff] [blame] | 1010 | } else { |
| 1011 | assert(PragmaName.getIdentifierInfo()->getName() == "unroll" && |
| 1012 | "Unexpected pragma name"); |
| 1013 | PragmaString = "unroll"; |
| 1014 | } |
| 1015 | return PragmaString; |
| 1016 | } |
| 1017 | |
Tyler Nowicki | 0c9b34b | 2014-07-31 20:15:14 +0000 | [diff] [blame] | 1018 | bool Parser::HandlePragmaLoopHint(LoopHint &Hint) { |
Eli Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 1019 | assert(Tok.is(tok::annot_pragma_loop_hint)); |
| 1020 | PragmaLoopHintInfo *Info = |
| 1021 | static_cast<PragmaLoopHintInfo *>(Tok.getAnnotationValue()); |
| 1022 | |
Tyler Nowicki | 0c9b34b | 2014-07-31 20:15:14 +0000 | [diff] [blame] | 1023 | IdentifierInfo *PragmaNameInfo = Info->PragmaName.getIdentifierInfo(); |
| 1024 | Hint.PragmaNameLoc = IdentifierLoc::create( |
| 1025 | Actions.Context, Info->PragmaName.getLocation(), PragmaNameInfo); |
Eli Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 1026 | |
Aaron Ballman | ef940aa | 2014-07-31 21:24:32 +0000 | [diff] [blame] | 1027 | // It is possible that the loop hint has no option identifier, such as |
| 1028 | // #pragma unroll(4). |
| 1029 | IdentifierInfo *OptionInfo = Info->Option.is(tok::identifier) |
| 1030 | ? Info->Option.getIdentifierInfo() |
| 1031 | : nullptr; |
Tyler Nowicki | 0c9b34b | 2014-07-31 20:15:14 +0000 | [diff] [blame] | 1032 | Hint.OptionLoc = IdentifierLoc::create( |
| 1033 | Actions.Context, Info->Option.getLocation(), OptionInfo); |
| 1034 | |
David Blaikie | 2eabcc9 | 2016-02-09 18:52:09 +0000 | [diff] [blame] | 1035 | llvm::ArrayRef<Token> Toks = Info->Toks; |
Tyler Nowicki | c724a83e | 2014-10-12 20:46:07 +0000 | [diff] [blame] | 1036 | |
Tyler Nowicki | 0c9b34b | 2014-07-31 20:15:14 +0000 | [diff] [blame] | 1037 | // Return a valid hint if pragma unroll or nounroll were specified |
| 1038 | // without an argument. |
| 1039 | bool PragmaUnroll = PragmaNameInfo->getName() == "unroll"; |
| 1040 | bool PragmaNoUnroll = PragmaNameInfo->getName() == "nounroll"; |
David Green | c8e3924 | 2018-08-01 14:36:12 +0000 | [diff] [blame] | 1041 | bool PragmaUnrollAndJam = PragmaNameInfo->getName() == "unroll_and_jam"; |
| 1042 | bool PragmaNoUnrollAndJam = PragmaNameInfo->getName() == "nounroll_and_jam"; |
| 1043 | if (Toks.empty() && (PragmaUnroll || PragmaNoUnroll || PragmaUnrollAndJam || |
| 1044 | PragmaNoUnrollAndJam)) { |
Richard Smith | af3b325 | 2017-05-18 19:21:48 +0000 | [diff] [blame] | 1045 | ConsumeAnnotationToken(); |
Tyler Nowicki | 0c9b34b | 2014-07-31 20:15:14 +0000 | [diff] [blame] | 1046 | Hint.Range = Info->PragmaName.getLocation(); |
| 1047 | return true; |
| 1048 | } |
| 1049 | |
Tyler Nowicki | c724a83e | 2014-10-12 20:46:07 +0000 | [diff] [blame] | 1050 | // The constant expression is always followed by an eof token, which increases |
| 1051 | // the TokSize by 1. |
David Blaikie | 2eabcc9 | 2016-02-09 18:52:09 +0000 | [diff] [blame] | 1052 | assert(!Toks.empty() && |
Tyler Nowicki | c724a83e | 2014-10-12 20:46:07 +0000 | [diff] [blame] | 1053 | "PragmaLoopHintInfo::Toks must contain at least one token."); |
| 1054 | |
| 1055 | // If no option is specified the argument is assumed to be a constant expr. |
Tyler Nowicki | 24853c1 | 2015-06-08 23:13:43 +0000 | [diff] [blame] | 1056 | bool OptionUnroll = false; |
David Green | c8e3924 | 2018-08-01 14:36:12 +0000 | [diff] [blame] | 1057 | bool OptionUnrollAndJam = false; |
Adam Nemet | 2de463e | 2016-06-14 12:04:26 +0000 | [diff] [blame] | 1058 | bool OptionDistribute = false; |
Aaron Ballman | 9bdf515 | 2019-01-04 17:20:00 +0000 | [diff] [blame] | 1059 | bool OptionPipelineDisabled = false; |
Tyler Nowicki | 0c9b34b | 2014-07-31 20:15:14 +0000 | [diff] [blame] | 1060 | bool StateOption = false; |
Tyler Nowicki | 24853c1 | 2015-06-08 23:13:43 +0000 | [diff] [blame] | 1061 | if (OptionInfo) { // Pragma Unroll does not specify an option. |
| 1062 | OptionUnroll = OptionInfo->isStr("unroll"); |
David Green | c8e3924 | 2018-08-01 14:36:12 +0000 | [diff] [blame] | 1063 | OptionUnrollAndJam = OptionInfo->isStr("unroll_and_jam"); |
Adam Nemet | 2de463e | 2016-06-14 12:04:26 +0000 | [diff] [blame] | 1064 | OptionDistribute = OptionInfo->isStr("distribute"); |
Aaron Ballman | 9bdf515 | 2019-01-04 17:20:00 +0000 | [diff] [blame] | 1065 | OptionPipelineDisabled = OptionInfo->isStr("pipeline"); |
Tyler Nowicki | 0c9b34b | 2014-07-31 20:15:14 +0000 | [diff] [blame] | 1066 | StateOption = llvm::StringSwitch<bool>(OptionInfo->getName()) |
| 1067 | .Case("vectorize", true) |
| 1068 | .Case("interleave", true) |
Adam Nemet | 2de463e | 2016-06-14 12:04:26 +0000 | [diff] [blame] | 1069 | .Default(false) || |
Aaron Ballman | 9bdf515 | 2019-01-04 17:20:00 +0000 | [diff] [blame] | 1070 | OptionUnroll || OptionUnrollAndJam || OptionDistribute || |
| 1071 | OptionPipelineDisabled; |
Tyler Nowicki | c724a83e | 2014-10-12 20:46:07 +0000 | [diff] [blame] | 1072 | } |
| 1073 | |
Aaron Ballman | 9bdf515 | 2019-01-04 17:20:00 +0000 | [diff] [blame] | 1074 | bool AssumeSafetyArg = !OptionUnroll && !OptionUnrollAndJam && |
| 1075 | !OptionDistribute && !OptionPipelineDisabled; |
Tyler Nowicki | c724a83e | 2014-10-12 20:46:07 +0000 | [diff] [blame] | 1076 | // Verify loop hint has an argument. |
| 1077 | if (Toks[0].is(tok::eof)) { |
Richard Smith | af3b325 | 2017-05-18 19:21:48 +0000 | [diff] [blame] | 1078 | ConsumeAnnotationToken(); |
Tyler Nowicki | c724a83e | 2014-10-12 20:46:07 +0000 | [diff] [blame] | 1079 | Diag(Toks[0].getLocation(), diag::err_pragma_loop_missing_argument) |
David Green | c8e3924 | 2018-08-01 14:36:12 +0000 | [diff] [blame] | 1080 | << /*StateArgument=*/StateOption |
| 1081 | << /*FullKeyword=*/(OptionUnroll || OptionUnrollAndJam) |
Adam Nemet | 2de463e | 2016-06-14 12:04:26 +0000 | [diff] [blame] | 1082 | << /*AssumeSafetyKeyword=*/AssumeSafetyArg; |
Tyler Nowicki | c724a83e | 2014-10-12 20:46:07 +0000 | [diff] [blame] | 1083 | return false; |
| 1084 | } |
Tyler Nowicki | 0c9b34b | 2014-07-31 20:15:14 +0000 | [diff] [blame] | 1085 | |
| 1086 | // Validate the argument. |
| 1087 | if (StateOption) { |
Richard Smith | af3b325 | 2017-05-18 19:21:48 +0000 | [diff] [blame] | 1088 | ConsumeAnnotationToken(); |
Tyler Nowicki | c724a83e | 2014-10-12 20:46:07 +0000 | [diff] [blame] | 1089 | SourceLocation StateLoc = Toks[0].getLocation(); |
| 1090 | IdentifierInfo *StateInfo = Toks[0].getIdentifierInfo(); |
Adam Nemet | 50de4e8 | 2016-04-19 22:17:45 +0000 | [diff] [blame] | 1091 | |
Aaron Ballman | 9bdf515 | 2019-01-04 17:20:00 +0000 | [diff] [blame] | 1092 | bool Valid = StateInfo && |
| 1093 | llvm::StringSwitch<bool>(StateInfo->getName()) |
| 1094 | .Case("disable", true) |
| 1095 | .Case("enable", !OptionPipelineDisabled) |
| 1096 | .Case("full", OptionUnroll || OptionUnrollAndJam) |
| 1097 | .Case("assume_safety", AssumeSafetyArg) |
| 1098 | .Default(false); |
Adam Nemet | 50de4e8 | 2016-04-19 22:17:45 +0000 | [diff] [blame] | 1099 | if (!Valid) { |
Aaron Ballman | 9bdf515 | 2019-01-04 17:20:00 +0000 | [diff] [blame] | 1100 | if (OptionPipelineDisabled) { |
| 1101 | Diag(Toks[0].getLocation(), diag::err_pragma_pipeline_invalid_keyword); |
| 1102 | } else { |
| 1103 | Diag(Toks[0].getLocation(), diag::err_pragma_invalid_keyword) |
| 1104 | << /*FullKeyword=*/(OptionUnroll || OptionUnrollAndJam) |
| 1105 | << /*AssumeSafetyKeyword=*/AssumeSafetyArg; |
| 1106 | } |
Tyler Nowicki | 0c9b34b | 2014-07-31 20:15:14 +0000 | [diff] [blame] | 1107 | return false; |
| 1108 | } |
David Blaikie | 2eabcc9 | 2016-02-09 18:52:09 +0000 | [diff] [blame] | 1109 | if (Toks.size() > 2) |
Tyler Nowicki | c724a83e | 2014-10-12 20:46:07 +0000 | [diff] [blame] | 1110 | Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) |
| 1111 | << PragmaLoopHintString(Info->PragmaName, Info->Option); |
Tyler Nowicki | 0c9b34b | 2014-07-31 20:15:14 +0000 | [diff] [blame] | 1112 | Hint.StateLoc = IdentifierLoc::create(Actions.Context, StateLoc, StateInfo); |
| 1113 | } else { |
Tyler Nowicki | c724a83e | 2014-10-12 20:46:07 +0000 | [diff] [blame] | 1114 | // Enter constant expression including eof terminator into token stream. |
David Blaikie | 2eabcc9 | 2016-02-09 18:52:09 +0000 | [diff] [blame] | 1115 | PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/false); |
Richard Smith | af3b325 | 2017-05-18 19:21:48 +0000 | [diff] [blame] | 1116 | ConsumeAnnotationToken(); |
Tyler Nowicki | c724a83e | 2014-10-12 20:46:07 +0000 | [diff] [blame] | 1117 | |
| 1118 | ExprResult R = ParseConstantExpression(); |
| 1119 | |
| 1120 | // Tokens following an error in an ill-formed constant expression will |
| 1121 | // remain in the token stream and must be removed. |
| 1122 | if (Tok.isNot(tok::eof)) { |
| 1123 | Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) |
| 1124 | << PragmaLoopHintString(Info->PragmaName, Info->Option); |
| 1125 | while (Tok.isNot(tok::eof)) |
| 1126 | ConsumeAnyToken(); |
Tyler Nowicki | 0c9b34b | 2014-07-31 20:15:14 +0000 | [diff] [blame] | 1127 | } |
Tyler Nowicki | c724a83e | 2014-10-12 20:46:07 +0000 | [diff] [blame] | 1128 | |
| 1129 | ConsumeToken(); // Consume the constant expression eof terminator. |
| 1130 | |
| 1131 | if (R.isInvalid() || |
| 1132 | Actions.CheckLoopHintExpr(R.get(), Toks[0].getLocation())) |
| 1133 | return false; |
| 1134 | |
| 1135 | // Argument is a constant expression with an integer type. |
| 1136 | Hint.ValueExpr = R.get(); |
Mark Heffernan | bd26f5e | 2014-07-21 18:08:34 +0000 | [diff] [blame] | 1137 | } |
Eli Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 1138 | |
Tyler Nowicki | c724a83e | 2014-10-12 20:46:07 +0000 | [diff] [blame] | 1139 | Hint.Range = SourceRange(Info->PragmaName.getLocation(), |
David Blaikie | 2eabcc9 | 2016-02-09 18:52:09 +0000 | [diff] [blame] | 1140 | Info->Toks.back().getLocation()); |
Tyler Nowicki | 0c9b34b | 2014-07-31 20:15:14 +0000 | [diff] [blame] | 1141 | return true; |
Eli Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 1142 | } |
| 1143 | |
Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 1144 | namespace { |
| 1145 | struct PragmaAttributeInfo { |
Erik Pilkington | 7d18094 | 2018-10-29 17:38:42 +0000 | [diff] [blame] | 1146 | enum ActionType { Push, Pop, Attribute }; |
Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 1147 | ParsedAttributes &Attributes; |
| 1148 | ActionType Action; |
Erik Pilkington | 0876cae | 2018-12-20 22:32:04 +0000 | [diff] [blame] | 1149 | const IdentifierInfo *Namespace = nullptr; |
Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 1150 | ArrayRef<Token> Tokens; |
| 1151 | |
| 1152 | PragmaAttributeInfo(ParsedAttributes &Attributes) : Attributes(Attributes) {} |
| 1153 | }; |
| 1154 | |
| 1155 | #include "clang/Parse/AttrSubMatchRulesParserStringSwitches.inc" |
| 1156 | |
| 1157 | } // end anonymous namespace |
| 1158 | |
| 1159 | static StringRef getIdentifier(const Token &Tok) { |
| 1160 | if (Tok.is(tok::identifier)) |
| 1161 | return Tok.getIdentifierInfo()->getName(); |
| 1162 | const char *S = tok::getKeywordSpelling(Tok.getKind()); |
| 1163 | if (!S) |
| 1164 | return ""; |
| 1165 | return S; |
| 1166 | } |
| 1167 | |
| 1168 | static bool isAbstractAttrMatcherRule(attr::SubjectMatchRule Rule) { |
| 1169 | using namespace attr; |
| 1170 | switch (Rule) { |
| 1171 | #define ATTR_MATCH_RULE(Value, Spelling, IsAbstract) \ |
| 1172 | case Value: \ |
| 1173 | return IsAbstract; |
| 1174 | #include "clang/Basic/AttrSubMatchRulesList.inc" |
| 1175 | } |
| 1176 | llvm_unreachable("Invalid attribute subject match rule"); |
| 1177 | return false; |
| 1178 | } |
| 1179 | |
| 1180 | static void diagnoseExpectedAttributeSubjectSubRule( |
| 1181 | Parser &PRef, attr::SubjectMatchRule PrimaryRule, StringRef PrimaryRuleName, |
| 1182 | SourceLocation SubRuleLoc) { |
| 1183 | auto Diagnostic = |
| 1184 | PRef.Diag(SubRuleLoc, |
| 1185 | diag::err_pragma_attribute_expected_subject_sub_identifier) |
| 1186 | << PrimaryRuleName; |
| 1187 | if (const char *SubRules = validAttributeSubjectMatchSubRules(PrimaryRule)) |
| 1188 | Diagnostic << /*SubRulesSupported=*/1 << SubRules; |
| 1189 | else |
| 1190 | Diagnostic << /*SubRulesSupported=*/0; |
| 1191 | } |
| 1192 | |
| 1193 | static void diagnoseUnknownAttributeSubjectSubRule( |
| 1194 | Parser &PRef, attr::SubjectMatchRule PrimaryRule, StringRef PrimaryRuleName, |
| 1195 | StringRef SubRuleName, SourceLocation SubRuleLoc) { |
| 1196 | |
| 1197 | auto Diagnostic = |
| 1198 | PRef.Diag(SubRuleLoc, diag::err_pragma_attribute_unknown_subject_sub_rule) |
| 1199 | << SubRuleName << PrimaryRuleName; |
| 1200 | if (const char *SubRules = validAttributeSubjectMatchSubRules(PrimaryRule)) |
| 1201 | Diagnostic << /*SubRulesSupported=*/1 << SubRules; |
| 1202 | else |
| 1203 | Diagnostic << /*SubRulesSupported=*/0; |
| 1204 | } |
| 1205 | |
| 1206 | bool Parser::ParsePragmaAttributeSubjectMatchRuleSet( |
| 1207 | attr::ParsedSubjectMatchRuleSet &SubjectMatchRules, SourceLocation &AnyLoc, |
| 1208 | SourceLocation &LastMatchRuleEndLoc) { |
| 1209 | bool IsAny = false; |
| 1210 | BalancedDelimiterTracker AnyParens(*this, tok::l_paren); |
| 1211 | if (getIdentifier(Tok) == "any") { |
| 1212 | AnyLoc = ConsumeToken(); |
| 1213 | IsAny = true; |
| 1214 | if (AnyParens.expectAndConsume()) |
| 1215 | return true; |
| 1216 | } |
| 1217 | |
| 1218 | do { |
| 1219 | // Parse the subject matcher rule. |
| 1220 | StringRef Name = getIdentifier(Tok); |
| 1221 | if (Name.empty()) { |
| 1222 | Diag(Tok, diag::err_pragma_attribute_expected_subject_identifier); |
| 1223 | return true; |
| 1224 | } |
| 1225 | std::pair<Optional<attr::SubjectMatchRule>, |
| 1226 | Optional<attr::SubjectMatchRule> (*)(StringRef, bool)> |
| 1227 | Rule = isAttributeSubjectMatchRule(Name); |
| 1228 | if (!Rule.first) { |
| 1229 | Diag(Tok, diag::err_pragma_attribute_unknown_subject_rule) << Name; |
| 1230 | return true; |
| 1231 | } |
| 1232 | attr::SubjectMatchRule PrimaryRule = *Rule.first; |
| 1233 | SourceLocation RuleLoc = ConsumeToken(); |
| 1234 | |
| 1235 | BalancedDelimiterTracker Parens(*this, tok::l_paren); |
| 1236 | if (isAbstractAttrMatcherRule(PrimaryRule)) { |
| 1237 | if (Parens.expectAndConsume()) |
| 1238 | return true; |
| 1239 | } else if (Parens.consumeOpen()) { |
| 1240 | if (!SubjectMatchRules |
| 1241 | .insert( |
| 1242 | std::make_pair(PrimaryRule, SourceRange(RuleLoc, RuleLoc))) |
| 1243 | .second) |
| 1244 | Diag(RuleLoc, diag::err_pragma_attribute_duplicate_subject) |
| 1245 | << Name |
| 1246 | << FixItHint::CreateRemoval(SourceRange( |
| 1247 | RuleLoc, Tok.is(tok::comma) ? Tok.getLocation() : RuleLoc)); |
| 1248 | LastMatchRuleEndLoc = RuleLoc; |
| 1249 | continue; |
| 1250 | } |
| 1251 | |
| 1252 | // Parse the sub-rules. |
| 1253 | StringRef SubRuleName = getIdentifier(Tok); |
| 1254 | if (SubRuleName.empty()) { |
| 1255 | diagnoseExpectedAttributeSubjectSubRule(*this, PrimaryRule, Name, |
| 1256 | Tok.getLocation()); |
| 1257 | return true; |
| 1258 | } |
| 1259 | attr::SubjectMatchRule SubRule; |
| 1260 | if (SubRuleName == "unless") { |
| 1261 | SourceLocation SubRuleLoc = ConsumeToken(); |
| 1262 | BalancedDelimiterTracker Parens(*this, tok::l_paren); |
| 1263 | if (Parens.expectAndConsume()) |
| 1264 | return true; |
| 1265 | SubRuleName = getIdentifier(Tok); |
| 1266 | if (SubRuleName.empty()) { |
| 1267 | diagnoseExpectedAttributeSubjectSubRule(*this, PrimaryRule, Name, |
| 1268 | SubRuleLoc); |
| 1269 | return true; |
| 1270 | } |
| 1271 | auto SubRuleOrNone = Rule.second(SubRuleName, /*IsUnless=*/true); |
| 1272 | if (!SubRuleOrNone) { |
| 1273 | std::string SubRuleUnlessName = "unless(" + SubRuleName.str() + ")"; |
| 1274 | diagnoseUnknownAttributeSubjectSubRule(*this, PrimaryRule, Name, |
| 1275 | SubRuleUnlessName, SubRuleLoc); |
| 1276 | return true; |
| 1277 | } |
| 1278 | SubRule = *SubRuleOrNone; |
| 1279 | ConsumeToken(); |
| 1280 | if (Parens.consumeClose()) |
| 1281 | return true; |
| 1282 | } else { |
| 1283 | auto SubRuleOrNone = Rule.second(SubRuleName, /*IsUnless=*/false); |
| 1284 | if (!SubRuleOrNone) { |
| 1285 | diagnoseUnknownAttributeSubjectSubRule(*this, PrimaryRule, Name, |
| 1286 | SubRuleName, Tok.getLocation()); |
| 1287 | return true; |
| 1288 | } |
| 1289 | SubRule = *SubRuleOrNone; |
| 1290 | ConsumeToken(); |
| 1291 | } |
| 1292 | SourceLocation RuleEndLoc = Tok.getLocation(); |
| 1293 | LastMatchRuleEndLoc = RuleEndLoc; |
| 1294 | if (Parens.consumeClose()) |
| 1295 | return true; |
| 1296 | if (!SubjectMatchRules |
| 1297 | .insert(std::make_pair(SubRule, SourceRange(RuleLoc, RuleEndLoc))) |
| 1298 | .second) { |
| 1299 | Diag(RuleLoc, diag::err_pragma_attribute_duplicate_subject) |
| 1300 | << attr::getSubjectMatchRuleSpelling(SubRule) |
| 1301 | << FixItHint::CreateRemoval(SourceRange( |
| 1302 | RuleLoc, Tok.is(tok::comma) ? Tok.getLocation() : RuleEndLoc)); |
| 1303 | continue; |
| 1304 | } |
| 1305 | } while (IsAny && TryConsumeToken(tok::comma)); |
| 1306 | |
| 1307 | if (IsAny) |
| 1308 | if (AnyParens.consumeClose()) |
| 1309 | return true; |
| 1310 | |
| 1311 | return false; |
| 1312 | } |
| 1313 | |
| 1314 | namespace { |
| 1315 | |
Alexander Kornienko | 2a8c18d | 2018-04-06 15:14:32 +0000 | [diff] [blame] | 1316 | /// Describes the stage at which attribute subject rule parsing was interrupted. |
Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 1317 | enum class MissingAttributeSubjectRulesRecoveryPoint { |
| 1318 | Comma, |
| 1319 | ApplyTo, |
| 1320 | Equals, |
| 1321 | Any, |
| 1322 | None, |
| 1323 | }; |
| 1324 | |
| 1325 | MissingAttributeSubjectRulesRecoveryPoint |
| 1326 | getAttributeSubjectRulesRecoveryPointForToken(const Token &Tok) { |
| 1327 | if (const auto *II = Tok.getIdentifierInfo()) { |
| 1328 | if (II->isStr("apply_to")) |
| 1329 | return MissingAttributeSubjectRulesRecoveryPoint::ApplyTo; |
| 1330 | if (II->isStr("any")) |
| 1331 | return MissingAttributeSubjectRulesRecoveryPoint::Any; |
| 1332 | } |
| 1333 | if (Tok.is(tok::equal)) |
| 1334 | return MissingAttributeSubjectRulesRecoveryPoint::Equals; |
| 1335 | return MissingAttributeSubjectRulesRecoveryPoint::None; |
| 1336 | } |
| 1337 | |
| 1338 | /// Creates a diagnostic for the attribute subject rule parsing diagnostic that |
| 1339 | /// suggests the possible attribute subject rules in a fix-it together with |
| 1340 | /// any other missing tokens. |
| 1341 | DiagnosticBuilder createExpectedAttributeSubjectRulesTokenDiagnostic( |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1342 | unsigned DiagID, ParsedAttr &Attribute, |
Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 1343 | MissingAttributeSubjectRulesRecoveryPoint Point, Parser &PRef) { |
| 1344 | SourceLocation Loc = PRef.getEndOfPreviousToken(); |
| 1345 | if (Loc.isInvalid()) |
| 1346 | Loc = PRef.getCurToken().getLocation(); |
| 1347 | auto Diagnostic = PRef.Diag(Loc, DiagID); |
| 1348 | std::string FixIt; |
| 1349 | MissingAttributeSubjectRulesRecoveryPoint EndPoint = |
| 1350 | getAttributeSubjectRulesRecoveryPointForToken(PRef.getCurToken()); |
| 1351 | if (Point == MissingAttributeSubjectRulesRecoveryPoint::Comma) |
| 1352 | FixIt = ", "; |
| 1353 | if (Point <= MissingAttributeSubjectRulesRecoveryPoint::ApplyTo && |
| 1354 | EndPoint > MissingAttributeSubjectRulesRecoveryPoint::ApplyTo) |
| 1355 | FixIt += "apply_to"; |
| 1356 | if (Point <= MissingAttributeSubjectRulesRecoveryPoint::Equals && |
| 1357 | EndPoint > MissingAttributeSubjectRulesRecoveryPoint::Equals) |
| 1358 | FixIt += " = "; |
| 1359 | SourceRange FixItRange(Loc); |
| 1360 | if (EndPoint == MissingAttributeSubjectRulesRecoveryPoint::None) { |
| 1361 | // Gather the subject match rules that are supported by the attribute. |
| 1362 | SmallVector<std::pair<attr::SubjectMatchRule, bool>, 4> SubjectMatchRuleSet; |
| 1363 | Attribute.getMatchRules(PRef.getLangOpts(), SubjectMatchRuleSet); |
| 1364 | if (SubjectMatchRuleSet.empty()) { |
| 1365 | // FIXME: We can emit a "fix-it" with a subject list placeholder when |
| 1366 | // placeholders will be supported by the fix-its. |
| 1367 | return Diagnostic; |
| 1368 | } |
| 1369 | FixIt += "any("; |
| 1370 | bool NeedsComma = false; |
| 1371 | for (const auto &I : SubjectMatchRuleSet) { |
| 1372 | // Ensure that the missing rule is reported in the fix-it only when it's |
| 1373 | // supported in the current language mode. |
| 1374 | if (!I.second) |
| 1375 | continue; |
| 1376 | if (NeedsComma) |
| 1377 | FixIt += ", "; |
| 1378 | else |
| 1379 | NeedsComma = true; |
| 1380 | FixIt += attr::getSubjectMatchRuleSpelling(I.first); |
| 1381 | } |
| 1382 | FixIt += ")"; |
| 1383 | // Check if we need to remove the range |
| 1384 | PRef.SkipUntil(tok::eof, Parser::StopBeforeMatch); |
| 1385 | FixItRange.setEnd(PRef.getCurToken().getLocation()); |
| 1386 | } |
| 1387 | if (FixItRange.getBegin() == FixItRange.getEnd()) |
| 1388 | Diagnostic << FixItHint::CreateInsertion(FixItRange.getBegin(), FixIt); |
| 1389 | else |
| 1390 | Diagnostic << FixItHint::CreateReplacement( |
| 1391 | CharSourceRange::getCharRange(FixItRange), FixIt); |
| 1392 | return Diagnostic; |
| 1393 | } |
| 1394 | |
| 1395 | } // end anonymous namespace |
| 1396 | |
| 1397 | void Parser::HandlePragmaAttribute() { |
| 1398 | assert(Tok.is(tok::annot_pragma_attribute) && |
| 1399 | "Expected #pragma attribute annotation token"); |
| 1400 | SourceLocation PragmaLoc = Tok.getLocation(); |
| 1401 | auto *Info = static_cast<PragmaAttributeInfo *>(Tok.getAnnotationValue()); |
| 1402 | if (Info->Action == PragmaAttributeInfo::Pop) { |
Richard Smith | af3b325 | 2017-05-18 19:21:48 +0000 | [diff] [blame] | 1403 | ConsumeAnnotationToken(); |
Erik Pilkington | 0876cae | 2018-12-20 22:32:04 +0000 | [diff] [blame] | 1404 | Actions.ActOnPragmaAttributePop(PragmaLoc, Info->Namespace); |
Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 1405 | return; |
| 1406 | } |
| 1407 | // Parse the actual attribute with its arguments. |
Erik Pilkington | 7d18094 | 2018-10-29 17:38:42 +0000 | [diff] [blame] | 1408 | assert((Info->Action == PragmaAttributeInfo::Push || |
| 1409 | Info->Action == PragmaAttributeInfo::Attribute) && |
Erik Pilkington | b287a01 | 2018-10-29 03:24:16 +0000 | [diff] [blame] | 1410 | "Unexpected #pragma attribute command"); |
Erik Pilkington | 7d18094 | 2018-10-29 17:38:42 +0000 | [diff] [blame] | 1411 | |
| 1412 | if (Info->Action == PragmaAttributeInfo::Push && Info->Tokens.empty()) { |
| 1413 | ConsumeAnnotationToken(); |
Erik Pilkington | 0876cae | 2018-12-20 22:32:04 +0000 | [diff] [blame] | 1414 | Actions.ActOnPragmaAttributeEmptyPush(PragmaLoc, Info->Namespace); |
Erik Pilkington | 7d18094 | 2018-10-29 17:38:42 +0000 | [diff] [blame] | 1415 | return; |
| 1416 | } |
| 1417 | |
Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 1418 | PP.EnterTokenStream(Info->Tokens, /*DisableMacroExpansion=*/false); |
Richard Smith | af3b325 | 2017-05-18 19:21:48 +0000 | [diff] [blame] | 1419 | ConsumeAnnotationToken(); |
Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 1420 | |
| 1421 | ParsedAttributes &Attrs = Info->Attributes; |
| 1422 | Attrs.clearListOnly(); |
| 1423 | |
| 1424 | auto SkipToEnd = [this]() { |
| 1425 | SkipUntil(tok::eof, StopBeforeMatch); |
| 1426 | ConsumeToken(); |
| 1427 | }; |
| 1428 | |
| 1429 | if (Tok.is(tok::l_square) && NextToken().is(tok::l_square)) { |
| 1430 | // Parse the CXX11 style attribute. |
| 1431 | ParseCXX11AttributeSpecifier(Attrs); |
| 1432 | } else if (Tok.is(tok::kw___attribute)) { |
| 1433 | ConsumeToken(); |
| 1434 | if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, |
| 1435 | "attribute")) |
| 1436 | return SkipToEnd(); |
| 1437 | if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) |
| 1438 | return SkipToEnd(); |
| 1439 | |
| 1440 | if (Tok.isNot(tok::identifier)) { |
| 1441 | Diag(Tok, diag::err_pragma_attribute_expected_attribute_name); |
| 1442 | SkipToEnd(); |
| 1443 | return; |
| 1444 | } |
| 1445 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
| 1446 | SourceLocation AttrNameLoc = ConsumeToken(); |
| 1447 | |
| 1448 | if (Tok.isNot(tok::l_paren)) |
| 1449 | Attrs.addNew(AttrName, AttrNameLoc, nullptr, AttrNameLoc, nullptr, 0, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1450 | ParsedAttr::AS_GNU); |
Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 1451 | else |
| 1452 | ParseGNUAttributeArgs(AttrName, AttrNameLoc, Attrs, /*EndLoc=*/nullptr, |
| 1453 | /*ScopeName=*/nullptr, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1454 | /*ScopeLoc=*/SourceLocation(), ParsedAttr::AS_GNU, |
Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 1455 | /*Declarator=*/nullptr); |
| 1456 | |
| 1457 | if (ExpectAndConsume(tok::r_paren)) |
| 1458 | return SkipToEnd(); |
| 1459 | if (ExpectAndConsume(tok::r_paren)) |
| 1460 | return SkipToEnd(); |
| 1461 | } else if (Tok.is(tok::kw___declspec)) { |
| 1462 | ParseMicrosoftDeclSpecs(Attrs); |
| 1463 | } else { |
| 1464 | Diag(Tok, diag::err_pragma_attribute_expected_attribute_syntax); |
| 1465 | if (Tok.getIdentifierInfo()) { |
| 1466 | // If we suspect that this is an attribute suggest the use of |
| 1467 | // '__attribute__'. |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1468 | if (ParsedAttr::getKind(Tok.getIdentifierInfo(), /*ScopeName=*/nullptr, |
| 1469 | ParsedAttr::AS_GNU) != |
| 1470 | ParsedAttr::UnknownAttribute) { |
Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 1471 | SourceLocation InsertStartLoc = Tok.getLocation(); |
| 1472 | ConsumeToken(); |
| 1473 | if (Tok.is(tok::l_paren)) { |
| 1474 | ConsumeAnyToken(); |
| 1475 | SkipUntil(tok::r_paren, StopBeforeMatch); |
| 1476 | if (Tok.isNot(tok::r_paren)) |
| 1477 | return SkipToEnd(); |
| 1478 | } |
| 1479 | Diag(Tok, diag::note_pragma_attribute_use_attribute_kw) |
| 1480 | << FixItHint::CreateInsertion(InsertStartLoc, "__attribute__((") |
| 1481 | << FixItHint::CreateInsertion(Tok.getEndLoc(), "))"); |
| 1482 | } |
| 1483 | } |
| 1484 | SkipToEnd(); |
| 1485 | return; |
| 1486 | } |
| 1487 | |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 1488 | if (Attrs.empty() || Attrs.begin()->isInvalid()) { |
Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 1489 | SkipToEnd(); |
| 1490 | return; |
| 1491 | } |
| 1492 | |
| 1493 | // Ensure that we don't have more than one attribute. |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 1494 | if (Attrs.size() > 1) { |
| 1495 | SourceLocation Loc = Attrs[1].getLoc(); |
Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 1496 | Diag(Loc, diag::err_pragma_attribute_multiple_attributes); |
| 1497 | SkipToEnd(); |
| 1498 | return; |
| 1499 | } |
| 1500 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1501 | ParsedAttr &Attribute = *Attrs.begin(); |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 1502 | if (!Attribute.isSupportedByPragmaAttribute()) { |
Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 1503 | Diag(PragmaLoc, diag::err_pragma_attribute_unsupported_attribute) |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 1504 | << Attribute.getName(); |
Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 1505 | SkipToEnd(); |
| 1506 | return; |
| 1507 | } |
Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 1508 | |
| 1509 | // Parse the subject-list. |
| 1510 | if (!TryConsumeToken(tok::comma)) { |
| 1511 | createExpectedAttributeSubjectRulesTokenDiagnostic( |
| 1512 | diag::err_expected, Attribute, |
| 1513 | MissingAttributeSubjectRulesRecoveryPoint::Comma, *this) |
| 1514 | << tok::comma; |
| 1515 | SkipToEnd(); |
| 1516 | return; |
| 1517 | } |
| 1518 | |
| 1519 | if (Tok.isNot(tok::identifier)) { |
| 1520 | createExpectedAttributeSubjectRulesTokenDiagnostic( |
| 1521 | diag::err_pragma_attribute_invalid_subject_set_specifier, Attribute, |
| 1522 | MissingAttributeSubjectRulesRecoveryPoint::ApplyTo, *this); |
| 1523 | SkipToEnd(); |
| 1524 | return; |
| 1525 | } |
| 1526 | const IdentifierInfo *II = Tok.getIdentifierInfo(); |
| 1527 | if (!II->isStr("apply_to")) { |
| 1528 | createExpectedAttributeSubjectRulesTokenDiagnostic( |
| 1529 | diag::err_pragma_attribute_invalid_subject_set_specifier, Attribute, |
| 1530 | MissingAttributeSubjectRulesRecoveryPoint::ApplyTo, *this); |
| 1531 | SkipToEnd(); |
| 1532 | return; |
| 1533 | } |
| 1534 | ConsumeToken(); |
| 1535 | |
| 1536 | if (!TryConsumeToken(tok::equal)) { |
| 1537 | createExpectedAttributeSubjectRulesTokenDiagnostic( |
| 1538 | diag::err_expected, Attribute, |
| 1539 | MissingAttributeSubjectRulesRecoveryPoint::Equals, *this) |
| 1540 | << tok::equal; |
| 1541 | SkipToEnd(); |
| 1542 | return; |
| 1543 | } |
| 1544 | |
| 1545 | attr::ParsedSubjectMatchRuleSet SubjectMatchRules; |
| 1546 | SourceLocation AnyLoc, LastMatchRuleEndLoc; |
| 1547 | if (ParsePragmaAttributeSubjectMatchRuleSet(SubjectMatchRules, AnyLoc, |
| 1548 | LastMatchRuleEndLoc)) { |
| 1549 | SkipToEnd(); |
| 1550 | return; |
| 1551 | } |
| 1552 | |
| 1553 | // Tokens following an ill-formed attribute will remain in the token stream |
| 1554 | // and must be removed. |
| 1555 | if (Tok.isNot(tok::eof)) { |
| 1556 | Diag(Tok, diag::err_pragma_attribute_extra_tokens_after_attribute); |
| 1557 | SkipToEnd(); |
| 1558 | return; |
| 1559 | } |
| 1560 | |
| 1561 | // Consume the eof terminator token. |
| 1562 | ConsumeToken(); |
| 1563 | |
Erik Pilkington | 7d18094 | 2018-10-29 17:38:42 +0000 | [diff] [blame] | 1564 | // Handle a mixed push/attribute by desurging to a push, then an attribute. |
| 1565 | if (Info->Action == PragmaAttributeInfo::Push) |
Erik Pilkington | 0876cae | 2018-12-20 22:32:04 +0000 | [diff] [blame] | 1566 | Actions.ActOnPragmaAttributeEmptyPush(PragmaLoc, Info->Namespace); |
Erik Pilkington | 7d18094 | 2018-10-29 17:38:42 +0000 | [diff] [blame] | 1567 | |
| 1568 | Actions.ActOnPragmaAttributeAttribute(Attribute, PragmaLoc, |
| 1569 | std::move(SubjectMatchRules)); |
Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 1570 | } |
| 1571 | |
Eli Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 1572 | // #pragma GCC visibility comes in two variants: |
| 1573 | // 'push' '(' [visibility] ')' |
| 1574 | // 'pop' |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1575 | void PragmaGCCVisibilityHandler::HandlePragma(Preprocessor &PP, |
Douglas Gregor | c7d6576 | 2010-09-09 22:45:38 +0000 | [diff] [blame] | 1576 | PragmaIntroducerKind Introducer, |
| 1577 | Token &VisTok) { |
Eli Friedman | 570024a | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 1578 | SourceLocation VisLoc = VisTok.getLocation(); |
| 1579 | |
| 1580 | Token Tok; |
Joerg Sonnenberger | 869f0b7 | 2011-07-20 01:03:50 +0000 | [diff] [blame] | 1581 | PP.LexUnexpandedToken(Tok); |
Eli Friedman | 570024a | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 1582 | |
| 1583 | const IdentifierInfo *PushPop = Tok.getIdentifierInfo(); |
| 1584 | |
Eli Friedman | 570024a | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 1585 | const IdentifierInfo *VisType; |
| 1586 | if (PushPop && PushPop->isStr("pop")) { |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 1587 | VisType = nullptr; |
Eli Friedman | 570024a | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 1588 | } else if (PushPop && PushPop->isStr("push")) { |
Joerg Sonnenberger | 869f0b7 | 2011-07-20 01:03:50 +0000 | [diff] [blame] | 1589 | PP.LexUnexpandedToken(Tok); |
Eli Friedman | 570024a | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 1590 | if (Tok.isNot(tok::l_paren)) { |
| 1591 | PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_lparen) |
| 1592 | << "visibility"; |
| 1593 | return; |
| 1594 | } |
Joerg Sonnenberger | 869f0b7 | 2011-07-20 01:03:50 +0000 | [diff] [blame] | 1595 | PP.LexUnexpandedToken(Tok); |
Eli Friedman | 570024a | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 1596 | VisType = Tok.getIdentifierInfo(); |
| 1597 | if (!VisType) { |
| 1598 | PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier) |
| 1599 | << "visibility"; |
| 1600 | return; |
| 1601 | } |
Joerg Sonnenberger | 869f0b7 | 2011-07-20 01:03:50 +0000 | [diff] [blame] | 1602 | PP.LexUnexpandedToken(Tok); |
Eli Friedman | 570024a | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 1603 | if (Tok.isNot(tok::r_paren)) { |
| 1604 | PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_rparen) |
| 1605 | << "visibility"; |
| 1606 | return; |
| 1607 | } |
| 1608 | } else { |
| 1609 | PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier) |
| 1610 | << "visibility"; |
| 1611 | return; |
| 1612 | } |
David Majnemer | a8f2f1d | 2015-03-19 00:10:23 +0000 | [diff] [blame] | 1613 | SourceLocation EndLoc = Tok.getLocation(); |
Joerg Sonnenberger | 869f0b7 | 2011-07-20 01:03:50 +0000 | [diff] [blame] | 1614 | PP.LexUnexpandedToken(Tok); |
Peter Collingbourne | 2f1e36b | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 1615 | if (Tok.isNot(tok::eod)) { |
Eli Friedman | 570024a | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 1616 | PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) |
| 1617 | << "visibility"; |
| 1618 | return; |
| 1619 | } |
| 1620 | |
David Blaikie | 2eabcc9 | 2016-02-09 18:52:09 +0000 | [diff] [blame] | 1621 | auto Toks = llvm::make_unique<Token[]>(1); |
Rafael Espindola | 273fd77 | 2012-01-26 02:02:57 +0000 | [diff] [blame] | 1622 | Toks[0].startToken(); |
| 1623 | Toks[0].setKind(tok::annot_pragma_vis); |
| 1624 | Toks[0].setLocation(VisLoc); |
David Majnemer | a8f2f1d | 2015-03-19 00:10:23 +0000 | [diff] [blame] | 1625 | Toks[0].setAnnotationEndLoc(EndLoc); |
Rafael Espindola | 273fd77 | 2012-01-26 02:02:57 +0000 | [diff] [blame] | 1626 | Toks[0].setAnnotationValue( |
| 1627 | const_cast<void*>(static_cast<const void*>(VisType))); |
David Blaikie | 2eabcc9 | 2016-02-09 18:52:09 +0000 | [diff] [blame] | 1628 | PP.EnterTokenStream(std::move(Toks), 1, /*DisableMacroExpansion=*/true); |
Eli Friedman | 570024a | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 1629 | } |
| 1630 | |
Daniel Dunbar | 921b968 | 2008-10-04 19:21:03 +0000 | [diff] [blame] | 1631 | // #pragma pack(...) comes in the following delicious flavors: |
| 1632 | // pack '(' [integer] ')' |
| 1633 | // pack '(' 'show' ')' |
| 1634 | // pack '(' ('push' | 'pop') [',' identifier] [, integer] ')' |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1635 | void PragmaPackHandler::HandlePragma(Preprocessor &PP, |
Douglas Gregor | c7d6576 | 2010-09-09 22:45:38 +0000 | [diff] [blame] | 1636 | PragmaIntroducerKind Introducer, |
| 1637 | Token &PackTok) { |
Daniel Dunbar | 921b968 | 2008-10-04 19:21:03 +0000 | [diff] [blame] | 1638 | SourceLocation PackLoc = PackTok.getLocation(); |
| 1639 | |
| 1640 | Token Tok; |
| 1641 | PP.Lex(Tok); |
| 1642 | if (Tok.isNot(tok::l_paren)) { |
Ted Kremenek | fd14fad | 2009-03-23 22:28:25 +0000 | [diff] [blame] | 1643 | PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_lparen) << "pack"; |
Daniel Dunbar | 921b968 | 2008-10-04 19:21:03 +0000 | [diff] [blame] | 1644 | return; |
| 1645 | } |
| 1646 | |
Denis Zobnin | 10c4f45 | 2016-04-29 18:17:40 +0000 | [diff] [blame] | 1647 | Sema::PragmaMsStackAction Action = Sema::PSK_Reset; |
| 1648 | StringRef SlotLabel; |
Eli Friedman | 68be164 | 2012-10-04 02:36:51 +0000 | [diff] [blame] | 1649 | Token Alignment; |
| 1650 | Alignment.startToken(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1651 | PP.Lex(Tok); |
Daniel Dunbar | 921b968 | 2008-10-04 19:21:03 +0000 | [diff] [blame] | 1652 | if (Tok.is(tok::numeric_constant)) { |
Eli Friedman | 68be164 | 2012-10-04 02:36:51 +0000 | [diff] [blame] | 1653 | Alignment = Tok; |
Daniel Dunbar | 921b968 | 2008-10-04 19:21:03 +0000 | [diff] [blame] | 1654 | |
| 1655 | PP.Lex(Tok); |
Eli Friedman | 055c970 | 2011-11-02 01:53:16 +0000 | [diff] [blame] | 1656 | |
| 1657 | // In MSVC/gcc, #pragma pack(4) sets the alignment without affecting |
| 1658 | // the push/pop stack. |
| 1659 | // In Apple gcc, #pragma pack(4) is equivalent to #pragma pack(push, 4) |
Denis Zobnin | 10c4f45 | 2016-04-29 18:17:40 +0000 | [diff] [blame] | 1660 | Action = |
| 1661 | PP.getLangOpts().ApplePragmaPack ? Sema::PSK_Push_Set : Sema::PSK_Set; |
Daniel Dunbar | 921b968 | 2008-10-04 19:21:03 +0000 | [diff] [blame] | 1662 | } else if (Tok.is(tok::identifier)) { |
| 1663 | const IdentifierInfo *II = Tok.getIdentifierInfo(); |
Chris Lattner | e3d20d9 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 1664 | if (II->isStr("show")) { |
Denis Zobnin | 10c4f45 | 2016-04-29 18:17:40 +0000 | [diff] [blame] | 1665 | Action = Sema::PSK_Show; |
Daniel Dunbar | 921b968 | 2008-10-04 19:21:03 +0000 | [diff] [blame] | 1666 | PP.Lex(Tok); |
| 1667 | } else { |
Chris Lattner | e3d20d9 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 1668 | if (II->isStr("push")) { |
Denis Zobnin | 10c4f45 | 2016-04-29 18:17:40 +0000 | [diff] [blame] | 1669 | Action = Sema::PSK_Push; |
Chris Lattner | e3d20d9 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 1670 | } else if (II->isStr("pop")) { |
Denis Zobnin | 10c4f45 | 2016-04-29 18:17:40 +0000 | [diff] [blame] | 1671 | Action = Sema::PSK_Pop; |
Daniel Dunbar | 921b968 | 2008-10-04 19:21:03 +0000 | [diff] [blame] | 1672 | } else { |
Reid Kleckner | c0dca6d | 2014-02-12 23:50:26 +0000 | [diff] [blame] | 1673 | PP.Diag(Tok.getLocation(), diag::warn_pragma_invalid_action) << "pack"; |
Daniel Dunbar | 921b968 | 2008-10-04 19:21:03 +0000 | [diff] [blame] | 1674 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1675 | } |
Daniel Dunbar | 921b968 | 2008-10-04 19:21:03 +0000 | [diff] [blame] | 1676 | PP.Lex(Tok); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1677 | |
Daniel Dunbar | 921b968 | 2008-10-04 19:21:03 +0000 | [diff] [blame] | 1678 | if (Tok.is(tok::comma)) { |
| 1679 | PP.Lex(Tok); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1680 | |
Daniel Dunbar | 921b968 | 2008-10-04 19:21:03 +0000 | [diff] [blame] | 1681 | if (Tok.is(tok::numeric_constant)) { |
Denis Zobnin | 10c4f45 | 2016-04-29 18:17:40 +0000 | [diff] [blame] | 1682 | Action = (Sema::PragmaMsStackAction)(Action | Sema::PSK_Set); |
Eli Friedman | 68be164 | 2012-10-04 02:36:51 +0000 | [diff] [blame] | 1683 | Alignment = Tok; |
Daniel Dunbar | 921b968 | 2008-10-04 19:21:03 +0000 | [diff] [blame] | 1684 | |
| 1685 | PP.Lex(Tok); |
| 1686 | } else if (Tok.is(tok::identifier)) { |
Denis Zobnin | 10c4f45 | 2016-04-29 18:17:40 +0000 | [diff] [blame] | 1687 | SlotLabel = Tok.getIdentifierInfo()->getName(); |
Daniel Dunbar | 921b968 | 2008-10-04 19:21:03 +0000 | [diff] [blame] | 1688 | PP.Lex(Tok); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1689 | |
Daniel Dunbar | 921b968 | 2008-10-04 19:21:03 +0000 | [diff] [blame] | 1690 | if (Tok.is(tok::comma)) { |
| 1691 | PP.Lex(Tok); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1692 | |
Daniel Dunbar | 921b968 | 2008-10-04 19:21:03 +0000 | [diff] [blame] | 1693 | if (Tok.isNot(tok::numeric_constant)) { |
Chris Lattner | e3d20d9 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 1694 | PP.Diag(Tok.getLocation(), diag::warn_pragma_pack_malformed); |
Daniel Dunbar | 921b968 | 2008-10-04 19:21:03 +0000 | [diff] [blame] | 1695 | return; |
| 1696 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1697 | |
Denis Zobnin | 10c4f45 | 2016-04-29 18:17:40 +0000 | [diff] [blame] | 1698 | Action = (Sema::PragmaMsStackAction)(Action | Sema::PSK_Set); |
Eli Friedman | 68be164 | 2012-10-04 02:36:51 +0000 | [diff] [blame] | 1699 | Alignment = Tok; |
Daniel Dunbar | 921b968 | 2008-10-04 19:21:03 +0000 | [diff] [blame] | 1700 | |
| 1701 | PP.Lex(Tok); |
| 1702 | } |
| 1703 | } else { |
Chris Lattner | e3d20d9 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 1704 | PP.Diag(Tok.getLocation(), diag::warn_pragma_pack_malformed); |
Daniel Dunbar | 921b968 | 2008-10-04 19:21:03 +0000 | [diff] [blame] | 1705 | return; |
| 1706 | } |
| 1707 | } |
| 1708 | } |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1709 | } else if (PP.getLangOpts().ApplePragmaPack) { |
Eli Friedman | 055c970 | 2011-11-02 01:53:16 +0000 | [diff] [blame] | 1710 | // In MSVC/gcc, #pragma pack() resets the alignment without affecting |
| 1711 | // the push/pop stack. |
| 1712 | // In Apple gcc #pragma pack() is equivalent to #pragma pack(pop). |
Denis Zobnin | 10c4f45 | 2016-04-29 18:17:40 +0000 | [diff] [blame] | 1713 | Action = Sema::PSK_Pop; |
Sebastian Redl | 17f2c7d | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1714 | } |
Daniel Dunbar | 921b968 | 2008-10-04 19:21:03 +0000 | [diff] [blame] | 1715 | |
| 1716 | if (Tok.isNot(tok::r_paren)) { |
Ted Kremenek | fd14fad | 2009-03-23 22:28:25 +0000 | [diff] [blame] | 1717 | PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_rparen) << "pack"; |
Daniel Dunbar | 921b968 | 2008-10-04 19:21:03 +0000 | [diff] [blame] | 1718 | return; |
| 1719 | } |
| 1720 | |
Daniel Dunbar | 75c9be7 | 2010-05-26 23:29:06 +0000 | [diff] [blame] | 1721 | SourceLocation RParenLoc = Tok.getLocation(); |
Eli Friedman | f5867dd | 2009-06-05 00:49:58 +0000 | [diff] [blame] | 1722 | PP.Lex(Tok); |
Peter Collingbourne | 2f1e36b | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 1723 | if (Tok.isNot(tok::eod)) { |
Eli Friedman | f5867dd | 2009-06-05 00:49:58 +0000 | [diff] [blame] | 1724 | PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) << "pack"; |
| 1725 | return; |
| 1726 | } |
| 1727 | |
David Blaikie | 2eabcc9 | 2016-02-09 18:52:09 +0000 | [diff] [blame] | 1728 | PragmaPackInfo *Info = |
| 1729 | PP.getPreprocessorAllocator().Allocate<PragmaPackInfo>(1); |
Denis Zobnin | 10c4f45 | 2016-04-29 18:17:40 +0000 | [diff] [blame] | 1730 | Info->Action = Action; |
| 1731 | Info->SlotLabel = SlotLabel; |
Eli Friedman | 68be164 | 2012-10-04 02:36:51 +0000 | [diff] [blame] | 1732 | Info->Alignment = Alignment; |
Eli Friedman | ec52f92 | 2012-02-23 23:47:16 +0000 | [diff] [blame] | 1733 | |
David Blaikie | 2eabcc9 | 2016-02-09 18:52:09 +0000 | [diff] [blame] | 1734 | MutableArrayRef<Token> Toks(PP.getPreprocessorAllocator().Allocate<Token>(1), |
| 1735 | 1); |
Eli Friedman | ec52f92 | 2012-02-23 23:47:16 +0000 | [diff] [blame] | 1736 | Toks[0].startToken(); |
| 1737 | Toks[0].setKind(tok::annot_pragma_pack); |
| 1738 | Toks[0].setLocation(PackLoc); |
David Majnemer | a8f2f1d | 2015-03-19 00:10:23 +0000 | [diff] [blame] | 1739 | Toks[0].setAnnotationEndLoc(RParenLoc); |
Eli Friedman | ec52f92 | 2012-02-23 23:47:16 +0000 | [diff] [blame] | 1740 | Toks[0].setAnnotationValue(static_cast<void*>(Info)); |
David Blaikie | 2eabcc9 | 2016-02-09 18:52:09 +0000 | [diff] [blame] | 1741 | PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true); |
Daniel Dunbar | 921b968 | 2008-10-04 19:21:03 +0000 | [diff] [blame] | 1742 | } |
| 1743 | |
Fariborz Jahanian | 743dda4 | 2011-04-25 18:49:15 +0000 | [diff] [blame] | 1744 | // #pragma ms_struct on |
| 1745 | // #pragma ms_struct off |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1746 | void PragmaMSStructHandler::HandlePragma(Preprocessor &PP, |
Fariborz Jahanian | 743dda4 | 2011-04-25 18:49:15 +0000 | [diff] [blame] | 1747 | PragmaIntroducerKind Introducer, |
| 1748 | Token &MSStructTok) { |
Nico Weber | 779355f | 2016-03-02 23:22:00 +0000 | [diff] [blame] | 1749 | PragmaMSStructKind Kind = PMSST_OFF; |
| 1750 | |
Fariborz Jahanian | 743dda4 | 2011-04-25 18:49:15 +0000 | [diff] [blame] | 1751 | Token Tok; |
| 1752 | PP.Lex(Tok); |
| 1753 | if (Tok.isNot(tok::identifier)) { |
| 1754 | PP.Diag(Tok.getLocation(), diag::warn_pragma_ms_struct); |
| 1755 | return; |
| 1756 | } |
David Majnemer | a8f2f1d | 2015-03-19 00:10:23 +0000 | [diff] [blame] | 1757 | SourceLocation EndLoc = Tok.getLocation(); |
Fariborz Jahanian | 743dda4 | 2011-04-25 18:49:15 +0000 | [diff] [blame] | 1758 | const IdentifierInfo *II = Tok.getIdentifierInfo(); |
| 1759 | if (II->isStr("on")) { |
Nico Weber | 779355f | 2016-03-02 23:22:00 +0000 | [diff] [blame] | 1760 | Kind = PMSST_ON; |
Fariborz Jahanian | 743dda4 | 2011-04-25 18:49:15 +0000 | [diff] [blame] | 1761 | PP.Lex(Tok); |
| 1762 | } |
| 1763 | else if (II->isStr("off") || II->isStr("reset")) |
| 1764 | PP.Lex(Tok); |
| 1765 | else { |
| 1766 | PP.Diag(Tok.getLocation(), diag::warn_pragma_ms_struct); |
| 1767 | return; |
| 1768 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1769 | |
Fariborz Jahanian | 743dda4 | 2011-04-25 18:49:15 +0000 | [diff] [blame] | 1770 | if (Tok.isNot(tok::eod)) { |
Daniel Dunbar | 340cf24 | 2012-02-29 01:38:22 +0000 | [diff] [blame] | 1771 | PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) |
| 1772 | << "ms_struct"; |
Fariborz Jahanian | 743dda4 | 2011-04-25 18:49:15 +0000 | [diff] [blame] | 1773 | return; |
| 1774 | } |
Eli Friedman | 68be164 | 2012-10-04 02:36:51 +0000 | [diff] [blame] | 1775 | |
David Blaikie | 2eabcc9 | 2016-02-09 18:52:09 +0000 | [diff] [blame] | 1776 | MutableArrayRef<Token> Toks(PP.getPreprocessorAllocator().Allocate<Token>(1), |
| 1777 | 1); |
Eli Friedman | 68be164 | 2012-10-04 02:36:51 +0000 | [diff] [blame] | 1778 | Toks[0].startToken(); |
| 1779 | Toks[0].setKind(tok::annot_pragma_msstruct); |
| 1780 | Toks[0].setLocation(MSStructTok.getLocation()); |
David Majnemer | a8f2f1d | 2015-03-19 00:10:23 +0000 | [diff] [blame] | 1781 | Toks[0].setAnnotationEndLoc(EndLoc); |
Eli Friedman | 68be164 | 2012-10-04 02:36:51 +0000 | [diff] [blame] | 1782 | Toks[0].setAnnotationValue(reinterpret_cast<void*>( |
| 1783 | static_cast<uintptr_t>(Kind))); |
David Blaikie | 2eabcc9 | 2016-02-09 18:52:09 +0000 | [diff] [blame] | 1784 | PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true); |
Fariborz Jahanian | 743dda4 | 2011-04-25 18:49:15 +0000 | [diff] [blame] | 1785 | } |
| 1786 | |
Javed Absar | 2a67c9e | 2017-06-05 10:11:57 +0000 | [diff] [blame] | 1787 | // #pragma clang section bss="abc" data="" rodata="def" text="" |
| 1788 | void PragmaClangSectionHandler::HandlePragma(Preprocessor &PP, |
| 1789 | PragmaIntroducerKind Introducer, Token &FirstToken) { |
| 1790 | |
| 1791 | Token Tok; |
| 1792 | auto SecKind = Sema::PragmaClangSectionKind::PCSK_Invalid; |
| 1793 | |
| 1794 | PP.Lex(Tok); // eat 'section' |
| 1795 | while (Tok.isNot(tok::eod)) { |
| 1796 | if (Tok.isNot(tok::identifier)) { |
| 1797 | PP.Diag(Tok.getLocation(), diag::err_pragma_expected_clang_section_name) << "clang section"; |
| 1798 | return; |
| 1799 | } |
| 1800 | |
| 1801 | const IdentifierInfo *SecType = Tok.getIdentifierInfo(); |
| 1802 | if (SecType->isStr("bss")) |
| 1803 | SecKind = Sema::PragmaClangSectionKind::PCSK_BSS; |
| 1804 | else if (SecType->isStr("data")) |
| 1805 | SecKind = Sema::PragmaClangSectionKind::PCSK_Data; |
| 1806 | else if (SecType->isStr("rodata")) |
| 1807 | SecKind = Sema::PragmaClangSectionKind::PCSK_Rodata; |
| 1808 | else if (SecType->isStr("text")) |
| 1809 | SecKind = Sema::PragmaClangSectionKind::PCSK_Text; |
| 1810 | else { |
| 1811 | PP.Diag(Tok.getLocation(), diag::err_pragma_expected_clang_section_name) << "clang section"; |
| 1812 | return; |
| 1813 | } |
| 1814 | |
| 1815 | PP.Lex(Tok); // eat ['bss'|'data'|'rodata'|'text'] |
| 1816 | if (Tok.isNot(tok::equal)) { |
| 1817 | PP.Diag(Tok.getLocation(), diag::err_pragma_clang_section_expected_equal) << SecKind; |
| 1818 | return; |
| 1819 | } |
| 1820 | |
| 1821 | std::string SecName; |
| 1822 | if (!PP.LexStringLiteral(Tok, SecName, "pragma clang section", false)) |
| 1823 | return; |
| 1824 | |
| 1825 | Actions.ActOnPragmaClangSection(Tok.getLocation(), |
| 1826 | (SecName.size()? Sema::PragmaClangSectionAction::PCSA_Set : |
| 1827 | Sema::PragmaClangSectionAction::PCSA_Clear), |
| 1828 | SecKind, SecName); |
| 1829 | } |
| 1830 | } |
| 1831 | |
Daniel Dunbar | cb82acb | 2010-07-31 19:17:07 +0000 | [diff] [blame] | 1832 | // #pragma 'align' '=' {'native','natural','mac68k','power','reset'} |
| 1833 | // #pragma 'options 'align' '=' {'native','natural','mac68k','power','reset'} |
Eli Friedman | 68be164 | 2012-10-04 02:36:51 +0000 | [diff] [blame] | 1834 | static void ParseAlignPragma(Preprocessor &PP, Token &FirstTok, |
Daniel Dunbar | cb82acb | 2010-07-31 19:17:07 +0000 | [diff] [blame] | 1835 | bool IsOptions) { |
Daniel Dunbar | 75c9be7 | 2010-05-26 23:29:06 +0000 | [diff] [blame] | 1836 | Token Tok; |
Daniel Dunbar | cb82acb | 2010-07-31 19:17:07 +0000 | [diff] [blame] | 1837 | |
| 1838 | if (IsOptions) { |
| 1839 | PP.Lex(Tok); |
| 1840 | if (Tok.isNot(tok::identifier) || |
| 1841 | !Tok.getIdentifierInfo()->isStr("align")) { |
| 1842 | PP.Diag(Tok.getLocation(), diag::warn_pragma_options_expected_align); |
| 1843 | return; |
| 1844 | } |
Daniel Dunbar | 75c9be7 | 2010-05-26 23:29:06 +0000 | [diff] [blame] | 1845 | } |
Daniel Dunbar | 663e809 | 2010-05-27 18:42:09 +0000 | [diff] [blame] | 1846 | |
Daniel Dunbar | 75c9be7 | 2010-05-26 23:29:06 +0000 | [diff] [blame] | 1847 | PP.Lex(Tok); |
| 1848 | if (Tok.isNot(tok::equal)) { |
Daniel Dunbar | cb82acb | 2010-07-31 19:17:07 +0000 | [diff] [blame] | 1849 | PP.Diag(Tok.getLocation(), diag::warn_pragma_align_expected_equal) |
| 1850 | << IsOptions; |
Daniel Dunbar | 75c9be7 | 2010-05-26 23:29:06 +0000 | [diff] [blame] | 1851 | return; |
| 1852 | } |
| 1853 | |
| 1854 | PP.Lex(Tok); |
| 1855 | if (Tok.isNot(tok::identifier)) { |
| 1856 | PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier) |
Daniel Dunbar | cb82acb | 2010-07-31 19:17:07 +0000 | [diff] [blame] | 1857 | << (IsOptions ? "options" : "align"); |
Daniel Dunbar | 75c9be7 | 2010-05-26 23:29:06 +0000 | [diff] [blame] | 1858 | return; |
| 1859 | } |
| 1860 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1861 | Sema::PragmaOptionsAlignKind Kind = Sema::POAK_Natural; |
Daniel Dunbar | 75c9be7 | 2010-05-26 23:29:06 +0000 | [diff] [blame] | 1862 | const IdentifierInfo *II = Tok.getIdentifierInfo(); |
Daniel Dunbar | 663e809 | 2010-05-27 18:42:09 +0000 | [diff] [blame] | 1863 | if (II->isStr("native")) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1864 | Kind = Sema::POAK_Native; |
Daniel Dunbar | 663e809 | 2010-05-27 18:42:09 +0000 | [diff] [blame] | 1865 | else if (II->isStr("natural")) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1866 | Kind = Sema::POAK_Natural; |
Daniel Dunbar | 9c84d4a | 2010-05-27 18:42:17 +0000 | [diff] [blame] | 1867 | else if (II->isStr("packed")) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1868 | Kind = Sema::POAK_Packed; |
Daniel Dunbar | 75c9be7 | 2010-05-26 23:29:06 +0000 | [diff] [blame] | 1869 | else if (II->isStr("power")) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1870 | Kind = Sema::POAK_Power; |
Daniel Dunbar | 75c9be7 | 2010-05-26 23:29:06 +0000 | [diff] [blame] | 1871 | else if (II->isStr("mac68k")) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1872 | Kind = Sema::POAK_Mac68k; |
Daniel Dunbar | 75c9be7 | 2010-05-26 23:29:06 +0000 | [diff] [blame] | 1873 | else if (II->isStr("reset")) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1874 | Kind = Sema::POAK_Reset; |
Daniel Dunbar | 75c9be7 | 2010-05-26 23:29:06 +0000 | [diff] [blame] | 1875 | else { |
Daniel Dunbar | cb82acb | 2010-07-31 19:17:07 +0000 | [diff] [blame] | 1876 | PP.Diag(Tok.getLocation(), diag::warn_pragma_align_invalid_option) |
| 1877 | << IsOptions; |
Daniel Dunbar | 75c9be7 | 2010-05-26 23:29:06 +0000 | [diff] [blame] | 1878 | return; |
| 1879 | } |
| 1880 | |
David Majnemer | a8f2f1d | 2015-03-19 00:10:23 +0000 | [diff] [blame] | 1881 | SourceLocation EndLoc = Tok.getLocation(); |
Daniel Dunbar | 75c9be7 | 2010-05-26 23:29:06 +0000 | [diff] [blame] | 1882 | PP.Lex(Tok); |
Peter Collingbourne | 2f1e36b | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 1883 | if (Tok.isNot(tok::eod)) { |
Daniel Dunbar | 75c9be7 | 2010-05-26 23:29:06 +0000 | [diff] [blame] | 1884 | PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) |
Daniel Dunbar | cb82acb | 2010-07-31 19:17:07 +0000 | [diff] [blame] | 1885 | << (IsOptions ? "options" : "align"); |
Daniel Dunbar | 75c9be7 | 2010-05-26 23:29:06 +0000 | [diff] [blame] | 1886 | return; |
| 1887 | } |
| 1888 | |
David Blaikie | 2eabcc9 | 2016-02-09 18:52:09 +0000 | [diff] [blame] | 1889 | MutableArrayRef<Token> Toks(PP.getPreprocessorAllocator().Allocate<Token>(1), |
| 1890 | 1); |
Eli Friedman | 68be164 | 2012-10-04 02:36:51 +0000 | [diff] [blame] | 1891 | Toks[0].startToken(); |
| 1892 | Toks[0].setKind(tok::annot_pragma_align); |
| 1893 | Toks[0].setLocation(FirstTok.getLocation()); |
David Majnemer | a8f2f1d | 2015-03-19 00:10:23 +0000 | [diff] [blame] | 1894 | Toks[0].setAnnotationEndLoc(EndLoc); |
Eli Friedman | 68be164 | 2012-10-04 02:36:51 +0000 | [diff] [blame] | 1895 | Toks[0].setAnnotationValue(reinterpret_cast<void*>( |
| 1896 | static_cast<uintptr_t>(Kind))); |
David Blaikie | 2eabcc9 | 2016-02-09 18:52:09 +0000 | [diff] [blame] | 1897 | PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true); |
Daniel Dunbar | cb82acb | 2010-07-31 19:17:07 +0000 | [diff] [blame] | 1898 | } |
| 1899 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1900 | void PragmaAlignHandler::HandlePragma(Preprocessor &PP, |
Douglas Gregor | c7d6576 | 2010-09-09 22:45:38 +0000 | [diff] [blame] | 1901 | PragmaIntroducerKind Introducer, |
| 1902 | Token &AlignTok) { |
Eli Friedman | 68be164 | 2012-10-04 02:36:51 +0000 | [diff] [blame] | 1903 | ParseAlignPragma(PP, AlignTok, /*IsOptions=*/false); |
Daniel Dunbar | cb82acb | 2010-07-31 19:17:07 +0000 | [diff] [blame] | 1904 | } |
| 1905 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1906 | void PragmaOptionsHandler::HandlePragma(Preprocessor &PP, |
Douglas Gregor | c7d6576 | 2010-09-09 22:45:38 +0000 | [diff] [blame] | 1907 | PragmaIntroducerKind Introducer, |
| 1908 | Token &OptionsTok) { |
Eli Friedman | 68be164 | 2012-10-04 02:36:51 +0000 | [diff] [blame] | 1909 | ParseAlignPragma(PP, OptionsTok, /*IsOptions=*/true); |
Daniel Dunbar | 75c9be7 | 2010-05-26 23:29:06 +0000 | [diff] [blame] | 1910 | } |
| 1911 | |
Ted Kremenek | fd14fad | 2009-03-23 22:28:25 +0000 | [diff] [blame] | 1912 | // #pragma unused(identifier) |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1913 | void PragmaUnusedHandler::HandlePragma(Preprocessor &PP, |
Douglas Gregor | c7d6576 | 2010-09-09 22:45:38 +0000 | [diff] [blame] | 1914 | PragmaIntroducerKind Introducer, |
| 1915 | Token &UnusedTok) { |
Ted Kremenek | fd14fad | 2009-03-23 22:28:25 +0000 | [diff] [blame] | 1916 | // FIXME: Should we be expanding macros here? My guess is no. |
| 1917 | SourceLocation UnusedLoc = UnusedTok.getLocation(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1918 | |
Ted Kremenek | fd14fad | 2009-03-23 22:28:25 +0000 | [diff] [blame] | 1919 | // Lex the left '('. |
| 1920 | Token Tok; |
| 1921 | PP.Lex(Tok); |
| 1922 | if (Tok.isNot(tok::l_paren)) { |
| 1923 | PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_lparen) << "unused"; |
| 1924 | return; |
| 1925 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1926 | |
Ted Kremenek | fd14fad | 2009-03-23 22:28:25 +0000 | [diff] [blame] | 1927 | // Lex the declaration reference(s). |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1928 | SmallVector<Token, 5> Identifiers; |
Ted Kremenek | fd14fad | 2009-03-23 22:28:25 +0000 | [diff] [blame] | 1929 | SourceLocation RParenLoc; |
| 1930 | bool LexID = true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1931 | |
Ted Kremenek | fd14fad | 2009-03-23 22:28:25 +0000 | [diff] [blame] | 1932 | while (true) { |
| 1933 | PP.Lex(Tok); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1934 | |
Ted Kremenek | fd14fad | 2009-03-23 22:28:25 +0000 | [diff] [blame] | 1935 | if (LexID) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1936 | if (Tok.is(tok::identifier)) { |
Ted Kremenek | fb50bf5 | 2009-08-03 23:24:57 +0000 | [diff] [blame] | 1937 | Identifiers.push_back(Tok); |
Ted Kremenek | fd14fad | 2009-03-23 22:28:25 +0000 | [diff] [blame] | 1938 | LexID = false; |
| 1939 | continue; |
| 1940 | } |
| 1941 | |
Ted Kremenek | fb50bf5 | 2009-08-03 23:24:57 +0000 | [diff] [blame] | 1942 | // Illegal token! |
Ted Kremenek | fd14fad | 2009-03-23 22:28:25 +0000 | [diff] [blame] | 1943 | PP.Diag(Tok.getLocation(), diag::warn_pragma_unused_expected_var); |
| 1944 | return; |
| 1945 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1946 | |
Ted Kremenek | fd14fad | 2009-03-23 22:28:25 +0000 | [diff] [blame] | 1947 | // We are execting a ')' or a ','. |
| 1948 | if (Tok.is(tok::comma)) { |
| 1949 | LexID = true; |
| 1950 | continue; |
| 1951 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1952 | |
Ted Kremenek | fd14fad | 2009-03-23 22:28:25 +0000 | [diff] [blame] | 1953 | if (Tok.is(tok::r_paren)) { |
| 1954 | RParenLoc = Tok.getLocation(); |
| 1955 | break; |
| 1956 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1957 | |
Ted Kremenek | fb50bf5 | 2009-08-03 23:24:57 +0000 | [diff] [blame] | 1958 | // Illegal token! |
David Majnemer | 8896981 | 2014-02-10 19:06:37 +0000 | [diff] [blame] | 1959 | PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_punc) << "unused"; |
Ted Kremenek | fd14fad | 2009-03-23 22:28:25 +0000 | [diff] [blame] | 1960 | return; |
| 1961 | } |
Eli Friedman | f5867dd | 2009-06-05 00:49:58 +0000 | [diff] [blame] | 1962 | |
| 1963 | PP.Lex(Tok); |
Peter Collingbourne | 2f1e36b | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 1964 | if (Tok.isNot(tok::eod)) { |
Eli Friedman | f5867dd | 2009-06-05 00:49:58 +0000 | [diff] [blame] | 1965 | PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) << |
| 1966 | "unused"; |
| 1967 | return; |
| 1968 | } |
| 1969 | |
Ted Kremenek | fd14fad | 2009-03-23 22:28:25 +0000 | [diff] [blame] | 1970 | // Verify that we have a location for the right parenthesis. |
| 1971 | assert(RParenLoc.isValid() && "Valid '#pragma unused' must have ')'"); |
Ted Kremenek | fb50bf5 | 2009-08-03 23:24:57 +0000 | [diff] [blame] | 1972 | assert(!Identifiers.empty() && "Valid '#pragma unused' must have arguments"); |
Ted Kremenek | fd14fad | 2009-03-23 22:28:25 +0000 | [diff] [blame] | 1973 | |
Argyrios Kyrtzidis | ee56962 | 2011-01-17 18:58:44 +0000 | [diff] [blame] | 1974 | // For each identifier token, insert into the token stream a |
| 1975 | // annot_pragma_unused token followed by the identifier token. |
| 1976 | // This allows us to cache a "#pragma unused" that occurs inside an inline |
| 1977 | // C++ member function. |
| 1978 | |
David Blaikie | 2eabcc9 | 2016-02-09 18:52:09 +0000 | [diff] [blame] | 1979 | MutableArrayRef<Token> Toks( |
| 1980 | PP.getPreprocessorAllocator().Allocate<Token>(2 * Identifiers.size()), |
| 1981 | 2 * Identifiers.size()); |
Argyrios Kyrtzidis | ee56962 | 2011-01-17 18:58:44 +0000 | [diff] [blame] | 1982 | for (unsigned i=0; i != Identifiers.size(); i++) { |
| 1983 | Token &pragmaUnusedTok = Toks[2*i], &idTok = Toks[2*i+1]; |
| 1984 | pragmaUnusedTok.startToken(); |
| 1985 | pragmaUnusedTok.setKind(tok::annot_pragma_unused); |
| 1986 | pragmaUnusedTok.setLocation(UnusedLoc); |
| 1987 | idTok = Identifiers[i]; |
| 1988 | } |
David Blaikie | 2eabcc9 | 2016-02-09 18:52:09 +0000 | [diff] [blame] | 1989 | PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true); |
Ted Kremenek | fd14fad | 2009-03-23 22:28:25 +0000 | [diff] [blame] | 1990 | } |
Eli Friedman | f5867dd | 2009-06-05 00:49:58 +0000 | [diff] [blame] | 1991 | |
| 1992 | // #pragma weak identifier |
| 1993 | // #pragma weak identifier '=' identifier |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1994 | void PragmaWeakHandler::HandlePragma(Preprocessor &PP, |
Douglas Gregor | c7d6576 | 2010-09-09 22:45:38 +0000 | [diff] [blame] | 1995 | PragmaIntroducerKind Introducer, |
| 1996 | Token &WeakTok) { |
Eli Friedman | f5867dd | 2009-06-05 00:49:58 +0000 | [diff] [blame] | 1997 | SourceLocation WeakLoc = WeakTok.getLocation(); |
| 1998 | |
| 1999 | Token Tok; |
| 2000 | PP.Lex(Tok); |
| 2001 | if (Tok.isNot(tok::identifier)) { |
| 2002 | PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier) << "weak"; |
| 2003 | return; |
| 2004 | } |
| 2005 | |
Eli Friedman | 68be164 | 2012-10-04 02:36:51 +0000 | [diff] [blame] | 2006 | Token WeakName = Tok; |
| 2007 | bool HasAlias = false; |
| 2008 | Token AliasName; |
Eli Friedman | f5867dd | 2009-06-05 00:49:58 +0000 | [diff] [blame] | 2009 | |
| 2010 | PP.Lex(Tok); |
| 2011 | if (Tok.is(tok::equal)) { |
Eli Friedman | 68be164 | 2012-10-04 02:36:51 +0000 | [diff] [blame] | 2012 | HasAlias = true; |
Eli Friedman | f5867dd | 2009-06-05 00:49:58 +0000 | [diff] [blame] | 2013 | PP.Lex(Tok); |
| 2014 | if (Tok.isNot(tok::identifier)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2015 | PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier) |
Eli Friedman | f5867dd | 2009-06-05 00:49:58 +0000 | [diff] [blame] | 2016 | << "weak"; |
| 2017 | return; |
| 2018 | } |
Eli Friedman | 68be164 | 2012-10-04 02:36:51 +0000 | [diff] [blame] | 2019 | AliasName = Tok; |
Eli Friedman | f5867dd | 2009-06-05 00:49:58 +0000 | [diff] [blame] | 2020 | PP.Lex(Tok); |
| 2021 | } |
| 2022 | |
Peter Collingbourne | 2f1e36b | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 2023 | if (Tok.isNot(tok::eod)) { |
Eli Friedman | f5867dd | 2009-06-05 00:49:58 +0000 | [diff] [blame] | 2024 | PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) << "weak"; |
| 2025 | return; |
| 2026 | } |
| 2027 | |
Eli Friedman | 68be164 | 2012-10-04 02:36:51 +0000 | [diff] [blame] | 2028 | if (HasAlias) { |
David Blaikie | 2eabcc9 | 2016-02-09 18:52:09 +0000 | [diff] [blame] | 2029 | MutableArrayRef<Token> Toks( |
| 2030 | PP.getPreprocessorAllocator().Allocate<Token>(3), 3); |
Eli Friedman | 68be164 | 2012-10-04 02:36:51 +0000 | [diff] [blame] | 2031 | Token &pragmaUnusedTok = Toks[0]; |
| 2032 | pragmaUnusedTok.startToken(); |
| 2033 | pragmaUnusedTok.setKind(tok::annot_pragma_weakalias); |
| 2034 | pragmaUnusedTok.setLocation(WeakLoc); |
David Majnemer | a8f2f1d | 2015-03-19 00:10:23 +0000 | [diff] [blame] | 2035 | pragmaUnusedTok.setAnnotationEndLoc(AliasName.getLocation()); |
Eli Friedman | 68be164 | 2012-10-04 02:36:51 +0000 | [diff] [blame] | 2036 | Toks[1] = WeakName; |
| 2037 | Toks[2] = AliasName; |
David Blaikie | 2eabcc9 | 2016-02-09 18:52:09 +0000 | [diff] [blame] | 2038 | PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true); |
Eli Friedman | f5867dd | 2009-06-05 00:49:58 +0000 | [diff] [blame] | 2039 | } else { |
David Blaikie | 2eabcc9 | 2016-02-09 18:52:09 +0000 | [diff] [blame] | 2040 | MutableArrayRef<Token> Toks( |
| 2041 | PP.getPreprocessorAllocator().Allocate<Token>(2), 2); |
Eli Friedman | 68be164 | 2012-10-04 02:36:51 +0000 | [diff] [blame] | 2042 | Token &pragmaUnusedTok = Toks[0]; |
| 2043 | pragmaUnusedTok.startToken(); |
| 2044 | pragmaUnusedTok.setKind(tok::annot_pragma_weak); |
| 2045 | pragmaUnusedTok.setLocation(WeakLoc); |
David Majnemer | a8f2f1d | 2015-03-19 00:10:23 +0000 | [diff] [blame] | 2046 | pragmaUnusedTok.setAnnotationEndLoc(WeakLoc); |
Eli Friedman | 68be164 | 2012-10-04 02:36:51 +0000 | [diff] [blame] | 2047 | Toks[1] = WeakName; |
David Blaikie | 2eabcc9 | 2016-02-09 18:52:09 +0000 | [diff] [blame] | 2048 | PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true); |
Eli Friedman | f5867dd | 2009-06-05 00:49:58 +0000 | [diff] [blame] | 2049 | } |
| 2050 | } |
Peter Collingbourne | 564c0fa | 2011-02-14 01:42:35 +0000 | [diff] [blame] | 2051 | |
David Chisnall | 0867d9c | 2012-02-18 16:12:34 +0000 | [diff] [blame] | 2052 | // #pragma redefine_extname identifier identifier |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2053 | void PragmaRedefineExtnameHandler::HandlePragma(Preprocessor &PP, |
David Chisnall | 0867d9c | 2012-02-18 16:12:34 +0000 | [diff] [blame] | 2054 | PragmaIntroducerKind Introducer, |
| 2055 | Token &RedefToken) { |
| 2056 | SourceLocation RedefLoc = RedefToken.getLocation(); |
| 2057 | |
| 2058 | Token Tok; |
| 2059 | PP.Lex(Tok); |
| 2060 | if (Tok.isNot(tok::identifier)) { |
| 2061 | PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier) << |
| 2062 | "redefine_extname"; |
| 2063 | return; |
| 2064 | } |
| 2065 | |
Eli Friedman | 68be164 | 2012-10-04 02:36:51 +0000 | [diff] [blame] | 2066 | Token RedefName = Tok; |
David Chisnall | 0867d9c | 2012-02-18 16:12:34 +0000 | [diff] [blame] | 2067 | PP.Lex(Tok); |
Eli Friedman | 68be164 | 2012-10-04 02:36:51 +0000 | [diff] [blame] | 2068 | |
David Chisnall | 0867d9c | 2012-02-18 16:12:34 +0000 | [diff] [blame] | 2069 | if (Tok.isNot(tok::identifier)) { |
| 2070 | PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier) |
| 2071 | << "redefine_extname"; |
| 2072 | return; |
| 2073 | } |
Eli Friedman | 68be164 | 2012-10-04 02:36:51 +0000 | [diff] [blame] | 2074 | |
| 2075 | Token AliasName = Tok; |
David Chisnall | 0867d9c | 2012-02-18 16:12:34 +0000 | [diff] [blame] | 2076 | PP.Lex(Tok); |
| 2077 | |
| 2078 | if (Tok.isNot(tok::eod)) { |
| 2079 | PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) << |
| 2080 | "redefine_extname"; |
| 2081 | return; |
| 2082 | } |
| 2083 | |
David Blaikie | 2eabcc9 | 2016-02-09 18:52:09 +0000 | [diff] [blame] | 2084 | MutableArrayRef<Token> Toks(PP.getPreprocessorAllocator().Allocate<Token>(3), |
| 2085 | 3); |
Eli Friedman | 68be164 | 2012-10-04 02:36:51 +0000 | [diff] [blame] | 2086 | Token &pragmaRedefTok = Toks[0]; |
| 2087 | pragmaRedefTok.startToken(); |
| 2088 | pragmaRedefTok.setKind(tok::annot_pragma_redefine_extname); |
| 2089 | pragmaRedefTok.setLocation(RedefLoc); |
David Majnemer | a8f2f1d | 2015-03-19 00:10:23 +0000 | [diff] [blame] | 2090 | pragmaRedefTok.setAnnotationEndLoc(AliasName.getLocation()); |
Eli Friedman | 68be164 | 2012-10-04 02:36:51 +0000 | [diff] [blame] | 2091 | Toks[1] = RedefName; |
| 2092 | Toks[2] = AliasName; |
David Blaikie | 2eabcc9 | 2016-02-09 18:52:09 +0000 | [diff] [blame] | 2093 | PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true); |
David Chisnall | 0867d9c | 2012-02-18 16:12:34 +0000 | [diff] [blame] | 2094 | } |
| 2095 | |
| 2096 | |
Peter Collingbourne | 564c0fa | 2011-02-14 01:42:35 +0000 | [diff] [blame] | 2097 | void |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2098 | PragmaFPContractHandler::HandlePragma(Preprocessor &PP, |
Peter Collingbourne | 564c0fa | 2011-02-14 01:42:35 +0000 | [diff] [blame] | 2099 | PragmaIntroducerKind Introducer, |
| 2100 | Token &Tok) { |
| 2101 | tok::OnOffSwitch OOS; |
| 2102 | if (PP.LexOnOffSwitch(OOS)) |
| 2103 | return; |
| 2104 | |
David Blaikie | 2eabcc9 | 2016-02-09 18:52:09 +0000 | [diff] [blame] | 2105 | MutableArrayRef<Token> Toks(PP.getPreprocessorAllocator().Allocate<Token>(1), |
| 2106 | 1); |
Eli Friedman | 68be164 | 2012-10-04 02:36:51 +0000 | [diff] [blame] | 2107 | Toks[0].startToken(); |
| 2108 | Toks[0].setKind(tok::annot_pragma_fp_contract); |
| 2109 | Toks[0].setLocation(Tok.getLocation()); |
David Majnemer | a8f2f1d | 2015-03-19 00:10:23 +0000 | [diff] [blame] | 2110 | Toks[0].setAnnotationEndLoc(Tok.getLocation()); |
Eli Friedman | 68be164 | 2012-10-04 02:36:51 +0000 | [diff] [blame] | 2111 | Toks[0].setAnnotationValue(reinterpret_cast<void*>( |
| 2112 | static_cast<uintptr_t>(OOS))); |
David Blaikie | 2eabcc9 | 2016-02-09 18:52:09 +0000 | [diff] [blame] | 2113 | PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true); |
Peter Collingbourne | 564c0fa | 2011-02-14 01:42:35 +0000 | [diff] [blame] | 2114 | } |
Peter Collingbourne | 7ce13fc | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 2115 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2116 | void |
| 2117 | PragmaOpenCLExtensionHandler::HandlePragma(Preprocessor &PP, |
Peter Collingbourne | 7ce13fc | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 2118 | PragmaIntroducerKind Introducer, |
| 2119 | Token &Tok) { |
Tanya Lattner | ee840b8 | 2011-04-14 23:35:31 +0000 | [diff] [blame] | 2120 | PP.LexUnexpandedToken(Tok); |
Peter Collingbourne | 7ce13fc | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 2121 | if (Tok.isNot(tok::identifier)) { |
| 2122 | PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier) << |
| 2123 | "OPENCL"; |
| 2124 | return; |
| 2125 | } |
Yaxun Liu | 5b74665 | 2016-12-18 05:18:55 +0000 | [diff] [blame] | 2126 | IdentifierInfo *Ext = Tok.getIdentifierInfo(); |
Peter Collingbourne | 7ce13fc | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 2127 | SourceLocation NameLoc = Tok.getLocation(); |
| 2128 | |
| 2129 | PP.Lex(Tok); |
| 2130 | if (Tok.isNot(tok::colon)) { |
Yaxun Liu | 5b74665 | 2016-12-18 05:18:55 +0000 | [diff] [blame] | 2131 | PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_colon) << Ext; |
Peter Collingbourne | 7ce13fc | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 2132 | return; |
| 2133 | } |
| 2134 | |
| 2135 | PP.Lex(Tok); |
| 2136 | if (Tok.isNot(tok::identifier)) { |
Yaxun Liu | 5b74665 | 2016-12-18 05:18:55 +0000 | [diff] [blame] | 2137 | PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_predicate) << 0; |
Peter Collingbourne | 7ce13fc | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 2138 | return; |
| 2139 | } |
Yaxun Liu | 5b74665 | 2016-12-18 05:18:55 +0000 | [diff] [blame] | 2140 | IdentifierInfo *Pred = Tok.getIdentifierInfo(); |
Peter Collingbourne | 7ce13fc | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 2141 | |
Yaxun Liu | 5b74665 | 2016-12-18 05:18:55 +0000 | [diff] [blame] | 2142 | OpenCLExtState State; |
| 2143 | if (Pred->isStr("enable")) { |
| 2144 | State = Enable; |
| 2145 | } else if (Pred->isStr("disable")) { |
| 2146 | State = Disable; |
| 2147 | } else if (Pred->isStr("begin")) |
| 2148 | State = Begin; |
| 2149 | else if (Pred->isStr("end")) |
| 2150 | State = End; |
| 2151 | else { |
| 2152 | PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_predicate) |
| 2153 | << Ext->isStr("all"); |
Peter Collingbourne | 7ce13fc | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 2154 | return; |
| 2155 | } |
Pekka Jaaskelainen | 1db1da2 | 2013-10-12 09:29:48 +0000 | [diff] [blame] | 2156 | SourceLocation StateLoc = Tok.getLocation(); |
Peter Collingbourne | 7ce13fc | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 2157 | |
Eli Friedman | 68be164 | 2012-10-04 02:36:51 +0000 | [diff] [blame] | 2158 | PP.Lex(Tok); |
| 2159 | if (Tok.isNot(tok::eod)) { |
| 2160 | PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) << |
| 2161 | "OPENCL EXTENSION"; |
Peter Collingbourne | 7ce13fc | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 2162 | return; |
| 2163 | } |
Eli Friedman | 68be164 | 2012-10-04 02:36:51 +0000 | [diff] [blame] | 2164 | |
Yaxun Liu | 5b74665 | 2016-12-18 05:18:55 +0000 | [diff] [blame] | 2165 | auto Info = PP.getPreprocessorAllocator().Allocate<OpenCLExtData>(1); |
| 2166 | Info->first = Ext; |
| 2167 | Info->second = State; |
David Blaikie | 2eabcc9 | 2016-02-09 18:52:09 +0000 | [diff] [blame] | 2168 | MutableArrayRef<Token> Toks(PP.getPreprocessorAllocator().Allocate<Token>(1), |
| 2169 | 1); |
Eli Friedman | 68be164 | 2012-10-04 02:36:51 +0000 | [diff] [blame] | 2170 | Toks[0].startToken(); |
| 2171 | Toks[0].setKind(tok::annot_pragma_opencl_extension); |
| 2172 | Toks[0].setLocation(NameLoc); |
Yaxun Liu | 5b74665 | 2016-12-18 05:18:55 +0000 | [diff] [blame] | 2173 | Toks[0].setAnnotationValue(static_cast<void*>(Info)); |
David Majnemer | a8f2f1d | 2015-03-19 00:10:23 +0000 | [diff] [blame] | 2174 | Toks[0].setAnnotationEndLoc(StateLoc); |
David Blaikie | 2eabcc9 | 2016-02-09 18:52:09 +0000 | [diff] [blame] | 2175 | PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true); |
Pekka Jaaskelainen | 1db1da2 | 2013-10-12 09:29:48 +0000 | [diff] [blame] | 2176 | |
| 2177 | if (PP.getPPCallbacks()) |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2178 | PP.getPPCallbacks()->PragmaOpenCLExtension(NameLoc, Ext, |
Yaxun Liu | 5b74665 | 2016-12-18 05:18:55 +0000 | [diff] [blame] | 2179 | StateLoc, State); |
Peter Collingbourne | 7ce13fc | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 2180 | } |
| 2181 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2182 | /// Handle '#pragma omp ...' when OpenMP is disabled. |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 2183 | /// |
| 2184 | void |
| 2185 | PragmaNoOpenMPHandler::HandlePragma(Preprocessor &PP, |
| 2186 | PragmaIntroducerKind Introducer, |
| 2187 | Token &FirstTok) { |
Alp Toker | d4a3f0e | 2014-06-15 23:30:39 +0000 | [diff] [blame] | 2188 | if (!PP.getDiagnostics().isIgnored(diag::warn_pragma_omp_ignored, |
| 2189 | FirstTok.getLocation())) { |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 2190 | PP.Diag(FirstTok, diag::warn_pragma_omp_ignored); |
Alp Toker | d576e00 | 2014-06-12 11:13:52 +0000 | [diff] [blame] | 2191 | PP.getDiagnostics().setSeverity(diag::warn_pragma_omp_ignored, |
| 2192 | diag::Severity::Ignored, SourceLocation()); |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 2193 | } |
| 2194 | PP.DiscardUntilEndOfDirective(); |
| 2195 | } |
| 2196 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2197 | /// Handle '#pragma omp ...' when OpenMP is enabled. |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 2198 | /// |
| 2199 | void |
| 2200 | PragmaOpenMPHandler::HandlePragma(Preprocessor &PP, |
| 2201 | PragmaIntroducerKind Introducer, |
| 2202 | Token &FirstTok) { |
| 2203 | SmallVector<Token, 16> Pragma; |
| 2204 | Token Tok; |
| 2205 | Tok.startToken(); |
| 2206 | Tok.setKind(tok::annot_pragma_openmp); |
| 2207 | Tok.setLocation(FirstTok.getLocation()); |
| 2208 | |
Alexey Bataev | 96dae81 | 2018-02-16 18:36:44 +0000 | [diff] [blame] | 2209 | while (Tok.isNot(tok::eod) && Tok.isNot(tok::eof)) { |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 2210 | Pragma.push_back(Tok); |
| 2211 | PP.Lex(Tok); |
Alexey Bataev | 96dae81 | 2018-02-16 18:36:44 +0000 | [diff] [blame] | 2212 | if (Tok.is(tok::annot_pragma_openmp)) { |
| 2213 | PP.Diag(Tok, diag::err_omp_unexpected_directive) << 0; |
| 2214 | unsigned InnerPragmaCnt = 1; |
| 2215 | while (InnerPragmaCnt != 0) { |
| 2216 | PP.Lex(Tok); |
| 2217 | if (Tok.is(tok::annot_pragma_openmp)) |
| 2218 | ++InnerPragmaCnt; |
| 2219 | else if (Tok.is(tok::annot_pragma_openmp_end)) |
| 2220 | --InnerPragmaCnt; |
| 2221 | } |
| 2222 | PP.Lex(Tok); |
| 2223 | } |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 2224 | } |
| 2225 | SourceLocation EodLoc = Tok.getLocation(); |
| 2226 | Tok.startToken(); |
| 2227 | Tok.setKind(tok::annot_pragma_openmp_end); |
| 2228 | Tok.setLocation(EodLoc); |
| 2229 | Pragma.push_back(Tok); |
| 2230 | |
David Blaikie | 2eabcc9 | 2016-02-09 18:52:09 +0000 | [diff] [blame] | 2231 | auto Toks = llvm::make_unique<Token[]>(Pragma.size()); |
| 2232 | std::copy(Pragma.begin(), Pragma.end(), Toks.get()); |
| 2233 | PP.EnterTokenStream(std::move(Toks), Pragma.size(), |
| 2234 | /*DisableMacroExpansion=*/false); |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 2235 | } |
Reid Kleckner | 002562a | 2013-05-06 21:02:12 +0000 | [diff] [blame] | 2236 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2237 | /// Handle '#pragma pointers_to_members' |
David Majnemer | 4bb0980 | 2014-02-10 19:50:15 +0000 | [diff] [blame] | 2238 | // The grammar for this pragma is as follows: |
| 2239 | // |
| 2240 | // <inheritance model> ::= ('single' | 'multiple' | 'virtual') '_inheritance' |
| 2241 | // |
| 2242 | // #pragma pointers_to_members '(' 'best_case' ')' |
| 2243 | // #pragma pointers_to_members '(' 'full_generality' [',' inheritance-model] ')' |
| 2244 | // #pragma pointers_to_members '(' inheritance-model ')' |
| 2245 | void PragmaMSPointersToMembers::HandlePragma(Preprocessor &PP, |
| 2246 | PragmaIntroducerKind Introducer, |
| 2247 | Token &Tok) { |
| 2248 | SourceLocation PointersToMembersLoc = Tok.getLocation(); |
| 2249 | PP.Lex(Tok); |
| 2250 | if (Tok.isNot(tok::l_paren)) { |
| 2251 | PP.Diag(PointersToMembersLoc, diag::warn_pragma_expected_lparen) |
| 2252 | << "pointers_to_members"; |
| 2253 | return; |
| 2254 | } |
| 2255 | PP.Lex(Tok); |
| 2256 | const IdentifierInfo *Arg = Tok.getIdentifierInfo(); |
| 2257 | if (!Arg) { |
| 2258 | PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier) |
| 2259 | << "pointers_to_members"; |
| 2260 | return; |
| 2261 | } |
| 2262 | PP.Lex(Tok); |
| 2263 | |
David Majnemer | 86c318f | 2014-02-11 21:05:00 +0000 | [diff] [blame] | 2264 | LangOptions::PragmaMSPointersToMembersKind RepresentationMethod; |
David Majnemer | 4bb0980 | 2014-02-10 19:50:15 +0000 | [diff] [blame] | 2265 | if (Arg->isStr("best_case")) { |
David Majnemer | 86c318f | 2014-02-11 21:05:00 +0000 | [diff] [blame] | 2266 | RepresentationMethod = LangOptions::PPTMK_BestCase; |
David Majnemer | 4bb0980 | 2014-02-10 19:50:15 +0000 | [diff] [blame] | 2267 | } else { |
| 2268 | if (Arg->isStr("full_generality")) { |
| 2269 | if (Tok.is(tok::comma)) { |
| 2270 | PP.Lex(Tok); |
| 2271 | |
| 2272 | Arg = Tok.getIdentifierInfo(); |
| 2273 | if (!Arg) { |
| 2274 | PP.Diag(Tok.getLocation(), |
| 2275 | diag::err_pragma_pointers_to_members_unknown_kind) |
| 2276 | << Tok.getKind() << /*OnlyInheritanceModels*/ 0; |
| 2277 | return; |
| 2278 | } |
| 2279 | PP.Lex(Tok); |
| 2280 | } else if (Tok.is(tok::r_paren)) { |
| 2281 | // #pragma pointers_to_members(full_generality) implicitly specifies |
| 2282 | // virtual_inheritance. |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 2283 | Arg = nullptr; |
David Majnemer | 86c318f | 2014-02-11 21:05:00 +0000 | [diff] [blame] | 2284 | RepresentationMethod = LangOptions::PPTMK_FullGeneralityVirtualInheritance; |
David Majnemer | 4bb0980 | 2014-02-10 19:50:15 +0000 | [diff] [blame] | 2285 | } else { |
| 2286 | PP.Diag(Tok.getLocation(), diag::err_expected_punc) |
| 2287 | << "full_generality"; |
| 2288 | return; |
| 2289 | } |
| 2290 | } |
| 2291 | |
| 2292 | if (Arg) { |
| 2293 | if (Arg->isStr("single_inheritance")) { |
David Majnemer | 86c318f | 2014-02-11 21:05:00 +0000 | [diff] [blame] | 2294 | RepresentationMethod = |
| 2295 | LangOptions::PPTMK_FullGeneralitySingleInheritance; |
David Majnemer | 4bb0980 | 2014-02-10 19:50:15 +0000 | [diff] [blame] | 2296 | } else if (Arg->isStr("multiple_inheritance")) { |
David Majnemer | 86c318f | 2014-02-11 21:05:00 +0000 | [diff] [blame] | 2297 | RepresentationMethod = |
| 2298 | LangOptions::PPTMK_FullGeneralityMultipleInheritance; |
David Majnemer | 4bb0980 | 2014-02-10 19:50:15 +0000 | [diff] [blame] | 2299 | } else if (Arg->isStr("virtual_inheritance")) { |
David Majnemer | 86c318f | 2014-02-11 21:05:00 +0000 | [diff] [blame] | 2300 | RepresentationMethod = |
| 2301 | LangOptions::PPTMK_FullGeneralityVirtualInheritance; |
David Majnemer | 4bb0980 | 2014-02-10 19:50:15 +0000 | [diff] [blame] | 2302 | } else { |
| 2303 | PP.Diag(Tok.getLocation(), |
| 2304 | diag::err_pragma_pointers_to_members_unknown_kind) |
| 2305 | << Arg << /*HasPointerDeclaration*/ 1; |
| 2306 | return; |
| 2307 | } |
| 2308 | } |
| 2309 | } |
| 2310 | |
| 2311 | if (Tok.isNot(tok::r_paren)) { |
| 2312 | PP.Diag(Tok.getLocation(), diag::err_expected_rparen_after) |
| 2313 | << (Arg ? Arg->getName() : "full_generality"); |
| 2314 | return; |
| 2315 | } |
| 2316 | |
David Majnemer | a8f2f1d | 2015-03-19 00:10:23 +0000 | [diff] [blame] | 2317 | SourceLocation EndLoc = Tok.getLocation(); |
David Majnemer | 4bb0980 | 2014-02-10 19:50:15 +0000 | [diff] [blame] | 2318 | PP.Lex(Tok); |
| 2319 | if (Tok.isNot(tok::eod)) { |
| 2320 | PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) |
| 2321 | << "pointers_to_members"; |
| 2322 | return; |
| 2323 | } |
| 2324 | |
| 2325 | Token AnnotTok; |
| 2326 | AnnotTok.startToken(); |
| 2327 | AnnotTok.setKind(tok::annot_pragma_ms_pointers_to_members); |
| 2328 | AnnotTok.setLocation(PointersToMembersLoc); |
David Majnemer | a8f2f1d | 2015-03-19 00:10:23 +0000 | [diff] [blame] | 2329 | AnnotTok.setAnnotationEndLoc(EndLoc); |
David Majnemer | 4bb0980 | 2014-02-10 19:50:15 +0000 | [diff] [blame] | 2330 | AnnotTok.setAnnotationValue( |
| 2331 | reinterpret_cast<void *>(static_cast<uintptr_t>(RepresentationMethod))); |
| 2332 | PP.EnterToken(AnnotTok); |
| 2333 | } |
| 2334 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2335 | /// Handle '#pragma vtordisp' |
Reid Kleckner | c0dca6d | 2014-02-12 23:50:26 +0000 | [diff] [blame] | 2336 | // The grammar for this pragma is as follows: |
| 2337 | // |
| 2338 | // <vtordisp-mode> ::= ('off' | 'on' | '0' | '1' | '2' ) |
| 2339 | // |
| 2340 | // #pragma vtordisp '(' ['push' ','] vtordisp-mode ')' |
| 2341 | // #pragma vtordisp '(' 'pop' ')' |
| 2342 | // #pragma vtordisp '(' ')' |
| 2343 | void PragmaMSVtorDisp::HandlePragma(Preprocessor &PP, |
| 2344 | PragmaIntroducerKind Introducer, |
| 2345 | Token &Tok) { |
| 2346 | SourceLocation VtorDispLoc = Tok.getLocation(); |
| 2347 | PP.Lex(Tok); |
| 2348 | if (Tok.isNot(tok::l_paren)) { |
| 2349 | PP.Diag(VtorDispLoc, diag::warn_pragma_expected_lparen) << "vtordisp"; |
| 2350 | return; |
| 2351 | } |
| 2352 | PP.Lex(Tok); |
| 2353 | |
Denis Zobnin | 2290dac | 2016-04-29 11:27:00 +0000 | [diff] [blame] | 2354 | Sema::PragmaMsStackAction Action = Sema::PSK_Set; |
Reid Kleckner | c0dca6d | 2014-02-12 23:50:26 +0000 | [diff] [blame] | 2355 | const IdentifierInfo *II = Tok.getIdentifierInfo(); |
| 2356 | if (II) { |
| 2357 | if (II->isStr("push")) { |
| 2358 | // #pragma vtordisp(push, mode) |
| 2359 | PP.Lex(Tok); |
| 2360 | if (Tok.isNot(tok::comma)) { |
| 2361 | PP.Diag(VtorDispLoc, diag::warn_pragma_expected_punc) << "vtordisp"; |
| 2362 | return; |
| 2363 | } |
| 2364 | PP.Lex(Tok); |
Denis Zobnin | 2290dac | 2016-04-29 11:27:00 +0000 | [diff] [blame] | 2365 | Action = Sema::PSK_Push_Set; |
Reid Kleckner | c0dca6d | 2014-02-12 23:50:26 +0000 | [diff] [blame] | 2366 | // not push, could be on/off |
| 2367 | } else if (II->isStr("pop")) { |
| 2368 | // #pragma vtordisp(pop) |
| 2369 | PP.Lex(Tok); |
Denis Zobnin | 2290dac | 2016-04-29 11:27:00 +0000 | [diff] [blame] | 2370 | Action = Sema::PSK_Pop; |
Reid Kleckner | c0dca6d | 2014-02-12 23:50:26 +0000 | [diff] [blame] | 2371 | } |
| 2372 | // not push or pop, could be on/off |
| 2373 | } else { |
| 2374 | if (Tok.is(tok::r_paren)) { |
| 2375 | // #pragma vtordisp() |
Denis Zobnin | 2290dac | 2016-04-29 11:27:00 +0000 | [diff] [blame] | 2376 | Action = Sema::PSK_Reset; |
Reid Kleckner | c0dca6d | 2014-02-12 23:50:26 +0000 | [diff] [blame] | 2377 | } |
| 2378 | } |
| 2379 | |
| 2380 | |
Reid Kleckner | a4b3b2d | 2014-02-13 00:44:34 +0000 | [diff] [blame] | 2381 | uint64_t Value = 0; |
Denis Zobnin | 2290dac | 2016-04-29 11:27:00 +0000 | [diff] [blame] | 2382 | if (Action & Sema::PSK_Push || Action & Sema::PSK_Set) { |
Reid Kleckner | c0dca6d | 2014-02-12 23:50:26 +0000 | [diff] [blame] | 2383 | const IdentifierInfo *II = Tok.getIdentifierInfo(); |
| 2384 | if (II && II->isStr("off")) { |
| 2385 | PP.Lex(Tok); |
| 2386 | Value = 0; |
| 2387 | } else if (II && II->isStr("on")) { |
| 2388 | PP.Lex(Tok); |
| 2389 | Value = 1; |
| 2390 | } else if (Tok.is(tok::numeric_constant) && |
| 2391 | PP.parseSimpleIntegerLiteral(Tok, Value)) { |
| 2392 | if (Value > 2) { |
| 2393 | PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_integer) |
| 2394 | << 0 << 2 << "vtordisp"; |
| 2395 | return; |
| 2396 | } |
| 2397 | } else { |
| 2398 | PP.Diag(Tok.getLocation(), diag::warn_pragma_invalid_action) |
| 2399 | << "vtordisp"; |
| 2400 | return; |
| 2401 | } |
| 2402 | } |
| 2403 | |
| 2404 | // Finish the pragma: ')' $ |
| 2405 | if (Tok.isNot(tok::r_paren)) { |
| 2406 | PP.Diag(VtorDispLoc, diag::warn_pragma_expected_rparen) << "vtordisp"; |
| 2407 | return; |
| 2408 | } |
David Majnemer | a8f2f1d | 2015-03-19 00:10:23 +0000 | [diff] [blame] | 2409 | SourceLocation EndLoc = Tok.getLocation(); |
Reid Kleckner | c0dca6d | 2014-02-12 23:50:26 +0000 | [diff] [blame] | 2410 | PP.Lex(Tok); |
| 2411 | if (Tok.isNot(tok::eod)) { |
| 2412 | PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) |
| 2413 | << "vtordisp"; |
| 2414 | return; |
| 2415 | } |
| 2416 | |
| 2417 | // Enter the annotation. |
| 2418 | Token AnnotTok; |
| 2419 | AnnotTok.startToken(); |
| 2420 | AnnotTok.setKind(tok::annot_pragma_ms_vtordisp); |
| 2421 | AnnotTok.setLocation(VtorDispLoc); |
David Majnemer | a8f2f1d | 2015-03-19 00:10:23 +0000 | [diff] [blame] | 2422 | AnnotTok.setAnnotationEndLoc(EndLoc); |
Reid Kleckner | a4b3b2d | 2014-02-13 00:44:34 +0000 | [diff] [blame] | 2423 | AnnotTok.setAnnotationValue(reinterpret_cast<void *>( |
Denis Zobnin | 2290dac | 2016-04-29 11:27:00 +0000 | [diff] [blame] | 2424 | static_cast<uintptr_t>((Action << 16) | (Value & 0xFFFF)))); |
Reid Kleckner | c0dca6d | 2014-02-12 23:50:26 +0000 | [diff] [blame] | 2425 | PP.EnterToken(AnnotTok); |
| 2426 | } |
| 2427 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2428 | /// Handle all MS pragmas. Simply forwards the tokens after inserting |
Warren Hunt | c3b1896 | 2014-04-08 22:30:47 +0000 | [diff] [blame] | 2429 | /// an annotation token. |
| 2430 | void PragmaMSPragma::HandlePragma(Preprocessor &PP, |
| 2431 | PragmaIntroducerKind Introducer, |
| 2432 | Token &Tok) { |
| 2433 | Token EoF, AnnotTok; |
| 2434 | EoF.startToken(); |
| 2435 | EoF.setKind(tok::eof); |
| 2436 | AnnotTok.startToken(); |
| 2437 | AnnotTok.setKind(tok::annot_pragma_ms_pragma); |
| 2438 | AnnotTok.setLocation(Tok.getLocation()); |
David Majnemer | a8f2f1d | 2015-03-19 00:10:23 +0000 | [diff] [blame] | 2439 | AnnotTok.setAnnotationEndLoc(Tok.getLocation()); |
Warren Hunt | c3b1896 | 2014-04-08 22:30:47 +0000 | [diff] [blame] | 2440 | SmallVector<Token, 8> TokenVector; |
| 2441 | // Suck up all of the tokens before the eod. |
David Majnemer | a8f2f1d | 2015-03-19 00:10:23 +0000 | [diff] [blame] | 2442 | for (; Tok.isNot(tok::eod); PP.Lex(Tok)) { |
Warren Hunt | c3b1896 | 2014-04-08 22:30:47 +0000 | [diff] [blame] | 2443 | TokenVector.push_back(Tok); |
David Majnemer | a8f2f1d | 2015-03-19 00:10:23 +0000 | [diff] [blame] | 2444 | AnnotTok.setAnnotationEndLoc(Tok.getLocation()); |
| 2445 | } |
Alexander Kornienko | 2a8c18d | 2018-04-06 15:14:32 +0000 | [diff] [blame] | 2446 | // Add a sentinel EoF token to the end of the list. |
Warren Hunt | c3b1896 | 2014-04-08 22:30:47 +0000 | [diff] [blame] | 2447 | TokenVector.push_back(EoF); |
| 2448 | // We must allocate this array with new because EnterTokenStream is going to |
| 2449 | // delete it later. |
David Blaikie | 2eabcc9 | 2016-02-09 18:52:09 +0000 | [diff] [blame] | 2450 | auto TokenArray = llvm::make_unique<Token[]>(TokenVector.size()); |
| 2451 | std::copy(TokenVector.begin(), TokenVector.end(), TokenArray.get()); |
Warren Hunt | c3b1896 | 2014-04-08 22:30:47 +0000 | [diff] [blame] | 2452 | auto Value = new (PP.getPreprocessorAllocator()) |
David Blaikie | 2eabcc9 | 2016-02-09 18:52:09 +0000 | [diff] [blame] | 2453 | std::pair<std::unique_ptr<Token[]>, size_t>(std::move(TokenArray), |
| 2454 | TokenVector.size()); |
Warren Hunt | c3b1896 | 2014-04-08 22:30:47 +0000 | [diff] [blame] | 2455 | AnnotTok.setAnnotationValue(Value); |
| 2456 | PP.EnterToken(AnnotTok); |
Reid Kleckner | d3923aa | 2014-04-03 19:04:24 +0000 | [diff] [blame] | 2457 | } |
| 2458 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2459 | /// Handle the Microsoft \#pragma detect_mismatch extension. |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 2460 | /// |
| 2461 | /// The syntax is: |
| 2462 | /// \code |
| 2463 | /// #pragma detect_mismatch("name", "value") |
| 2464 | /// \endcode |
| 2465 | /// Where 'name' and 'value' are quoted strings. The values are embedded in |
| 2466 | /// the object file and passed along to the linker. If the linker detects a |
| 2467 | /// mismatch in the object file's values for the given name, a LNK2038 error |
| 2468 | /// is emitted. See MSDN for more details. |
| 2469 | void PragmaDetectMismatchHandler::HandlePragma(Preprocessor &PP, |
| 2470 | PragmaIntroducerKind Introducer, |
| 2471 | Token &Tok) { |
Nico Weber | cbbaeb1 | 2016-03-02 19:28:54 +0000 | [diff] [blame] | 2472 | SourceLocation DetectMismatchLoc = Tok.getLocation(); |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 2473 | PP.Lex(Tok); |
| 2474 | if (Tok.isNot(tok::l_paren)) { |
Nico Weber | cbbaeb1 | 2016-03-02 19:28:54 +0000 | [diff] [blame] | 2475 | PP.Diag(DetectMismatchLoc, diag::err_expected) << tok::l_paren; |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 2476 | return; |
| 2477 | } |
| 2478 | |
| 2479 | // Read the name to embed, which must be a string literal. |
| 2480 | std::string NameString; |
| 2481 | if (!PP.LexStringLiteral(Tok, NameString, |
| 2482 | "pragma detect_mismatch", |
| 2483 | /*MacroExpansion=*/true)) |
| 2484 | return; |
| 2485 | |
| 2486 | // Read the comma followed by a second string literal. |
| 2487 | std::string ValueString; |
| 2488 | if (Tok.isNot(tok::comma)) { |
| 2489 | PP.Diag(Tok.getLocation(), diag::err_pragma_detect_mismatch_malformed); |
| 2490 | return; |
| 2491 | } |
| 2492 | |
| 2493 | if (!PP.LexStringLiteral(Tok, ValueString, "pragma detect_mismatch", |
| 2494 | /*MacroExpansion=*/true)) |
| 2495 | return; |
| 2496 | |
| 2497 | if (Tok.isNot(tok::r_paren)) { |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 2498 | PP.Diag(Tok.getLocation(), diag::err_expected) << tok::r_paren; |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 2499 | return; |
| 2500 | } |
| 2501 | PP.Lex(Tok); // Eat the r_paren. |
| 2502 | |
| 2503 | if (Tok.isNot(tok::eod)) { |
| 2504 | PP.Diag(Tok.getLocation(), diag::err_pragma_detect_mismatch_malformed); |
| 2505 | return; |
| 2506 | } |
| 2507 | |
Reid Kleckner | 71966c9 | 2014-02-20 23:37:45 +0000 | [diff] [blame] | 2508 | // If the pragma is lexically sound, notify any interested PPCallbacks. |
| 2509 | if (PP.getPPCallbacks()) |
Nico Weber | cbbaeb1 | 2016-03-02 19:28:54 +0000 | [diff] [blame] | 2510 | PP.getPPCallbacks()->PragmaDetectMismatch(DetectMismatchLoc, NameString, |
Reid Kleckner | 71966c9 | 2014-02-20 23:37:45 +0000 | [diff] [blame] | 2511 | ValueString); |
| 2512 | |
Nico Weber | cbbaeb1 | 2016-03-02 19:28:54 +0000 | [diff] [blame] | 2513 | Actions.ActOnPragmaDetectMismatch(DetectMismatchLoc, NameString, ValueString); |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 2514 | } |
| 2515 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2516 | /// Handle the microsoft \#pragma comment extension. |
Reid Kleckner | 002562a | 2013-05-06 21:02:12 +0000 | [diff] [blame] | 2517 | /// |
| 2518 | /// The syntax is: |
| 2519 | /// \code |
| 2520 | /// #pragma comment(linker, "foo") |
| 2521 | /// \endcode |
| 2522 | /// 'linker' is one of five identifiers: compiler, exestr, lib, linker, user. |
| 2523 | /// "foo" is a string, which is fully macro expanded, and permits string |
| 2524 | /// concatenation, embedded escape characters etc. See MSDN for more details. |
| 2525 | void PragmaCommentHandler::HandlePragma(Preprocessor &PP, |
| 2526 | PragmaIntroducerKind Introducer, |
| 2527 | Token &Tok) { |
| 2528 | SourceLocation CommentLoc = Tok.getLocation(); |
| 2529 | PP.Lex(Tok); |
| 2530 | if (Tok.isNot(tok::l_paren)) { |
| 2531 | PP.Diag(CommentLoc, diag::err_pragma_comment_malformed); |
| 2532 | return; |
| 2533 | } |
| 2534 | |
| 2535 | // Read the identifier. |
| 2536 | PP.Lex(Tok); |
| 2537 | if (Tok.isNot(tok::identifier)) { |
| 2538 | PP.Diag(CommentLoc, diag::err_pragma_comment_malformed); |
| 2539 | return; |
| 2540 | } |
| 2541 | |
| 2542 | // Verify that this is one of the 5 whitelisted options. |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2543 | IdentifierInfo *II = Tok.getIdentifierInfo(); |
Nico Weber | 6622029 | 2016-03-02 17:28:48 +0000 | [diff] [blame] | 2544 | PragmaMSCommentKind Kind = |
| 2545 | llvm::StringSwitch<PragmaMSCommentKind>(II->getName()) |
| 2546 | .Case("linker", PCK_Linker) |
| 2547 | .Case("lib", PCK_Lib) |
| 2548 | .Case("compiler", PCK_Compiler) |
| 2549 | .Case("exestr", PCK_ExeStr) |
| 2550 | .Case("user", PCK_User) |
| 2551 | .Default(PCK_Unknown); |
| 2552 | if (Kind == PCK_Unknown) { |
Reid Kleckner | 002562a | 2013-05-06 21:02:12 +0000 | [diff] [blame] | 2553 | PP.Diag(Tok.getLocation(), diag::err_pragma_comment_unknown_kind); |
| 2554 | return; |
| 2555 | } |
| 2556 | |
Saleem Abdulrasool | fd4db53 | 2018-02-07 01:46:46 +0000 | [diff] [blame] | 2557 | if (PP.getTargetInfo().getTriple().isOSBinFormatELF() && Kind != PCK_Lib) { |
| 2558 | PP.Diag(Tok.getLocation(), diag::warn_pragma_comment_ignored) |
| 2559 | << II->getName(); |
| 2560 | return; |
| 2561 | } |
| 2562 | |
Yunzhong Gao | 99efc03 | 2015-03-23 20:41:42 +0000 | [diff] [blame] | 2563 | // On PS4, issue a warning about any pragma comments other than |
| 2564 | // #pragma comment lib. |
Nico Weber | 6622029 | 2016-03-02 17:28:48 +0000 | [diff] [blame] | 2565 | if (PP.getTargetInfo().getTriple().isPS4() && Kind != PCK_Lib) { |
Yunzhong Gao | 99efc03 | 2015-03-23 20:41:42 +0000 | [diff] [blame] | 2566 | PP.Diag(Tok.getLocation(), diag::warn_pragma_comment_ignored) |
| 2567 | << II->getName(); |
| 2568 | return; |
| 2569 | } |
| 2570 | |
Reid Kleckner | 002562a | 2013-05-06 21:02:12 +0000 | [diff] [blame] | 2571 | // Read the optional string if present. |
| 2572 | PP.Lex(Tok); |
| 2573 | std::string ArgumentString; |
| 2574 | if (Tok.is(tok::comma) && !PP.LexStringLiteral(Tok, ArgumentString, |
| 2575 | "pragma comment", |
| 2576 | /*MacroExpansion=*/true)) |
| 2577 | return; |
| 2578 | |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2579 | // FIXME: warn that 'exestr' is deprecated. |
Reid Kleckner | 002562a | 2013-05-06 21:02:12 +0000 | [diff] [blame] | 2580 | // FIXME: If the kind is "compiler" warn if the string is present (it is |
| 2581 | // ignored). |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2582 | // The MSDN docs say that "lib" and "linker" require a string and have a short |
| 2583 | // whitelist of linker options they support, but in practice MSVC doesn't |
| 2584 | // issue a diagnostic. Therefore neither does clang. |
Reid Kleckner | 002562a | 2013-05-06 21:02:12 +0000 | [diff] [blame] | 2585 | |
| 2586 | if (Tok.isNot(tok::r_paren)) { |
| 2587 | PP.Diag(Tok.getLocation(), diag::err_pragma_comment_malformed); |
| 2588 | return; |
| 2589 | } |
| 2590 | PP.Lex(Tok); // eat the r_paren. |
| 2591 | |
| 2592 | if (Tok.isNot(tok::eod)) { |
| 2593 | PP.Diag(Tok.getLocation(), diag::err_pragma_comment_malformed); |
| 2594 | return; |
| 2595 | } |
| 2596 | |
Reid Kleckner | 71966c9 | 2014-02-20 23:37:45 +0000 | [diff] [blame] | 2597 | // If the pragma is lexically sound, notify any interested PPCallbacks. |
| 2598 | if (PP.getPPCallbacks()) |
| 2599 | PP.getPPCallbacks()->PragmaComment(CommentLoc, II, ArgumentString); |
| 2600 | |
Nico Weber | 6622029 | 2016-03-02 17:28:48 +0000 | [diff] [blame] | 2601 | Actions.ActOnPragmaMSComment(CommentLoc, Kind, ArgumentString); |
Reid Kleckner | 002562a | 2013-05-06 21:02:12 +0000 | [diff] [blame] | 2602 | } |
Dario Domizioli | 13a0a38 | 2014-05-23 12:13:25 +0000 | [diff] [blame] | 2603 | |
| 2604 | // #pragma clang optimize off |
| 2605 | // #pragma clang optimize on |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2606 | void PragmaOptimizeHandler::HandlePragma(Preprocessor &PP, |
Dario Domizioli | 13a0a38 | 2014-05-23 12:13:25 +0000 | [diff] [blame] | 2607 | PragmaIntroducerKind Introducer, |
| 2608 | Token &FirstToken) { |
| 2609 | Token Tok; |
| 2610 | PP.Lex(Tok); |
| 2611 | if (Tok.is(tok::eod)) { |
Mark Heffernan | bd26f5e | 2014-07-21 18:08:34 +0000 | [diff] [blame] | 2612 | PP.Diag(Tok.getLocation(), diag::err_pragma_missing_argument) |
Mark Heffernan | 450c238 | 2014-07-23 17:31:31 +0000 | [diff] [blame] | 2613 | << "clang optimize" << /*Expected=*/true << "'on' or 'off'"; |
Dario Domizioli | 13a0a38 | 2014-05-23 12:13:25 +0000 | [diff] [blame] | 2614 | return; |
| 2615 | } |
| 2616 | if (Tok.isNot(tok::identifier)) { |
| 2617 | PP.Diag(Tok.getLocation(), diag::err_pragma_optimize_invalid_argument) |
| 2618 | << PP.getSpelling(Tok); |
| 2619 | return; |
| 2620 | } |
| 2621 | const IdentifierInfo *II = Tok.getIdentifierInfo(); |
| 2622 | // The only accepted values are 'on' or 'off'. |
| 2623 | bool IsOn = false; |
| 2624 | if (II->isStr("on")) { |
| 2625 | IsOn = true; |
| 2626 | } else if (!II->isStr("off")) { |
| 2627 | PP.Diag(Tok.getLocation(), diag::err_pragma_optimize_invalid_argument) |
| 2628 | << PP.getSpelling(Tok); |
| 2629 | return; |
| 2630 | } |
| 2631 | PP.Lex(Tok); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2632 | |
Dario Domizioli | 13a0a38 | 2014-05-23 12:13:25 +0000 | [diff] [blame] | 2633 | if (Tok.isNot(tok::eod)) { |
| 2634 | PP.Diag(Tok.getLocation(), diag::err_pragma_optimize_extra_argument) |
| 2635 | << PP.getSpelling(Tok); |
| 2636 | return; |
| 2637 | } |
Eli Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 2638 | |
| 2639 | Actions.ActOnPragmaOptimize(IsOn, FirstToken.getLocation()); |
| 2640 | } |
| 2641 | |
Adam Nemet | 60d3264 | 2017-04-04 21:18:36 +0000 | [diff] [blame] | 2642 | namespace { |
| 2643 | /// Used as the annotation value for tok::annot_pragma_fp. |
| 2644 | struct TokFPAnnotValue { |
| 2645 | enum FlagKinds { Contract }; |
| 2646 | enum FlagValues { On, Off, Fast }; |
| 2647 | |
| 2648 | FlagKinds FlagKind; |
| 2649 | FlagValues FlagValue; |
| 2650 | }; |
| 2651 | } // end anonymous namespace |
| 2652 | |
| 2653 | void PragmaFPHandler::HandlePragma(Preprocessor &PP, |
| 2654 | PragmaIntroducerKind Introducer, |
| 2655 | Token &Tok) { |
| 2656 | // fp |
| 2657 | Token PragmaName = Tok; |
| 2658 | SmallVector<Token, 1> TokenList; |
| 2659 | |
| 2660 | PP.Lex(Tok); |
| 2661 | if (Tok.isNot(tok::identifier)) { |
| 2662 | PP.Diag(Tok.getLocation(), diag::err_pragma_fp_invalid_option) |
| 2663 | << /*MissingOption=*/true << ""; |
| 2664 | return; |
| 2665 | } |
| 2666 | |
| 2667 | while (Tok.is(tok::identifier)) { |
| 2668 | IdentifierInfo *OptionInfo = Tok.getIdentifierInfo(); |
| 2669 | |
| 2670 | auto FlagKind = |
| 2671 | llvm::StringSwitch<llvm::Optional<TokFPAnnotValue::FlagKinds>>( |
| 2672 | OptionInfo->getName()) |
| 2673 | .Case("contract", TokFPAnnotValue::Contract) |
| 2674 | .Default(None); |
| 2675 | if (!FlagKind) { |
| 2676 | PP.Diag(Tok.getLocation(), diag::err_pragma_fp_invalid_option) |
| 2677 | << /*MissingOption=*/false << OptionInfo; |
| 2678 | return; |
| 2679 | } |
| 2680 | PP.Lex(Tok); |
| 2681 | |
| 2682 | // Read '(' |
| 2683 | if (Tok.isNot(tok::l_paren)) { |
| 2684 | PP.Diag(Tok.getLocation(), diag::err_expected) << tok::l_paren; |
| 2685 | return; |
| 2686 | } |
| 2687 | PP.Lex(Tok); |
| 2688 | |
| 2689 | if (Tok.isNot(tok::identifier)) { |
| 2690 | PP.Diag(Tok.getLocation(), diag::err_pragma_fp_invalid_argument) |
| 2691 | << PP.getSpelling(Tok) << OptionInfo->getName(); |
| 2692 | return; |
| 2693 | } |
| 2694 | const IdentifierInfo *II = Tok.getIdentifierInfo(); |
| 2695 | |
| 2696 | auto FlagValue = |
| 2697 | llvm::StringSwitch<llvm::Optional<TokFPAnnotValue::FlagValues>>( |
| 2698 | II->getName()) |
| 2699 | .Case("on", TokFPAnnotValue::On) |
| 2700 | .Case("off", TokFPAnnotValue::Off) |
| 2701 | .Case("fast", TokFPAnnotValue::Fast) |
| 2702 | .Default(llvm::None); |
| 2703 | |
| 2704 | if (!FlagValue) { |
| 2705 | PP.Diag(Tok.getLocation(), diag::err_pragma_fp_invalid_argument) |
| 2706 | << PP.getSpelling(Tok) << OptionInfo->getName(); |
| 2707 | return; |
| 2708 | } |
| 2709 | PP.Lex(Tok); |
| 2710 | |
| 2711 | // Read ')' |
| 2712 | if (Tok.isNot(tok::r_paren)) { |
| 2713 | PP.Diag(Tok.getLocation(), diag::err_expected) << tok::r_paren; |
| 2714 | return; |
| 2715 | } |
| 2716 | PP.Lex(Tok); |
| 2717 | |
| 2718 | auto *AnnotValue = new (PP.getPreprocessorAllocator()) |
| 2719 | TokFPAnnotValue{*FlagKind, *FlagValue}; |
| 2720 | // Generate the loop hint token. |
| 2721 | Token FPTok; |
| 2722 | FPTok.startToken(); |
| 2723 | FPTok.setKind(tok::annot_pragma_fp); |
| 2724 | FPTok.setLocation(PragmaName.getLocation()); |
| 2725 | FPTok.setAnnotationEndLoc(PragmaName.getLocation()); |
| 2726 | FPTok.setAnnotationValue(reinterpret_cast<void *>(AnnotValue)); |
| 2727 | TokenList.push_back(FPTok); |
| 2728 | } |
| 2729 | |
| 2730 | if (Tok.isNot(tok::eod)) { |
| 2731 | PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) |
| 2732 | << "clang fp"; |
| 2733 | return; |
| 2734 | } |
| 2735 | |
| 2736 | auto TokenArray = llvm::make_unique<Token[]>(TokenList.size()); |
| 2737 | std::copy(TokenList.begin(), TokenList.end(), TokenArray.get()); |
| 2738 | |
| 2739 | PP.EnterTokenStream(std::move(TokenArray), TokenList.size(), |
| 2740 | /*DisableMacroExpansion=*/false); |
| 2741 | } |
| 2742 | |
| 2743 | void Parser::HandlePragmaFP() { |
| 2744 | assert(Tok.is(tok::annot_pragma_fp)); |
| 2745 | auto *AnnotValue = |
| 2746 | reinterpret_cast<TokFPAnnotValue *>(Tok.getAnnotationValue()); |
| 2747 | |
| 2748 | LangOptions::FPContractModeKind FPC; |
| 2749 | switch (AnnotValue->FlagValue) { |
| 2750 | case TokFPAnnotValue::On: |
| 2751 | FPC = LangOptions::FPC_On; |
| 2752 | break; |
| 2753 | case TokFPAnnotValue::Fast: |
| 2754 | FPC = LangOptions::FPC_Fast; |
| 2755 | break; |
| 2756 | case TokFPAnnotValue::Off: |
| 2757 | FPC = LangOptions::FPC_Off; |
| 2758 | break; |
| 2759 | } |
| 2760 | |
| 2761 | Actions.ActOnPragmaFPContract(FPC); |
Richard Smith | af3b325 | 2017-05-18 19:21:48 +0000 | [diff] [blame] | 2762 | ConsumeAnnotationToken(); |
Adam Nemet | 60d3264 | 2017-04-04 21:18:36 +0000 | [diff] [blame] | 2763 | } |
| 2764 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2765 | /// Parses loop or unroll pragma hint value and fills in Info. |
Tyler Nowicki | 0c9b34b | 2014-07-31 20:15:14 +0000 | [diff] [blame] | 2766 | static bool ParseLoopHintValue(Preprocessor &PP, Token &Tok, Token PragmaName, |
| 2767 | Token Option, bool ValueInParens, |
Mark Heffernan | bd26f5e | 2014-07-21 18:08:34 +0000 | [diff] [blame] | 2768 | PragmaLoopHintInfo &Info) { |
Tyler Nowicki | c724a83e | 2014-10-12 20:46:07 +0000 | [diff] [blame] | 2769 | SmallVector<Token, 1> ValueList; |
| 2770 | int OpenParens = ValueInParens ? 1 : 0; |
| 2771 | // Read constant expression. |
| 2772 | while (Tok.isNot(tok::eod)) { |
| 2773 | if (Tok.is(tok::l_paren)) |
| 2774 | OpenParens++; |
| 2775 | else if (Tok.is(tok::r_paren)) { |
| 2776 | OpenParens--; |
| 2777 | if (OpenParens == 0 && ValueInParens) |
| 2778 | break; |
Mark Heffernan | bd26f5e | 2014-07-21 18:08:34 +0000 | [diff] [blame] | 2779 | } |
Mark Heffernan | bd26f5e | 2014-07-21 18:08:34 +0000 | [diff] [blame] | 2780 | |
Tyler Nowicki | c724a83e | 2014-10-12 20:46:07 +0000 | [diff] [blame] | 2781 | ValueList.push_back(Tok); |
| 2782 | PP.Lex(Tok); |
| 2783 | } |
Mark Heffernan | bd26f5e | 2014-07-21 18:08:34 +0000 | [diff] [blame] | 2784 | |
| 2785 | if (ValueInParens) { |
Tyler Nowicki | 0c9b34b | 2014-07-31 20:15:14 +0000 | [diff] [blame] | 2786 | // Read ')' |
Mark Heffernan | bd26f5e | 2014-07-21 18:08:34 +0000 | [diff] [blame] | 2787 | if (Tok.isNot(tok::r_paren)) { |
| 2788 | PP.Diag(Tok.getLocation(), diag::err_expected) << tok::r_paren; |
| 2789 | return true; |
| 2790 | } |
Tyler Nowicki | 0c9b34b | 2014-07-31 20:15:14 +0000 | [diff] [blame] | 2791 | PP.Lex(Tok); |
Mark Heffernan | bd26f5e | 2014-07-21 18:08:34 +0000 | [diff] [blame] | 2792 | } |
| 2793 | |
Tyler Nowicki | c724a83e | 2014-10-12 20:46:07 +0000 | [diff] [blame] | 2794 | Token EOFTok; |
| 2795 | EOFTok.startToken(); |
| 2796 | EOFTok.setKind(tok::eof); |
| 2797 | EOFTok.setLocation(Tok.getLocation()); |
| 2798 | ValueList.push_back(EOFTok); // Terminates expression for parsing. |
| 2799 | |
Benjamin Kramer | fa7f855 | 2015-08-05 09:39:57 +0000 | [diff] [blame] | 2800 | Info.Toks = llvm::makeArrayRef(ValueList).copy(PP.getPreprocessorAllocator()); |
Tyler Nowicki | c724a83e | 2014-10-12 20:46:07 +0000 | [diff] [blame] | 2801 | |
Mark Heffernan | bd26f5e | 2014-07-21 18:08:34 +0000 | [diff] [blame] | 2802 | Info.PragmaName = PragmaName; |
| 2803 | Info.Option = Option; |
Mark Heffernan | bd26f5e | 2014-07-21 18:08:34 +0000 | [diff] [blame] | 2804 | return false; |
| 2805 | } |
| 2806 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2807 | /// Handle the \#pragma clang loop directive. |
Eli Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 2808 | /// #pragma clang 'loop' loop-hints |
| 2809 | /// |
| 2810 | /// loop-hints: |
| 2811 | /// loop-hint loop-hints[opt] |
| 2812 | /// |
| 2813 | /// loop-hint: |
| 2814 | /// 'vectorize' '(' loop-hint-keyword ')' |
| 2815 | /// 'interleave' '(' loop-hint-keyword ')' |
Mark Heffernan | 450c238 | 2014-07-23 17:31:31 +0000 | [diff] [blame] | 2816 | /// 'unroll' '(' unroll-hint-keyword ')' |
Eli Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 2817 | /// 'vectorize_width' '(' loop-hint-value ')' |
| 2818 | /// 'interleave_count' '(' loop-hint-value ')' |
Eli Bendersky | 86483b3 | 2014-06-11 17:56:26 +0000 | [diff] [blame] | 2819 | /// 'unroll_count' '(' loop-hint-value ')' |
Aaron Ballman | 9bdf515 | 2019-01-04 17:20:00 +0000 | [diff] [blame] | 2820 | /// 'pipeline' '(' disable ')' |
| 2821 | /// 'pipeline_initiation_interval' '(' loop-hint-value ')' |
Eli Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 2822 | /// |
| 2823 | /// loop-hint-keyword: |
| 2824 | /// 'enable' |
| 2825 | /// 'disable' |
Tyler Nowicki | 9d268e1 | 2015-06-11 23:23:17 +0000 | [diff] [blame] | 2826 | /// 'assume_safety' |
Eli Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 2827 | /// |
Mark Heffernan | 450c238 | 2014-07-23 17:31:31 +0000 | [diff] [blame] | 2828 | /// unroll-hint-keyword: |
Mark Heffernan | 397a98d | 2015-08-10 17:29:39 +0000 | [diff] [blame] | 2829 | /// 'enable' |
Mark Heffernan | 450c238 | 2014-07-23 17:31:31 +0000 | [diff] [blame] | 2830 | /// 'disable' |
Mark Heffernan | 397a98d | 2015-08-10 17:29:39 +0000 | [diff] [blame] | 2831 | /// 'full' |
Mark Heffernan | 450c238 | 2014-07-23 17:31:31 +0000 | [diff] [blame] | 2832 | /// |
Eli Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 2833 | /// loop-hint-value: |
| 2834 | /// constant-expression |
| 2835 | /// |
| 2836 | /// Specifying vectorize(enable) or vectorize_width(_value_) instructs llvm to |
| 2837 | /// try vectorizing the instructions of the loop it precedes. Specifying |
| 2838 | /// interleave(enable) or interleave_count(_value_) instructs llvm to try |
| 2839 | /// interleaving multiple iterations of the loop it precedes. The width of the |
| 2840 | /// vector instructions is specified by vectorize_width() and the number of |
| 2841 | /// interleaved loop iterations is specified by interleave_count(). Specifying a |
| 2842 | /// value of 1 effectively disables vectorization/interleaving, even if it is |
| 2843 | /// possible and profitable, and 0 is invalid. The loop vectorizer currently |
| 2844 | /// only works on inner loops. |
| 2845 | /// |
Eli Bendersky | 86483b3 | 2014-06-11 17:56:26 +0000 | [diff] [blame] | 2846 | /// The unroll and unroll_count directives control the concatenation |
Mark Heffernan | 397a98d | 2015-08-10 17:29:39 +0000 | [diff] [blame] | 2847 | /// unroller. Specifying unroll(enable) instructs llvm to unroll the loop |
| 2848 | /// completely if the trip count is known at compile time and unroll partially |
| 2849 | /// if the trip count is not known. Specifying unroll(full) is similar to |
| 2850 | /// unroll(enable) but will unroll the loop only if the trip count is known at |
| 2851 | /// compile time. Specifying unroll(disable) disables unrolling for the |
| 2852 | /// loop. Specifying unroll_count(_value_) instructs llvm to try to unroll the |
| 2853 | /// loop the number of times indicated by the value. |
Eli Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 2854 | void PragmaLoopHintHandler::HandlePragma(Preprocessor &PP, |
| 2855 | PragmaIntroducerKind Introducer, |
| 2856 | Token &Tok) { |
Mark Heffernan | bd26f5e | 2014-07-21 18:08:34 +0000 | [diff] [blame] | 2857 | // Incoming token is "loop" from "#pragma clang loop". |
| 2858 | Token PragmaName = Tok; |
Eli Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 2859 | SmallVector<Token, 1> TokenList; |
| 2860 | |
| 2861 | // Lex the optimization option and verify it is an identifier. |
| 2862 | PP.Lex(Tok); |
| 2863 | if (Tok.isNot(tok::identifier)) { |
| 2864 | PP.Diag(Tok.getLocation(), diag::err_pragma_loop_invalid_option) |
| 2865 | << /*MissingOption=*/true << ""; |
| 2866 | return; |
| 2867 | } |
| 2868 | |
| 2869 | while (Tok.is(tok::identifier)) { |
| 2870 | Token Option = Tok; |
| 2871 | IdentifierInfo *OptionInfo = Tok.getIdentifierInfo(); |
| 2872 | |
Eli Bendersky | 86483b3 | 2014-06-11 17:56:26 +0000 | [diff] [blame] | 2873 | bool OptionValid = llvm::StringSwitch<bool>(OptionInfo->getName()) |
Mark Heffernan | bd26f5e | 2014-07-21 18:08:34 +0000 | [diff] [blame] | 2874 | .Case("vectorize", true) |
| 2875 | .Case("interleave", true) |
| 2876 | .Case("unroll", true) |
Adam Nemet | 2de463e | 2016-06-14 12:04:26 +0000 | [diff] [blame] | 2877 | .Case("distribute", true) |
Mark Heffernan | bd26f5e | 2014-07-21 18:08:34 +0000 | [diff] [blame] | 2878 | .Case("vectorize_width", true) |
| 2879 | .Case("interleave_count", true) |
| 2880 | .Case("unroll_count", true) |
Aaron Ballman | 9bdf515 | 2019-01-04 17:20:00 +0000 | [diff] [blame] | 2881 | .Case("pipeline", true) |
| 2882 | .Case("pipeline_initiation_interval", true) |
Mark Heffernan | bd26f5e | 2014-07-21 18:08:34 +0000 | [diff] [blame] | 2883 | .Default(false); |
Eli Bendersky | 86483b3 | 2014-06-11 17:56:26 +0000 | [diff] [blame] | 2884 | if (!OptionValid) { |
Eli Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 2885 | PP.Diag(Tok.getLocation(), diag::err_pragma_loop_invalid_option) |
| 2886 | << /*MissingOption=*/false << OptionInfo; |
| 2887 | return; |
| 2888 | } |
NAKAMURA Takumi | db9552f | 2014-07-31 01:52:33 +0000 | [diff] [blame] | 2889 | PP.Lex(Tok); |
Mark Heffernan | bd26f5e | 2014-07-21 18:08:34 +0000 | [diff] [blame] | 2890 | |
Tyler Nowicki | 0c9b34b | 2014-07-31 20:15:14 +0000 | [diff] [blame] | 2891 | // Read '(' |
| 2892 | if (Tok.isNot(tok::l_paren)) { |
| 2893 | PP.Diag(Tok.getLocation(), diag::err_expected) << tok::l_paren; |
NAKAMURA Takumi | db9552f | 2014-07-31 01:52:33 +0000 | [diff] [blame] | 2894 | return; |
| 2895 | } |
Tyler Nowicki | 0c9b34b | 2014-07-31 20:15:14 +0000 | [diff] [blame] | 2896 | PP.Lex(Tok); |
| 2897 | |
| 2898 | auto *Info = new (PP.getPreprocessorAllocator()) PragmaLoopHintInfo; |
| 2899 | if (ParseLoopHintValue(PP, Tok, PragmaName, Option, /*ValueInParens=*/true, |
| 2900 | *Info)) |
| 2901 | return; |
NAKAMURA Takumi | db9552f | 2014-07-31 01:52:33 +0000 | [diff] [blame] | 2902 | |
Mark Heffernan | bd26f5e | 2014-07-21 18:08:34 +0000 | [diff] [blame] | 2903 | // Generate the loop hint token. |
Eli Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 2904 | Token LoopHintTok; |
| 2905 | LoopHintTok.startToken(); |
| 2906 | LoopHintTok.setKind(tok::annot_pragma_loop_hint); |
Mark Heffernan | bd26f5e | 2014-07-21 18:08:34 +0000 | [diff] [blame] | 2907 | LoopHintTok.setLocation(PragmaName.getLocation()); |
David Majnemer | a8f2f1d | 2015-03-19 00:10:23 +0000 | [diff] [blame] | 2908 | LoopHintTok.setAnnotationEndLoc(PragmaName.getLocation()); |
Eli Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 2909 | LoopHintTok.setAnnotationValue(static_cast<void *>(Info)); |
| 2910 | TokenList.push_back(LoopHintTok); |
| 2911 | } |
| 2912 | |
| 2913 | if (Tok.isNot(tok::eod)) { |
| 2914 | PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) |
| 2915 | << "clang loop"; |
| 2916 | return; |
| 2917 | } |
| 2918 | |
David Blaikie | 2eabcc9 | 2016-02-09 18:52:09 +0000 | [diff] [blame] | 2919 | auto TokenArray = llvm::make_unique<Token[]>(TokenList.size()); |
| 2920 | std::copy(TokenList.begin(), TokenList.end(), TokenArray.get()); |
Eli Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 2921 | |
David Blaikie | 2eabcc9 | 2016-02-09 18:52:09 +0000 | [diff] [blame] | 2922 | PP.EnterTokenStream(std::move(TokenArray), TokenList.size(), |
| 2923 | /*DisableMacroExpansion=*/false); |
Eli Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 2924 | } |
Mark Heffernan | bd26f5e | 2014-07-21 18:08:34 +0000 | [diff] [blame] | 2925 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2926 | /// Handle the loop unroll optimization pragmas. |
Mark Heffernan | bd26f5e | 2014-07-21 18:08:34 +0000 | [diff] [blame] | 2927 | /// #pragma unroll |
| 2928 | /// #pragma unroll unroll-hint-value |
| 2929 | /// #pragma unroll '(' unroll-hint-value ')' |
Mark Heffernan | c888e41 | 2014-07-24 18:09:38 +0000 | [diff] [blame] | 2930 | /// #pragma nounroll |
David Green | c8e3924 | 2018-08-01 14:36:12 +0000 | [diff] [blame] | 2931 | /// #pragma unroll_and_jam |
| 2932 | /// #pragma unroll_and_jam unroll-hint-value |
| 2933 | /// #pragma unroll_and_jam '(' unroll-hint-value ')' |
| 2934 | /// #pragma nounroll_and_jam |
Mark Heffernan | bd26f5e | 2014-07-21 18:08:34 +0000 | [diff] [blame] | 2935 | /// |
| 2936 | /// unroll-hint-value: |
| 2937 | /// constant-expression |
| 2938 | /// |
Mark Heffernan | c888e41 | 2014-07-24 18:09:38 +0000 | [diff] [blame] | 2939 | /// Loop unrolling hints can be specified with '#pragma unroll' or |
| 2940 | /// '#pragma nounroll'. '#pragma unroll' can take a numeric argument optionally |
| 2941 | /// contained in parentheses. With no argument the directive instructs llvm to |
| 2942 | /// try to unroll the loop completely. A positive integer argument can be |
| 2943 | /// specified to indicate the number of times the loop should be unrolled. To |
| 2944 | /// maximize compatibility with other compilers the unroll count argument can be |
| 2945 | /// specified with or without parentheses. Specifying, '#pragma nounroll' |
| 2946 | /// disables unrolling of the loop. |
Mark Heffernan | bd26f5e | 2014-07-21 18:08:34 +0000 | [diff] [blame] | 2947 | void PragmaUnrollHintHandler::HandlePragma(Preprocessor &PP, |
| 2948 | PragmaIntroducerKind Introducer, |
| 2949 | Token &Tok) { |
Mark Heffernan | c888e41 | 2014-07-24 18:09:38 +0000 | [diff] [blame] | 2950 | // Incoming token is "unroll" for "#pragma unroll", or "nounroll" for |
| 2951 | // "#pragma nounroll". |
Mark Heffernan | bd26f5e | 2014-07-21 18:08:34 +0000 | [diff] [blame] | 2952 | Token PragmaName = Tok; |
| 2953 | PP.Lex(Tok); |
| 2954 | auto *Info = new (PP.getPreprocessorAllocator()) PragmaLoopHintInfo; |
| 2955 | if (Tok.is(tok::eod)) { |
Mark Heffernan | c888e41 | 2014-07-24 18:09:38 +0000 | [diff] [blame] | 2956 | // nounroll or unroll pragma without an argument. |
Mark Heffernan | bd26f5e | 2014-07-21 18:08:34 +0000 | [diff] [blame] | 2957 | Info->PragmaName = PragmaName; |
Aaron Ballman | d6807da | 2014-08-01 12:41:37 +0000 | [diff] [blame] | 2958 | Info->Option.startToken(); |
David Green | c8e3924 | 2018-08-01 14:36:12 +0000 | [diff] [blame] | 2959 | } else if (PragmaName.getIdentifierInfo()->getName() == "nounroll" || |
| 2960 | PragmaName.getIdentifierInfo()->getName() == "nounroll_and_jam") { |
Mark Heffernan | c888e41 | 2014-07-24 18:09:38 +0000 | [diff] [blame] | 2961 | PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) |
David Green | c8e3924 | 2018-08-01 14:36:12 +0000 | [diff] [blame] | 2962 | << PragmaName.getIdentifierInfo()->getName(); |
Mark Heffernan | c888e41 | 2014-07-24 18:09:38 +0000 | [diff] [blame] | 2963 | return; |
Mark Heffernan | bd26f5e | 2014-07-21 18:08:34 +0000 | [diff] [blame] | 2964 | } else { |
| 2965 | // Unroll pragma with an argument: "#pragma unroll N" or |
| 2966 | // "#pragma unroll(N)". |
Tyler Nowicki | 0c9b34b | 2014-07-31 20:15:14 +0000 | [diff] [blame] | 2967 | // Read '(' if it exists. |
| 2968 | bool ValueInParens = Tok.is(tok::l_paren); |
| 2969 | if (ValueInParens) |
| 2970 | PP.Lex(Tok); |
| 2971 | |
Aaron Ballman | d0b090d | 2014-08-01 12:20:20 +0000 | [diff] [blame] | 2972 | Token Option; |
| 2973 | Option.startToken(); |
| 2974 | if (ParseLoopHintValue(PP, Tok, PragmaName, Option, ValueInParens, *Info)) |
Mark Heffernan | bd26f5e | 2014-07-21 18:08:34 +0000 | [diff] [blame] | 2975 | return; |
| 2976 | |
| 2977 | // In CUDA, the argument to '#pragma unroll' should not be contained in |
| 2978 | // parentheses. |
| 2979 | if (PP.getLangOpts().CUDA && ValueInParens) |
Tyler Nowicki | c724a83e | 2014-10-12 20:46:07 +0000 | [diff] [blame] | 2980 | PP.Diag(Info->Toks[0].getLocation(), |
Mark Heffernan | bd26f5e | 2014-07-21 18:08:34 +0000 | [diff] [blame] | 2981 | diag::warn_pragma_unroll_cuda_value_in_parens); |
| 2982 | |
Mark Heffernan | bd26f5e | 2014-07-21 18:08:34 +0000 | [diff] [blame] | 2983 | if (Tok.isNot(tok::eod)) { |
| 2984 | PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) |
| 2985 | << "unroll"; |
| 2986 | return; |
| 2987 | } |
| 2988 | } |
| 2989 | |
| 2990 | // Generate the hint token. |
David Blaikie | 2eabcc9 | 2016-02-09 18:52:09 +0000 | [diff] [blame] | 2991 | auto TokenArray = llvm::make_unique<Token[]>(1); |
Mark Heffernan | bd26f5e | 2014-07-21 18:08:34 +0000 | [diff] [blame] | 2992 | TokenArray[0].startToken(); |
| 2993 | TokenArray[0].setKind(tok::annot_pragma_loop_hint); |
| 2994 | TokenArray[0].setLocation(PragmaName.getLocation()); |
David Majnemer | a8f2f1d | 2015-03-19 00:10:23 +0000 | [diff] [blame] | 2995 | TokenArray[0].setAnnotationEndLoc(PragmaName.getLocation()); |
Mark Heffernan | bd26f5e | 2014-07-21 18:08:34 +0000 | [diff] [blame] | 2996 | TokenArray[0].setAnnotationValue(static_cast<void *>(Info)); |
David Blaikie | 2eabcc9 | 2016-02-09 18:52:09 +0000 | [diff] [blame] | 2997 | PP.EnterTokenStream(std::move(TokenArray), 1, |
| 2998 | /*DisableMacroExpansion=*/false); |
Mark Heffernan | bd26f5e | 2014-07-21 18:08:34 +0000 | [diff] [blame] | 2999 | } |
Reid Kleckner | 3f1ec62 | 2016-09-07 16:38:32 +0000 | [diff] [blame] | 3000 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3001 | /// Handle the Microsoft \#pragma intrinsic extension. |
Reid Kleckner | 3f1ec62 | 2016-09-07 16:38:32 +0000 | [diff] [blame] | 3002 | /// |
| 3003 | /// The syntax is: |
| 3004 | /// \code |
| 3005 | /// #pragma intrinsic(memset) |
| 3006 | /// #pragma intrinsic(strlen, memcpy) |
| 3007 | /// \endcode |
| 3008 | /// |
| 3009 | /// Pragma intrisic tells the compiler to use a builtin version of the |
| 3010 | /// function. Clang does it anyway, so the pragma doesn't really do anything. |
| 3011 | /// Anyway, we emit a warning if the function specified in \#pragma intrinsic |
| 3012 | /// isn't an intrinsic in clang and suggest to include intrin.h. |
| 3013 | void PragmaMSIntrinsicHandler::HandlePragma(Preprocessor &PP, |
| 3014 | PragmaIntroducerKind Introducer, |
| 3015 | Token &Tok) { |
| 3016 | PP.Lex(Tok); |
| 3017 | |
| 3018 | if (Tok.isNot(tok::l_paren)) { |
| 3019 | PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_lparen) |
| 3020 | << "intrinsic"; |
| 3021 | return; |
| 3022 | } |
| 3023 | PP.Lex(Tok); |
| 3024 | |
| 3025 | bool SuggestIntrinH = !PP.isMacroDefined("__INTRIN_H"); |
| 3026 | |
| 3027 | while (Tok.is(tok::identifier)) { |
| 3028 | IdentifierInfo *II = Tok.getIdentifierInfo(); |
| 3029 | if (!II->getBuiltinID()) |
| 3030 | PP.Diag(Tok.getLocation(), diag::warn_pragma_intrinsic_builtin) |
| 3031 | << II << SuggestIntrinH; |
| 3032 | |
| 3033 | PP.Lex(Tok); |
| 3034 | if (Tok.isNot(tok::comma)) |
| 3035 | break; |
| 3036 | PP.Lex(Tok); |
| 3037 | } |
| 3038 | |
| 3039 | if (Tok.isNot(tok::r_paren)) { |
| 3040 | PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_rparen) |
| 3041 | << "intrinsic"; |
| 3042 | return; |
| 3043 | } |
| 3044 | PP.Lex(Tok); |
| 3045 | |
| 3046 | if (Tok.isNot(tok::eod)) |
| 3047 | PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) |
| 3048 | << "intrinsic"; |
| 3049 | } |
Hans Wennborg | 1bbe00e | 2018-03-20 08:53:11 +0000 | [diff] [blame] | 3050 | |
| 3051 | // #pragma optimize("gsty", on|off) |
| 3052 | void PragmaMSOptimizeHandler::HandlePragma(Preprocessor &PP, |
| 3053 | PragmaIntroducerKind Introducer, |
| 3054 | Token &Tok) { |
| 3055 | SourceLocation StartLoc = Tok.getLocation(); |
| 3056 | PP.Lex(Tok); |
| 3057 | |
| 3058 | if (Tok.isNot(tok::l_paren)) { |
| 3059 | PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_lparen) << "optimize"; |
| 3060 | return; |
| 3061 | } |
| 3062 | PP.Lex(Tok); |
| 3063 | |
| 3064 | if (Tok.isNot(tok::string_literal)) { |
| 3065 | PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_string) << "optimize"; |
| 3066 | return; |
| 3067 | } |
| 3068 | // We could syntax check the string but it's probably not worth the effort. |
| 3069 | PP.Lex(Tok); |
| 3070 | |
| 3071 | if (Tok.isNot(tok::comma)) { |
| 3072 | PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_comma) << "optimize"; |
| 3073 | return; |
| 3074 | } |
| 3075 | PP.Lex(Tok); |
| 3076 | |
| 3077 | if (Tok.is(tok::eod) || Tok.is(tok::r_paren)) { |
| 3078 | PP.Diag(Tok.getLocation(), diag::warn_pragma_missing_argument) |
| 3079 | << "optimize" << /*Expected=*/true << "'on' or 'off'"; |
| 3080 | return; |
| 3081 | } |
| 3082 | IdentifierInfo *II = Tok.getIdentifierInfo(); |
| 3083 | if (!II || (!II->isStr("on") && !II->isStr("off"))) { |
| 3084 | PP.Diag(Tok.getLocation(), diag::warn_pragma_invalid_argument) |
| 3085 | << PP.getSpelling(Tok) << "optimize" << /*Expected=*/true |
| 3086 | << "'on' or 'off'"; |
| 3087 | return; |
| 3088 | } |
| 3089 | PP.Lex(Tok); |
| 3090 | |
| 3091 | if (Tok.isNot(tok::r_paren)) { |
| 3092 | PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_rparen) << "optimize"; |
| 3093 | return; |
| 3094 | } |
| 3095 | PP.Lex(Tok); |
| 3096 | |
| 3097 | if (Tok.isNot(tok::eod)) { |
| 3098 | PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) |
| 3099 | << "optimize"; |
| 3100 | return; |
| 3101 | } |
| 3102 | PP.Diag(StartLoc, diag::warn_pragma_optimize); |
| 3103 | } |
| 3104 | |
Justin Lebar | 67a78a6 | 2016-10-08 22:15:58 +0000 | [diff] [blame] | 3105 | void PragmaForceCUDAHostDeviceHandler::HandlePragma( |
| 3106 | Preprocessor &PP, PragmaIntroducerKind Introducer, Token &Tok) { |
| 3107 | Token FirstTok = Tok; |
| 3108 | |
| 3109 | PP.Lex(Tok); |
| 3110 | IdentifierInfo *Info = Tok.getIdentifierInfo(); |
| 3111 | if (!Info || (!Info->isStr("begin") && !Info->isStr("end"))) { |
| 3112 | PP.Diag(FirstTok.getLocation(), |
| 3113 | diag::warn_pragma_force_cuda_host_device_bad_arg); |
| 3114 | return; |
| 3115 | } |
| 3116 | |
| 3117 | if (Info->isStr("begin")) |
| 3118 | Actions.PushForceCUDAHostDevice(); |
| 3119 | else if (!Actions.PopForceCUDAHostDevice()) |
| 3120 | PP.Diag(FirstTok.getLocation(), |
| 3121 | diag::err_pragma_cannot_end_force_cuda_host_device); |
| 3122 | |
| 3123 | PP.Lex(Tok); |
| 3124 | if (!Tok.is(tok::eod)) |
| 3125 | PP.Diag(FirstTok.getLocation(), |
| 3126 | diag::warn_pragma_force_cuda_host_device_bad_arg); |
| 3127 | } |
Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 3128 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3129 | /// Handle the #pragma clang attribute directive. |
Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 3130 | /// |
| 3131 | /// The syntax is: |
| 3132 | /// \code |
Erik Pilkington | 0876cae | 2018-12-20 22:32:04 +0000 | [diff] [blame] | 3133 | /// #pragma clang attribute push (attribute, subject-set) |
Erik Pilkington | 7d18094 | 2018-10-29 17:38:42 +0000 | [diff] [blame] | 3134 | /// #pragma clang attribute push |
| 3135 | /// #pragma clang attribute (attribute, subject-set) |
Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 3136 | /// #pragma clang attribute pop |
| 3137 | /// \endcode |
| 3138 | /// |
Erik Pilkington | 0876cae | 2018-12-20 22:32:04 +0000 | [diff] [blame] | 3139 | /// There are also 'namespace' variants of push and pop directives. The bare |
| 3140 | /// '#pragma clang attribute (attribute, subject-set)' version doesn't require a |
| 3141 | /// namespace, since it always applies attributes to the most recently pushed |
| 3142 | /// group, regardless of namespace. |
| 3143 | /// \code |
| 3144 | /// #pragma clang attribute namespace.push (attribute, subject-set) |
| 3145 | /// #pragma clang attribute namespace.push |
| 3146 | /// #pragma clang attribute namespace.pop |
| 3147 | /// \endcode |
| 3148 | /// |
Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 3149 | /// The subject-set clause defines the set of declarations which receive the |
| 3150 | /// attribute. Its exact syntax is described in the LanguageExtensions document |
| 3151 | /// in Clang's documentation. |
| 3152 | /// |
| 3153 | /// This directive instructs the compiler to begin/finish applying the specified |
| 3154 | /// attribute to the set of attribute-specific declarations in the active range |
| 3155 | /// of the pragma. |
| 3156 | void PragmaAttributeHandler::HandlePragma(Preprocessor &PP, |
| 3157 | PragmaIntroducerKind Introducer, |
| 3158 | Token &FirstToken) { |
| 3159 | Token Tok; |
| 3160 | PP.Lex(Tok); |
| 3161 | auto *Info = new (PP.getPreprocessorAllocator()) |
| 3162 | PragmaAttributeInfo(AttributesForPragmaAttribute); |
| 3163 | |
Erik Pilkington | 0876cae | 2018-12-20 22:32:04 +0000 | [diff] [blame] | 3164 | // Parse the optional namespace followed by a period. |
| 3165 | if (Tok.is(tok::identifier)) { |
| 3166 | IdentifierInfo *II = Tok.getIdentifierInfo(); |
| 3167 | if (!II->isStr("push") && !II->isStr("pop")) { |
| 3168 | Info->Namespace = II; |
| 3169 | PP.Lex(Tok); |
| 3170 | |
| 3171 | if (!Tok.is(tok::period)) { |
| 3172 | PP.Diag(Tok.getLocation(), diag::err_pragma_attribute_expected_period) |
| 3173 | << II; |
| 3174 | return; |
| 3175 | } |
| 3176 | PP.Lex(Tok); |
| 3177 | } |
| 3178 | } |
| 3179 | |
Erik Pilkington | 7d18094 | 2018-10-29 17:38:42 +0000 | [diff] [blame] | 3180 | if (!Tok.isOneOf(tok::identifier, tok::l_paren)) { |
| 3181 | PP.Diag(Tok.getLocation(), |
| 3182 | diag::err_pragma_attribute_expected_push_pop_paren); |
Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 3183 | return; |
| 3184 | } |
Erik Pilkington | 7d18094 | 2018-10-29 17:38:42 +0000 | [diff] [blame] | 3185 | |
| 3186 | // Determine what action this pragma clang attribute represents. |
Erik Pilkington | 0876cae | 2018-12-20 22:32:04 +0000 | [diff] [blame] | 3187 | if (Tok.is(tok::l_paren)) { |
| 3188 | if (Info->Namespace) { |
| 3189 | PP.Diag(Tok.getLocation(), |
| 3190 | diag::err_pragma_attribute_namespace_on_attribute); |
| 3191 | PP.Diag(Tok.getLocation(), |
| 3192 | diag::note_pragma_attribute_namespace_on_attribute); |
| 3193 | return; |
| 3194 | } |
Erik Pilkington | 7d18094 | 2018-10-29 17:38:42 +0000 | [diff] [blame] | 3195 | Info->Action = PragmaAttributeInfo::Attribute; |
Erik Pilkington | 0876cae | 2018-12-20 22:32:04 +0000 | [diff] [blame] | 3196 | } else { |
Erik Pilkington | 7d18094 | 2018-10-29 17:38:42 +0000 | [diff] [blame] | 3197 | const IdentifierInfo *II = Tok.getIdentifierInfo(); |
| 3198 | if (II->isStr("push")) |
| 3199 | Info->Action = PragmaAttributeInfo::Push; |
| 3200 | else if (II->isStr("pop")) |
| 3201 | Info->Action = PragmaAttributeInfo::Pop; |
| 3202 | else { |
| 3203 | PP.Diag(Tok.getLocation(), diag::err_pragma_attribute_invalid_argument) |
| 3204 | << PP.getSpelling(Tok); |
| 3205 | return; |
| 3206 | } |
| 3207 | |
| 3208 | PP.Lex(Tok); |
Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 3209 | } |
Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 3210 | |
| 3211 | // Parse the actual attribute. |
Erik Pilkington | 7d18094 | 2018-10-29 17:38:42 +0000 | [diff] [blame] | 3212 | if ((Info->Action == PragmaAttributeInfo::Push && Tok.isNot(tok::eod)) || |
| 3213 | Info->Action == PragmaAttributeInfo::Attribute) { |
Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 3214 | if (Tok.isNot(tok::l_paren)) { |
| 3215 | PP.Diag(Tok.getLocation(), diag::err_expected) << tok::l_paren; |
| 3216 | return; |
| 3217 | } |
| 3218 | PP.Lex(Tok); |
| 3219 | |
| 3220 | // Lex the attribute tokens. |
| 3221 | SmallVector<Token, 16> AttributeTokens; |
| 3222 | int OpenParens = 1; |
| 3223 | while (Tok.isNot(tok::eod)) { |
| 3224 | if (Tok.is(tok::l_paren)) |
| 3225 | OpenParens++; |
| 3226 | else if (Tok.is(tok::r_paren)) { |
| 3227 | OpenParens--; |
| 3228 | if (OpenParens == 0) |
| 3229 | break; |
| 3230 | } |
| 3231 | |
| 3232 | AttributeTokens.push_back(Tok); |
| 3233 | PP.Lex(Tok); |
| 3234 | } |
| 3235 | |
| 3236 | if (AttributeTokens.empty()) { |
| 3237 | PP.Diag(Tok.getLocation(), diag::err_pragma_attribute_expected_attribute); |
| 3238 | return; |
| 3239 | } |
| 3240 | if (Tok.isNot(tok::r_paren)) { |
| 3241 | PP.Diag(Tok.getLocation(), diag::err_expected) << tok::r_paren; |
| 3242 | return; |
| 3243 | } |
| 3244 | SourceLocation EndLoc = Tok.getLocation(); |
| 3245 | PP.Lex(Tok); |
| 3246 | |
| 3247 | // Terminate the attribute for parsing. |
| 3248 | Token EOFTok; |
| 3249 | EOFTok.startToken(); |
| 3250 | EOFTok.setKind(tok::eof); |
| 3251 | EOFTok.setLocation(EndLoc); |
| 3252 | AttributeTokens.push_back(EOFTok); |
| 3253 | |
| 3254 | Info->Tokens = |
| 3255 | llvm::makeArrayRef(AttributeTokens).copy(PP.getPreprocessorAllocator()); |
| 3256 | } |
| 3257 | |
| 3258 | if (Tok.isNot(tok::eod)) |
| 3259 | PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) |
| 3260 | << "clang attribute"; |
| 3261 | |
| 3262 | // Generate the annotated pragma token. |
| 3263 | auto TokenArray = llvm::make_unique<Token[]>(1); |
| 3264 | TokenArray[0].startToken(); |
| 3265 | TokenArray[0].setKind(tok::annot_pragma_attribute); |
| 3266 | TokenArray[0].setLocation(FirstToken.getLocation()); |
| 3267 | TokenArray[0].setAnnotationEndLoc(FirstToken.getLocation()); |
| 3268 | TokenArray[0].setAnnotationValue(static_cast<void *>(Info)); |
| 3269 | PP.EnterTokenStream(std::move(TokenArray), 1, |
| 3270 | /*DisableMacroExpansion=*/false); |
| 3271 | } |