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" |
| 18 | #include "clang/Parse/ParseDiagnostic.h" |
| 19 | #include "clang/Parse/Parser.h" |
Vassil Vassilev | 11ad339 | 2017-03-23 15:11:07 +0000 | [diff] [blame] | 20 | #include "clang/Parse/RAIIObjectsForParser.h" |
Eli Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 21 | #include "clang/Sema/LoopHint.h" |
| 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 | |
| 52 | struct PragmaMSStructHandler : public PragmaHandler { |
| 53 | explicit PragmaMSStructHandler() : PragmaHandler("ms_struct") {} |
Craig Topper | 2b07f02 | 2014-03-12 05:09:18 +0000 | [diff] [blame] | 54 | void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 55 | Token &FirstToken) override; |
Reid Kleckner | 5b08646 | 2014-02-20 22:52:09 +0000 | [diff] [blame] | 56 | }; |
| 57 | |
| 58 | struct PragmaUnusedHandler : public PragmaHandler { |
| 59 | PragmaUnusedHandler() : PragmaHandler("unused") {} |
Craig Topper | 2b07f02 | 2014-03-12 05:09:18 +0000 | [diff] [blame] | 60 | void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 61 | Token &FirstToken) override; |
Reid Kleckner | 5b08646 | 2014-02-20 22:52:09 +0000 | [diff] [blame] | 62 | }; |
| 63 | |
| 64 | struct PragmaWeakHandler : public PragmaHandler { |
| 65 | explicit PragmaWeakHandler() : PragmaHandler("weak") {} |
Craig Topper | 2b07f02 | 2014-03-12 05:09:18 +0000 | [diff] [blame] | 66 | void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 67 | Token &FirstToken) override; |
Reid Kleckner | 5b08646 | 2014-02-20 22:52:09 +0000 | [diff] [blame] | 68 | }; |
| 69 | |
| 70 | struct PragmaRedefineExtnameHandler : public PragmaHandler { |
| 71 | explicit PragmaRedefineExtnameHandler() : PragmaHandler("redefine_extname") {} |
Craig Topper | 2b07f02 | 2014-03-12 05:09:18 +0000 | [diff] [blame] | 72 | void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 73 | Token &FirstToken) override; |
Reid Kleckner | 5b08646 | 2014-02-20 22:52:09 +0000 | [diff] [blame] | 74 | }; |
| 75 | |
| 76 | struct PragmaOpenCLExtensionHandler : public PragmaHandler { |
| 77 | PragmaOpenCLExtensionHandler() : PragmaHandler("EXTENSION") {} |
Craig Topper | 2b07f02 | 2014-03-12 05:09:18 +0000 | [diff] [blame] | 78 | void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 79 | Token &FirstToken) override; |
Reid Kleckner | 5b08646 | 2014-02-20 22:52:09 +0000 | [diff] [blame] | 80 | }; |
| 81 | |
| 82 | |
| 83 | struct PragmaFPContractHandler : public PragmaHandler { |
| 84 | PragmaFPContractHandler() : PragmaHandler("FP_CONTRACT") {} |
Craig Topper | 2b07f02 | 2014-03-12 05:09:18 +0000 | [diff] [blame] | 85 | void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 86 | Token &FirstToken) override; |
Reid Kleckner | 5b08646 | 2014-02-20 22:52:09 +0000 | [diff] [blame] | 87 | }; |
| 88 | |
Adam Nemet | 60d3264 | 2017-04-04 21:18:36 +0000 | [diff] [blame^] | 89 | struct PragmaFPHandler : public PragmaHandler { |
| 90 | PragmaFPHandler() : PragmaHandler("fp") {} |
| 91 | void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 92 | Token &FirstToken) override; |
| 93 | }; |
| 94 | |
Reid Kleckner | 5b08646 | 2014-02-20 22:52:09 +0000 | [diff] [blame] | 95 | struct PragmaNoOpenMPHandler : public PragmaHandler { |
| 96 | PragmaNoOpenMPHandler() : PragmaHandler("omp") { } |
Craig Topper | 2b07f02 | 2014-03-12 05:09:18 +0000 | [diff] [blame] | 97 | void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 98 | Token &FirstToken) override; |
Reid Kleckner | 5b08646 | 2014-02-20 22:52:09 +0000 | [diff] [blame] | 99 | }; |
| 100 | |
| 101 | struct PragmaOpenMPHandler : public PragmaHandler { |
| 102 | PragmaOpenMPHandler() : PragmaHandler("omp") { } |
Craig Topper | 2b07f02 | 2014-03-12 05:09:18 +0000 | [diff] [blame] | 103 | void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 104 | Token &FirstToken) override; |
Reid Kleckner | 5b08646 | 2014-02-20 22:52:09 +0000 | [diff] [blame] | 105 | }; |
| 106 | |
| 107 | /// PragmaCommentHandler - "\#pragma comment ...". |
| 108 | struct PragmaCommentHandler : public PragmaHandler { |
| 109 | PragmaCommentHandler(Sema &Actions) |
| 110 | : PragmaHandler("comment"), Actions(Actions) {} |
Craig Topper | 2b07f02 | 2014-03-12 05:09:18 +0000 | [diff] [blame] | 111 | void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 112 | Token &FirstToken) override; |
Reid Kleckner | 5b08646 | 2014-02-20 22:52:09 +0000 | [diff] [blame] | 113 | private: |
| 114 | Sema &Actions; |
| 115 | }; |
| 116 | |
| 117 | struct PragmaDetectMismatchHandler : public PragmaHandler { |
| 118 | PragmaDetectMismatchHandler(Sema &Actions) |
| 119 | : PragmaHandler("detect_mismatch"), Actions(Actions) {} |
Craig Topper | 2b07f02 | 2014-03-12 05:09:18 +0000 | [diff] [blame] | 120 | void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 121 | Token &FirstToken) override; |
Reid Kleckner | 5b08646 | 2014-02-20 22:52:09 +0000 | [diff] [blame] | 122 | private: |
| 123 | Sema &Actions; |
| 124 | }; |
| 125 | |
| 126 | struct PragmaMSPointersToMembers : public PragmaHandler { |
| 127 | explicit PragmaMSPointersToMembers() : PragmaHandler("pointers_to_members") {} |
Craig Topper | 2b07f02 | 2014-03-12 05:09:18 +0000 | [diff] [blame] | 128 | void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 129 | Token &FirstToken) override; |
Reid Kleckner | 5b08646 | 2014-02-20 22:52:09 +0000 | [diff] [blame] | 130 | }; |
| 131 | |
| 132 | struct PragmaMSVtorDisp : public PragmaHandler { |
| 133 | explicit PragmaMSVtorDisp() : PragmaHandler("vtordisp") {} |
Craig Topper | 2b07f02 | 2014-03-12 05:09:18 +0000 | [diff] [blame] | 134 | void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 135 | Token &FirstToken) override; |
Reid Kleckner | 5b08646 | 2014-02-20 22:52:09 +0000 | [diff] [blame] | 136 | }; |
| 137 | |
Warren Hunt | c3b1896 | 2014-04-08 22:30:47 +0000 | [diff] [blame] | 138 | struct PragmaMSPragma : public PragmaHandler { |
| 139 | explicit PragmaMSPragma(const char *name) : PragmaHandler(name) {} |
Reid Kleckner | d3923aa | 2014-04-03 19:04:24 +0000 | [diff] [blame] | 140 | void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 141 | Token &FirstToken) override; |
| 142 | }; |
| 143 | |
Dario Domizioli | 13a0a38 | 2014-05-23 12:13:25 +0000 | [diff] [blame] | 144 | /// PragmaOptimizeHandler - "\#pragma clang optimize on/off". |
| 145 | struct PragmaOptimizeHandler : public PragmaHandler { |
| 146 | PragmaOptimizeHandler(Sema &S) |
| 147 | : PragmaHandler("optimize"), Actions(S) {} |
| 148 | void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 149 | Token &FirstToken) override; |
| 150 | private: |
Eli Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 151 | Sema &Actions; |
| 152 | }; |
| 153 | |
| 154 | struct PragmaLoopHintHandler : public PragmaHandler { |
| 155 | PragmaLoopHintHandler() : PragmaHandler("loop") {} |
| 156 | void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 157 | Token &FirstToken) override; |
| 158 | }; |
| 159 | |
Mark Heffernan | bd26f5e | 2014-07-21 18:08:34 +0000 | [diff] [blame] | 160 | struct PragmaUnrollHintHandler : public PragmaHandler { |
| 161 | PragmaUnrollHintHandler(const char *name) : PragmaHandler(name) {} |
| 162 | void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 163 | Token &FirstToken) override; |
| 164 | }; |
| 165 | |
Hans Wennborg | 7357bbc | 2015-10-12 20:47:58 +0000 | [diff] [blame] | 166 | struct PragmaMSRuntimeChecksHandler : public EmptyPragmaHandler { |
| 167 | PragmaMSRuntimeChecksHandler() : EmptyPragmaHandler("runtime_checks") {} |
| 168 | }; |
| 169 | |
Reid Kleckner | 3f1ec62 | 2016-09-07 16:38:32 +0000 | [diff] [blame] | 170 | struct PragmaMSIntrinsicHandler : public PragmaHandler { |
| 171 | PragmaMSIntrinsicHandler() : PragmaHandler("intrinsic") {} |
| 172 | void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 173 | Token &FirstToken) override; |
| 174 | }; |
| 175 | |
Justin Lebar | 67a78a6 | 2016-10-08 22:15:58 +0000 | [diff] [blame] | 176 | struct PragmaForceCUDAHostDeviceHandler : public PragmaHandler { |
| 177 | PragmaForceCUDAHostDeviceHandler(Sema &Actions) |
| 178 | : PragmaHandler("force_cuda_host_device"), Actions(Actions) {} |
| 179 | void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 180 | Token &FirstToken) override; |
| 181 | |
| 182 | private: |
| 183 | Sema &Actions; |
| 184 | }; |
| 185 | |
Eli Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 186 | } // end namespace |
| 187 | |
| 188 | void Parser::initializePragmaHandlers() { |
Reid Kleckner | 5b08646 | 2014-02-20 22:52:09 +0000 | [diff] [blame] | 189 | AlignHandler.reset(new PragmaAlignHandler()); |
| 190 | PP.AddPragmaHandler(AlignHandler.get()); |
| 191 | |
| 192 | GCCVisibilityHandler.reset(new PragmaGCCVisibilityHandler()); |
| 193 | PP.AddPragmaHandler("GCC", GCCVisibilityHandler.get()); |
| 194 | |
| 195 | OptionsHandler.reset(new PragmaOptionsHandler()); |
| 196 | PP.AddPragmaHandler(OptionsHandler.get()); |
| 197 | |
| 198 | PackHandler.reset(new PragmaPackHandler()); |
| 199 | PP.AddPragmaHandler(PackHandler.get()); |
| 200 | |
| 201 | MSStructHandler.reset(new PragmaMSStructHandler()); |
| 202 | PP.AddPragmaHandler(MSStructHandler.get()); |
| 203 | |
| 204 | UnusedHandler.reset(new PragmaUnusedHandler()); |
| 205 | PP.AddPragmaHandler(UnusedHandler.get()); |
| 206 | |
| 207 | WeakHandler.reset(new PragmaWeakHandler()); |
| 208 | PP.AddPragmaHandler(WeakHandler.get()); |
| 209 | |
| 210 | RedefineExtnameHandler.reset(new PragmaRedefineExtnameHandler()); |
| 211 | PP.AddPragmaHandler(RedefineExtnameHandler.get()); |
| 212 | |
| 213 | FPContractHandler.reset(new PragmaFPContractHandler()); |
| 214 | PP.AddPragmaHandler("STDC", FPContractHandler.get()); |
| 215 | |
| 216 | if (getLangOpts().OpenCL) { |
| 217 | OpenCLExtensionHandler.reset(new PragmaOpenCLExtensionHandler()); |
| 218 | PP.AddPragmaHandler("OPENCL", OpenCLExtensionHandler.get()); |
| 219 | |
| 220 | PP.AddPragmaHandler("OPENCL", FPContractHandler.get()); |
| 221 | } |
| 222 | if (getLangOpts().OpenMP) |
| 223 | OpenMPHandler.reset(new PragmaOpenMPHandler()); |
| 224 | else |
| 225 | OpenMPHandler.reset(new PragmaNoOpenMPHandler()); |
| 226 | PP.AddPragmaHandler(OpenMPHandler.get()); |
| 227 | |
Yunzhong Gao | 99efc03 | 2015-03-23 20:41:42 +0000 | [diff] [blame] | 228 | if (getLangOpts().MicrosoftExt || getTargetInfo().getTriple().isPS4()) { |
Reid Kleckner | 5b08646 | 2014-02-20 22:52:09 +0000 | [diff] [blame] | 229 | MSCommentHandler.reset(new PragmaCommentHandler(Actions)); |
| 230 | PP.AddPragmaHandler(MSCommentHandler.get()); |
Yunzhong Gao | 99efc03 | 2015-03-23 20:41:42 +0000 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | if (getLangOpts().MicrosoftExt) { |
Reid Kleckner | 5b08646 | 2014-02-20 22:52:09 +0000 | [diff] [blame] | 234 | MSDetectMismatchHandler.reset(new PragmaDetectMismatchHandler(Actions)); |
| 235 | PP.AddPragmaHandler(MSDetectMismatchHandler.get()); |
| 236 | MSPointersToMembers.reset(new PragmaMSPointersToMembers()); |
| 237 | PP.AddPragmaHandler(MSPointersToMembers.get()); |
| 238 | MSVtorDisp.reset(new PragmaMSVtorDisp()); |
| 239 | PP.AddPragmaHandler(MSVtorDisp.get()); |
Warren Hunt | c3b1896 | 2014-04-08 22:30:47 +0000 | [diff] [blame] | 240 | MSInitSeg.reset(new PragmaMSPragma("init_seg")); |
Reid Kleckner | d3923aa | 2014-04-03 19:04:24 +0000 | [diff] [blame] | 241 | PP.AddPragmaHandler(MSInitSeg.get()); |
Warren Hunt | c3b1896 | 2014-04-08 22:30:47 +0000 | [diff] [blame] | 242 | MSDataSeg.reset(new PragmaMSPragma("data_seg")); |
| 243 | PP.AddPragmaHandler(MSDataSeg.get()); |
| 244 | MSBSSSeg.reset(new PragmaMSPragma("bss_seg")); |
| 245 | PP.AddPragmaHandler(MSBSSSeg.get()); |
| 246 | MSConstSeg.reset(new PragmaMSPragma("const_seg")); |
| 247 | PP.AddPragmaHandler(MSConstSeg.get()); |
| 248 | MSCodeSeg.reset(new PragmaMSPragma("code_seg")); |
| 249 | PP.AddPragmaHandler(MSCodeSeg.get()); |
| 250 | MSSection.reset(new PragmaMSPragma("section")); |
| 251 | PP.AddPragmaHandler(MSSection.get()); |
Hans Wennborg | 7357bbc | 2015-10-12 20:47:58 +0000 | [diff] [blame] | 252 | MSRuntimeChecks.reset(new PragmaMSRuntimeChecksHandler()); |
| 253 | PP.AddPragmaHandler(MSRuntimeChecks.get()); |
Reid Kleckner | 3f1ec62 | 2016-09-07 16:38:32 +0000 | [diff] [blame] | 254 | MSIntrinsic.reset(new PragmaMSIntrinsicHandler()); |
| 255 | PP.AddPragmaHandler(MSIntrinsic.get()); |
Reid Kleckner | 5b08646 | 2014-02-20 22:52:09 +0000 | [diff] [blame] | 256 | } |
Eli Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 257 | |
Justin Lebar | 67a78a6 | 2016-10-08 22:15:58 +0000 | [diff] [blame] | 258 | if (getLangOpts().CUDA) { |
| 259 | CUDAForceHostDeviceHandler.reset( |
| 260 | new PragmaForceCUDAHostDeviceHandler(Actions)); |
| 261 | PP.AddPragmaHandler("clang", CUDAForceHostDeviceHandler.get()); |
| 262 | } |
| 263 | |
Eli Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 264 | OptimizeHandler.reset(new PragmaOptimizeHandler(Actions)); |
| 265 | PP.AddPragmaHandler("clang", OptimizeHandler.get()); |
| 266 | |
| 267 | LoopHintHandler.reset(new PragmaLoopHintHandler()); |
| 268 | PP.AddPragmaHandler("clang", LoopHintHandler.get()); |
Mark Heffernan | bd26f5e | 2014-07-21 18:08:34 +0000 | [diff] [blame] | 269 | |
| 270 | UnrollHintHandler.reset(new PragmaUnrollHintHandler("unroll")); |
| 271 | PP.AddPragmaHandler(UnrollHintHandler.get()); |
Mark Heffernan | c888e41 | 2014-07-24 18:09:38 +0000 | [diff] [blame] | 272 | |
| 273 | NoUnrollHintHandler.reset(new PragmaUnrollHintHandler("nounroll")); |
| 274 | PP.AddPragmaHandler(NoUnrollHintHandler.get()); |
Adam Nemet | 60d3264 | 2017-04-04 21:18:36 +0000 | [diff] [blame^] | 275 | |
| 276 | FPHandler.reset(new PragmaFPHandler()); |
| 277 | PP.AddPragmaHandler("clang", FPHandler.get()); |
Eli Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | void Parser::resetPragmaHandlers() { |
Reid Kleckner | 5b08646 | 2014-02-20 22:52:09 +0000 | [diff] [blame] | 281 | // Remove the pragma handlers we installed. |
| 282 | PP.RemovePragmaHandler(AlignHandler.get()); |
| 283 | AlignHandler.reset(); |
| 284 | PP.RemovePragmaHandler("GCC", GCCVisibilityHandler.get()); |
| 285 | GCCVisibilityHandler.reset(); |
| 286 | PP.RemovePragmaHandler(OptionsHandler.get()); |
| 287 | OptionsHandler.reset(); |
| 288 | PP.RemovePragmaHandler(PackHandler.get()); |
| 289 | PackHandler.reset(); |
| 290 | PP.RemovePragmaHandler(MSStructHandler.get()); |
| 291 | MSStructHandler.reset(); |
| 292 | PP.RemovePragmaHandler(UnusedHandler.get()); |
| 293 | UnusedHandler.reset(); |
| 294 | PP.RemovePragmaHandler(WeakHandler.get()); |
| 295 | WeakHandler.reset(); |
| 296 | PP.RemovePragmaHandler(RedefineExtnameHandler.get()); |
| 297 | RedefineExtnameHandler.reset(); |
| 298 | |
| 299 | if (getLangOpts().OpenCL) { |
| 300 | PP.RemovePragmaHandler("OPENCL", OpenCLExtensionHandler.get()); |
| 301 | OpenCLExtensionHandler.reset(); |
| 302 | PP.RemovePragmaHandler("OPENCL", FPContractHandler.get()); |
| 303 | } |
| 304 | PP.RemovePragmaHandler(OpenMPHandler.get()); |
| 305 | OpenMPHandler.reset(); |
| 306 | |
Yunzhong Gao | 99efc03 | 2015-03-23 20:41:42 +0000 | [diff] [blame] | 307 | if (getLangOpts().MicrosoftExt || getTargetInfo().getTriple().isPS4()) { |
Reid Kleckner | 5b08646 | 2014-02-20 22:52:09 +0000 | [diff] [blame] | 308 | PP.RemovePragmaHandler(MSCommentHandler.get()); |
| 309 | MSCommentHandler.reset(); |
Yunzhong Gao | 99efc03 | 2015-03-23 20:41:42 +0000 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | if (getLangOpts().MicrosoftExt) { |
Reid Kleckner | 5b08646 | 2014-02-20 22:52:09 +0000 | [diff] [blame] | 313 | PP.RemovePragmaHandler(MSDetectMismatchHandler.get()); |
| 314 | MSDetectMismatchHandler.reset(); |
| 315 | PP.RemovePragmaHandler(MSPointersToMembers.get()); |
| 316 | MSPointersToMembers.reset(); |
| 317 | PP.RemovePragmaHandler(MSVtorDisp.get()); |
| 318 | MSVtorDisp.reset(); |
Reid Kleckner | d3923aa | 2014-04-03 19:04:24 +0000 | [diff] [blame] | 319 | PP.RemovePragmaHandler(MSInitSeg.get()); |
| 320 | MSInitSeg.reset(); |
Warren Hunt | c3b1896 | 2014-04-08 22:30:47 +0000 | [diff] [blame] | 321 | PP.RemovePragmaHandler(MSDataSeg.get()); |
| 322 | MSDataSeg.reset(); |
| 323 | PP.RemovePragmaHandler(MSBSSSeg.get()); |
| 324 | MSBSSSeg.reset(); |
| 325 | PP.RemovePragmaHandler(MSConstSeg.get()); |
| 326 | MSConstSeg.reset(); |
| 327 | PP.RemovePragmaHandler(MSCodeSeg.get()); |
| 328 | MSCodeSeg.reset(); |
| 329 | PP.RemovePragmaHandler(MSSection.get()); |
| 330 | MSSection.reset(); |
Hans Wennborg | 7357bbc | 2015-10-12 20:47:58 +0000 | [diff] [blame] | 331 | PP.RemovePragmaHandler(MSRuntimeChecks.get()); |
| 332 | MSRuntimeChecks.reset(); |
Reid Kleckner | 3f1ec62 | 2016-09-07 16:38:32 +0000 | [diff] [blame] | 333 | PP.RemovePragmaHandler(MSIntrinsic.get()); |
| 334 | MSIntrinsic.reset(); |
Reid Kleckner | 5b08646 | 2014-02-20 22:52:09 +0000 | [diff] [blame] | 335 | } |
| 336 | |
Justin Lebar | 67a78a6 | 2016-10-08 22:15:58 +0000 | [diff] [blame] | 337 | if (getLangOpts().CUDA) { |
| 338 | PP.RemovePragmaHandler("clang", CUDAForceHostDeviceHandler.get()); |
| 339 | CUDAForceHostDeviceHandler.reset(); |
| 340 | } |
| 341 | |
Reid Kleckner | 5b08646 | 2014-02-20 22:52:09 +0000 | [diff] [blame] | 342 | PP.RemovePragmaHandler("STDC", FPContractHandler.get()); |
| 343 | FPContractHandler.reset(); |
Eli Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 344 | |
| 345 | PP.RemovePragmaHandler("clang", OptimizeHandler.get()); |
| 346 | OptimizeHandler.reset(); |
| 347 | |
| 348 | PP.RemovePragmaHandler("clang", LoopHintHandler.get()); |
| 349 | LoopHintHandler.reset(); |
Mark Heffernan | bd26f5e | 2014-07-21 18:08:34 +0000 | [diff] [blame] | 350 | |
| 351 | PP.RemovePragmaHandler(UnrollHintHandler.get()); |
| 352 | UnrollHintHandler.reset(); |
Mark Heffernan | c888e41 | 2014-07-24 18:09:38 +0000 | [diff] [blame] | 353 | |
| 354 | PP.RemovePragmaHandler(NoUnrollHintHandler.get()); |
| 355 | NoUnrollHintHandler.reset(); |
Adam Nemet | 60d3264 | 2017-04-04 21:18:36 +0000 | [diff] [blame^] | 356 | |
| 357 | PP.RemovePragmaHandler("clang", FPHandler.get()); |
| 358 | FPHandler.reset(); |
Eli Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | /// \brief Handle the annotation token produced for #pragma unused(...) |
Argyrios Kyrtzidis | ee56962 | 2011-01-17 18:58:44 +0000 | [diff] [blame] | 362 | /// |
| 363 | /// Each annot_pragma_unused is followed by the argument token so e.g. |
| 364 | /// "#pragma unused(x,y)" becomes: |
| 365 | /// annot_pragma_unused 'x' annot_pragma_unused 'y' |
| 366 | void Parser::HandlePragmaUnused() { |
| 367 | assert(Tok.is(tok::annot_pragma_unused)); |
| 368 | SourceLocation UnusedLoc = ConsumeToken(); |
| 369 | Actions.ActOnPragmaUnused(Tok, getCurScope(), UnusedLoc); |
| 370 | ConsumeToken(); // The argument token. |
| 371 | } |
Eli Friedman | 570024a | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 372 | |
Rafael Espindola | 273fd77 | 2012-01-26 02:02:57 +0000 | [diff] [blame] | 373 | void Parser::HandlePragmaVisibility() { |
| 374 | assert(Tok.is(tok::annot_pragma_vis)); |
| 375 | const IdentifierInfo *VisType = |
| 376 | static_cast<IdentifierInfo *>(Tok.getAnnotationValue()); |
| 377 | SourceLocation VisLoc = ConsumeToken(); |
| 378 | Actions.ActOnPragmaVisibility(VisType, VisLoc); |
| 379 | } |
| 380 | |
Benjamin Kramer | e003ca2 | 2015-10-28 13:54:16 +0000 | [diff] [blame] | 381 | namespace { |
Eli Friedman | ec52f92 | 2012-02-23 23:47:16 +0000 | [diff] [blame] | 382 | struct PragmaPackInfo { |
Denis Zobnin | 10c4f45 | 2016-04-29 18:17:40 +0000 | [diff] [blame] | 383 | Sema::PragmaMsStackAction Action; |
| 384 | StringRef SlotLabel; |
Eli Friedman | 68be164 | 2012-10-04 02:36:51 +0000 | [diff] [blame] | 385 | Token Alignment; |
Eli Friedman | ec52f92 | 2012-02-23 23:47:16 +0000 | [diff] [blame] | 386 | }; |
Benjamin Kramer | e003ca2 | 2015-10-28 13:54:16 +0000 | [diff] [blame] | 387 | } // end anonymous namespace |
Eli Friedman | ec52f92 | 2012-02-23 23:47:16 +0000 | [diff] [blame] | 388 | |
| 389 | void Parser::HandlePragmaPack() { |
| 390 | assert(Tok.is(tok::annot_pragma_pack)); |
| 391 | PragmaPackInfo *Info = |
| 392 | static_cast<PragmaPackInfo *>(Tok.getAnnotationValue()); |
| 393 | SourceLocation PragmaLoc = ConsumeToken(); |
Eli Friedman | 68be164 | 2012-10-04 02:36:51 +0000 | [diff] [blame] | 394 | ExprResult Alignment; |
| 395 | if (Info->Alignment.is(tok::numeric_constant)) { |
| 396 | Alignment = Actions.ActOnNumericConstant(Info->Alignment); |
| 397 | if (Alignment.isInvalid()) |
| 398 | return; |
| 399 | } |
Denis Zobnin | 10c4f45 | 2016-04-29 18:17:40 +0000 | [diff] [blame] | 400 | Actions.ActOnPragmaPack(PragmaLoc, Info->Action, Info->SlotLabel, |
| 401 | Alignment.get()); |
Eli Friedman | ec52f92 | 2012-02-23 23:47:16 +0000 | [diff] [blame] | 402 | } |
| 403 | |
Eli Friedman | 68be164 | 2012-10-04 02:36:51 +0000 | [diff] [blame] | 404 | void Parser::HandlePragmaMSStruct() { |
| 405 | assert(Tok.is(tok::annot_pragma_msstruct)); |
Nico Weber | 779355f | 2016-03-02 23:22:00 +0000 | [diff] [blame] | 406 | PragmaMSStructKind Kind = static_cast<PragmaMSStructKind>( |
| 407 | reinterpret_cast<uintptr_t>(Tok.getAnnotationValue())); |
Eli Friedman | 68be164 | 2012-10-04 02:36:51 +0000 | [diff] [blame] | 408 | Actions.ActOnPragmaMSStruct(Kind); |
| 409 | ConsumeToken(); // The annotation token. |
| 410 | } |
| 411 | |
| 412 | void Parser::HandlePragmaAlign() { |
| 413 | assert(Tok.is(tok::annot_pragma_align)); |
| 414 | Sema::PragmaOptionsAlignKind Kind = |
| 415 | static_cast<Sema::PragmaOptionsAlignKind>( |
| 416 | reinterpret_cast<uintptr_t>(Tok.getAnnotationValue())); |
| 417 | SourceLocation PragmaLoc = ConsumeToken(); |
| 418 | Actions.ActOnPragmaOptionsAlign(Kind, PragmaLoc); |
| 419 | } |
| 420 | |
Richard Smith | ba3a4f9 | 2016-01-12 21:59:26 +0000 | [diff] [blame] | 421 | void Parser::HandlePragmaDump() { |
| 422 | assert(Tok.is(tok::annot_pragma_dump)); |
| 423 | IdentifierInfo *II = |
| 424 | reinterpret_cast<IdentifierInfo *>(Tok.getAnnotationValue()); |
| 425 | Actions.ActOnPragmaDump(getCurScope(), Tok.getLocation(), II); |
| 426 | ConsumeToken(); |
| 427 | } |
| 428 | |
Eli Friedman | 68be164 | 2012-10-04 02:36:51 +0000 | [diff] [blame] | 429 | void Parser::HandlePragmaWeak() { |
| 430 | assert(Tok.is(tok::annot_pragma_weak)); |
| 431 | SourceLocation PragmaLoc = ConsumeToken(); |
| 432 | Actions.ActOnPragmaWeakID(Tok.getIdentifierInfo(), PragmaLoc, |
| 433 | Tok.getLocation()); |
| 434 | ConsumeToken(); // The weak name. |
| 435 | } |
| 436 | |
| 437 | void Parser::HandlePragmaWeakAlias() { |
| 438 | assert(Tok.is(tok::annot_pragma_weakalias)); |
| 439 | SourceLocation PragmaLoc = ConsumeToken(); |
| 440 | IdentifierInfo *WeakName = Tok.getIdentifierInfo(); |
| 441 | SourceLocation WeakNameLoc = Tok.getLocation(); |
| 442 | ConsumeToken(); |
| 443 | IdentifierInfo *AliasName = Tok.getIdentifierInfo(); |
| 444 | SourceLocation AliasNameLoc = Tok.getLocation(); |
| 445 | ConsumeToken(); |
| 446 | Actions.ActOnPragmaWeakAlias(WeakName, AliasName, PragmaLoc, |
| 447 | WeakNameLoc, AliasNameLoc); |
| 448 | |
| 449 | } |
| 450 | |
| 451 | void Parser::HandlePragmaRedefineExtname() { |
| 452 | assert(Tok.is(tok::annot_pragma_redefine_extname)); |
| 453 | SourceLocation RedefLoc = ConsumeToken(); |
| 454 | IdentifierInfo *RedefName = Tok.getIdentifierInfo(); |
| 455 | SourceLocation RedefNameLoc = Tok.getLocation(); |
| 456 | ConsumeToken(); |
| 457 | IdentifierInfo *AliasName = Tok.getIdentifierInfo(); |
| 458 | SourceLocation AliasNameLoc = Tok.getLocation(); |
| 459 | ConsumeToken(); |
| 460 | Actions.ActOnPragmaRedefineExtname(RedefName, AliasName, RedefLoc, |
| 461 | RedefNameLoc, AliasNameLoc); |
| 462 | } |
| 463 | |
| 464 | void Parser::HandlePragmaFPContract() { |
| 465 | assert(Tok.is(tok::annot_pragma_fp_contract)); |
| 466 | tok::OnOffSwitch OOS = |
| 467 | static_cast<tok::OnOffSwitch>( |
| 468 | reinterpret_cast<uintptr_t>(Tok.getAnnotationValue())); |
Adam Nemet | 60d3264 | 2017-04-04 21:18:36 +0000 | [diff] [blame^] | 469 | |
| 470 | LangOptions::FPContractModeKind FPC; |
| 471 | switch (OOS) { |
| 472 | case tok::OOS_ON: |
| 473 | FPC = LangOptions::FPC_On; |
| 474 | break; |
| 475 | case tok::OOS_OFF: |
| 476 | FPC = LangOptions::FPC_Off; |
| 477 | break; |
| 478 | case tok::OOS_DEFAULT: |
| 479 | FPC = getLangOpts().getDefaultFPContractMode(); |
| 480 | break; |
| 481 | } |
| 482 | |
| 483 | Actions.ActOnPragmaFPContract(FPC); |
Eli Friedman | 68be164 | 2012-10-04 02:36:51 +0000 | [diff] [blame] | 484 | ConsumeToken(); // The annotation token. |
| 485 | } |
| 486 | |
Tareq A. Siraj | 0de0dd4 | 2013-04-16 18:41:26 +0000 | [diff] [blame] | 487 | StmtResult Parser::HandlePragmaCaptured() |
| 488 | { |
| 489 | assert(Tok.is(tok::annot_pragma_captured)); |
| 490 | ConsumeToken(); |
| 491 | |
| 492 | if (Tok.isNot(tok::l_brace)) { |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 493 | PP.Diag(Tok, diag::err_expected) << tok::l_brace; |
Tareq A. Siraj | 0de0dd4 | 2013-04-16 18:41:26 +0000 | [diff] [blame] | 494 | return StmtError(); |
| 495 | } |
| 496 | |
Tareq A. Siraj | 6dfa25a | 2013-04-16 19:37:38 +0000 | [diff] [blame] | 497 | SourceLocation Loc = Tok.getLocation(); |
| 498 | |
| 499 | ParseScope CapturedRegionScope(this, Scope::FnScope | Scope::DeclScope); |
Ben Langmuir | 37943a7 | 2013-05-03 19:00:33 +0000 | [diff] [blame] | 500 | Actions.ActOnCapturedRegionStart(Loc, getCurScope(), CR_Default, |
| 501 | /*NumParams=*/1); |
Tareq A. Siraj | 6dfa25a | 2013-04-16 19:37:38 +0000 | [diff] [blame] | 502 | |
| 503 | StmtResult R = ParseCompoundStatement(); |
| 504 | CapturedRegionScope.Exit(); |
| 505 | |
| 506 | if (R.isInvalid()) { |
| 507 | Actions.ActOnCapturedRegionError(); |
| 508 | return StmtError(); |
| 509 | } |
| 510 | |
| 511 | return Actions.ActOnCapturedRegionEnd(R.get()); |
Tareq A. Siraj | 0de0dd4 | 2013-04-16 18:41:26 +0000 | [diff] [blame] | 512 | } |
| 513 | |
Eli Friedman | 68be164 | 2012-10-04 02:36:51 +0000 | [diff] [blame] | 514 | namespace { |
Yaxun Liu | 5b74665 | 2016-12-18 05:18:55 +0000 | [diff] [blame] | 515 | enum OpenCLExtState : char { |
| 516 | Disable, Enable, Begin, End |
| 517 | }; |
| 518 | typedef std::pair<const IdentifierInfo *, OpenCLExtState> OpenCLExtData; |
Eli Friedman | 68be164 | 2012-10-04 02:36:51 +0000 | [diff] [blame] | 519 | } |
| 520 | |
| 521 | void Parser::HandlePragmaOpenCLExtension() { |
| 522 | assert(Tok.is(tok::annot_pragma_opencl_extension)); |
Yaxun Liu | 5b74665 | 2016-12-18 05:18:55 +0000 | [diff] [blame] | 523 | OpenCLExtData *Data = static_cast<OpenCLExtData*>(Tok.getAnnotationValue()); |
| 524 | auto State = Data->second; |
| 525 | auto Ident = Data->first; |
Eli Friedman | 68be164 | 2012-10-04 02:36:51 +0000 | [diff] [blame] | 526 | SourceLocation NameLoc = Tok.getLocation(); |
| 527 | ConsumeToken(); // The annotation token. |
| 528 | |
Yaxun Liu | 5b74665 | 2016-12-18 05:18:55 +0000 | [diff] [blame] | 529 | auto &Opt = Actions.getOpenCLOptions(); |
| 530 | auto Name = Ident->getName(); |
Eli Friedman | 68be164 | 2012-10-04 02:36:51 +0000 | [diff] [blame] | 531 | // OpenCL 1.1 9.1: "The all variant sets the behavior for all extensions, |
| 532 | // overriding all previously issued extension directives, but only if the |
| 533 | // behavior is set to disable." |
Yaxun Liu | 5b74665 | 2016-12-18 05:18:55 +0000 | [diff] [blame] | 534 | if (Name == "all") { |
Konstantin Zhuravlyov | de70a88 | 2017-01-06 16:14:41 +0000 | [diff] [blame] | 535 | if (State == Disable) { |
Yaxun Liu | 5b74665 | 2016-12-18 05:18:55 +0000 | [diff] [blame] | 536 | Opt.disableAll(); |
Konstantin Zhuravlyov | de70a88 | 2017-01-06 16:14:41 +0000 | [diff] [blame] | 537 | Opt.enableSupportedCore(getLangOpts().OpenCLVersion); |
| 538 | } else { |
Yaxun Liu | 5b74665 | 2016-12-18 05:18:55 +0000 | [diff] [blame] | 539 | PP.Diag(NameLoc, diag::warn_pragma_expected_predicate) << 1; |
Konstantin Zhuravlyov | de70a88 | 2017-01-06 16:14:41 +0000 | [diff] [blame] | 540 | } |
Yaxun Liu | 5b74665 | 2016-12-18 05:18:55 +0000 | [diff] [blame] | 541 | } else if (State == Begin) { |
| 542 | if (!Opt.isKnown(Name) || |
| 543 | !Opt.isSupported(Name, getLangOpts().OpenCLVersion)) { |
| 544 | Opt.support(Name); |
| 545 | } |
| 546 | Actions.setCurrentOpenCLExtension(Name); |
| 547 | } else if (State == End) { |
| 548 | if (Name != Actions.getCurrentOpenCLExtension()) |
| 549 | PP.Diag(NameLoc, diag::warn_pragma_begin_end_mismatch); |
| 550 | Actions.setCurrentOpenCLExtension(""); |
| 551 | } else if (!Opt.isKnown(Name)) |
| 552 | PP.Diag(NameLoc, diag::warn_pragma_unknown_extension) << Ident; |
| 553 | else if (Opt.isSupportedExtension(Name, getLangOpts().OpenCLVersion)) |
| 554 | Opt.enable(Name, State == Enable); |
| 555 | else if (Opt.isSupportedCore(Name, getLangOpts().OpenCLVersion)) |
| 556 | PP.Diag(NameLoc, diag::warn_pragma_extension_is_core) << Ident; |
| 557 | else |
| 558 | PP.Diag(NameLoc, diag::warn_pragma_unsupported_extension) << Ident; |
Eli Friedman | 68be164 | 2012-10-04 02:36:51 +0000 | [diff] [blame] | 559 | } |
| 560 | |
David Majnemer | 4bb0980 | 2014-02-10 19:50:15 +0000 | [diff] [blame] | 561 | void Parser::HandlePragmaMSPointersToMembers() { |
| 562 | assert(Tok.is(tok::annot_pragma_ms_pointers_to_members)); |
David Majnemer | 86c318f | 2014-02-11 21:05:00 +0000 | [diff] [blame] | 563 | LangOptions::PragmaMSPointersToMembersKind RepresentationMethod = |
| 564 | static_cast<LangOptions::PragmaMSPointersToMembersKind>( |
David Majnemer | 4bb0980 | 2014-02-10 19:50:15 +0000 | [diff] [blame] | 565 | reinterpret_cast<uintptr_t>(Tok.getAnnotationValue())); |
| 566 | SourceLocation PragmaLoc = ConsumeToken(); // The annotation token. |
| 567 | Actions.ActOnPragmaMSPointersToMembers(RepresentationMethod, PragmaLoc); |
| 568 | } |
Tareq A. Siraj | 0de0dd4 | 2013-04-16 18:41:26 +0000 | [diff] [blame] | 569 | |
Reid Kleckner | c0dca6d | 2014-02-12 23:50:26 +0000 | [diff] [blame] | 570 | void Parser::HandlePragmaMSVtorDisp() { |
| 571 | assert(Tok.is(tok::annot_pragma_ms_vtordisp)); |
| 572 | uintptr_t Value = reinterpret_cast<uintptr_t>(Tok.getAnnotationValue()); |
Denis Zobnin | 2290dac | 2016-04-29 11:27:00 +0000 | [diff] [blame] | 573 | Sema::PragmaMsStackAction Action = |
| 574 | static_cast<Sema::PragmaMsStackAction>((Value >> 16) & 0xFFFF); |
Reid Kleckner | c0dca6d | 2014-02-12 23:50:26 +0000 | [diff] [blame] | 575 | MSVtorDispAttr::Mode Mode = MSVtorDispAttr::Mode(Value & 0xFFFF); |
| 576 | SourceLocation PragmaLoc = ConsumeToken(); // The annotation token. |
Denis Zobnin | 2290dac | 2016-04-29 11:27:00 +0000 | [diff] [blame] | 577 | Actions.ActOnPragmaMSVtorDisp(Action, PragmaLoc, Mode); |
Reid Kleckner | c0dca6d | 2014-02-12 23:50:26 +0000 | [diff] [blame] | 578 | } |
Tareq A. Siraj | 0de0dd4 | 2013-04-16 18:41:26 +0000 | [diff] [blame] | 579 | |
Warren Hunt | c3b1896 | 2014-04-08 22:30:47 +0000 | [diff] [blame] | 580 | void Parser::HandlePragmaMSPragma() { |
| 581 | assert(Tok.is(tok::annot_pragma_ms_pragma)); |
| 582 | // 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] | 583 | auto TheTokens = |
| 584 | (std::pair<std::unique_ptr<Token[]>, size_t> *)Tok.getAnnotationValue(); |
| 585 | PP.EnterTokenStream(std::move(TheTokens->first), TheTokens->second, true); |
Warren Hunt | c3b1896 | 2014-04-08 22:30:47 +0000 | [diff] [blame] | 586 | SourceLocation PragmaLocation = ConsumeToken(); // The annotation token. |
| 587 | assert(Tok.isAnyIdentifier()); |
Reid Kleckner | 722b1df | 2014-07-18 00:13:16 +0000 | [diff] [blame] | 588 | StringRef PragmaName = Tok.getIdentifierInfo()->getName(); |
Warren Hunt | c3b1896 | 2014-04-08 22:30:47 +0000 | [diff] [blame] | 589 | PP.Lex(Tok); // pragma kind |
Reid Kleckner | 722b1df | 2014-07-18 00:13:16 +0000 | [diff] [blame] | 590 | |
Warren Hunt | c3b1896 | 2014-04-08 22:30:47 +0000 | [diff] [blame] | 591 | // Figure out which #pragma we're dealing with. The switch has no default |
| 592 | // because lex shouldn't emit the annotation token for unrecognized pragmas. |
Reid Kleckner | 722b1df | 2014-07-18 00:13:16 +0000 | [diff] [blame] | 593 | typedef bool (Parser::*PragmaHandler)(StringRef, SourceLocation); |
Warren Hunt | c3b1896 | 2014-04-08 22:30:47 +0000 | [diff] [blame] | 594 | PragmaHandler Handler = llvm::StringSwitch<PragmaHandler>(PragmaName) |
| 595 | .Case("data_seg", &Parser::HandlePragmaMSSegment) |
| 596 | .Case("bss_seg", &Parser::HandlePragmaMSSegment) |
| 597 | .Case("const_seg", &Parser::HandlePragmaMSSegment) |
| 598 | .Case("code_seg", &Parser::HandlePragmaMSSegment) |
| 599 | .Case("section", &Parser::HandlePragmaMSSection) |
| 600 | .Case("init_seg", &Parser::HandlePragmaMSInitSeg); |
Reid Kleckner | 722b1df | 2014-07-18 00:13:16 +0000 | [diff] [blame] | 601 | |
| 602 | if (!(this->*Handler)(PragmaName, PragmaLocation)) { |
| 603 | // Pragma handling failed, and has been diagnosed. Slurp up the tokens |
| 604 | // until eof (really end of line) to prevent follow-on errors. |
Warren Hunt | c3b1896 | 2014-04-08 22:30:47 +0000 | [diff] [blame] | 605 | while (Tok.isNot(tok::eof)) |
| 606 | PP.Lex(Tok); |
| 607 | PP.Lex(Tok); |
| 608 | } |
| 609 | } |
| 610 | |
Reid Kleckner | 722b1df | 2014-07-18 00:13:16 +0000 | [diff] [blame] | 611 | bool Parser::HandlePragmaMSSection(StringRef PragmaName, |
| 612 | SourceLocation PragmaLocation) { |
| 613 | if (Tok.isNot(tok::l_paren)) { |
| 614 | PP.Diag(PragmaLocation, diag::warn_pragma_expected_lparen) << PragmaName; |
| 615 | return false; |
| 616 | } |
Warren Hunt | c3b1896 | 2014-04-08 22:30:47 +0000 | [diff] [blame] | 617 | PP.Lex(Tok); // ( |
| 618 | // Parsing code for pragma section |
Reid Kleckner | 722b1df | 2014-07-18 00:13:16 +0000 | [diff] [blame] | 619 | if (Tok.isNot(tok::string_literal)) { |
| 620 | PP.Diag(PragmaLocation, diag::warn_pragma_expected_section_name) |
| 621 | << PragmaName; |
| 622 | return false; |
| 623 | } |
| 624 | ExprResult StringResult = ParseStringLiteralExpression(); |
| 625 | if (StringResult.isInvalid()) |
| 626 | return false; // Already diagnosed. |
| 627 | StringLiteral *SegmentName = cast<StringLiteral>(StringResult.get()); |
| 628 | if (SegmentName->getCharByteWidth() != 1) { |
| 629 | PP.Diag(PragmaLocation, diag::warn_pragma_expected_non_wide_string) |
| 630 | << PragmaName; |
| 631 | return false; |
| 632 | } |
David Majnemer | 48c28fa | 2014-10-22 21:08:43 +0000 | [diff] [blame] | 633 | int SectionFlags = ASTContext::PSF_Read; |
| 634 | bool SectionFlagsAreDefault = true; |
Warren Hunt | c3b1896 | 2014-04-08 22:30:47 +0000 | [diff] [blame] | 635 | while (Tok.is(tok::comma)) { |
| 636 | PP.Lex(Tok); // , |
David Majnemer | 48c28fa | 2014-10-22 21:08:43 +0000 | [diff] [blame] | 637 | // Ignore "long" and "short". |
| 638 | // They are undocumented, but widely used, section attributes which appear |
| 639 | // to do nothing. |
| 640 | if (Tok.is(tok::kw_long) || Tok.is(tok::kw_short)) { |
| 641 | PP.Lex(Tok); // long/short |
| 642 | continue; |
| 643 | } |
| 644 | |
Reid Kleckner | 722b1df | 2014-07-18 00:13:16 +0000 | [diff] [blame] | 645 | if (!Tok.isAnyIdentifier()) { |
| 646 | PP.Diag(PragmaLocation, diag::warn_pragma_expected_action_or_r_paren) |
| 647 | << PragmaName; |
| 648 | return false; |
| 649 | } |
Hans Wennborg | 899ded9 | 2014-10-16 20:52:46 +0000 | [diff] [blame] | 650 | ASTContext::PragmaSectionFlag Flag = |
| 651 | llvm::StringSwitch<ASTContext::PragmaSectionFlag>( |
Warren Hunt | c3b1896 | 2014-04-08 22:30:47 +0000 | [diff] [blame] | 652 | Tok.getIdentifierInfo()->getName()) |
Hans Wennborg | 899ded9 | 2014-10-16 20:52:46 +0000 | [diff] [blame] | 653 | .Case("read", ASTContext::PSF_Read) |
| 654 | .Case("write", ASTContext::PSF_Write) |
| 655 | .Case("execute", ASTContext::PSF_Execute) |
| 656 | .Case("shared", ASTContext::PSF_Invalid) |
| 657 | .Case("nopage", ASTContext::PSF_Invalid) |
| 658 | .Case("nocache", ASTContext::PSF_Invalid) |
| 659 | .Case("discard", ASTContext::PSF_Invalid) |
| 660 | .Case("remove", ASTContext::PSF_Invalid) |
| 661 | .Default(ASTContext::PSF_None); |
| 662 | if (Flag == ASTContext::PSF_None || Flag == ASTContext::PSF_Invalid) { |
| 663 | PP.Diag(PragmaLocation, Flag == ASTContext::PSF_None |
Reid Kleckner | 722b1df | 2014-07-18 00:13:16 +0000 | [diff] [blame] | 664 | ? diag::warn_pragma_invalid_specific_action |
| 665 | : diag::warn_pragma_unsupported_action) |
Warren Hunt | c3b1896 | 2014-04-08 22:30:47 +0000 | [diff] [blame] | 666 | << PragmaName << Tok.getIdentifierInfo()->getName(); |
Reid Kleckner | 722b1df | 2014-07-18 00:13:16 +0000 | [diff] [blame] | 667 | return false; |
Warren Hunt | c3b1896 | 2014-04-08 22:30:47 +0000 | [diff] [blame] | 668 | } |
| 669 | SectionFlags |= Flag; |
David Majnemer | 48c28fa | 2014-10-22 21:08:43 +0000 | [diff] [blame] | 670 | SectionFlagsAreDefault = false; |
Warren Hunt | c3b1896 | 2014-04-08 22:30:47 +0000 | [diff] [blame] | 671 | PP.Lex(Tok); // Identifier |
| 672 | } |
David Majnemer | 48c28fa | 2014-10-22 21:08:43 +0000 | [diff] [blame] | 673 | // If no section attributes are specified, the section will be marked as |
| 674 | // read/write. |
| 675 | if (SectionFlagsAreDefault) |
| 676 | SectionFlags |= ASTContext::PSF_Write; |
Reid Kleckner | 722b1df | 2014-07-18 00:13:16 +0000 | [diff] [blame] | 677 | if (Tok.isNot(tok::r_paren)) { |
| 678 | PP.Diag(PragmaLocation, diag::warn_pragma_expected_rparen) << PragmaName; |
| 679 | return false; |
| 680 | } |
Warren Hunt | c3b1896 | 2014-04-08 22:30:47 +0000 | [diff] [blame] | 681 | PP.Lex(Tok); // ) |
Reid Kleckner | 722b1df | 2014-07-18 00:13:16 +0000 | [diff] [blame] | 682 | if (Tok.isNot(tok::eof)) { |
| 683 | PP.Diag(PragmaLocation, diag::warn_pragma_extra_tokens_at_eol) |
| 684 | << PragmaName; |
| 685 | return false; |
| 686 | } |
Warren Hunt | c3b1896 | 2014-04-08 22:30:47 +0000 | [diff] [blame] | 687 | PP.Lex(Tok); // eof |
| 688 | Actions.ActOnPragmaMSSection(PragmaLocation, SectionFlags, SegmentName); |
Reid Kleckner | 722b1df | 2014-07-18 00:13:16 +0000 | [diff] [blame] | 689 | return true; |
Warren Hunt | c3b1896 | 2014-04-08 22:30:47 +0000 | [diff] [blame] | 690 | } |
| 691 | |
Reid Kleckner | 722b1df | 2014-07-18 00:13:16 +0000 | [diff] [blame] | 692 | bool Parser::HandlePragmaMSSegment(StringRef PragmaName, |
| 693 | SourceLocation PragmaLocation) { |
| 694 | if (Tok.isNot(tok::l_paren)) { |
| 695 | PP.Diag(PragmaLocation, diag::warn_pragma_expected_lparen) << PragmaName; |
| 696 | return false; |
| 697 | } |
Warren Hunt | c3b1896 | 2014-04-08 22:30:47 +0000 | [diff] [blame] | 698 | PP.Lex(Tok); // ( |
| 699 | Sema::PragmaMsStackAction Action = Sema::PSK_Reset; |
Reid Kleckner | 722b1df | 2014-07-18 00:13:16 +0000 | [diff] [blame] | 700 | StringRef SlotLabel; |
Warren Hunt | c3b1896 | 2014-04-08 22:30:47 +0000 | [diff] [blame] | 701 | if (Tok.isAnyIdentifier()) { |
Reid Kleckner | 722b1df | 2014-07-18 00:13:16 +0000 | [diff] [blame] | 702 | StringRef PushPop = Tok.getIdentifierInfo()->getName(); |
Warren Hunt | c3b1896 | 2014-04-08 22:30:47 +0000 | [diff] [blame] | 703 | if (PushPop == "push") |
| 704 | Action = Sema::PSK_Push; |
| 705 | else if (PushPop == "pop") |
| 706 | Action = Sema::PSK_Pop; |
Reid Kleckner | 722b1df | 2014-07-18 00:13:16 +0000 | [diff] [blame] | 707 | else { |
| 708 | PP.Diag(PragmaLocation, |
| 709 | diag::warn_pragma_expected_section_push_pop_or_name) |
| 710 | << PragmaName; |
| 711 | return false; |
| 712 | } |
Warren Hunt | c3b1896 | 2014-04-08 22:30:47 +0000 | [diff] [blame] | 713 | if (Action != Sema::PSK_Reset) { |
| 714 | PP.Lex(Tok); // push | pop |
| 715 | if (Tok.is(tok::comma)) { |
| 716 | PP.Lex(Tok); // , |
| 717 | // If we've got a comma, we either need a label or a string. |
| 718 | if (Tok.isAnyIdentifier()) { |
| 719 | SlotLabel = Tok.getIdentifierInfo()->getName(); |
| 720 | PP.Lex(Tok); // identifier |
| 721 | if (Tok.is(tok::comma)) |
| 722 | PP.Lex(Tok); |
Reid Kleckner | 722b1df | 2014-07-18 00:13:16 +0000 | [diff] [blame] | 723 | else if (Tok.isNot(tok::r_paren)) { |
| 724 | PP.Diag(PragmaLocation, diag::warn_pragma_expected_punc) |
| 725 | << PragmaName; |
| 726 | return false; |
| 727 | } |
Warren Hunt | c3b1896 | 2014-04-08 22:30:47 +0000 | [diff] [blame] | 728 | } |
Reid Kleckner | 722b1df | 2014-07-18 00:13:16 +0000 | [diff] [blame] | 729 | } else if (Tok.isNot(tok::r_paren)) { |
| 730 | PP.Diag(PragmaLocation, diag::warn_pragma_expected_punc) << PragmaName; |
| 731 | return false; |
| 732 | } |
Warren Hunt | c3b1896 | 2014-04-08 22:30:47 +0000 | [diff] [blame] | 733 | } |
| 734 | } |
| 735 | // Grab the string literal for our section name. |
| 736 | StringLiteral *SegmentName = nullptr; |
| 737 | if (Tok.isNot(tok::r_paren)) { |
Reid Kleckner | 722b1df | 2014-07-18 00:13:16 +0000 | [diff] [blame] | 738 | if (Tok.isNot(tok::string_literal)) { |
| 739 | unsigned DiagID = Action != Sema::PSK_Reset ? !SlotLabel.empty() ? |
Warren Hunt | c3b1896 | 2014-04-08 22:30:47 +0000 | [diff] [blame] | 740 | diag::warn_pragma_expected_section_name : |
| 741 | diag::warn_pragma_expected_section_label_or_name : |
| 742 | diag::warn_pragma_expected_section_push_pop_or_name; |
Reid Kleckner | 722b1df | 2014-07-18 00:13:16 +0000 | [diff] [blame] | 743 | PP.Diag(PragmaLocation, DiagID) << PragmaName; |
| 744 | return false; |
| 745 | } |
| 746 | ExprResult StringResult = ParseStringLiteralExpression(); |
| 747 | if (StringResult.isInvalid()) |
| 748 | return false; // Already diagnosed. |
| 749 | SegmentName = cast<StringLiteral>(StringResult.get()); |
| 750 | if (SegmentName->getCharByteWidth() != 1) { |
| 751 | PP.Diag(PragmaLocation, diag::warn_pragma_expected_non_wide_string) |
| 752 | << PragmaName; |
| 753 | return false; |
| 754 | } |
Warren Hunt | c3b1896 | 2014-04-08 22:30:47 +0000 | [diff] [blame] | 755 | // Setting section "" has no effect |
| 756 | if (SegmentName->getLength()) |
| 757 | Action = (Sema::PragmaMsStackAction)(Action | Sema::PSK_Set); |
| 758 | } |
Reid Kleckner | 722b1df | 2014-07-18 00:13:16 +0000 | [diff] [blame] | 759 | if (Tok.isNot(tok::r_paren)) { |
| 760 | PP.Diag(PragmaLocation, diag::warn_pragma_expected_rparen) << PragmaName; |
| 761 | return false; |
| 762 | } |
Warren Hunt | c3b1896 | 2014-04-08 22:30:47 +0000 | [diff] [blame] | 763 | PP.Lex(Tok); // ) |
Reid Kleckner | 722b1df | 2014-07-18 00:13:16 +0000 | [diff] [blame] | 764 | if (Tok.isNot(tok::eof)) { |
| 765 | PP.Diag(PragmaLocation, diag::warn_pragma_extra_tokens_at_eol) |
| 766 | << PragmaName; |
| 767 | return false; |
| 768 | } |
Warren Hunt | c3b1896 | 2014-04-08 22:30:47 +0000 | [diff] [blame] | 769 | PP.Lex(Tok); // eof |
| 770 | Actions.ActOnPragmaMSSeg(PragmaLocation, Action, SlotLabel, |
| 771 | SegmentName, PragmaName); |
Reid Kleckner | 722b1df | 2014-07-18 00:13:16 +0000 | [diff] [blame] | 772 | return true; |
Warren Hunt | c3b1896 | 2014-04-08 22:30:47 +0000 | [diff] [blame] | 773 | } |
| 774 | |
Reid Kleckner | 1a711b1 | 2014-07-22 00:53:05 +0000 | [diff] [blame] | 775 | // #pragma init_seg({ compiler | lib | user | "section-name" [, func-name]} ) |
Reid Kleckner | 722b1df | 2014-07-18 00:13:16 +0000 | [diff] [blame] | 776 | bool Parser::HandlePragmaMSInitSeg(StringRef PragmaName, |
| 777 | SourceLocation PragmaLocation) { |
David Majnemer | ad2986e | 2014-08-14 06:35:08 +0000 | [diff] [blame] | 778 | if (getTargetInfo().getTriple().getEnvironment() != llvm::Triple::MSVC) { |
| 779 | PP.Diag(PragmaLocation, diag::warn_pragma_init_seg_unsupported_target); |
| 780 | return false; |
| 781 | } |
| 782 | |
Reid Kleckner | 1a711b1 | 2014-07-22 00:53:05 +0000 | [diff] [blame] | 783 | if (ExpectAndConsume(tok::l_paren, diag::warn_pragma_expected_lparen, |
| 784 | PragmaName)) |
| 785 | return false; |
| 786 | |
| 787 | // Parse either the known section names or the string section name. |
| 788 | StringLiteral *SegmentName = nullptr; |
| 789 | if (Tok.isAnyIdentifier()) { |
| 790 | auto *II = Tok.getIdentifierInfo(); |
| 791 | StringRef Section = llvm::StringSwitch<StringRef>(II->getName()) |
| 792 | .Case("compiler", "\".CRT$XCC\"") |
| 793 | .Case("lib", "\".CRT$XCL\"") |
| 794 | .Case("user", "\".CRT$XCU\"") |
| 795 | .Default(""); |
| 796 | |
| 797 | if (!Section.empty()) { |
| 798 | // Pretend the user wrote the appropriate string literal here. |
| 799 | Token Toks[1]; |
| 800 | Toks[0].startToken(); |
| 801 | Toks[0].setKind(tok::string_literal); |
| 802 | Toks[0].setLocation(Tok.getLocation()); |
| 803 | Toks[0].setLiteralData(Section.data()); |
| 804 | Toks[0].setLength(Section.size()); |
| 805 | SegmentName = |
| 806 | cast<StringLiteral>(Actions.ActOnStringLiteral(Toks, nullptr).get()); |
| 807 | PP.Lex(Tok); |
| 808 | } |
| 809 | } else if (Tok.is(tok::string_literal)) { |
| 810 | ExprResult StringResult = ParseStringLiteralExpression(); |
| 811 | if (StringResult.isInvalid()) |
| 812 | return false; |
| 813 | SegmentName = cast<StringLiteral>(StringResult.get()); |
| 814 | if (SegmentName->getCharByteWidth() != 1) { |
| 815 | PP.Diag(PragmaLocation, diag::warn_pragma_expected_non_wide_string) |
| 816 | << PragmaName; |
| 817 | return false; |
| 818 | } |
| 819 | // FIXME: Add support for the '[, func-name]' part of the pragma. |
| 820 | } |
| 821 | |
| 822 | if (!SegmentName) { |
| 823 | PP.Diag(PragmaLocation, diag::warn_pragma_expected_init_seg) << PragmaName; |
| 824 | return false; |
| 825 | } |
| 826 | |
| 827 | if (ExpectAndConsume(tok::r_paren, diag::warn_pragma_expected_rparen, |
| 828 | PragmaName) || |
| 829 | ExpectAndConsume(tok::eof, diag::warn_pragma_extra_tokens_at_eol, |
| 830 | PragmaName)) |
| 831 | return false; |
| 832 | |
| 833 | Actions.ActOnPragmaMSInitSeg(PragmaLocation, SegmentName); |
| 834 | return true; |
Eli Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 835 | } |
| 836 | |
Benjamin Kramer | e003ca2 | 2015-10-28 13:54:16 +0000 | [diff] [blame] | 837 | namespace { |
Eli Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 838 | struct PragmaLoopHintInfo { |
Mark Heffernan | bd26f5e | 2014-07-21 18:08:34 +0000 | [diff] [blame] | 839 | Token PragmaName; |
Eli Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 840 | Token Option; |
Benjamin Kramer | fa7f855 | 2015-08-05 09:39:57 +0000 | [diff] [blame] | 841 | ArrayRef<Token> Toks; |
Eli Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 842 | }; |
Benjamin Kramer | e003ca2 | 2015-10-28 13:54:16 +0000 | [diff] [blame] | 843 | } // end anonymous namespace |
Eli Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 844 | |
Tyler Nowicki | c724a83e | 2014-10-12 20:46:07 +0000 | [diff] [blame] | 845 | static std::string PragmaLoopHintString(Token PragmaName, Token Option) { |
| 846 | std::string PragmaString; |
| 847 | if (PragmaName.getIdentifierInfo()->getName() == "loop") { |
| 848 | PragmaString = "clang loop "; |
| 849 | PragmaString += Option.getIdentifierInfo()->getName(); |
| 850 | } else { |
| 851 | assert(PragmaName.getIdentifierInfo()->getName() == "unroll" && |
| 852 | "Unexpected pragma name"); |
| 853 | PragmaString = "unroll"; |
| 854 | } |
| 855 | return PragmaString; |
| 856 | } |
| 857 | |
Tyler Nowicki | 0c9b34b | 2014-07-31 20:15:14 +0000 | [diff] [blame] | 858 | bool Parser::HandlePragmaLoopHint(LoopHint &Hint) { |
Eli Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 859 | assert(Tok.is(tok::annot_pragma_loop_hint)); |
| 860 | PragmaLoopHintInfo *Info = |
| 861 | static_cast<PragmaLoopHintInfo *>(Tok.getAnnotationValue()); |
| 862 | |
Tyler Nowicki | 0c9b34b | 2014-07-31 20:15:14 +0000 | [diff] [blame] | 863 | IdentifierInfo *PragmaNameInfo = Info->PragmaName.getIdentifierInfo(); |
| 864 | Hint.PragmaNameLoc = IdentifierLoc::create( |
| 865 | Actions.Context, Info->PragmaName.getLocation(), PragmaNameInfo); |
Eli Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 866 | |
Aaron Ballman | ef940aa | 2014-07-31 21:24:32 +0000 | [diff] [blame] | 867 | // It is possible that the loop hint has no option identifier, such as |
| 868 | // #pragma unroll(4). |
| 869 | IdentifierInfo *OptionInfo = Info->Option.is(tok::identifier) |
| 870 | ? Info->Option.getIdentifierInfo() |
| 871 | : nullptr; |
Tyler Nowicki | 0c9b34b | 2014-07-31 20:15:14 +0000 | [diff] [blame] | 872 | Hint.OptionLoc = IdentifierLoc::create( |
| 873 | Actions.Context, Info->Option.getLocation(), OptionInfo); |
| 874 | |
David Blaikie | 2eabcc9 | 2016-02-09 18:52:09 +0000 | [diff] [blame] | 875 | llvm::ArrayRef<Token> Toks = Info->Toks; |
Tyler Nowicki | c724a83e | 2014-10-12 20:46:07 +0000 | [diff] [blame] | 876 | |
Tyler Nowicki | 0c9b34b | 2014-07-31 20:15:14 +0000 | [diff] [blame] | 877 | // Return a valid hint if pragma unroll or nounroll were specified |
| 878 | // without an argument. |
| 879 | bool PragmaUnroll = PragmaNameInfo->getName() == "unroll"; |
| 880 | bool PragmaNoUnroll = PragmaNameInfo->getName() == "nounroll"; |
David Blaikie | 2eabcc9 | 2016-02-09 18:52:09 +0000 | [diff] [blame] | 881 | if (Toks.empty() && (PragmaUnroll || PragmaNoUnroll)) { |
Tyler Nowicki | c724a83e | 2014-10-12 20:46:07 +0000 | [diff] [blame] | 882 | ConsumeToken(); // The annotation token. |
Tyler Nowicki | 0c9b34b | 2014-07-31 20:15:14 +0000 | [diff] [blame] | 883 | Hint.Range = Info->PragmaName.getLocation(); |
| 884 | return true; |
| 885 | } |
| 886 | |
Tyler Nowicki | c724a83e | 2014-10-12 20:46:07 +0000 | [diff] [blame] | 887 | // The constant expression is always followed by an eof token, which increases |
| 888 | // the TokSize by 1. |
David Blaikie | 2eabcc9 | 2016-02-09 18:52:09 +0000 | [diff] [blame] | 889 | assert(!Toks.empty() && |
Tyler Nowicki | c724a83e | 2014-10-12 20:46:07 +0000 | [diff] [blame] | 890 | "PragmaLoopHintInfo::Toks must contain at least one token."); |
| 891 | |
| 892 | // 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] | 893 | bool OptionUnroll = false; |
Adam Nemet | 2de463e | 2016-06-14 12:04:26 +0000 | [diff] [blame] | 894 | bool OptionDistribute = false; |
Tyler Nowicki | 0c9b34b | 2014-07-31 20:15:14 +0000 | [diff] [blame] | 895 | bool StateOption = false; |
Tyler Nowicki | 24853c1 | 2015-06-08 23:13:43 +0000 | [diff] [blame] | 896 | if (OptionInfo) { // Pragma Unroll does not specify an option. |
| 897 | OptionUnroll = OptionInfo->isStr("unroll"); |
Adam Nemet | 2de463e | 2016-06-14 12:04:26 +0000 | [diff] [blame] | 898 | OptionDistribute = OptionInfo->isStr("distribute"); |
Tyler Nowicki | 0c9b34b | 2014-07-31 20:15:14 +0000 | [diff] [blame] | 899 | StateOption = llvm::StringSwitch<bool>(OptionInfo->getName()) |
| 900 | .Case("vectorize", true) |
| 901 | .Case("interleave", true) |
Adam Nemet | 2de463e | 2016-06-14 12:04:26 +0000 | [diff] [blame] | 902 | .Default(false) || |
| 903 | OptionUnroll || OptionDistribute; |
Tyler Nowicki | c724a83e | 2014-10-12 20:46:07 +0000 | [diff] [blame] | 904 | } |
| 905 | |
Adam Nemet | 2de463e | 2016-06-14 12:04:26 +0000 | [diff] [blame] | 906 | bool AssumeSafetyArg = !OptionUnroll && !OptionDistribute; |
Tyler Nowicki | c724a83e | 2014-10-12 20:46:07 +0000 | [diff] [blame] | 907 | // Verify loop hint has an argument. |
| 908 | if (Toks[0].is(tok::eof)) { |
| 909 | ConsumeToken(); // The annotation token. |
| 910 | Diag(Toks[0].getLocation(), diag::err_pragma_loop_missing_argument) |
Adam Nemet | 2de463e | 2016-06-14 12:04:26 +0000 | [diff] [blame] | 911 | << /*StateArgument=*/StateOption << /*FullKeyword=*/OptionUnroll |
| 912 | << /*AssumeSafetyKeyword=*/AssumeSafetyArg; |
Tyler Nowicki | c724a83e | 2014-10-12 20:46:07 +0000 | [diff] [blame] | 913 | return false; |
| 914 | } |
Tyler Nowicki | 0c9b34b | 2014-07-31 20:15:14 +0000 | [diff] [blame] | 915 | |
| 916 | // Validate the argument. |
| 917 | if (StateOption) { |
Tyler Nowicki | c724a83e | 2014-10-12 20:46:07 +0000 | [diff] [blame] | 918 | ConsumeToken(); // The annotation token. |
Tyler Nowicki | c724a83e | 2014-10-12 20:46:07 +0000 | [diff] [blame] | 919 | SourceLocation StateLoc = Toks[0].getLocation(); |
| 920 | IdentifierInfo *StateInfo = Toks[0].getIdentifierInfo(); |
Adam Nemet | 50de4e8 | 2016-04-19 22:17:45 +0000 | [diff] [blame] | 921 | |
| 922 | bool Valid = StateInfo && |
| 923 | llvm::StringSwitch<bool>(StateInfo->getName()) |
| 924 | .Cases("enable", "disable", true) |
| 925 | .Case("full", OptionUnroll) |
Adam Nemet | 2de463e | 2016-06-14 12:04:26 +0000 | [diff] [blame] | 926 | .Case("assume_safety", AssumeSafetyArg) |
Adam Nemet | 50de4e8 | 2016-04-19 22:17:45 +0000 | [diff] [blame] | 927 | .Default(false); |
| 928 | if (!Valid) { |
Tyler Nowicki | c724a83e | 2014-10-12 20:46:07 +0000 | [diff] [blame] | 929 | Diag(Toks[0].getLocation(), diag::err_pragma_invalid_keyword) |
Adam Nemet | 2de463e | 2016-06-14 12:04:26 +0000 | [diff] [blame] | 930 | << /*FullKeyword=*/OptionUnroll |
| 931 | << /*AssumeSafetyKeyword=*/AssumeSafetyArg; |
Tyler Nowicki | 0c9b34b | 2014-07-31 20:15:14 +0000 | [diff] [blame] | 932 | return false; |
| 933 | } |
David Blaikie | 2eabcc9 | 2016-02-09 18:52:09 +0000 | [diff] [blame] | 934 | if (Toks.size() > 2) |
Tyler Nowicki | c724a83e | 2014-10-12 20:46:07 +0000 | [diff] [blame] | 935 | Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) |
| 936 | << PragmaLoopHintString(Info->PragmaName, Info->Option); |
Tyler Nowicki | 0c9b34b | 2014-07-31 20:15:14 +0000 | [diff] [blame] | 937 | Hint.StateLoc = IdentifierLoc::create(Actions.Context, StateLoc, StateInfo); |
| 938 | } else { |
Tyler Nowicki | c724a83e | 2014-10-12 20:46:07 +0000 | [diff] [blame] | 939 | // Enter constant expression including eof terminator into token stream. |
David Blaikie | 2eabcc9 | 2016-02-09 18:52:09 +0000 | [diff] [blame] | 940 | PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/false); |
Tyler Nowicki | c724a83e | 2014-10-12 20:46:07 +0000 | [diff] [blame] | 941 | ConsumeToken(); // The annotation token. |
| 942 | |
| 943 | ExprResult R = ParseConstantExpression(); |
| 944 | |
| 945 | // Tokens following an error in an ill-formed constant expression will |
| 946 | // remain in the token stream and must be removed. |
| 947 | if (Tok.isNot(tok::eof)) { |
| 948 | Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) |
| 949 | << PragmaLoopHintString(Info->PragmaName, Info->Option); |
| 950 | while (Tok.isNot(tok::eof)) |
| 951 | ConsumeAnyToken(); |
Tyler Nowicki | 0c9b34b | 2014-07-31 20:15:14 +0000 | [diff] [blame] | 952 | } |
Tyler Nowicki | c724a83e | 2014-10-12 20:46:07 +0000 | [diff] [blame] | 953 | |
| 954 | ConsumeToken(); // Consume the constant expression eof terminator. |
| 955 | |
| 956 | if (R.isInvalid() || |
| 957 | Actions.CheckLoopHintExpr(R.get(), Toks[0].getLocation())) |
| 958 | return false; |
| 959 | |
| 960 | // Argument is a constant expression with an integer type. |
| 961 | Hint.ValueExpr = R.get(); |
Mark Heffernan | bd26f5e | 2014-07-21 18:08:34 +0000 | [diff] [blame] | 962 | } |
Eli Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 963 | |
Tyler Nowicki | c724a83e | 2014-10-12 20:46:07 +0000 | [diff] [blame] | 964 | Hint.Range = SourceRange(Info->PragmaName.getLocation(), |
David Blaikie | 2eabcc9 | 2016-02-09 18:52:09 +0000 | [diff] [blame] | 965 | Info->Toks.back().getLocation()); |
Tyler Nowicki | 0c9b34b | 2014-07-31 20:15:14 +0000 | [diff] [blame] | 966 | return true; |
Eli Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 967 | } |
| 968 | |
| 969 | // #pragma GCC visibility comes in two variants: |
| 970 | // 'push' '(' [visibility] ')' |
| 971 | // 'pop' |
Douglas Gregor | c7d6576 | 2010-09-09 22:45:38 +0000 | [diff] [blame] | 972 | void PragmaGCCVisibilityHandler::HandlePragma(Preprocessor &PP, |
| 973 | PragmaIntroducerKind Introducer, |
| 974 | Token &VisTok) { |
Eli Friedman | 570024a | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 975 | SourceLocation VisLoc = VisTok.getLocation(); |
| 976 | |
| 977 | Token Tok; |
Joerg Sonnenberger | 869f0b7 | 2011-07-20 01:03:50 +0000 | [diff] [blame] | 978 | PP.LexUnexpandedToken(Tok); |
Eli Friedman | 570024a | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 979 | |
| 980 | const IdentifierInfo *PushPop = Tok.getIdentifierInfo(); |
| 981 | |
Eli Friedman | 570024a | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 982 | const IdentifierInfo *VisType; |
| 983 | if (PushPop && PushPop->isStr("pop")) { |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 984 | VisType = nullptr; |
Eli Friedman | 570024a | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 985 | } else if (PushPop && PushPop->isStr("push")) { |
Joerg Sonnenberger | 869f0b7 | 2011-07-20 01:03:50 +0000 | [diff] [blame] | 986 | PP.LexUnexpandedToken(Tok); |
Eli Friedman | 570024a | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 987 | if (Tok.isNot(tok::l_paren)) { |
| 988 | PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_lparen) |
| 989 | << "visibility"; |
| 990 | return; |
| 991 | } |
Joerg Sonnenberger | 869f0b7 | 2011-07-20 01:03:50 +0000 | [diff] [blame] | 992 | PP.LexUnexpandedToken(Tok); |
Eli Friedman | 570024a | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 993 | VisType = Tok.getIdentifierInfo(); |
| 994 | if (!VisType) { |
| 995 | PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier) |
| 996 | << "visibility"; |
| 997 | return; |
| 998 | } |
Joerg Sonnenberger | 869f0b7 | 2011-07-20 01:03:50 +0000 | [diff] [blame] | 999 | PP.LexUnexpandedToken(Tok); |
Eli Friedman | 570024a | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 1000 | if (Tok.isNot(tok::r_paren)) { |
| 1001 | PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_rparen) |
| 1002 | << "visibility"; |
| 1003 | return; |
| 1004 | } |
| 1005 | } else { |
| 1006 | PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier) |
| 1007 | << "visibility"; |
| 1008 | return; |
| 1009 | } |
David Majnemer | a8f2f1d | 2015-03-19 00:10:23 +0000 | [diff] [blame] | 1010 | SourceLocation EndLoc = Tok.getLocation(); |
Joerg Sonnenberger | 869f0b7 | 2011-07-20 01:03:50 +0000 | [diff] [blame] | 1011 | PP.LexUnexpandedToken(Tok); |
Peter Collingbourne | 2f1e36b | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 1012 | if (Tok.isNot(tok::eod)) { |
Eli Friedman | 570024a | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 1013 | PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) |
| 1014 | << "visibility"; |
| 1015 | return; |
| 1016 | } |
| 1017 | |
David Blaikie | 2eabcc9 | 2016-02-09 18:52:09 +0000 | [diff] [blame] | 1018 | auto Toks = llvm::make_unique<Token[]>(1); |
Rafael Espindola | 273fd77 | 2012-01-26 02:02:57 +0000 | [diff] [blame] | 1019 | Toks[0].startToken(); |
| 1020 | Toks[0].setKind(tok::annot_pragma_vis); |
| 1021 | Toks[0].setLocation(VisLoc); |
David Majnemer | a8f2f1d | 2015-03-19 00:10:23 +0000 | [diff] [blame] | 1022 | Toks[0].setAnnotationEndLoc(EndLoc); |
Rafael Espindola | 273fd77 | 2012-01-26 02:02:57 +0000 | [diff] [blame] | 1023 | Toks[0].setAnnotationValue( |
| 1024 | const_cast<void*>(static_cast<const void*>(VisType))); |
David Blaikie | 2eabcc9 | 2016-02-09 18:52:09 +0000 | [diff] [blame] | 1025 | PP.EnterTokenStream(std::move(Toks), 1, /*DisableMacroExpansion=*/true); |
Eli Friedman | 570024a | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 1026 | } |
| 1027 | |
Daniel Dunbar | 921b968 | 2008-10-04 19:21:03 +0000 | [diff] [blame] | 1028 | // #pragma pack(...) comes in the following delicious flavors: |
| 1029 | // pack '(' [integer] ')' |
| 1030 | // pack '(' 'show' ')' |
| 1031 | // pack '(' ('push' | 'pop') [',' identifier] [, integer] ')' |
Douglas Gregor | c7d6576 | 2010-09-09 22:45:38 +0000 | [diff] [blame] | 1032 | void PragmaPackHandler::HandlePragma(Preprocessor &PP, |
| 1033 | PragmaIntroducerKind Introducer, |
| 1034 | Token &PackTok) { |
Daniel Dunbar | 921b968 | 2008-10-04 19:21:03 +0000 | [diff] [blame] | 1035 | SourceLocation PackLoc = PackTok.getLocation(); |
| 1036 | |
| 1037 | Token Tok; |
| 1038 | PP.Lex(Tok); |
| 1039 | if (Tok.isNot(tok::l_paren)) { |
Ted Kremenek | fd14fad | 2009-03-23 22:28:25 +0000 | [diff] [blame] | 1040 | PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_lparen) << "pack"; |
Daniel Dunbar | 921b968 | 2008-10-04 19:21:03 +0000 | [diff] [blame] | 1041 | return; |
| 1042 | } |
| 1043 | |
Denis Zobnin | 10c4f45 | 2016-04-29 18:17:40 +0000 | [diff] [blame] | 1044 | Sema::PragmaMsStackAction Action = Sema::PSK_Reset; |
| 1045 | StringRef SlotLabel; |
Eli Friedman | 68be164 | 2012-10-04 02:36:51 +0000 | [diff] [blame] | 1046 | Token Alignment; |
| 1047 | Alignment.startToken(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1048 | PP.Lex(Tok); |
Daniel Dunbar | 921b968 | 2008-10-04 19:21:03 +0000 | [diff] [blame] | 1049 | if (Tok.is(tok::numeric_constant)) { |
Eli Friedman | 68be164 | 2012-10-04 02:36:51 +0000 | [diff] [blame] | 1050 | Alignment = Tok; |
Daniel Dunbar | 921b968 | 2008-10-04 19:21:03 +0000 | [diff] [blame] | 1051 | |
| 1052 | PP.Lex(Tok); |
Eli Friedman | 055c970 | 2011-11-02 01:53:16 +0000 | [diff] [blame] | 1053 | |
| 1054 | // In MSVC/gcc, #pragma pack(4) sets the alignment without affecting |
| 1055 | // the push/pop stack. |
| 1056 | // 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] | 1057 | Action = |
| 1058 | PP.getLangOpts().ApplePragmaPack ? Sema::PSK_Push_Set : Sema::PSK_Set; |
Daniel Dunbar | 921b968 | 2008-10-04 19:21:03 +0000 | [diff] [blame] | 1059 | } else if (Tok.is(tok::identifier)) { |
| 1060 | const IdentifierInfo *II = Tok.getIdentifierInfo(); |
Chris Lattner | e3d20d9 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 1061 | if (II->isStr("show")) { |
Denis Zobnin | 10c4f45 | 2016-04-29 18:17:40 +0000 | [diff] [blame] | 1062 | Action = Sema::PSK_Show; |
Daniel Dunbar | 921b968 | 2008-10-04 19:21:03 +0000 | [diff] [blame] | 1063 | PP.Lex(Tok); |
| 1064 | } else { |
Chris Lattner | e3d20d9 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 1065 | if (II->isStr("push")) { |
Denis Zobnin | 10c4f45 | 2016-04-29 18:17:40 +0000 | [diff] [blame] | 1066 | Action = Sema::PSK_Push; |
Chris Lattner | e3d20d9 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 1067 | } else if (II->isStr("pop")) { |
Denis Zobnin | 10c4f45 | 2016-04-29 18:17:40 +0000 | [diff] [blame] | 1068 | Action = Sema::PSK_Pop; |
Daniel Dunbar | 921b968 | 2008-10-04 19:21:03 +0000 | [diff] [blame] | 1069 | } else { |
Reid Kleckner | c0dca6d | 2014-02-12 23:50:26 +0000 | [diff] [blame] | 1070 | PP.Diag(Tok.getLocation(), diag::warn_pragma_invalid_action) << "pack"; |
Daniel Dunbar | 921b968 | 2008-10-04 19:21:03 +0000 | [diff] [blame] | 1071 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1072 | } |
Daniel Dunbar | 921b968 | 2008-10-04 19:21:03 +0000 | [diff] [blame] | 1073 | PP.Lex(Tok); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1074 | |
Daniel Dunbar | 921b968 | 2008-10-04 19:21:03 +0000 | [diff] [blame] | 1075 | if (Tok.is(tok::comma)) { |
| 1076 | PP.Lex(Tok); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1077 | |
Daniel Dunbar | 921b968 | 2008-10-04 19:21:03 +0000 | [diff] [blame] | 1078 | if (Tok.is(tok::numeric_constant)) { |
Denis Zobnin | 10c4f45 | 2016-04-29 18:17:40 +0000 | [diff] [blame] | 1079 | Action = (Sema::PragmaMsStackAction)(Action | Sema::PSK_Set); |
Eli Friedman | 68be164 | 2012-10-04 02:36:51 +0000 | [diff] [blame] | 1080 | Alignment = Tok; |
Daniel Dunbar | 921b968 | 2008-10-04 19:21:03 +0000 | [diff] [blame] | 1081 | |
| 1082 | PP.Lex(Tok); |
| 1083 | } else if (Tok.is(tok::identifier)) { |
Denis Zobnin | 10c4f45 | 2016-04-29 18:17:40 +0000 | [diff] [blame] | 1084 | SlotLabel = Tok.getIdentifierInfo()->getName(); |
Daniel Dunbar | 921b968 | 2008-10-04 19:21:03 +0000 | [diff] [blame] | 1085 | PP.Lex(Tok); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1086 | |
Daniel Dunbar | 921b968 | 2008-10-04 19:21:03 +0000 | [diff] [blame] | 1087 | if (Tok.is(tok::comma)) { |
| 1088 | PP.Lex(Tok); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1089 | |
Daniel Dunbar | 921b968 | 2008-10-04 19:21:03 +0000 | [diff] [blame] | 1090 | if (Tok.isNot(tok::numeric_constant)) { |
Chris Lattner | e3d20d9 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 1091 | PP.Diag(Tok.getLocation(), diag::warn_pragma_pack_malformed); |
Daniel Dunbar | 921b968 | 2008-10-04 19:21:03 +0000 | [diff] [blame] | 1092 | return; |
| 1093 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1094 | |
Denis Zobnin | 10c4f45 | 2016-04-29 18:17:40 +0000 | [diff] [blame] | 1095 | Action = (Sema::PragmaMsStackAction)(Action | Sema::PSK_Set); |
Eli Friedman | 68be164 | 2012-10-04 02:36:51 +0000 | [diff] [blame] | 1096 | Alignment = Tok; |
Daniel Dunbar | 921b968 | 2008-10-04 19:21:03 +0000 | [diff] [blame] | 1097 | |
| 1098 | PP.Lex(Tok); |
| 1099 | } |
| 1100 | } else { |
Chris Lattner | e3d20d9 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 1101 | PP.Diag(Tok.getLocation(), diag::warn_pragma_pack_malformed); |
Daniel Dunbar | 921b968 | 2008-10-04 19:21:03 +0000 | [diff] [blame] | 1102 | return; |
| 1103 | } |
| 1104 | } |
| 1105 | } |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1106 | } else if (PP.getLangOpts().ApplePragmaPack) { |
Eli Friedman | 055c970 | 2011-11-02 01:53:16 +0000 | [diff] [blame] | 1107 | // In MSVC/gcc, #pragma pack() resets the alignment without affecting |
| 1108 | // the push/pop stack. |
| 1109 | // In Apple gcc #pragma pack() is equivalent to #pragma pack(pop). |
Denis Zobnin | 10c4f45 | 2016-04-29 18:17:40 +0000 | [diff] [blame] | 1110 | Action = Sema::PSK_Pop; |
Sebastian Redl | 17f2c7d | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1111 | } |
Daniel Dunbar | 921b968 | 2008-10-04 19:21:03 +0000 | [diff] [blame] | 1112 | |
| 1113 | if (Tok.isNot(tok::r_paren)) { |
Ted Kremenek | fd14fad | 2009-03-23 22:28:25 +0000 | [diff] [blame] | 1114 | PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_rparen) << "pack"; |
Daniel Dunbar | 921b968 | 2008-10-04 19:21:03 +0000 | [diff] [blame] | 1115 | return; |
| 1116 | } |
| 1117 | |
Daniel Dunbar | 75c9be7 | 2010-05-26 23:29:06 +0000 | [diff] [blame] | 1118 | SourceLocation RParenLoc = Tok.getLocation(); |
Eli Friedman | f5867dd | 2009-06-05 00:49:58 +0000 | [diff] [blame] | 1119 | PP.Lex(Tok); |
Peter Collingbourne | 2f1e36b | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 1120 | if (Tok.isNot(tok::eod)) { |
Eli Friedman | f5867dd | 2009-06-05 00:49:58 +0000 | [diff] [blame] | 1121 | PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) << "pack"; |
| 1122 | return; |
| 1123 | } |
| 1124 | |
David Blaikie | 2eabcc9 | 2016-02-09 18:52:09 +0000 | [diff] [blame] | 1125 | PragmaPackInfo *Info = |
| 1126 | PP.getPreprocessorAllocator().Allocate<PragmaPackInfo>(1); |
Denis Zobnin | 10c4f45 | 2016-04-29 18:17:40 +0000 | [diff] [blame] | 1127 | Info->Action = Action; |
| 1128 | Info->SlotLabel = SlotLabel; |
Eli Friedman | 68be164 | 2012-10-04 02:36:51 +0000 | [diff] [blame] | 1129 | Info->Alignment = Alignment; |
Eli Friedman | ec52f92 | 2012-02-23 23:47:16 +0000 | [diff] [blame] | 1130 | |
David Blaikie | 2eabcc9 | 2016-02-09 18:52:09 +0000 | [diff] [blame] | 1131 | MutableArrayRef<Token> Toks(PP.getPreprocessorAllocator().Allocate<Token>(1), |
| 1132 | 1); |
Eli Friedman | ec52f92 | 2012-02-23 23:47:16 +0000 | [diff] [blame] | 1133 | Toks[0].startToken(); |
| 1134 | Toks[0].setKind(tok::annot_pragma_pack); |
| 1135 | Toks[0].setLocation(PackLoc); |
David Majnemer | a8f2f1d | 2015-03-19 00:10:23 +0000 | [diff] [blame] | 1136 | Toks[0].setAnnotationEndLoc(RParenLoc); |
Eli Friedman | ec52f92 | 2012-02-23 23:47:16 +0000 | [diff] [blame] | 1137 | Toks[0].setAnnotationValue(static_cast<void*>(Info)); |
David Blaikie | 2eabcc9 | 2016-02-09 18:52:09 +0000 | [diff] [blame] | 1138 | PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true); |
Daniel Dunbar | 921b968 | 2008-10-04 19:21:03 +0000 | [diff] [blame] | 1139 | } |
| 1140 | |
Fariborz Jahanian | 743dda4 | 2011-04-25 18:49:15 +0000 | [diff] [blame] | 1141 | // #pragma ms_struct on |
| 1142 | // #pragma ms_struct off |
| 1143 | void PragmaMSStructHandler::HandlePragma(Preprocessor &PP, |
| 1144 | PragmaIntroducerKind Introducer, |
| 1145 | Token &MSStructTok) { |
Nico Weber | 779355f | 2016-03-02 23:22:00 +0000 | [diff] [blame] | 1146 | PragmaMSStructKind Kind = PMSST_OFF; |
| 1147 | |
Fariborz Jahanian | 743dda4 | 2011-04-25 18:49:15 +0000 | [diff] [blame] | 1148 | Token Tok; |
| 1149 | PP.Lex(Tok); |
| 1150 | if (Tok.isNot(tok::identifier)) { |
| 1151 | PP.Diag(Tok.getLocation(), diag::warn_pragma_ms_struct); |
| 1152 | return; |
| 1153 | } |
David Majnemer | a8f2f1d | 2015-03-19 00:10:23 +0000 | [diff] [blame] | 1154 | SourceLocation EndLoc = Tok.getLocation(); |
Fariborz Jahanian | 743dda4 | 2011-04-25 18:49:15 +0000 | [diff] [blame] | 1155 | const IdentifierInfo *II = Tok.getIdentifierInfo(); |
| 1156 | if (II->isStr("on")) { |
Nico Weber | 779355f | 2016-03-02 23:22:00 +0000 | [diff] [blame] | 1157 | Kind = PMSST_ON; |
Fariborz Jahanian | 743dda4 | 2011-04-25 18:49:15 +0000 | [diff] [blame] | 1158 | PP.Lex(Tok); |
| 1159 | } |
| 1160 | else if (II->isStr("off") || II->isStr("reset")) |
| 1161 | PP.Lex(Tok); |
| 1162 | else { |
| 1163 | PP.Diag(Tok.getLocation(), diag::warn_pragma_ms_struct); |
| 1164 | return; |
| 1165 | } |
| 1166 | |
| 1167 | if (Tok.isNot(tok::eod)) { |
Daniel Dunbar | 340cf24 | 2012-02-29 01:38:22 +0000 | [diff] [blame] | 1168 | PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) |
| 1169 | << "ms_struct"; |
Fariborz Jahanian | 743dda4 | 2011-04-25 18:49:15 +0000 | [diff] [blame] | 1170 | return; |
| 1171 | } |
Eli Friedman | 68be164 | 2012-10-04 02:36:51 +0000 | [diff] [blame] | 1172 | |
David Blaikie | 2eabcc9 | 2016-02-09 18:52:09 +0000 | [diff] [blame] | 1173 | MutableArrayRef<Token> Toks(PP.getPreprocessorAllocator().Allocate<Token>(1), |
| 1174 | 1); |
Eli Friedman | 68be164 | 2012-10-04 02:36:51 +0000 | [diff] [blame] | 1175 | Toks[0].startToken(); |
| 1176 | Toks[0].setKind(tok::annot_pragma_msstruct); |
| 1177 | Toks[0].setLocation(MSStructTok.getLocation()); |
David Majnemer | a8f2f1d | 2015-03-19 00:10:23 +0000 | [diff] [blame] | 1178 | Toks[0].setAnnotationEndLoc(EndLoc); |
Eli Friedman | 68be164 | 2012-10-04 02:36:51 +0000 | [diff] [blame] | 1179 | Toks[0].setAnnotationValue(reinterpret_cast<void*>( |
| 1180 | static_cast<uintptr_t>(Kind))); |
David Blaikie | 2eabcc9 | 2016-02-09 18:52:09 +0000 | [diff] [blame] | 1181 | PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true); |
Fariborz Jahanian | 743dda4 | 2011-04-25 18:49:15 +0000 | [diff] [blame] | 1182 | } |
| 1183 | |
Daniel Dunbar | cb82acb | 2010-07-31 19:17:07 +0000 | [diff] [blame] | 1184 | // #pragma 'align' '=' {'native','natural','mac68k','power','reset'} |
| 1185 | // #pragma 'options 'align' '=' {'native','natural','mac68k','power','reset'} |
Eli Friedman | 68be164 | 2012-10-04 02:36:51 +0000 | [diff] [blame] | 1186 | static void ParseAlignPragma(Preprocessor &PP, Token &FirstTok, |
Daniel Dunbar | cb82acb | 2010-07-31 19:17:07 +0000 | [diff] [blame] | 1187 | bool IsOptions) { |
Daniel Dunbar | 75c9be7 | 2010-05-26 23:29:06 +0000 | [diff] [blame] | 1188 | Token Tok; |
Daniel Dunbar | cb82acb | 2010-07-31 19:17:07 +0000 | [diff] [blame] | 1189 | |
| 1190 | if (IsOptions) { |
| 1191 | PP.Lex(Tok); |
| 1192 | if (Tok.isNot(tok::identifier) || |
| 1193 | !Tok.getIdentifierInfo()->isStr("align")) { |
| 1194 | PP.Diag(Tok.getLocation(), diag::warn_pragma_options_expected_align); |
| 1195 | return; |
| 1196 | } |
Daniel Dunbar | 75c9be7 | 2010-05-26 23:29:06 +0000 | [diff] [blame] | 1197 | } |
Daniel Dunbar | 663e809 | 2010-05-27 18:42:09 +0000 | [diff] [blame] | 1198 | |
Daniel Dunbar | 75c9be7 | 2010-05-26 23:29:06 +0000 | [diff] [blame] | 1199 | PP.Lex(Tok); |
| 1200 | if (Tok.isNot(tok::equal)) { |
Daniel Dunbar | cb82acb | 2010-07-31 19:17:07 +0000 | [diff] [blame] | 1201 | PP.Diag(Tok.getLocation(), diag::warn_pragma_align_expected_equal) |
| 1202 | << IsOptions; |
Daniel Dunbar | 75c9be7 | 2010-05-26 23:29:06 +0000 | [diff] [blame] | 1203 | return; |
| 1204 | } |
| 1205 | |
| 1206 | PP.Lex(Tok); |
| 1207 | if (Tok.isNot(tok::identifier)) { |
| 1208 | PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier) |
Daniel Dunbar | cb82acb | 2010-07-31 19:17:07 +0000 | [diff] [blame] | 1209 | << (IsOptions ? "options" : "align"); |
Daniel Dunbar | 75c9be7 | 2010-05-26 23:29:06 +0000 | [diff] [blame] | 1210 | return; |
| 1211 | } |
| 1212 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1213 | Sema::PragmaOptionsAlignKind Kind = Sema::POAK_Natural; |
Daniel Dunbar | 75c9be7 | 2010-05-26 23:29:06 +0000 | [diff] [blame] | 1214 | const IdentifierInfo *II = Tok.getIdentifierInfo(); |
Daniel Dunbar | 663e809 | 2010-05-27 18:42:09 +0000 | [diff] [blame] | 1215 | if (II->isStr("native")) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1216 | Kind = Sema::POAK_Native; |
Daniel Dunbar | 663e809 | 2010-05-27 18:42:09 +0000 | [diff] [blame] | 1217 | else if (II->isStr("natural")) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1218 | Kind = Sema::POAK_Natural; |
Daniel Dunbar | 9c84d4a | 2010-05-27 18:42:17 +0000 | [diff] [blame] | 1219 | else if (II->isStr("packed")) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1220 | Kind = Sema::POAK_Packed; |
Daniel Dunbar | 75c9be7 | 2010-05-26 23:29:06 +0000 | [diff] [blame] | 1221 | else if (II->isStr("power")) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1222 | Kind = Sema::POAK_Power; |
Daniel Dunbar | 75c9be7 | 2010-05-26 23:29:06 +0000 | [diff] [blame] | 1223 | else if (II->isStr("mac68k")) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1224 | Kind = Sema::POAK_Mac68k; |
Daniel Dunbar | 75c9be7 | 2010-05-26 23:29:06 +0000 | [diff] [blame] | 1225 | else if (II->isStr("reset")) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1226 | Kind = Sema::POAK_Reset; |
Daniel Dunbar | 75c9be7 | 2010-05-26 23:29:06 +0000 | [diff] [blame] | 1227 | else { |
Daniel Dunbar | cb82acb | 2010-07-31 19:17:07 +0000 | [diff] [blame] | 1228 | PP.Diag(Tok.getLocation(), diag::warn_pragma_align_invalid_option) |
| 1229 | << IsOptions; |
Daniel Dunbar | 75c9be7 | 2010-05-26 23:29:06 +0000 | [diff] [blame] | 1230 | return; |
| 1231 | } |
| 1232 | |
David Majnemer | a8f2f1d | 2015-03-19 00:10:23 +0000 | [diff] [blame] | 1233 | SourceLocation EndLoc = Tok.getLocation(); |
Daniel Dunbar | 75c9be7 | 2010-05-26 23:29:06 +0000 | [diff] [blame] | 1234 | PP.Lex(Tok); |
Peter Collingbourne | 2f1e36b | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 1235 | if (Tok.isNot(tok::eod)) { |
Daniel Dunbar | 75c9be7 | 2010-05-26 23:29:06 +0000 | [diff] [blame] | 1236 | PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) |
Daniel Dunbar | cb82acb | 2010-07-31 19:17:07 +0000 | [diff] [blame] | 1237 | << (IsOptions ? "options" : "align"); |
Daniel Dunbar | 75c9be7 | 2010-05-26 23:29:06 +0000 | [diff] [blame] | 1238 | return; |
| 1239 | } |
| 1240 | |
David Blaikie | 2eabcc9 | 2016-02-09 18:52:09 +0000 | [diff] [blame] | 1241 | MutableArrayRef<Token> Toks(PP.getPreprocessorAllocator().Allocate<Token>(1), |
| 1242 | 1); |
Eli Friedman | 68be164 | 2012-10-04 02:36:51 +0000 | [diff] [blame] | 1243 | Toks[0].startToken(); |
| 1244 | Toks[0].setKind(tok::annot_pragma_align); |
| 1245 | Toks[0].setLocation(FirstTok.getLocation()); |
David Majnemer | a8f2f1d | 2015-03-19 00:10:23 +0000 | [diff] [blame] | 1246 | Toks[0].setAnnotationEndLoc(EndLoc); |
Eli Friedman | 68be164 | 2012-10-04 02:36:51 +0000 | [diff] [blame] | 1247 | Toks[0].setAnnotationValue(reinterpret_cast<void*>( |
| 1248 | static_cast<uintptr_t>(Kind))); |
David Blaikie | 2eabcc9 | 2016-02-09 18:52:09 +0000 | [diff] [blame] | 1249 | PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true); |
Daniel Dunbar | cb82acb | 2010-07-31 19:17:07 +0000 | [diff] [blame] | 1250 | } |
| 1251 | |
Douglas Gregor | c7d6576 | 2010-09-09 22:45:38 +0000 | [diff] [blame] | 1252 | void PragmaAlignHandler::HandlePragma(Preprocessor &PP, |
| 1253 | PragmaIntroducerKind Introducer, |
| 1254 | Token &AlignTok) { |
Eli Friedman | 68be164 | 2012-10-04 02:36:51 +0000 | [diff] [blame] | 1255 | ParseAlignPragma(PP, AlignTok, /*IsOptions=*/false); |
Daniel Dunbar | cb82acb | 2010-07-31 19:17:07 +0000 | [diff] [blame] | 1256 | } |
| 1257 | |
Douglas Gregor | c7d6576 | 2010-09-09 22:45:38 +0000 | [diff] [blame] | 1258 | void PragmaOptionsHandler::HandlePragma(Preprocessor &PP, |
| 1259 | PragmaIntroducerKind Introducer, |
| 1260 | Token &OptionsTok) { |
Eli Friedman | 68be164 | 2012-10-04 02:36:51 +0000 | [diff] [blame] | 1261 | ParseAlignPragma(PP, OptionsTok, /*IsOptions=*/true); |
Daniel Dunbar | 75c9be7 | 2010-05-26 23:29:06 +0000 | [diff] [blame] | 1262 | } |
| 1263 | |
Ted Kremenek | fd14fad | 2009-03-23 22:28:25 +0000 | [diff] [blame] | 1264 | // #pragma unused(identifier) |
Douglas Gregor | c7d6576 | 2010-09-09 22:45:38 +0000 | [diff] [blame] | 1265 | void PragmaUnusedHandler::HandlePragma(Preprocessor &PP, |
| 1266 | PragmaIntroducerKind Introducer, |
| 1267 | Token &UnusedTok) { |
Ted Kremenek | fd14fad | 2009-03-23 22:28:25 +0000 | [diff] [blame] | 1268 | // FIXME: Should we be expanding macros here? My guess is no. |
| 1269 | SourceLocation UnusedLoc = UnusedTok.getLocation(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1270 | |
Ted Kremenek | fd14fad | 2009-03-23 22:28:25 +0000 | [diff] [blame] | 1271 | // Lex the left '('. |
| 1272 | Token Tok; |
| 1273 | PP.Lex(Tok); |
| 1274 | if (Tok.isNot(tok::l_paren)) { |
| 1275 | PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_lparen) << "unused"; |
| 1276 | return; |
| 1277 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1278 | |
Ted Kremenek | fd14fad | 2009-03-23 22:28:25 +0000 | [diff] [blame] | 1279 | // Lex the declaration reference(s). |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1280 | SmallVector<Token, 5> Identifiers; |
Ted Kremenek | fd14fad | 2009-03-23 22:28:25 +0000 | [diff] [blame] | 1281 | SourceLocation RParenLoc; |
| 1282 | bool LexID = true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1283 | |
Ted Kremenek | fd14fad | 2009-03-23 22:28:25 +0000 | [diff] [blame] | 1284 | while (true) { |
| 1285 | PP.Lex(Tok); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1286 | |
Ted Kremenek | fd14fad | 2009-03-23 22:28:25 +0000 | [diff] [blame] | 1287 | if (LexID) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1288 | if (Tok.is(tok::identifier)) { |
Ted Kremenek | fb50bf5 | 2009-08-03 23:24:57 +0000 | [diff] [blame] | 1289 | Identifiers.push_back(Tok); |
Ted Kremenek | fd14fad | 2009-03-23 22:28:25 +0000 | [diff] [blame] | 1290 | LexID = false; |
| 1291 | continue; |
| 1292 | } |
| 1293 | |
Ted Kremenek | fb50bf5 | 2009-08-03 23:24:57 +0000 | [diff] [blame] | 1294 | // Illegal token! |
Ted Kremenek | fd14fad | 2009-03-23 22:28:25 +0000 | [diff] [blame] | 1295 | PP.Diag(Tok.getLocation(), diag::warn_pragma_unused_expected_var); |
| 1296 | return; |
| 1297 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1298 | |
Ted Kremenek | fd14fad | 2009-03-23 22:28:25 +0000 | [diff] [blame] | 1299 | // We are execting a ')' or a ','. |
| 1300 | if (Tok.is(tok::comma)) { |
| 1301 | LexID = true; |
| 1302 | continue; |
| 1303 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1304 | |
Ted Kremenek | fd14fad | 2009-03-23 22:28:25 +0000 | [diff] [blame] | 1305 | if (Tok.is(tok::r_paren)) { |
| 1306 | RParenLoc = Tok.getLocation(); |
| 1307 | break; |
| 1308 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1309 | |
Ted Kremenek | fb50bf5 | 2009-08-03 23:24:57 +0000 | [diff] [blame] | 1310 | // Illegal token! |
David Majnemer | 8896981 | 2014-02-10 19:06:37 +0000 | [diff] [blame] | 1311 | PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_punc) << "unused"; |
Ted Kremenek | fd14fad | 2009-03-23 22:28:25 +0000 | [diff] [blame] | 1312 | return; |
| 1313 | } |
Eli Friedman | f5867dd | 2009-06-05 00:49:58 +0000 | [diff] [blame] | 1314 | |
| 1315 | PP.Lex(Tok); |
Peter Collingbourne | 2f1e36b | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 1316 | if (Tok.isNot(tok::eod)) { |
Eli Friedman | f5867dd | 2009-06-05 00:49:58 +0000 | [diff] [blame] | 1317 | PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) << |
| 1318 | "unused"; |
| 1319 | return; |
| 1320 | } |
| 1321 | |
Ted Kremenek | fd14fad | 2009-03-23 22:28:25 +0000 | [diff] [blame] | 1322 | // Verify that we have a location for the right parenthesis. |
| 1323 | assert(RParenLoc.isValid() && "Valid '#pragma unused' must have ')'"); |
Ted Kremenek | fb50bf5 | 2009-08-03 23:24:57 +0000 | [diff] [blame] | 1324 | assert(!Identifiers.empty() && "Valid '#pragma unused' must have arguments"); |
Ted Kremenek | fd14fad | 2009-03-23 22:28:25 +0000 | [diff] [blame] | 1325 | |
Argyrios Kyrtzidis | ee56962 | 2011-01-17 18:58:44 +0000 | [diff] [blame] | 1326 | // For each identifier token, insert into the token stream a |
| 1327 | // annot_pragma_unused token followed by the identifier token. |
| 1328 | // This allows us to cache a "#pragma unused" that occurs inside an inline |
| 1329 | // C++ member function. |
| 1330 | |
David Blaikie | 2eabcc9 | 2016-02-09 18:52:09 +0000 | [diff] [blame] | 1331 | MutableArrayRef<Token> Toks( |
| 1332 | PP.getPreprocessorAllocator().Allocate<Token>(2 * Identifiers.size()), |
| 1333 | 2 * Identifiers.size()); |
Argyrios Kyrtzidis | ee56962 | 2011-01-17 18:58:44 +0000 | [diff] [blame] | 1334 | for (unsigned i=0; i != Identifiers.size(); i++) { |
| 1335 | Token &pragmaUnusedTok = Toks[2*i], &idTok = Toks[2*i+1]; |
| 1336 | pragmaUnusedTok.startToken(); |
| 1337 | pragmaUnusedTok.setKind(tok::annot_pragma_unused); |
| 1338 | pragmaUnusedTok.setLocation(UnusedLoc); |
| 1339 | idTok = Identifiers[i]; |
| 1340 | } |
David Blaikie | 2eabcc9 | 2016-02-09 18:52:09 +0000 | [diff] [blame] | 1341 | PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true); |
Ted Kremenek | fd14fad | 2009-03-23 22:28:25 +0000 | [diff] [blame] | 1342 | } |
Eli Friedman | f5867dd | 2009-06-05 00:49:58 +0000 | [diff] [blame] | 1343 | |
| 1344 | // #pragma weak identifier |
| 1345 | // #pragma weak identifier '=' identifier |
Douglas Gregor | c7d6576 | 2010-09-09 22:45:38 +0000 | [diff] [blame] | 1346 | void PragmaWeakHandler::HandlePragma(Preprocessor &PP, |
| 1347 | PragmaIntroducerKind Introducer, |
| 1348 | Token &WeakTok) { |
Eli Friedman | f5867dd | 2009-06-05 00:49:58 +0000 | [diff] [blame] | 1349 | SourceLocation WeakLoc = WeakTok.getLocation(); |
| 1350 | |
| 1351 | Token Tok; |
| 1352 | PP.Lex(Tok); |
| 1353 | if (Tok.isNot(tok::identifier)) { |
| 1354 | PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier) << "weak"; |
| 1355 | return; |
| 1356 | } |
| 1357 | |
Eli Friedman | 68be164 | 2012-10-04 02:36:51 +0000 | [diff] [blame] | 1358 | Token WeakName = Tok; |
| 1359 | bool HasAlias = false; |
| 1360 | Token AliasName; |
Eli Friedman | f5867dd | 2009-06-05 00:49:58 +0000 | [diff] [blame] | 1361 | |
| 1362 | PP.Lex(Tok); |
| 1363 | if (Tok.is(tok::equal)) { |
Eli Friedman | 68be164 | 2012-10-04 02:36:51 +0000 | [diff] [blame] | 1364 | HasAlias = true; |
Eli Friedman | f5867dd | 2009-06-05 00:49:58 +0000 | [diff] [blame] | 1365 | PP.Lex(Tok); |
| 1366 | if (Tok.isNot(tok::identifier)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1367 | PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier) |
Eli Friedman | f5867dd | 2009-06-05 00:49:58 +0000 | [diff] [blame] | 1368 | << "weak"; |
| 1369 | return; |
| 1370 | } |
Eli Friedman | 68be164 | 2012-10-04 02:36:51 +0000 | [diff] [blame] | 1371 | AliasName = Tok; |
Eli Friedman | f5867dd | 2009-06-05 00:49:58 +0000 | [diff] [blame] | 1372 | PP.Lex(Tok); |
| 1373 | } |
| 1374 | |
Peter Collingbourne | 2f1e36b | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 1375 | if (Tok.isNot(tok::eod)) { |
Eli Friedman | f5867dd | 2009-06-05 00:49:58 +0000 | [diff] [blame] | 1376 | PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) << "weak"; |
| 1377 | return; |
| 1378 | } |
| 1379 | |
Eli Friedman | 68be164 | 2012-10-04 02:36:51 +0000 | [diff] [blame] | 1380 | if (HasAlias) { |
David Blaikie | 2eabcc9 | 2016-02-09 18:52:09 +0000 | [diff] [blame] | 1381 | MutableArrayRef<Token> Toks( |
| 1382 | PP.getPreprocessorAllocator().Allocate<Token>(3), 3); |
Eli Friedman | 68be164 | 2012-10-04 02:36:51 +0000 | [diff] [blame] | 1383 | Token &pragmaUnusedTok = Toks[0]; |
| 1384 | pragmaUnusedTok.startToken(); |
| 1385 | pragmaUnusedTok.setKind(tok::annot_pragma_weakalias); |
| 1386 | pragmaUnusedTok.setLocation(WeakLoc); |
David Majnemer | a8f2f1d | 2015-03-19 00:10:23 +0000 | [diff] [blame] | 1387 | pragmaUnusedTok.setAnnotationEndLoc(AliasName.getLocation()); |
Eli Friedman | 68be164 | 2012-10-04 02:36:51 +0000 | [diff] [blame] | 1388 | Toks[1] = WeakName; |
| 1389 | Toks[2] = AliasName; |
David Blaikie | 2eabcc9 | 2016-02-09 18:52:09 +0000 | [diff] [blame] | 1390 | PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true); |
Eli Friedman | f5867dd | 2009-06-05 00:49:58 +0000 | [diff] [blame] | 1391 | } else { |
David Blaikie | 2eabcc9 | 2016-02-09 18:52:09 +0000 | [diff] [blame] | 1392 | MutableArrayRef<Token> Toks( |
| 1393 | PP.getPreprocessorAllocator().Allocate<Token>(2), 2); |
Eli Friedman | 68be164 | 2012-10-04 02:36:51 +0000 | [diff] [blame] | 1394 | Token &pragmaUnusedTok = Toks[0]; |
| 1395 | pragmaUnusedTok.startToken(); |
| 1396 | pragmaUnusedTok.setKind(tok::annot_pragma_weak); |
| 1397 | pragmaUnusedTok.setLocation(WeakLoc); |
David Majnemer | a8f2f1d | 2015-03-19 00:10:23 +0000 | [diff] [blame] | 1398 | pragmaUnusedTok.setAnnotationEndLoc(WeakLoc); |
Eli Friedman | 68be164 | 2012-10-04 02:36:51 +0000 | [diff] [blame] | 1399 | Toks[1] = WeakName; |
David Blaikie | 2eabcc9 | 2016-02-09 18:52:09 +0000 | [diff] [blame] | 1400 | PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true); |
Eli Friedman | f5867dd | 2009-06-05 00:49:58 +0000 | [diff] [blame] | 1401 | } |
| 1402 | } |
Peter Collingbourne | 564c0fa | 2011-02-14 01:42:35 +0000 | [diff] [blame] | 1403 | |
David Chisnall | 0867d9c | 2012-02-18 16:12:34 +0000 | [diff] [blame] | 1404 | // #pragma redefine_extname identifier identifier |
| 1405 | void PragmaRedefineExtnameHandler::HandlePragma(Preprocessor &PP, |
| 1406 | PragmaIntroducerKind Introducer, |
| 1407 | Token &RedefToken) { |
| 1408 | SourceLocation RedefLoc = RedefToken.getLocation(); |
| 1409 | |
| 1410 | Token Tok; |
| 1411 | PP.Lex(Tok); |
| 1412 | if (Tok.isNot(tok::identifier)) { |
| 1413 | PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier) << |
| 1414 | "redefine_extname"; |
| 1415 | return; |
| 1416 | } |
| 1417 | |
Eli Friedman | 68be164 | 2012-10-04 02:36:51 +0000 | [diff] [blame] | 1418 | Token RedefName = Tok; |
David Chisnall | 0867d9c | 2012-02-18 16:12:34 +0000 | [diff] [blame] | 1419 | PP.Lex(Tok); |
Eli Friedman | 68be164 | 2012-10-04 02:36:51 +0000 | [diff] [blame] | 1420 | |
David Chisnall | 0867d9c | 2012-02-18 16:12:34 +0000 | [diff] [blame] | 1421 | if (Tok.isNot(tok::identifier)) { |
| 1422 | PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier) |
| 1423 | << "redefine_extname"; |
| 1424 | return; |
| 1425 | } |
Eli Friedman | 68be164 | 2012-10-04 02:36:51 +0000 | [diff] [blame] | 1426 | |
| 1427 | Token AliasName = Tok; |
David Chisnall | 0867d9c | 2012-02-18 16:12:34 +0000 | [diff] [blame] | 1428 | PP.Lex(Tok); |
| 1429 | |
| 1430 | if (Tok.isNot(tok::eod)) { |
| 1431 | PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) << |
| 1432 | "redefine_extname"; |
| 1433 | return; |
| 1434 | } |
| 1435 | |
David Blaikie | 2eabcc9 | 2016-02-09 18:52:09 +0000 | [diff] [blame] | 1436 | MutableArrayRef<Token> Toks(PP.getPreprocessorAllocator().Allocate<Token>(3), |
| 1437 | 3); |
Eli Friedman | 68be164 | 2012-10-04 02:36:51 +0000 | [diff] [blame] | 1438 | Token &pragmaRedefTok = Toks[0]; |
| 1439 | pragmaRedefTok.startToken(); |
| 1440 | pragmaRedefTok.setKind(tok::annot_pragma_redefine_extname); |
| 1441 | pragmaRedefTok.setLocation(RedefLoc); |
David Majnemer | a8f2f1d | 2015-03-19 00:10:23 +0000 | [diff] [blame] | 1442 | pragmaRedefTok.setAnnotationEndLoc(AliasName.getLocation()); |
Eli Friedman | 68be164 | 2012-10-04 02:36:51 +0000 | [diff] [blame] | 1443 | Toks[1] = RedefName; |
| 1444 | Toks[2] = AliasName; |
David Blaikie | 2eabcc9 | 2016-02-09 18:52:09 +0000 | [diff] [blame] | 1445 | PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true); |
David Chisnall | 0867d9c | 2012-02-18 16:12:34 +0000 | [diff] [blame] | 1446 | } |
| 1447 | |
| 1448 | |
Peter Collingbourne | 564c0fa | 2011-02-14 01:42:35 +0000 | [diff] [blame] | 1449 | void |
| 1450 | PragmaFPContractHandler::HandlePragma(Preprocessor &PP, |
| 1451 | PragmaIntroducerKind Introducer, |
| 1452 | Token &Tok) { |
| 1453 | tok::OnOffSwitch OOS; |
| 1454 | if (PP.LexOnOffSwitch(OOS)) |
| 1455 | return; |
| 1456 | |
David Blaikie | 2eabcc9 | 2016-02-09 18:52:09 +0000 | [diff] [blame] | 1457 | MutableArrayRef<Token> Toks(PP.getPreprocessorAllocator().Allocate<Token>(1), |
| 1458 | 1); |
Eli Friedman | 68be164 | 2012-10-04 02:36:51 +0000 | [diff] [blame] | 1459 | Toks[0].startToken(); |
| 1460 | Toks[0].setKind(tok::annot_pragma_fp_contract); |
| 1461 | Toks[0].setLocation(Tok.getLocation()); |
David Majnemer | a8f2f1d | 2015-03-19 00:10:23 +0000 | [diff] [blame] | 1462 | Toks[0].setAnnotationEndLoc(Tok.getLocation()); |
Eli Friedman | 68be164 | 2012-10-04 02:36:51 +0000 | [diff] [blame] | 1463 | Toks[0].setAnnotationValue(reinterpret_cast<void*>( |
| 1464 | static_cast<uintptr_t>(OOS))); |
David Blaikie | 2eabcc9 | 2016-02-09 18:52:09 +0000 | [diff] [blame] | 1465 | PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true); |
Peter Collingbourne | 564c0fa | 2011-02-14 01:42:35 +0000 | [diff] [blame] | 1466 | } |
Peter Collingbourne | 7ce13fc | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 1467 | |
| 1468 | void |
| 1469 | PragmaOpenCLExtensionHandler::HandlePragma(Preprocessor &PP, |
| 1470 | PragmaIntroducerKind Introducer, |
| 1471 | Token &Tok) { |
Tanya Lattner | ee840b8 | 2011-04-14 23:35:31 +0000 | [diff] [blame] | 1472 | PP.LexUnexpandedToken(Tok); |
Peter Collingbourne | 7ce13fc | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 1473 | if (Tok.isNot(tok::identifier)) { |
| 1474 | PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier) << |
| 1475 | "OPENCL"; |
| 1476 | return; |
| 1477 | } |
Yaxun Liu | 5b74665 | 2016-12-18 05:18:55 +0000 | [diff] [blame] | 1478 | IdentifierInfo *Ext = Tok.getIdentifierInfo(); |
Peter Collingbourne | 7ce13fc | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 1479 | SourceLocation NameLoc = Tok.getLocation(); |
| 1480 | |
| 1481 | PP.Lex(Tok); |
| 1482 | if (Tok.isNot(tok::colon)) { |
Yaxun Liu | 5b74665 | 2016-12-18 05:18:55 +0000 | [diff] [blame] | 1483 | PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_colon) << Ext; |
Peter Collingbourne | 7ce13fc | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 1484 | return; |
| 1485 | } |
| 1486 | |
| 1487 | PP.Lex(Tok); |
| 1488 | if (Tok.isNot(tok::identifier)) { |
Yaxun Liu | 5b74665 | 2016-12-18 05:18:55 +0000 | [diff] [blame] | 1489 | PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_predicate) << 0; |
Peter Collingbourne | 7ce13fc | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 1490 | return; |
| 1491 | } |
Yaxun Liu | 5b74665 | 2016-12-18 05:18:55 +0000 | [diff] [blame] | 1492 | IdentifierInfo *Pred = Tok.getIdentifierInfo(); |
Peter Collingbourne | 7ce13fc | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 1493 | |
Yaxun Liu | 5b74665 | 2016-12-18 05:18:55 +0000 | [diff] [blame] | 1494 | OpenCLExtState State; |
| 1495 | if (Pred->isStr("enable")) { |
| 1496 | State = Enable; |
| 1497 | } else if (Pred->isStr("disable")) { |
| 1498 | State = Disable; |
| 1499 | } else if (Pred->isStr("begin")) |
| 1500 | State = Begin; |
| 1501 | else if (Pred->isStr("end")) |
| 1502 | State = End; |
| 1503 | else { |
| 1504 | PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_predicate) |
| 1505 | << Ext->isStr("all"); |
Peter Collingbourne | 7ce13fc | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 1506 | return; |
| 1507 | } |
Pekka Jaaskelainen | 1db1da2 | 2013-10-12 09:29:48 +0000 | [diff] [blame] | 1508 | SourceLocation StateLoc = Tok.getLocation(); |
Peter Collingbourne | 7ce13fc | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 1509 | |
Eli Friedman | 68be164 | 2012-10-04 02:36:51 +0000 | [diff] [blame] | 1510 | PP.Lex(Tok); |
| 1511 | if (Tok.isNot(tok::eod)) { |
| 1512 | PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) << |
| 1513 | "OPENCL EXTENSION"; |
Peter Collingbourne | 7ce13fc | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 1514 | return; |
| 1515 | } |
Eli Friedman | 68be164 | 2012-10-04 02:36:51 +0000 | [diff] [blame] | 1516 | |
Yaxun Liu | 5b74665 | 2016-12-18 05:18:55 +0000 | [diff] [blame] | 1517 | auto Info = PP.getPreprocessorAllocator().Allocate<OpenCLExtData>(1); |
| 1518 | Info->first = Ext; |
| 1519 | Info->second = State; |
David Blaikie | 2eabcc9 | 2016-02-09 18:52:09 +0000 | [diff] [blame] | 1520 | MutableArrayRef<Token> Toks(PP.getPreprocessorAllocator().Allocate<Token>(1), |
| 1521 | 1); |
Eli Friedman | 68be164 | 2012-10-04 02:36:51 +0000 | [diff] [blame] | 1522 | Toks[0].startToken(); |
| 1523 | Toks[0].setKind(tok::annot_pragma_opencl_extension); |
| 1524 | Toks[0].setLocation(NameLoc); |
Yaxun Liu | 5b74665 | 2016-12-18 05:18:55 +0000 | [diff] [blame] | 1525 | Toks[0].setAnnotationValue(static_cast<void*>(Info)); |
David Majnemer | a8f2f1d | 2015-03-19 00:10:23 +0000 | [diff] [blame] | 1526 | Toks[0].setAnnotationEndLoc(StateLoc); |
David Blaikie | 2eabcc9 | 2016-02-09 18:52:09 +0000 | [diff] [blame] | 1527 | PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true); |
Pekka Jaaskelainen | 1db1da2 | 2013-10-12 09:29:48 +0000 | [diff] [blame] | 1528 | |
| 1529 | if (PP.getPPCallbacks()) |
Yaxun Liu | 5b74665 | 2016-12-18 05:18:55 +0000 | [diff] [blame] | 1530 | PP.getPPCallbacks()->PragmaOpenCLExtension(NameLoc, Ext, |
| 1531 | StateLoc, State); |
Peter Collingbourne | 7ce13fc | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 1532 | } |
| 1533 | |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 1534 | /// \brief Handle '#pragma omp ...' when OpenMP is disabled. |
| 1535 | /// |
| 1536 | void |
| 1537 | PragmaNoOpenMPHandler::HandlePragma(Preprocessor &PP, |
| 1538 | PragmaIntroducerKind Introducer, |
| 1539 | Token &FirstTok) { |
Alp Toker | d4a3f0e | 2014-06-15 23:30:39 +0000 | [diff] [blame] | 1540 | if (!PP.getDiagnostics().isIgnored(diag::warn_pragma_omp_ignored, |
| 1541 | FirstTok.getLocation())) { |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 1542 | PP.Diag(FirstTok, diag::warn_pragma_omp_ignored); |
Alp Toker | d576e00 | 2014-06-12 11:13:52 +0000 | [diff] [blame] | 1543 | PP.getDiagnostics().setSeverity(diag::warn_pragma_omp_ignored, |
| 1544 | diag::Severity::Ignored, SourceLocation()); |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 1545 | } |
| 1546 | PP.DiscardUntilEndOfDirective(); |
| 1547 | } |
| 1548 | |
| 1549 | /// \brief Handle '#pragma omp ...' when OpenMP is enabled. |
| 1550 | /// |
| 1551 | void |
| 1552 | PragmaOpenMPHandler::HandlePragma(Preprocessor &PP, |
| 1553 | PragmaIntroducerKind Introducer, |
| 1554 | Token &FirstTok) { |
| 1555 | SmallVector<Token, 16> Pragma; |
| 1556 | Token Tok; |
| 1557 | Tok.startToken(); |
| 1558 | Tok.setKind(tok::annot_pragma_openmp); |
| 1559 | Tok.setLocation(FirstTok.getLocation()); |
| 1560 | |
| 1561 | while (Tok.isNot(tok::eod)) { |
| 1562 | Pragma.push_back(Tok); |
| 1563 | PP.Lex(Tok); |
| 1564 | } |
| 1565 | SourceLocation EodLoc = Tok.getLocation(); |
| 1566 | Tok.startToken(); |
| 1567 | Tok.setKind(tok::annot_pragma_openmp_end); |
| 1568 | Tok.setLocation(EodLoc); |
| 1569 | Pragma.push_back(Tok); |
| 1570 | |
David Blaikie | 2eabcc9 | 2016-02-09 18:52:09 +0000 | [diff] [blame] | 1571 | auto Toks = llvm::make_unique<Token[]>(Pragma.size()); |
| 1572 | std::copy(Pragma.begin(), Pragma.end(), Toks.get()); |
| 1573 | PP.EnterTokenStream(std::move(Toks), Pragma.size(), |
| 1574 | /*DisableMacroExpansion=*/false); |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 1575 | } |
Reid Kleckner | 002562a | 2013-05-06 21:02:12 +0000 | [diff] [blame] | 1576 | |
David Majnemer | 4bb0980 | 2014-02-10 19:50:15 +0000 | [diff] [blame] | 1577 | /// \brief Handle '#pragma pointers_to_members' |
| 1578 | // The grammar for this pragma is as follows: |
| 1579 | // |
| 1580 | // <inheritance model> ::= ('single' | 'multiple' | 'virtual') '_inheritance' |
| 1581 | // |
| 1582 | // #pragma pointers_to_members '(' 'best_case' ')' |
| 1583 | // #pragma pointers_to_members '(' 'full_generality' [',' inheritance-model] ')' |
| 1584 | // #pragma pointers_to_members '(' inheritance-model ')' |
| 1585 | void PragmaMSPointersToMembers::HandlePragma(Preprocessor &PP, |
| 1586 | PragmaIntroducerKind Introducer, |
| 1587 | Token &Tok) { |
| 1588 | SourceLocation PointersToMembersLoc = Tok.getLocation(); |
| 1589 | PP.Lex(Tok); |
| 1590 | if (Tok.isNot(tok::l_paren)) { |
| 1591 | PP.Diag(PointersToMembersLoc, diag::warn_pragma_expected_lparen) |
| 1592 | << "pointers_to_members"; |
| 1593 | return; |
| 1594 | } |
| 1595 | PP.Lex(Tok); |
| 1596 | const IdentifierInfo *Arg = Tok.getIdentifierInfo(); |
| 1597 | if (!Arg) { |
| 1598 | PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier) |
| 1599 | << "pointers_to_members"; |
| 1600 | return; |
| 1601 | } |
| 1602 | PP.Lex(Tok); |
| 1603 | |
David Majnemer | 86c318f | 2014-02-11 21:05:00 +0000 | [diff] [blame] | 1604 | LangOptions::PragmaMSPointersToMembersKind RepresentationMethod; |
David Majnemer | 4bb0980 | 2014-02-10 19:50:15 +0000 | [diff] [blame] | 1605 | if (Arg->isStr("best_case")) { |
David Majnemer | 86c318f | 2014-02-11 21:05:00 +0000 | [diff] [blame] | 1606 | RepresentationMethod = LangOptions::PPTMK_BestCase; |
David Majnemer | 4bb0980 | 2014-02-10 19:50:15 +0000 | [diff] [blame] | 1607 | } else { |
| 1608 | if (Arg->isStr("full_generality")) { |
| 1609 | if (Tok.is(tok::comma)) { |
| 1610 | PP.Lex(Tok); |
| 1611 | |
| 1612 | Arg = Tok.getIdentifierInfo(); |
| 1613 | if (!Arg) { |
| 1614 | PP.Diag(Tok.getLocation(), |
| 1615 | diag::err_pragma_pointers_to_members_unknown_kind) |
| 1616 | << Tok.getKind() << /*OnlyInheritanceModels*/ 0; |
| 1617 | return; |
| 1618 | } |
| 1619 | PP.Lex(Tok); |
| 1620 | } else if (Tok.is(tok::r_paren)) { |
| 1621 | // #pragma pointers_to_members(full_generality) implicitly specifies |
| 1622 | // virtual_inheritance. |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 1623 | Arg = nullptr; |
David Majnemer | 86c318f | 2014-02-11 21:05:00 +0000 | [diff] [blame] | 1624 | RepresentationMethod = LangOptions::PPTMK_FullGeneralityVirtualInheritance; |
David Majnemer | 4bb0980 | 2014-02-10 19:50:15 +0000 | [diff] [blame] | 1625 | } else { |
| 1626 | PP.Diag(Tok.getLocation(), diag::err_expected_punc) |
| 1627 | << "full_generality"; |
| 1628 | return; |
| 1629 | } |
| 1630 | } |
| 1631 | |
| 1632 | if (Arg) { |
| 1633 | if (Arg->isStr("single_inheritance")) { |
David Majnemer | 86c318f | 2014-02-11 21:05:00 +0000 | [diff] [blame] | 1634 | RepresentationMethod = |
| 1635 | LangOptions::PPTMK_FullGeneralitySingleInheritance; |
David Majnemer | 4bb0980 | 2014-02-10 19:50:15 +0000 | [diff] [blame] | 1636 | } else if (Arg->isStr("multiple_inheritance")) { |
David Majnemer | 86c318f | 2014-02-11 21:05:00 +0000 | [diff] [blame] | 1637 | RepresentationMethod = |
| 1638 | LangOptions::PPTMK_FullGeneralityMultipleInheritance; |
David Majnemer | 4bb0980 | 2014-02-10 19:50:15 +0000 | [diff] [blame] | 1639 | } else if (Arg->isStr("virtual_inheritance")) { |
David Majnemer | 86c318f | 2014-02-11 21:05:00 +0000 | [diff] [blame] | 1640 | RepresentationMethod = |
| 1641 | LangOptions::PPTMK_FullGeneralityVirtualInheritance; |
David Majnemer | 4bb0980 | 2014-02-10 19:50:15 +0000 | [diff] [blame] | 1642 | } else { |
| 1643 | PP.Diag(Tok.getLocation(), |
| 1644 | diag::err_pragma_pointers_to_members_unknown_kind) |
| 1645 | << Arg << /*HasPointerDeclaration*/ 1; |
| 1646 | return; |
| 1647 | } |
| 1648 | } |
| 1649 | } |
| 1650 | |
| 1651 | if (Tok.isNot(tok::r_paren)) { |
| 1652 | PP.Diag(Tok.getLocation(), diag::err_expected_rparen_after) |
| 1653 | << (Arg ? Arg->getName() : "full_generality"); |
| 1654 | return; |
| 1655 | } |
| 1656 | |
David Majnemer | a8f2f1d | 2015-03-19 00:10:23 +0000 | [diff] [blame] | 1657 | SourceLocation EndLoc = Tok.getLocation(); |
David Majnemer | 4bb0980 | 2014-02-10 19:50:15 +0000 | [diff] [blame] | 1658 | PP.Lex(Tok); |
| 1659 | if (Tok.isNot(tok::eod)) { |
| 1660 | PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) |
| 1661 | << "pointers_to_members"; |
| 1662 | return; |
| 1663 | } |
| 1664 | |
| 1665 | Token AnnotTok; |
| 1666 | AnnotTok.startToken(); |
| 1667 | AnnotTok.setKind(tok::annot_pragma_ms_pointers_to_members); |
| 1668 | AnnotTok.setLocation(PointersToMembersLoc); |
David Majnemer | a8f2f1d | 2015-03-19 00:10:23 +0000 | [diff] [blame] | 1669 | AnnotTok.setAnnotationEndLoc(EndLoc); |
David Majnemer | 4bb0980 | 2014-02-10 19:50:15 +0000 | [diff] [blame] | 1670 | AnnotTok.setAnnotationValue( |
| 1671 | reinterpret_cast<void *>(static_cast<uintptr_t>(RepresentationMethod))); |
| 1672 | PP.EnterToken(AnnotTok); |
| 1673 | } |
| 1674 | |
Reid Kleckner | c0dca6d | 2014-02-12 23:50:26 +0000 | [diff] [blame] | 1675 | /// \brief Handle '#pragma vtordisp' |
| 1676 | // The grammar for this pragma is as follows: |
| 1677 | // |
| 1678 | // <vtordisp-mode> ::= ('off' | 'on' | '0' | '1' | '2' ) |
| 1679 | // |
| 1680 | // #pragma vtordisp '(' ['push' ','] vtordisp-mode ')' |
| 1681 | // #pragma vtordisp '(' 'pop' ')' |
| 1682 | // #pragma vtordisp '(' ')' |
| 1683 | void PragmaMSVtorDisp::HandlePragma(Preprocessor &PP, |
| 1684 | PragmaIntroducerKind Introducer, |
| 1685 | Token &Tok) { |
| 1686 | SourceLocation VtorDispLoc = Tok.getLocation(); |
| 1687 | PP.Lex(Tok); |
| 1688 | if (Tok.isNot(tok::l_paren)) { |
| 1689 | PP.Diag(VtorDispLoc, diag::warn_pragma_expected_lparen) << "vtordisp"; |
| 1690 | return; |
| 1691 | } |
| 1692 | PP.Lex(Tok); |
| 1693 | |
Denis Zobnin | 2290dac | 2016-04-29 11:27:00 +0000 | [diff] [blame] | 1694 | Sema::PragmaMsStackAction Action = Sema::PSK_Set; |
Reid Kleckner | c0dca6d | 2014-02-12 23:50:26 +0000 | [diff] [blame] | 1695 | const IdentifierInfo *II = Tok.getIdentifierInfo(); |
| 1696 | if (II) { |
| 1697 | if (II->isStr("push")) { |
| 1698 | // #pragma vtordisp(push, mode) |
| 1699 | PP.Lex(Tok); |
| 1700 | if (Tok.isNot(tok::comma)) { |
| 1701 | PP.Diag(VtorDispLoc, diag::warn_pragma_expected_punc) << "vtordisp"; |
| 1702 | return; |
| 1703 | } |
| 1704 | PP.Lex(Tok); |
Denis Zobnin | 2290dac | 2016-04-29 11:27:00 +0000 | [diff] [blame] | 1705 | Action = Sema::PSK_Push_Set; |
Reid Kleckner | c0dca6d | 2014-02-12 23:50:26 +0000 | [diff] [blame] | 1706 | // not push, could be on/off |
| 1707 | } else if (II->isStr("pop")) { |
| 1708 | // #pragma vtordisp(pop) |
| 1709 | PP.Lex(Tok); |
Denis Zobnin | 2290dac | 2016-04-29 11:27:00 +0000 | [diff] [blame] | 1710 | Action = Sema::PSK_Pop; |
Reid Kleckner | c0dca6d | 2014-02-12 23:50:26 +0000 | [diff] [blame] | 1711 | } |
| 1712 | // not push or pop, could be on/off |
| 1713 | } else { |
| 1714 | if (Tok.is(tok::r_paren)) { |
| 1715 | // #pragma vtordisp() |
Denis Zobnin | 2290dac | 2016-04-29 11:27:00 +0000 | [diff] [blame] | 1716 | Action = Sema::PSK_Reset; |
Reid Kleckner | c0dca6d | 2014-02-12 23:50:26 +0000 | [diff] [blame] | 1717 | } |
| 1718 | } |
| 1719 | |
| 1720 | |
Reid Kleckner | a4b3b2d | 2014-02-13 00:44:34 +0000 | [diff] [blame] | 1721 | uint64_t Value = 0; |
Denis Zobnin | 2290dac | 2016-04-29 11:27:00 +0000 | [diff] [blame] | 1722 | if (Action & Sema::PSK_Push || Action & Sema::PSK_Set) { |
Reid Kleckner | c0dca6d | 2014-02-12 23:50:26 +0000 | [diff] [blame] | 1723 | const IdentifierInfo *II = Tok.getIdentifierInfo(); |
| 1724 | if (II && II->isStr("off")) { |
| 1725 | PP.Lex(Tok); |
| 1726 | Value = 0; |
| 1727 | } else if (II && II->isStr("on")) { |
| 1728 | PP.Lex(Tok); |
| 1729 | Value = 1; |
| 1730 | } else if (Tok.is(tok::numeric_constant) && |
| 1731 | PP.parseSimpleIntegerLiteral(Tok, Value)) { |
| 1732 | if (Value > 2) { |
| 1733 | PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_integer) |
| 1734 | << 0 << 2 << "vtordisp"; |
| 1735 | return; |
| 1736 | } |
| 1737 | } else { |
| 1738 | PP.Diag(Tok.getLocation(), diag::warn_pragma_invalid_action) |
| 1739 | << "vtordisp"; |
| 1740 | return; |
| 1741 | } |
| 1742 | } |
| 1743 | |
| 1744 | // Finish the pragma: ')' $ |
| 1745 | if (Tok.isNot(tok::r_paren)) { |
| 1746 | PP.Diag(VtorDispLoc, diag::warn_pragma_expected_rparen) << "vtordisp"; |
| 1747 | return; |
| 1748 | } |
David Majnemer | a8f2f1d | 2015-03-19 00:10:23 +0000 | [diff] [blame] | 1749 | SourceLocation EndLoc = Tok.getLocation(); |
Reid Kleckner | c0dca6d | 2014-02-12 23:50:26 +0000 | [diff] [blame] | 1750 | PP.Lex(Tok); |
| 1751 | if (Tok.isNot(tok::eod)) { |
| 1752 | PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) |
| 1753 | << "vtordisp"; |
| 1754 | return; |
| 1755 | } |
| 1756 | |
| 1757 | // Enter the annotation. |
| 1758 | Token AnnotTok; |
| 1759 | AnnotTok.startToken(); |
| 1760 | AnnotTok.setKind(tok::annot_pragma_ms_vtordisp); |
| 1761 | AnnotTok.setLocation(VtorDispLoc); |
David Majnemer | a8f2f1d | 2015-03-19 00:10:23 +0000 | [diff] [blame] | 1762 | AnnotTok.setAnnotationEndLoc(EndLoc); |
Reid Kleckner | a4b3b2d | 2014-02-13 00:44:34 +0000 | [diff] [blame] | 1763 | AnnotTok.setAnnotationValue(reinterpret_cast<void *>( |
Denis Zobnin | 2290dac | 2016-04-29 11:27:00 +0000 | [diff] [blame] | 1764 | static_cast<uintptr_t>((Action << 16) | (Value & 0xFFFF)))); |
Reid Kleckner | c0dca6d | 2014-02-12 23:50:26 +0000 | [diff] [blame] | 1765 | PP.EnterToken(AnnotTok); |
| 1766 | } |
| 1767 | |
Warren Hunt | c3b1896 | 2014-04-08 22:30:47 +0000 | [diff] [blame] | 1768 | /// \brief Handle all MS pragmas. Simply forwards the tokens after inserting |
| 1769 | /// an annotation token. |
| 1770 | void PragmaMSPragma::HandlePragma(Preprocessor &PP, |
| 1771 | PragmaIntroducerKind Introducer, |
| 1772 | Token &Tok) { |
| 1773 | Token EoF, AnnotTok; |
| 1774 | EoF.startToken(); |
| 1775 | EoF.setKind(tok::eof); |
| 1776 | AnnotTok.startToken(); |
| 1777 | AnnotTok.setKind(tok::annot_pragma_ms_pragma); |
| 1778 | AnnotTok.setLocation(Tok.getLocation()); |
David Majnemer | a8f2f1d | 2015-03-19 00:10:23 +0000 | [diff] [blame] | 1779 | AnnotTok.setAnnotationEndLoc(Tok.getLocation()); |
Warren Hunt | c3b1896 | 2014-04-08 22:30:47 +0000 | [diff] [blame] | 1780 | SmallVector<Token, 8> TokenVector; |
| 1781 | // Suck up all of the tokens before the eod. |
David Majnemer | a8f2f1d | 2015-03-19 00:10:23 +0000 | [diff] [blame] | 1782 | for (; Tok.isNot(tok::eod); PP.Lex(Tok)) { |
Warren Hunt | c3b1896 | 2014-04-08 22:30:47 +0000 | [diff] [blame] | 1783 | TokenVector.push_back(Tok); |
David Majnemer | a8f2f1d | 2015-03-19 00:10:23 +0000 | [diff] [blame] | 1784 | AnnotTok.setAnnotationEndLoc(Tok.getLocation()); |
| 1785 | } |
Warren Hunt | c3b1896 | 2014-04-08 22:30:47 +0000 | [diff] [blame] | 1786 | // Add a sentinal EoF token to the end of the list. |
| 1787 | TokenVector.push_back(EoF); |
| 1788 | // We must allocate this array with new because EnterTokenStream is going to |
| 1789 | // delete it later. |
David Blaikie | 2eabcc9 | 2016-02-09 18:52:09 +0000 | [diff] [blame] | 1790 | auto TokenArray = llvm::make_unique<Token[]>(TokenVector.size()); |
| 1791 | std::copy(TokenVector.begin(), TokenVector.end(), TokenArray.get()); |
Warren Hunt | c3b1896 | 2014-04-08 22:30:47 +0000 | [diff] [blame] | 1792 | auto Value = new (PP.getPreprocessorAllocator()) |
David Blaikie | 2eabcc9 | 2016-02-09 18:52:09 +0000 | [diff] [blame] | 1793 | std::pair<std::unique_ptr<Token[]>, size_t>(std::move(TokenArray), |
| 1794 | TokenVector.size()); |
Warren Hunt | c3b1896 | 2014-04-08 22:30:47 +0000 | [diff] [blame] | 1795 | AnnotTok.setAnnotationValue(Value); |
| 1796 | PP.EnterToken(AnnotTok); |
Reid Kleckner | d3923aa | 2014-04-03 19:04:24 +0000 | [diff] [blame] | 1797 | } |
| 1798 | |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 1799 | /// \brief Handle the Microsoft \#pragma detect_mismatch extension. |
| 1800 | /// |
| 1801 | /// The syntax is: |
| 1802 | /// \code |
| 1803 | /// #pragma detect_mismatch("name", "value") |
| 1804 | /// \endcode |
| 1805 | /// Where 'name' and 'value' are quoted strings. The values are embedded in |
| 1806 | /// the object file and passed along to the linker. If the linker detects a |
| 1807 | /// mismatch in the object file's values for the given name, a LNK2038 error |
| 1808 | /// is emitted. See MSDN for more details. |
| 1809 | void PragmaDetectMismatchHandler::HandlePragma(Preprocessor &PP, |
| 1810 | PragmaIntroducerKind Introducer, |
| 1811 | Token &Tok) { |
Nico Weber | cbbaeb1 | 2016-03-02 19:28:54 +0000 | [diff] [blame] | 1812 | SourceLocation DetectMismatchLoc = Tok.getLocation(); |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 1813 | PP.Lex(Tok); |
| 1814 | if (Tok.isNot(tok::l_paren)) { |
Nico Weber | cbbaeb1 | 2016-03-02 19:28:54 +0000 | [diff] [blame] | 1815 | PP.Diag(DetectMismatchLoc, diag::err_expected) << tok::l_paren; |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 1816 | return; |
| 1817 | } |
| 1818 | |
| 1819 | // Read the name to embed, which must be a string literal. |
| 1820 | std::string NameString; |
| 1821 | if (!PP.LexStringLiteral(Tok, NameString, |
| 1822 | "pragma detect_mismatch", |
| 1823 | /*MacroExpansion=*/true)) |
| 1824 | return; |
| 1825 | |
| 1826 | // Read the comma followed by a second string literal. |
| 1827 | std::string ValueString; |
| 1828 | if (Tok.isNot(tok::comma)) { |
| 1829 | PP.Diag(Tok.getLocation(), diag::err_pragma_detect_mismatch_malformed); |
| 1830 | return; |
| 1831 | } |
| 1832 | |
| 1833 | if (!PP.LexStringLiteral(Tok, ValueString, "pragma detect_mismatch", |
| 1834 | /*MacroExpansion=*/true)) |
| 1835 | return; |
| 1836 | |
| 1837 | if (Tok.isNot(tok::r_paren)) { |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 1838 | PP.Diag(Tok.getLocation(), diag::err_expected) << tok::r_paren; |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 1839 | return; |
| 1840 | } |
| 1841 | PP.Lex(Tok); // Eat the r_paren. |
| 1842 | |
| 1843 | if (Tok.isNot(tok::eod)) { |
| 1844 | PP.Diag(Tok.getLocation(), diag::err_pragma_detect_mismatch_malformed); |
| 1845 | return; |
| 1846 | } |
| 1847 | |
Reid Kleckner | 71966c9 | 2014-02-20 23:37:45 +0000 | [diff] [blame] | 1848 | // If the pragma is lexically sound, notify any interested PPCallbacks. |
| 1849 | if (PP.getPPCallbacks()) |
Nico Weber | cbbaeb1 | 2016-03-02 19:28:54 +0000 | [diff] [blame] | 1850 | PP.getPPCallbacks()->PragmaDetectMismatch(DetectMismatchLoc, NameString, |
Reid Kleckner | 71966c9 | 2014-02-20 23:37:45 +0000 | [diff] [blame] | 1851 | ValueString); |
| 1852 | |
Nico Weber | cbbaeb1 | 2016-03-02 19:28:54 +0000 | [diff] [blame] | 1853 | Actions.ActOnPragmaDetectMismatch(DetectMismatchLoc, NameString, ValueString); |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 1854 | } |
| 1855 | |
Reid Kleckner | 002562a | 2013-05-06 21:02:12 +0000 | [diff] [blame] | 1856 | /// \brief Handle the microsoft \#pragma comment extension. |
| 1857 | /// |
| 1858 | /// The syntax is: |
| 1859 | /// \code |
| 1860 | /// #pragma comment(linker, "foo") |
| 1861 | /// \endcode |
| 1862 | /// 'linker' is one of five identifiers: compiler, exestr, lib, linker, user. |
| 1863 | /// "foo" is a string, which is fully macro expanded, and permits string |
| 1864 | /// concatenation, embedded escape characters etc. See MSDN for more details. |
| 1865 | void PragmaCommentHandler::HandlePragma(Preprocessor &PP, |
| 1866 | PragmaIntroducerKind Introducer, |
| 1867 | Token &Tok) { |
| 1868 | SourceLocation CommentLoc = Tok.getLocation(); |
| 1869 | PP.Lex(Tok); |
| 1870 | if (Tok.isNot(tok::l_paren)) { |
| 1871 | PP.Diag(CommentLoc, diag::err_pragma_comment_malformed); |
| 1872 | return; |
| 1873 | } |
| 1874 | |
| 1875 | // Read the identifier. |
| 1876 | PP.Lex(Tok); |
| 1877 | if (Tok.isNot(tok::identifier)) { |
| 1878 | PP.Diag(CommentLoc, diag::err_pragma_comment_malformed); |
| 1879 | return; |
| 1880 | } |
| 1881 | |
| 1882 | // Verify that this is one of the 5 whitelisted options. |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1883 | IdentifierInfo *II = Tok.getIdentifierInfo(); |
Nico Weber | 6622029 | 2016-03-02 17:28:48 +0000 | [diff] [blame] | 1884 | PragmaMSCommentKind Kind = |
| 1885 | llvm::StringSwitch<PragmaMSCommentKind>(II->getName()) |
| 1886 | .Case("linker", PCK_Linker) |
| 1887 | .Case("lib", PCK_Lib) |
| 1888 | .Case("compiler", PCK_Compiler) |
| 1889 | .Case("exestr", PCK_ExeStr) |
| 1890 | .Case("user", PCK_User) |
| 1891 | .Default(PCK_Unknown); |
| 1892 | if (Kind == PCK_Unknown) { |
Reid Kleckner | 002562a | 2013-05-06 21:02:12 +0000 | [diff] [blame] | 1893 | PP.Diag(Tok.getLocation(), diag::err_pragma_comment_unknown_kind); |
| 1894 | return; |
| 1895 | } |
| 1896 | |
Yunzhong Gao | 99efc03 | 2015-03-23 20:41:42 +0000 | [diff] [blame] | 1897 | // On PS4, issue a warning about any pragma comments other than |
| 1898 | // #pragma comment lib. |
Nico Weber | 6622029 | 2016-03-02 17:28:48 +0000 | [diff] [blame] | 1899 | if (PP.getTargetInfo().getTriple().isPS4() && Kind != PCK_Lib) { |
Yunzhong Gao | 99efc03 | 2015-03-23 20:41:42 +0000 | [diff] [blame] | 1900 | PP.Diag(Tok.getLocation(), diag::warn_pragma_comment_ignored) |
| 1901 | << II->getName(); |
| 1902 | return; |
| 1903 | } |
| 1904 | |
Reid Kleckner | 002562a | 2013-05-06 21:02:12 +0000 | [diff] [blame] | 1905 | // Read the optional string if present. |
| 1906 | PP.Lex(Tok); |
| 1907 | std::string ArgumentString; |
| 1908 | if (Tok.is(tok::comma) && !PP.LexStringLiteral(Tok, ArgumentString, |
| 1909 | "pragma comment", |
| 1910 | /*MacroExpansion=*/true)) |
| 1911 | return; |
| 1912 | |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1913 | // FIXME: warn that 'exestr' is deprecated. |
Reid Kleckner | 002562a | 2013-05-06 21:02:12 +0000 | [diff] [blame] | 1914 | // FIXME: If the kind is "compiler" warn if the string is present (it is |
| 1915 | // ignored). |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1916 | // The MSDN docs say that "lib" and "linker" require a string and have a short |
| 1917 | // whitelist of linker options they support, but in practice MSVC doesn't |
| 1918 | // issue a diagnostic. Therefore neither does clang. |
Reid Kleckner | 002562a | 2013-05-06 21:02:12 +0000 | [diff] [blame] | 1919 | |
| 1920 | if (Tok.isNot(tok::r_paren)) { |
| 1921 | PP.Diag(Tok.getLocation(), diag::err_pragma_comment_malformed); |
| 1922 | return; |
| 1923 | } |
| 1924 | PP.Lex(Tok); // eat the r_paren. |
| 1925 | |
| 1926 | if (Tok.isNot(tok::eod)) { |
| 1927 | PP.Diag(Tok.getLocation(), diag::err_pragma_comment_malformed); |
| 1928 | return; |
| 1929 | } |
| 1930 | |
Reid Kleckner | 71966c9 | 2014-02-20 23:37:45 +0000 | [diff] [blame] | 1931 | // If the pragma is lexically sound, notify any interested PPCallbacks. |
| 1932 | if (PP.getPPCallbacks()) |
| 1933 | PP.getPPCallbacks()->PragmaComment(CommentLoc, II, ArgumentString); |
| 1934 | |
Nico Weber | 6622029 | 2016-03-02 17:28:48 +0000 | [diff] [blame] | 1935 | Actions.ActOnPragmaMSComment(CommentLoc, Kind, ArgumentString); |
Reid Kleckner | 002562a | 2013-05-06 21:02:12 +0000 | [diff] [blame] | 1936 | } |
Dario Domizioli | 13a0a38 | 2014-05-23 12:13:25 +0000 | [diff] [blame] | 1937 | |
| 1938 | // #pragma clang optimize off |
| 1939 | // #pragma clang optimize on |
| 1940 | void PragmaOptimizeHandler::HandlePragma(Preprocessor &PP, |
| 1941 | PragmaIntroducerKind Introducer, |
| 1942 | Token &FirstToken) { |
| 1943 | Token Tok; |
| 1944 | PP.Lex(Tok); |
| 1945 | if (Tok.is(tok::eod)) { |
Mark Heffernan | bd26f5e | 2014-07-21 18:08:34 +0000 | [diff] [blame] | 1946 | PP.Diag(Tok.getLocation(), diag::err_pragma_missing_argument) |
Mark Heffernan | 450c238 | 2014-07-23 17:31:31 +0000 | [diff] [blame] | 1947 | << "clang optimize" << /*Expected=*/true << "'on' or 'off'"; |
Dario Domizioli | 13a0a38 | 2014-05-23 12:13:25 +0000 | [diff] [blame] | 1948 | return; |
| 1949 | } |
| 1950 | if (Tok.isNot(tok::identifier)) { |
| 1951 | PP.Diag(Tok.getLocation(), diag::err_pragma_optimize_invalid_argument) |
| 1952 | << PP.getSpelling(Tok); |
| 1953 | return; |
| 1954 | } |
| 1955 | const IdentifierInfo *II = Tok.getIdentifierInfo(); |
| 1956 | // The only accepted values are 'on' or 'off'. |
| 1957 | bool IsOn = false; |
| 1958 | if (II->isStr("on")) { |
| 1959 | IsOn = true; |
| 1960 | } else if (!II->isStr("off")) { |
| 1961 | PP.Diag(Tok.getLocation(), diag::err_pragma_optimize_invalid_argument) |
| 1962 | << PP.getSpelling(Tok); |
| 1963 | return; |
| 1964 | } |
| 1965 | PP.Lex(Tok); |
| 1966 | |
| 1967 | if (Tok.isNot(tok::eod)) { |
| 1968 | PP.Diag(Tok.getLocation(), diag::err_pragma_optimize_extra_argument) |
| 1969 | << PP.getSpelling(Tok); |
| 1970 | return; |
| 1971 | } |
Eli Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 1972 | |
| 1973 | Actions.ActOnPragmaOptimize(IsOn, FirstToken.getLocation()); |
| 1974 | } |
| 1975 | |
Adam Nemet | 60d3264 | 2017-04-04 21:18:36 +0000 | [diff] [blame^] | 1976 | namespace { |
| 1977 | /// Used as the annotation value for tok::annot_pragma_fp. |
| 1978 | struct TokFPAnnotValue { |
| 1979 | enum FlagKinds { Contract }; |
| 1980 | enum FlagValues { On, Off, Fast }; |
| 1981 | |
| 1982 | FlagKinds FlagKind; |
| 1983 | FlagValues FlagValue; |
| 1984 | }; |
| 1985 | } // end anonymous namespace |
| 1986 | |
| 1987 | void PragmaFPHandler::HandlePragma(Preprocessor &PP, |
| 1988 | PragmaIntroducerKind Introducer, |
| 1989 | Token &Tok) { |
| 1990 | // fp |
| 1991 | Token PragmaName = Tok; |
| 1992 | SmallVector<Token, 1> TokenList; |
| 1993 | |
| 1994 | PP.Lex(Tok); |
| 1995 | if (Tok.isNot(tok::identifier)) { |
| 1996 | PP.Diag(Tok.getLocation(), diag::err_pragma_fp_invalid_option) |
| 1997 | << /*MissingOption=*/true << ""; |
| 1998 | return; |
| 1999 | } |
| 2000 | |
| 2001 | while (Tok.is(tok::identifier)) { |
| 2002 | IdentifierInfo *OptionInfo = Tok.getIdentifierInfo(); |
| 2003 | |
| 2004 | auto FlagKind = |
| 2005 | llvm::StringSwitch<llvm::Optional<TokFPAnnotValue::FlagKinds>>( |
| 2006 | OptionInfo->getName()) |
| 2007 | .Case("contract", TokFPAnnotValue::Contract) |
| 2008 | .Default(None); |
| 2009 | if (!FlagKind) { |
| 2010 | PP.Diag(Tok.getLocation(), diag::err_pragma_fp_invalid_option) |
| 2011 | << /*MissingOption=*/false << OptionInfo; |
| 2012 | return; |
| 2013 | } |
| 2014 | PP.Lex(Tok); |
| 2015 | |
| 2016 | // Read '(' |
| 2017 | if (Tok.isNot(tok::l_paren)) { |
| 2018 | PP.Diag(Tok.getLocation(), diag::err_expected) << tok::l_paren; |
| 2019 | return; |
| 2020 | } |
| 2021 | PP.Lex(Tok); |
| 2022 | |
| 2023 | if (Tok.isNot(tok::identifier)) { |
| 2024 | PP.Diag(Tok.getLocation(), diag::err_pragma_fp_invalid_argument) |
| 2025 | << PP.getSpelling(Tok) << OptionInfo->getName(); |
| 2026 | return; |
| 2027 | } |
| 2028 | const IdentifierInfo *II = Tok.getIdentifierInfo(); |
| 2029 | |
| 2030 | auto FlagValue = |
| 2031 | llvm::StringSwitch<llvm::Optional<TokFPAnnotValue::FlagValues>>( |
| 2032 | II->getName()) |
| 2033 | .Case("on", TokFPAnnotValue::On) |
| 2034 | .Case("off", TokFPAnnotValue::Off) |
| 2035 | .Case("fast", TokFPAnnotValue::Fast) |
| 2036 | .Default(llvm::None); |
| 2037 | |
| 2038 | if (!FlagValue) { |
| 2039 | PP.Diag(Tok.getLocation(), diag::err_pragma_fp_invalid_argument) |
| 2040 | << PP.getSpelling(Tok) << OptionInfo->getName(); |
| 2041 | return; |
| 2042 | } |
| 2043 | PP.Lex(Tok); |
| 2044 | |
| 2045 | // Read ')' |
| 2046 | if (Tok.isNot(tok::r_paren)) { |
| 2047 | PP.Diag(Tok.getLocation(), diag::err_expected) << tok::r_paren; |
| 2048 | return; |
| 2049 | } |
| 2050 | PP.Lex(Tok); |
| 2051 | |
| 2052 | auto *AnnotValue = new (PP.getPreprocessorAllocator()) |
| 2053 | TokFPAnnotValue{*FlagKind, *FlagValue}; |
| 2054 | // Generate the loop hint token. |
| 2055 | Token FPTok; |
| 2056 | FPTok.startToken(); |
| 2057 | FPTok.setKind(tok::annot_pragma_fp); |
| 2058 | FPTok.setLocation(PragmaName.getLocation()); |
| 2059 | FPTok.setAnnotationEndLoc(PragmaName.getLocation()); |
| 2060 | FPTok.setAnnotationValue(reinterpret_cast<void *>(AnnotValue)); |
| 2061 | TokenList.push_back(FPTok); |
| 2062 | } |
| 2063 | |
| 2064 | if (Tok.isNot(tok::eod)) { |
| 2065 | PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) |
| 2066 | << "clang fp"; |
| 2067 | return; |
| 2068 | } |
| 2069 | |
| 2070 | auto TokenArray = llvm::make_unique<Token[]>(TokenList.size()); |
| 2071 | std::copy(TokenList.begin(), TokenList.end(), TokenArray.get()); |
| 2072 | |
| 2073 | PP.EnterTokenStream(std::move(TokenArray), TokenList.size(), |
| 2074 | /*DisableMacroExpansion=*/false); |
| 2075 | } |
| 2076 | |
| 2077 | void Parser::HandlePragmaFP() { |
| 2078 | assert(Tok.is(tok::annot_pragma_fp)); |
| 2079 | auto *AnnotValue = |
| 2080 | reinterpret_cast<TokFPAnnotValue *>(Tok.getAnnotationValue()); |
| 2081 | |
| 2082 | LangOptions::FPContractModeKind FPC; |
| 2083 | switch (AnnotValue->FlagValue) { |
| 2084 | case TokFPAnnotValue::On: |
| 2085 | FPC = LangOptions::FPC_On; |
| 2086 | break; |
| 2087 | case TokFPAnnotValue::Fast: |
| 2088 | FPC = LangOptions::FPC_Fast; |
| 2089 | break; |
| 2090 | case TokFPAnnotValue::Off: |
| 2091 | FPC = LangOptions::FPC_Off; |
| 2092 | break; |
| 2093 | } |
| 2094 | |
| 2095 | Actions.ActOnPragmaFPContract(FPC); |
| 2096 | ConsumeToken(); // The annotation token. |
| 2097 | } |
| 2098 | |
Mark Heffernan | bd26f5e | 2014-07-21 18:08:34 +0000 | [diff] [blame] | 2099 | /// \brief Parses loop or unroll pragma hint value and fills in Info. |
Tyler Nowicki | 0c9b34b | 2014-07-31 20:15:14 +0000 | [diff] [blame] | 2100 | static bool ParseLoopHintValue(Preprocessor &PP, Token &Tok, Token PragmaName, |
| 2101 | Token Option, bool ValueInParens, |
Mark Heffernan | bd26f5e | 2014-07-21 18:08:34 +0000 | [diff] [blame] | 2102 | PragmaLoopHintInfo &Info) { |
Tyler Nowicki | c724a83e | 2014-10-12 20:46:07 +0000 | [diff] [blame] | 2103 | SmallVector<Token, 1> ValueList; |
| 2104 | int OpenParens = ValueInParens ? 1 : 0; |
| 2105 | // Read constant expression. |
| 2106 | while (Tok.isNot(tok::eod)) { |
| 2107 | if (Tok.is(tok::l_paren)) |
| 2108 | OpenParens++; |
| 2109 | else if (Tok.is(tok::r_paren)) { |
| 2110 | OpenParens--; |
| 2111 | if (OpenParens == 0 && ValueInParens) |
| 2112 | break; |
Mark Heffernan | bd26f5e | 2014-07-21 18:08:34 +0000 | [diff] [blame] | 2113 | } |
Mark Heffernan | bd26f5e | 2014-07-21 18:08:34 +0000 | [diff] [blame] | 2114 | |
Tyler Nowicki | c724a83e | 2014-10-12 20:46:07 +0000 | [diff] [blame] | 2115 | ValueList.push_back(Tok); |
| 2116 | PP.Lex(Tok); |
| 2117 | } |
Mark Heffernan | bd26f5e | 2014-07-21 18:08:34 +0000 | [diff] [blame] | 2118 | |
| 2119 | if (ValueInParens) { |
Tyler Nowicki | 0c9b34b | 2014-07-31 20:15:14 +0000 | [diff] [blame] | 2120 | // Read ')' |
Mark Heffernan | bd26f5e | 2014-07-21 18:08:34 +0000 | [diff] [blame] | 2121 | if (Tok.isNot(tok::r_paren)) { |
| 2122 | PP.Diag(Tok.getLocation(), diag::err_expected) << tok::r_paren; |
| 2123 | return true; |
| 2124 | } |
Tyler Nowicki | 0c9b34b | 2014-07-31 20:15:14 +0000 | [diff] [blame] | 2125 | PP.Lex(Tok); |
Mark Heffernan | bd26f5e | 2014-07-21 18:08:34 +0000 | [diff] [blame] | 2126 | } |
| 2127 | |
Tyler Nowicki | c724a83e | 2014-10-12 20:46:07 +0000 | [diff] [blame] | 2128 | Token EOFTok; |
| 2129 | EOFTok.startToken(); |
| 2130 | EOFTok.setKind(tok::eof); |
| 2131 | EOFTok.setLocation(Tok.getLocation()); |
| 2132 | ValueList.push_back(EOFTok); // Terminates expression for parsing. |
| 2133 | |
Benjamin Kramer | fa7f855 | 2015-08-05 09:39:57 +0000 | [diff] [blame] | 2134 | Info.Toks = llvm::makeArrayRef(ValueList).copy(PP.getPreprocessorAllocator()); |
Tyler Nowicki | c724a83e | 2014-10-12 20:46:07 +0000 | [diff] [blame] | 2135 | |
Mark Heffernan | bd26f5e | 2014-07-21 18:08:34 +0000 | [diff] [blame] | 2136 | Info.PragmaName = PragmaName; |
| 2137 | Info.Option = Option; |
Mark Heffernan | bd26f5e | 2014-07-21 18:08:34 +0000 | [diff] [blame] | 2138 | return false; |
| 2139 | } |
| 2140 | |
Eli Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 2141 | /// \brief Handle the \#pragma clang loop directive. |
| 2142 | /// #pragma clang 'loop' loop-hints |
| 2143 | /// |
| 2144 | /// loop-hints: |
| 2145 | /// loop-hint loop-hints[opt] |
| 2146 | /// |
| 2147 | /// loop-hint: |
| 2148 | /// 'vectorize' '(' loop-hint-keyword ')' |
| 2149 | /// 'interleave' '(' loop-hint-keyword ')' |
Mark Heffernan | 450c238 | 2014-07-23 17:31:31 +0000 | [diff] [blame] | 2150 | /// 'unroll' '(' unroll-hint-keyword ')' |
Eli Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 2151 | /// 'vectorize_width' '(' loop-hint-value ')' |
| 2152 | /// 'interleave_count' '(' loop-hint-value ')' |
Eli Bendersky | 86483b3 | 2014-06-11 17:56:26 +0000 | [diff] [blame] | 2153 | /// 'unroll_count' '(' loop-hint-value ')' |
Eli Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 2154 | /// |
| 2155 | /// loop-hint-keyword: |
| 2156 | /// 'enable' |
| 2157 | /// 'disable' |
Tyler Nowicki | 9d268e1 | 2015-06-11 23:23:17 +0000 | [diff] [blame] | 2158 | /// 'assume_safety' |
Eli Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 2159 | /// |
Mark Heffernan | 450c238 | 2014-07-23 17:31:31 +0000 | [diff] [blame] | 2160 | /// unroll-hint-keyword: |
Mark Heffernan | 397a98d | 2015-08-10 17:29:39 +0000 | [diff] [blame] | 2161 | /// 'enable' |
Mark Heffernan | 450c238 | 2014-07-23 17:31:31 +0000 | [diff] [blame] | 2162 | /// 'disable' |
Mark Heffernan | 397a98d | 2015-08-10 17:29:39 +0000 | [diff] [blame] | 2163 | /// 'full' |
Mark Heffernan | 450c238 | 2014-07-23 17:31:31 +0000 | [diff] [blame] | 2164 | /// |
Eli Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 2165 | /// loop-hint-value: |
| 2166 | /// constant-expression |
| 2167 | /// |
| 2168 | /// Specifying vectorize(enable) or vectorize_width(_value_) instructs llvm to |
| 2169 | /// try vectorizing the instructions of the loop it precedes. Specifying |
| 2170 | /// interleave(enable) or interleave_count(_value_) instructs llvm to try |
| 2171 | /// interleaving multiple iterations of the loop it precedes. The width of the |
| 2172 | /// vector instructions is specified by vectorize_width() and the number of |
| 2173 | /// interleaved loop iterations is specified by interleave_count(). Specifying a |
| 2174 | /// value of 1 effectively disables vectorization/interleaving, even if it is |
| 2175 | /// possible and profitable, and 0 is invalid. The loop vectorizer currently |
| 2176 | /// only works on inner loops. |
| 2177 | /// |
Eli Bendersky | 86483b3 | 2014-06-11 17:56:26 +0000 | [diff] [blame] | 2178 | /// The unroll and unroll_count directives control the concatenation |
Mark Heffernan | 397a98d | 2015-08-10 17:29:39 +0000 | [diff] [blame] | 2179 | /// unroller. Specifying unroll(enable) instructs llvm to unroll the loop |
| 2180 | /// completely if the trip count is known at compile time and unroll partially |
| 2181 | /// if the trip count is not known. Specifying unroll(full) is similar to |
| 2182 | /// unroll(enable) but will unroll the loop only if the trip count is known at |
| 2183 | /// compile time. Specifying unroll(disable) disables unrolling for the |
| 2184 | /// loop. Specifying unroll_count(_value_) instructs llvm to try to unroll the |
| 2185 | /// loop the number of times indicated by the value. |
Eli Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 2186 | void PragmaLoopHintHandler::HandlePragma(Preprocessor &PP, |
| 2187 | PragmaIntroducerKind Introducer, |
| 2188 | Token &Tok) { |
Mark Heffernan | bd26f5e | 2014-07-21 18:08:34 +0000 | [diff] [blame] | 2189 | // Incoming token is "loop" from "#pragma clang loop". |
| 2190 | Token PragmaName = Tok; |
Eli Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 2191 | SmallVector<Token, 1> TokenList; |
| 2192 | |
| 2193 | // Lex the optimization option and verify it is an identifier. |
| 2194 | PP.Lex(Tok); |
| 2195 | if (Tok.isNot(tok::identifier)) { |
| 2196 | PP.Diag(Tok.getLocation(), diag::err_pragma_loop_invalid_option) |
| 2197 | << /*MissingOption=*/true << ""; |
| 2198 | return; |
| 2199 | } |
| 2200 | |
| 2201 | while (Tok.is(tok::identifier)) { |
| 2202 | Token Option = Tok; |
| 2203 | IdentifierInfo *OptionInfo = Tok.getIdentifierInfo(); |
| 2204 | |
Eli Bendersky | 86483b3 | 2014-06-11 17:56:26 +0000 | [diff] [blame] | 2205 | bool OptionValid = llvm::StringSwitch<bool>(OptionInfo->getName()) |
Mark Heffernan | bd26f5e | 2014-07-21 18:08:34 +0000 | [diff] [blame] | 2206 | .Case("vectorize", true) |
| 2207 | .Case("interleave", true) |
| 2208 | .Case("unroll", true) |
Adam Nemet | 2de463e | 2016-06-14 12:04:26 +0000 | [diff] [blame] | 2209 | .Case("distribute", true) |
Mark Heffernan | bd26f5e | 2014-07-21 18:08:34 +0000 | [diff] [blame] | 2210 | .Case("vectorize_width", true) |
| 2211 | .Case("interleave_count", true) |
| 2212 | .Case("unroll_count", true) |
| 2213 | .Default(false); |
Eli Bendersky | 86483b3 | 2014-06-11 17:56:26 +0000 | [diff] [blame] | 2214 | if (!OptionValid) { |
Eli Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 2215 | PP.Diag(Tok.getLocation(), diag::err_pragma_loop_invalid_option) |
| 2216 | << /*MissingOption=*/false << OptionInfo; |
| 2217 | return; |
| 2218 | } |
NAKAMURA Takumi | db9552f | 2014-07-31 01:52:33 +0000 | [diff] [blame] | 2219 | PP.Lex(Tok); |
Mark Heffernan | bd26f5e | 2014-07-21 18:08:34 +0000 | [diff] [blame] | 2220 | |
Tyler Nowicki | 0c9b34b | 2014-07-31 20:15:14 +0000 | [diff] [blame] | 2221 | // Read '(' |
| 2222 | if (Tok.isNot(tok::l_paren)) { |
| 2223 | PP.Diag(Tok.getLocation(), diag::err_expected) << tok::l_paren; |
NAKAMURA Takumi | db9552f | 2014-07-31 01:52:33 +0000 | [diff] [blame] | 2224 | return; |
| 2225 | } |
Tyler Nowicki | 0c9b34b | 2014-07-31 20:15:14 +0000 | [diff] [blame] | 2226 | PP.Lex(Tok); |
| 2227 | |
| 2228 | auto *Info = new (PP.getPreprocessorAllocator()) PragmaLoopHintInfo; |
| 2229 | if (ParseLoopHintValue(PP, Tok, PragmaName, Option, /*ValueInParens=*/true, |
| 2230 | *Info)) |
| 2231 | return; |
NAKAMURA Takumi | db9552f | 2014-07-31 01:52:33 +0000 | [diff] [blame] | 2232 | |
Mark Heffernan | bd26f5e | 2014-07-21 18:08:34 +0000 | [diff] [blame] | 2233 | // Generate the loop hint token. |
Eli Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 2234 | Token LoopHintTok; |
| 2235 | LoopHintTok.startToken(); |
| 2236 | LoopHintTok.setKind(tok::annot_pragma_loop_hint); |
Mark Heffernan | bd26f5e | 2014-07-21 18:08:34 +0000 | [diff] [blame] | 2237 | LoopHintTok.setLocation(PragmaName.getLocation()); |
David Majnemer | a8f2f1d | 2015-03-19 00:10:23 +0000 | [diff] [blame] | 2238 | LoopHintTok.setAnnotationEndLoc(PragmaName.getLocation()); |
Eli Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 2239 | LoopHintTok.setAnnotationValue(static_cast<void *>(Info)); |
| 2240 | TokenList.push_back(LoopHintTok); |
| 2241 | } |
| 2242 | |
| 2243 | if (Tok.isNot(tok::eod)) { |
| 2244 | PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) |
| 2245 | << "clang loop"; |
| 2246 | return; |
| 2247 | } |
| 2248 | |
David Blaikie | 2eabcc9 | 2016-02-09 18:52:09 +0000 | [diff] [blame] | 2249 | auto TokenArray = llvm::make_unique<Token[]>(TokenList.size()); |
| 2250 | std::copy(TokenList.begin(), TokenList.end(), TokenArray.get()); |
Eli Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 2251 | |
David Blaikie | 2eabcc9 | 2016-02-09 18:52:09 +0000 | [diff] [blame] | 2252 | PP.EnterTokenStream(std::move(TokenArray), TokenList.size(), |
| 2253 | /*DisableMacroExpansion=*/false); |
Eli Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 2254 | } |
Mark Heffernan | bd26f5e | 2014-07-21 18:08:34 +0000 | [diff] [blame] | 2255 | |
| 2256 | /// \brief Handle the loop unroll optimization pragmas. |
| 2257 | /// #pragma unroll |
| 2258 | /// #pragma unroll unroll-hint-value |
| 2259 | /// #pragma unroll '(' unroll-hint-value ')' |
Mark Heffernan | c888e41 | 2014-07-24 18:09:38 +0000 | [diff] [blame] | 2260 | /// #pragma nounroll |
Mark Heffernan | bd26f5e | 2014-07-21 18:08:34 +0000 | [diff] [blame] | 2261 | /// |
| 2262 | /// unroll-hint-value: |
| 2263 | /// constant-expression |
| 2264 | /// |
Mark Heffernan | c888e41 | 2014-07-24 18:09:38 +0000 | [diff] [blame] | 2265 | /// Loop unrolling hints can be specified with '#pragma unroll' or |
| 2266 | /// '#pragma nounroll'. '#pragma unroll' can take a numeric argument optionally |
| 2267 | /// contained in parentheses. With no argument the directive instructs llvm to |
| 2268 | /// try to unroll the loop completely. A positive integer argument can be |
| 2269 | /// specified to indicate the number of times the loop should be unrolled. To |
| 2270 | /// maximize compatibility with other compilers the unroll count argument can be |
| 2271 | /// specified with or without parentheses. Specifying, '#pragma nounroll' |
| 2272 | /// disables unrolling of the loop. |
Mark Heffernan | bd26f5e | 2014-07-21 18:08:34 +0000 | [diff] [blame] | 2273 | void PragmaUnrollHintHandler::HandlePragma(Preprocessor &PP, |
| 2274 | PragmaIntroducerKind Introducer, |
| 2275 | Token &Tok) { |
Mark Heffernan | c888e41 | 2014-07-24 18:09:38 +0000 | [diff] [blame] | 2276 | // Incoming token is "unroll" for "#pragma unroll", or "nounroll" for |
| 2277 | // "#pragma nounroll". |
Mark Heffernan | bd26f5e | 2014-07-21 18:08:34 +0000 | [diff] [blame] | 2278 | Token PragmaName = Tok; |
| 2279 | PP.Lex(Tok); |
| 2280 | auto *Info = new (PP.getPreprocessorAllocator()) PragmaLoopHintInfo; |
| 2281 | if (Tok.is(tok::eod)) { |
Mark Heffernan | c888e41 | 2014-07-24 18:09:38 +0000 | [diff] [blame] | 2282 | // nounroll or unroll pragma without an argument. |
Mark Heffernan | bd26f5e | 2014-07-21 18:08:34 +0000 | [diff] [blame] | 2283 | Info->PragmaName = PragmaName; |
Aaron Ballman | d6807da | 2014-08-01 12:41:37 +0000 | [diff] [blame] | 2284 | Info->Option.startToken(); |
Mark Heffernan | c888e41 | 2014-07-24 18:09:38 +0000 | [diff] [blame] | 2285 | } else if (PragmaName.getIdentifierInfo()->getName() == "nounroll") { |
| 2286 | PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) |
| 2287 | << "nounroll"; |
| 2288 | return; |
Mark Heffernan | bd26f5e | 2014-07-21 18:08:34 +0000 | [diff] [blame] | 2289 | } else { |
| 2290 | // Unroll pragma with an argument: "#pragma unroll N" or |
| 2291 | // "#pragma unroll(N)". |
Tyler Nowicki | 0c9b34b | 2014-07-31 20:15:14 +0000 | [diff] [blame] | 2292 | // Read '(' if it exists. |
| 2293 | bool ValueInParens = Tok.is(tok::l_paren); |
| 2294 | if (ValueInParens) |
| 2295 | PP.Lex(Tok); |
| 2296 | |
Aaron Ballman | d0b090d | 2014-08-01 12:20:20 +0000 | [diff] [blame] | 2297 | Token Option; |
| 2298 | Option.startToken(); |
| 2299 | if (ParseLoopHintValue(PP, Tok, PragmaName, Option, ValueInParens, *Info)) |
Mark Heffernan | bd26f5e | 2014-07-21 18:08:34 +0000 | [diff] [blame] | 2300 | return; |
| 2301 | |
| 2302 | // In CUDA, the argument to '#pragma unroll' should not be contained in |
| 2303 | // parentheses. |
| 2304 | if (PP.getLangOpts().CUDA && ValueInParens) |
Tyler Nowicki | c724a83e | 2014-10-12 20:46:07 +0000 | [diff] [blame] | 2305 | PP.Diag(Info->Toks[0].getLocation(), |
Mark Heffernan | bd26f5e | 2014-07-21 18:08:34 +0000 | [diff] [blame] | 2306 | diag::warn_pragma_unroll_cuda_value_in_parens); |
| 2307 | |
Mark Heffernan | bd26f5e | 2014-07-21 18:08:34 +0000 | [diff] [blame] | 2308 | if (Tok.isNot(tok::eod)) { |
| 2309 | PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) |
| 2310 | << "unroll"; |
| 2311 | return; |
| 2312 | } |
| 2313 | } |
| 2314 | |
| 2315 | // Generate the hint token. |
David Blaikie | 2eabcc9 | 2016-02-09 18:52:09 +0000 | [diff] [blame] | 2316 | auto TokenArray = llvm::make_unique<Token[]>(1); |
Mark Heffernan | bd26f5e | 2014-07-21 18:08:34 +0000 | [diff] [blame] | 2317 | TokenArray[0].startToken(); |
| 2318 | TokenArray[0].setKind(tok::annot_pragma_loop_hint); |
| 2319 | TokenArray[0].setLocation(PragmaName.getLocation()); |
David Majnemer | a8f2f1d | 2015-03-19 00:10:23 +0000 | [diff] [blame] | 2320 | TokenArray[0].setAnnotationEndLoc(PragmaName.getLocation()); |
Mark Heffernan | bd26f5e | 2014-07-21 18:08:34 +0000 | [diff] [blame] | 2321 | TokenArray[0].setAnnotationValue(static_cast<void *>(Info)); |
David Blaikie | 2eabcc9 | 2016-02-09 18:52:09 +0000 | [diff] [blame] | 2322 | PP.EnterTokenStream(std::move(TokenArray), 1, |
| 2323 | /*DisableMacroExpansion=*/false); |
Mark Heffernan | bd26f5e | 2014-07-21 18:08:34 +0000 | [diff] [blame] | 2324 | } |
Reid Kleckner | 3f1ec62 | 2016-09-07 16:38:32 +0000 | [diff] [blame] | 2325 | |
| 2326 | /// \brief Handle the Microsoft \#pragma intrinsic extension. |
| 2327 | /// |
| 2328 | /// The syntax is: |
| 2329 | /// \code |
| 2330 | /// #pragma intrinsic(memset) |
| 2331 | /// #pragma intrinsic(strlen, memcpy) |
| 2332 | /// \endcode |
| 2333 | /// |
| 2334 | /// Pragma intrisic tells the compiler to use a builtin version of the |
| 2335 | /// function. Clang does it anyway, so the pragma doesn't really do anything. |
| 2336 | /// Anyway, we emit a warning if the function specified in \#pragma intrinsic |
| 2337 | /// isn't an intrinsic in clang and suggest to include intrin.h. |
| 2338 | void PragmaMSIntrinsicHandler::HandlePragma(Preprocessor &PP, |
| 2339 | PragmaIntroducerKind Introducer, |
| 2340 | Token &Tok) { |
| 2341 | PP.Lex(Tok); |
| 2342 | |
| 2343 | if (Tok.isNot(tok::l_paren)) { |
| 2344 | PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_lparen) |
| 2345 | << "intrinsic"; |
| 2346 | return; |
| 2347 | } |
| 2348 | PP.Lex(Tok); |
| 2349 | |
| 2350 | bool SuggestIntrinH = !PP.isMacroDefined("__INTRIN_H"); |
| 2351 | |
| 2352 | while (Tok.is(tok::identifier)) { |
| 2353 | IdentifierInfo *II = Tok.getIdentifierInfo(); |
| 2354 | if (!II->getBuiltinID()) |
| 2355 | PP.Diag(Tok.getLocation(), diag::warn_pragma_intrinsic_builtin) |
| 2356 | << II << SuggestIntrinH; |
| 2357 | |
| 2358 | PP.Lex(Tok); |
| 2359 | if (Tok.isNot(tok::comma)) |
| 2360 | break; |
| 2361 | PP.Lex(Tok); |
| 2362 | } |
| 2363 | |
| 2364 | if (Tok.isNot(tok::r_paren)) { |
| 2365 | PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_rparen) |
| 2366 | << "intrinsic"; |
| 2367 | return; |
| 2368 | } |
| 2369 | PP.Lex(Tok); |
| 2370 | |
| 2371 | if (Tok.isNot(tok::eod)) |
| 2372 | PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) |
| 2373 | << "intrinsic"; |
| 2374 | } |
Justin Lebar | 67a78a6 | 2016-10-08 22:15:58 +0000 | [diff] [blame] | 2375 | void PragmaForceCUDAHostDeviceHandler::HandlePragma( |
| 2376 | Preprocessor &PP, PragmaIntroducerKind Introducer, Token &Tok) { |
| 2377 | Token FirstTok = Tok; |
| 2378 | |
| 2379 | PP.Lex(Tok); |
| 2380 | IdentifierInfo *Info = Tok.getIdentifierInfo(); |
| 2381 | if (!Info || (!Info->isStr("begin") && !Info->isStr("end"))) { |
| 2382 | PP.Diag(FirstTok.getLocation(), |
| 2383 | diag::warn_pragma_force_cuda_host_device_bad_arg); |
| 2384 | return; |
| 2385 | } |
| 2386 | |
| 2387 | if (Info->isStr("begin")) |
| 2388 | Actions.PushForceCUDAHostDevice(); |
| 2389 | else if (!Actions.PopForceCUDAHostDevice()) |
| 2390 | PP.Diag(FirstTok.getLocation(), |
| 2391 | diag::err_pragma_cannot_end_force_cuda_host_device); |
| 2392 | |
| 2393 | PP.Lex(Tok); |
| 2394 | if (!Tok.is(tok::eod)) |
| 2395 | PP.Diag(FirstTok.getLocation(), |
| 2396 | diag::warn_pragma_force_cuda_host_device_bad_arg); |
| 2397 | } |