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