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