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