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