Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 1 | //===--- SemaAttr.cpp - Semantic Analysis for Attributes ------------------===// |
Chris Lattner | 2eccbc1 | 2009-02-17 00:57:29 +0000 | [diff] [blame] | 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 |
Chris Lattner | 2eccbc1 | 2009-02-17 00:57:29 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
Chris Lattner | 31180bb | 2009-02-17 01:09:29 +0000 | [diff] [blame] | 9 | // This file implements semantic analysis for non-trivial attributes and |
| 10 | // pragmas. |
Chris Lattner | 2eccbc1 | 2009-02-17 00:57:29 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 14 | #include "clang/AST/ASTConsumer.h" |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 15 | #include "clang/AST/Attr.h" |
Chris Lattner | 2eccbc1 | 2009-02-17 00:57:29 +0000 | [diff] [blame] | 16 | #include "clang/AST/Expr.h" |
Daniel Dunbar | bd60652 | 2010-05-27 00:35:16 +0000 | [diff] [blame] | 17 | #include "clang/Basic/TargetInfo.h" |
| 18 | #include "clang/Lex/Preprocessor.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 19 | #include "clang/Sema/Lookup.h" |
Mehdi Amini | 9670f84 | 2016-07-18 19:02:11 +0000 | [diff] [blame] | 20 | #include "clang/Sema/SemaInternal.h" |
Chris Lattner | 2eccbc1 | 2009-02-17 00:57:29 +0000 | [diff] [blame] | 21 | using namespace clang; |
| 22 | |
Chris Lattner | 31180bb | 2009-02-17 01:09:29 +0000 | [diff] [blame] | 23 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | 69dac58 | 2010-05-27 00:04:40 +0000 | [diff] [blame] | 24 | // Pragma 'pack' and 'options align' |
Chris Lattner | 31180bb | 2009-02-17 01:09:29 +0000 | [diff] [blame] | 25 | //===----------------------------------------------------------------------===// |
| 26 | |
Denis Zobnin | 2290dac | 2016-04-29 11:27:00 +0000 | [diff] [blame] | 27 | Sema::PragmaStackSentinelRAII::PragmaStackSentinelRAII(Sema &S, |
| 28 | StringRef SlotLabel, |
| 29 | bool ShouldAct) |
| 30 | : S(S), SlotLabel(SlotLabel), ShouldAct(ShouldAct) { |
| 31 | if (ShouldAct) { |
| 32 | S.VtorDispStack.SentinelAction(PSK_Push, SlotLabel); |
| 33 | S.DataSegStack.SentinelAction(PSK_Push, SlotLabel); |
| 34 | S.BSSSegStack.SentinelAction(PSK_Push, SlotLabel); |
| 35 | S.ConstSegStack.SentinelAction(PSK_Push, SlotLabel); |
| 36 | S.CodeSegStack.SentinelAction(PSK_Push, SlotLabel); |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | Sema::PragmaStackSentinelRAII::~PragmaStackSentinelRAII() { |
| 41 | if (ShouldAct) { |
| 42 | S.VtorDispStack.SentinelAction(PSK_Pop, SlotLabel); |
| 43 | S.DataSegStack.SentinelAction(PSK_Pop, SlotLabel); |
| 44 | S.BSSSegStack.SentinelAction(PSK_Pop, SlotLabel); |
| 45 | S.ConstSegStack.SentinelAction(PSK_Pop, SlotLabel); |
| 46 | S.CodeSegStack.SentinelAction(PSK_Pop, SlotLabel); |
| 47 | } |
| 48 | } |
Chris Lattner | 31180bb | 2009-02-17 01:09:29 +0000 | [diff] [blame] | 49 | |
Daniel Dunbar | 8804f2e | 2010-05-27 01:53:40 +0000 | [diff] [blame] | 50 | void Sema::AddAlignmentAttributesForRecord(RecordDecl *RD) { |
Denis Zobnin | 10c4f45 | 2016-04-29 18:17:40 +0000 | [diff] [blame] | 51 | // If there is no pack value, we don't need any attributes. |
| 52 | if (!PackStack.CurrentValue) |
Daniel Dunbar | 8804f2e | 2010-05-27 01:53:40 +0000 | [diff] [blame] | 53 | return; |
| 54 | |
Daniel Dunbar | 8804f2e | 2010-05-27 01:53:40 +0000 | [diff] [blame] | 55 | // Otherwise, check to see if we need a max field alignment attribute. |
Denis Zobnin | 10c4f45 | 2016-04-29 18:17:40 +0000 | [diff] [blame] | 56 | if (unsigned Alignment = PackStack.CurrentValue) { |
| 57 | if (Alignment == Sema::kMac68kAlignmentSentinel) |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 58 | RD->addAttr(AlignMac68kAttr::CreateImplicit(Context)); |
Daniel Dunbar | 6da1098 | 2010-05-27 05:45:51 +0000 | [diff] [blame] | 59 | else |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 60 | RD->addAttr(MaxFieldAlignmentAttr::CreateImplicit(Context, |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 61 | Alignment * 8)); |
Daniel Dunbar | 6da1098 | 2010-05-27 05:45:51 +0000 | [diff] [blame] | 62 | } |
Alex Lorenz | 45b4014 | 2017-07-28 14:41:21 +0000 | [diff] [blame] | 63 | if (PackIncludeStack.empty()) |
| 64 | return; |
| 65 | // The #pragma pack affected a record in an included file, so Clang should |
| 66 | // warn when that pragma was written in a file that included the included |
| 67 | // file. |
| 68 | for (auto &PackedInclude : llvm::reverse(PackIncludeStack)) { |
| 69 | if (PackedInclude.CurrentPragmaLocation != PackStack.CurrentPragmaLocation) |
| 70 | break; |
| 71 | if (PackedInclude.HasNonDefaultValue) |
| 72 | PackedInclude.ShouldWarnOnInclude = true; |
| 73 | } |
Chris Lattner | 31180bb | 2009-02-17 01:09:29 +0000 | [diff] [blame] | 74 | } |
| 75 | |
Fariborz Jahanian | 6b4e26b | 2011-04-26 17:54:40 +0000 | [diff] [blame] | 76 | void Sema::AddMsStructLayoutForRecord(RecordDecl *RD) { |
Reid Kleckner | c0dca6d | 2014-02-12 23:50:26 +0000 | [diff] [blame] | 77 | if (MSStructPragmaOn) |
David Majnemer | 8ab003a | 2015-02-02 19:30:52 +0000 | [diff] [blame] | 78 | RD->addAttr(MSStructAttr::CreateImplicit(Context)); |
Reid Kleckner | c0dca6d | 2014-02-12 23:50:26 +0000 | [diff] [blame] | 79 | |
| 80 | // FIXME: We should merge AddAlignmentAttributesForRecord with |
| 81 | // AddMsStructLayoutForRecord into AddPragmaAttributesForRecord, which takes |
| 82 | // all active pragmas and applies them as attributes to class definitions. |
Reid Kleckner | 2692eb0 | 2019-11-22 14:55:49 -0800 | [diff] [blame] | 83 | if (VtorDispStack.CurrentValue != getLangOpts().getVtorDispMode()) |
| 84 | RD->addAttr(MSVtorDispAttr::CreateImplicit( |
| 85 | Context, unsigned(VtorDispStack.CurrentValue))); |
Fariborz Jahanian | 6b4e26b | 2011-04-26 17:54:40 +0000 | [diff] [blame] | 86 | } |
| 87 | |
Matthias Gehre | 23092ca | 2019-08-07 10:45:36 +0000 | [diff] [blame] | 88 | template <typename Attribute> |
| 89 | static void addGslOwnerPointerAttributeIfNotExisting(ASTContext &Context, |
| 90 | CXXRecordDecl *Record) { |
Matthias Gehre | f64f488 | 2019-09-06 08:56:30 +0000 | [diff] [blame] | 91 | if (Record->hasAttr<OwnerAttr>() || Record->hasAttr<PointerAttr>()) |
Matthias Gehre | 23092ca | 2019-08-07 10:45:36 +0000 | [diff] [blame] | 92 | return; |
| 93 | |
Matthias Gehre | f64f488 | 2019-09-06 08:56:30 +0000 | [diff] [blame] | 94 | for (Decl *Redecl : Record->redecls()) |
| 95 | Redecl->addAttr(Attribute::CreateImplicit(Context, /*DerefType=*/nullptr)); |
Matthias Gehre | 23092ca | 2019-08-07 10:45:36 +0000 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | void Sema::inferGslPointerAttribute(NamedDecl *ND, |
| 99 | CXXRecordDecl *UnderlyingRecord) { |
| 100 | if (!UnderlyingRecord) |
| 101 | return; |
| 102 | |
| 103 | const auto *Parent = dyn_cast<CXXRecordDecl>(ND->getDeclContext()); |
| 104 | if (!Parent) |
| 105 | return; |
| 106 | |
| 107 | static llvm::StringSet<> Containers{ |
| 108 | "array", |
| 109 | "basic_string", |
| 110 | "deque", |
| 111 | "forward_list", |
| 112 | "vector", |
| 113 | "list", |
| 114 | "map", |
| 115 | "multiset", |
| 116 | "multimap", |
| 117 | "priority_queue", |
| 118 | "queue", |
| 119 | "set", |
| 120 | "stack", |
| 121 | "unordered_set", |
| 122 | "unordered_map", |
| 123 | "unordered_multiset", |
| 124 | "unordered_multimap", |
| 125 | }; |
| 126 | |
| 127 | static llvm::StringSet<> Iterators{"iterator", "const_iterator", |
| 128 | "reverse_iterator", |
| 129 | "const_reverse_iterator"}; |
| 130 | |
| 131 | if (Parent->isInStdNamespace() && Iterators.count(ND->getName()) && |
| 132 | Containers.count(Parent->getName())) |
| 133 | addGslOwnerPointerAttributeIfNotExisting<PointerAttr>(Context, |
| 134 | UnderlyingRecord); |
| 135 | } |
| 136 | |
| 137 | void Sema::inferGslPointerAttribute(TypedefNameDecl *TD) { |
| 138 | |
| 139 | QualType Canonical = TD->getUnderlyingType().getCanonicalType(); |
| 140 | |
| 141 | CXXRecordDecl *RD = Canonical->getAsCXXRecordDecl(); |
| 142 | if (!RD) { |
| 143 | if (auto *TST = |
| 144 | dyn_cast<TemplateSpecializationType>(Canonical.getTypePtr())) { |
| 145 | |
| 146 | RD = dyn_cast_or_null<CXXRecordDecl>( |
| 147 | TST->getTemplateName().getAsTemplateDecl()->getTemplatedDecl()); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | inferGslPointerAttribute(TD, RD); |
| 152 | } |
| 153 | |
| 154 | void Sema::inferGslOwnerPointerAttribute(CXXRecordDecl *Record) { |
| 155 | static llvm::StringSet<> StdOwners{ |
| 156 | "any", |
| 157 | "array", |
| 158 | "basic_regex", |
| 159 | "basic_string", |
| 160 | "deque", |
| 161 | "forward_list", |
| 162 | "vector", |
| 163 | "list", |
| 164 | "map", |
| 165 | "multiset", |
| 166 | "multimap", |
| 167 | "optional", |
| 168 | "priority_queue", |
| 169 | "queue", |
| 170 | "set", |
| 171 | "stack", |
| 172 | "unique_ptr", |
| 173 | "unordered_set", |
| 174 | "unordered_map", |
| 175 | "unordered_multiset", |
| 176 | "unordered_multimap", |
| 177 | "variant", |
| 178 | }; |
| 179 | static llvm::StringSet<> StdPointers{ |
| 180 | "basic_string_view", |
| 181 | "reference_wrapper", |
| 182 | "regex_iterator", |
| 183 | }; |
| 184 | |
| 185 | if (!Record->getIdentifier()) |
| 186 | return; |
| 187 | |
| 188 | // Handle classes that directly appear in std namespace. |
| 189 | if (Record->isInStdNamespace()) { |
Matthias Gehre | f64f488 | 2019-09-06 08:56:30 +0000 | [diff] [blame] | 190 | if (Record->hasAttr<OwnerAttr>() || Record->hasAttr<PointerAttr>()) |
Matthias Gehre | 23092ca | 2019-08-07 10:45:36 +0000 | [diff] [blame] | 191 | return; |
| 192 | |
| 193 | if (StdOwners.count(Record->getName())) |
| 194 | addGslOwnerPointerAttributeIfNotExisting<OwnerAttr>(Context, Record); |
| 195 | else if (StdPointers.count(Record->getName())) |
| 196 | addGslOwnerPointerAttributeIfNotExisting<PointerAttr>(Context, Record); |
| 197 | |
| 198 | return; |
| 199 | } |
| 200 | |
| 201 | // Handle nested classes that could be a gsl::Pointer. |
| 202 | inferGslPointerAttribute(Record, Record); |
| 203 | } |
| 204 | |
Daniel Dunbar | 69dac58 | 2010-05-27 00:04:40 +0000 | [diff] [blame] | 205 | void Sema::ActOnPragmaOptionsAlign(PragmaOptionsAlignKind Kind, |
Eli Friedman | 68be164 | 2012-10-04 02:36:51 +0000 | [diff] [blame] | 206 | SourceLocation PragmaLoc) { |
Denis Zobnin | 10c4f45 | 2016-04-29 18:17:40 +0000 | [diff] [blame] | 207 | PragmaMsStackAction Action = Sema::PSK_Reset; |
Denis Zobnin | 3f287c2 | 2016-04-29 22:50:16 +0000 | [diff] [blame] | 208 | unsigned Alignment = 0; |
Daniel Dunbar | 69dac58 | 2010-05-27 00:04:40 +0000 | [diff] [blame] | 209 | switch (Kind) { |
Daniel Dunbar | 663e809 | 2010-05-27 18:42:09 +0000 | [diff] [blame] | 210 | // For all targets we support native and natural are the same. |
| 211 | // |
| 212 | // FIXME: This is not true on Darwin/PPC. |
| 213 | case POAK_Native: |
Daniel Dunbar | 5794c6f | 2010-05-28 19:43:33 +0000 | [diff] [blame] | 214 | case POAK_Power: |
Daniel Dunbar | a688566 | 2010-05-28 20:08:00 +0000 | [diff] [blame] | 215 | case POAK_Natural: |
Denis Zobnin | 10c4f45 | 2016-04-29 18:17:40 +0000 | [diff] [blame] | 216 | Action = Sema::PSK_Push_Set; |
| 217 | Alignment = 0; |
Daniel Dunbar | 5794c6f | 2010-05-28 19:43:33 +0000 | [diff] [blame] | 218 | break; |
| 219 | |
Daniel Dunbar | 9c84d4a | 2010-05-27 18:42:17 +0000 | [diff] [blame] | 220 | // Note that '#pragma options align=packed' is not equivalent to attribute |
| 221 | // packed, it has a different precedence relative to attribute aligned. |
| 222 | case POAK_Packed: |
Denis Zobnin | 10c4f45 | 2016-04-29 18:17:40 +0000 | [diff] [blame] | 223 | Action = Sema::PSK_Push_Set; |
| 224 | Alignment = 1; |
Daniel Dunbar | 9c84d4a | 2010-05-27 18:42:17 +0000 | [diff] [blame] | 225 | break; |
| 226 | |
Daniel Dunbar | bd60652 | 2010-05-27 00:35:16 +0000 | [diff] [blame] | 227 | case POAK_Mac68k: |
| 228 | // Check if the target supports this. |
Alp Toker | b6cc592 | 2014-05-03 03:45:55 +0000 | [diff] [blame] | 229 | if (!this->Context.getTargetInfo().hasAlignMac68kSupport()) { |
Daniel Dunbar | bd60652 | 2010-05-27 00:35:16 +0000 | [diff] [blame] | 230 | Diag(PragmaLoc, diag::err_pragma_options_align_mac68k_target_unsupported); |
| 231 | return; |
Daniel Dunbar | bd60652 | 2010-05-27 00:35:16 +0000 | [diff] [blame] | 232 | } |
Denis Zobnin | 10c4f45 | 2016-04-29 18:17:40 +0000 | [diff] [blame] | 233 | Action = Sema::PSK_Push_Set; |
| 234 | Alignment = Sema::kMac68kAlignmentSentinel; |
Daniel Dunbar | bd60652 | 2010-05-27 00:35:16 +0000 | [diff] [blame] | 235 | break; |
| 236 | |
Eli Friedman | 68be164 | 2012-10-04 02:36:51 +0000 | [diff] [blame] | 237 | case POAK_Reset: |
| 238 | // Reset just pops the top of the stack, or resets the current alignment to |
| 239 | // default. |
Denis Zobnin | 10c4f45 | 2016-04-29 18:17:40 +0000 | [diff] [blame] | 240 | Action = Sema::PSK_Pop; |
| 241 | if (PackStack.Stack.empty()) { |
| 242 | if (PackStack.CurrentValue) { |
| 243 | Action = Sema::PSK_Reset; |
| 244 | } else { |
| 245 | Diag(PragmaLoc, diag::warn_pragma_options_align_reset_failed) |
| 246 | << "stack empty"; |
| 247 | return; |
| 248 | } |
Eli Friedman | 68be164 | 2012-10-04 02:36:51 +0000 | [diff] [blame] | 249 | } |
Daniel Dunbar | 69dac58 | 2010-05-27 00:04:40 +0000 | [diff] [blame] | 250 | break; |
| 251 | } |
Denis Zobnin | 10c4f45 | 2016-04-29 18:17:40 +0000 | [diff] [blame] | 252 | |
| 253 | PackStack.Act(PragmaLoc, Action, StringRef(), Alignment); |
Daniel Dunbar | 69dac58 | 2010-05-27 00:04:40 +0000 | [diff] [blame] | 254 | } |
| 255 | |
Javed Absar | 2a67c9e | 2017-06-05 10:11:57 +0000 | [diff] [blame] | 256 | void Sema::ActOnPragmaClangSection(SourceLocation PragmaLoc, PragmaClangSectionAction Action, |
| 257 | PragmaClangSectionKind SecKind, StringRef SecName) { |
| 258 | PragmaClangSection *CSec; |
Lucas Prates | 0dac639 | 2020-04-16 13:55:18 +0100 | [diff] [blame] | 259 | int SectionFlags = ASTContext::PSF_Read; |
Javed Absar | 2a67c9e | 2017-06-05 10:11:57 +0000 | [diff] [blame] | 260 | switch (SecKind) { |
| 261 | case PragmaClangSectionKind::PCSK_BSS: |
| 262 | CSec = &PragmaClangBSSSection; |
Lucas Prates | 0dac639 | 2020-04-16 13:55:18 +0100 | [diff] [blame] | 263 | SectionFlags |= ASTContext::PSF_Write | ASTContext::PSF_ZeroInit; |
Javed Absar | 2a67c9e | 2017-06-05 10:11:57 +0000 | [diff] [blame] | 264 | break; |
| 265 | case PragmaClangSectionKind::PCSK_Data: |
| 266 | CSec = &PragmaClangDataSection; |
Lucas Prates | 0dac639 | 2020-04-16 13:55:18 +0100 | [diff] [blame] | 267 | SectionFlags |= ASTContext::PSF_Write; |
Javed Absar | 2a67c9e | 2017-06-05 10:11:57 +0000 | [diff] [blame] | 268 | break; |
| 269 | case PragmaClangSectionKind::PCSK_Rodata: |
| 270 | CSec = &PragmaClangRodataSection; |
| 271 | break; |
Dmitry Mikulin | f14642f | 2019-10-15 18:31:10 +0000 | [diff] [blame] | 272 | case PragmaClangSectionKind::PCSK_Relro: |
| 273 | CSec = &PragmaClangRelroSection; |
| 274 | break; |
Javed Absar | 2a67c9e | 2017-06-05 10:11:57 +0000 | [diff] [blame] | 275 | case PragmaClangSectionKind::PCSK_Text: |
| 276 | CSec = &PragmaClangTextSection; |
Lucas Prates | 0dac639 | 2020-04-16 13:55:18 +0100 | [diff] [blame] | 277 | SectionFlags |= ASTContext::PSF_Execute; |
Javed Absar | 2a67c9e | 2017-06-05 10:11:57 +0000 | [diff] [blame] | 278 | break; |
| 279 | default: |
| 280 | llvm_unreachable("invalid clang section kind"); |
| 281 | } |
| 282 | |
| 283 | if (Action == PragmaClangSectionAction::PCSA_Clear) { |
| 284 | CSec->Valid = false; |
| 285 | return; |
| 286 | } |
| 287 | |
Lucas Prates | 0dac639 | 2020-04-16 13:55:18 +0100 | [diff] [blame] | 288 | if (UnifySection(SecName, SectionFlags, PragmaLoc)) |
| 289 | return; |
| 290 | |
Javed Absar | 2a67c9e | 2017-06-05 10:11:57 +0000 | [diff] [blame] | 291 | CSec->Valid = true; |
Benjamin Kramer | adcd026 | 2020-01-28 20:23:46 +0100 | [diff] [blame] | 292 | CSec->SectionName = std::string(SecName); |
Javed Absar | 2a67c9e | 2017-06-05 10:11:57 +0000 | [diff] [blame] | 293 | CSec->PragmaLocation = PragmaLoc; |
| 294 | } |
| 295 | |
Denis Zobnin | 10c4f45 | 2016-04-29 18:17:40 +0000 | [diff] [blame] | 296 | void Sema::ActOnPragmaPack(SourceLocation PragmaLoc, PragmaMsStackAction Action, |
| 297 | StringRef SlotLabel, Expr *alignment) { |
Chris Lattner | 2eccbc1 | 2009-02-17 00:57:29 +0000 | [diff] [blame] | 298 | Expr *Alignment = static_cast<Expr *>(alignment); |
| 299 | |
| 300 | // If specified then alignment must be a "small" power of two. |
| 301 | unsigned AlignmentVal = 0; |
| 302 | if (Alignment) { |
| 303 | llvm::APSInt Val; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 304 | |
Daniel Dunbar | e03c610 | 2009-03-06 20:45:54 +0000 | [diff] [blame] | 305 | // pack(0) is like pack(), which just works out since that is what |
| 306 | // we use 0 for in PackAttr. |
Douglas Gregor | bdb604a | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 307 | if (Alignment->isTypeDependent() || |
| 308 | Alignment->isValueDependent() || |
| 309 | !Alignment->isIntegerConstantExpr(Val, Context) || |
Daniel Dunbar | e03c610 | 2009-03-06 20:45:54 +0000 | [diff] [blame] | 310 | !(Val == 0 || Val.isPowerOf2()) || |
Chris Lattner | 2eccbc1 | 2009-02-17 00:57:29 +0000 | [diff] [blame] | 311 | Val.getZExtValue() > 16) { |
| 312 | Diag(PragmaLoc, diag::warn_pragma_pack_invalid_alignment); |
Chris Lattner | 2eccbc1 | 2009-02-17 00:57:29 +0000 | [diff] [blame] | 313 | return; // Ignore |
| 314 | } |
| 315 | |
| 316 | AlignmentVal = (unsigned) Val.getZExtValue(); |
| 317 | } |
Denis Zobnin | 10c4f45 | 2016-04-29 18:17:40 +0000 | [diff] [blame] | 318 | if (Action == Sema::PSK_Show) { |
Chris Lattner | 2eccbc1 | 2009-02-17 00:57:29 +0000 | [diff] [blame] | 319 | // Show the current alignment, making sure to show the right value |
| 320 | // for the default. |
Chris Lattner | 2eccbc1 | 2009-02-17 00:57:29 +0000 | [diff] [blame] | 321 | // FIXME: This should come from the target. |
Denis Zobnin | 10c4f45 | 2016-04-29 18:17:40 +0000 | [diff] [blame] | 322 | AlignmentVal = PackStack.CurrentValue; |
Chris Lattner | 2eccbc1 | 2009-02-17 00:57:29 +0000 | [diff] [blame] | 323 | if (AlignmentVal == 0) |
| 324 | AlignmentVal = 8; |
Denis Zobnin | 10c4f45 | 2016-04-29 18:17:40 +0000 | [diff] [blame] | 325 | if (AlignmentVal == Sema::kMac68kAlignmentSentinel) |
Daniel Dunbar | 6da1098 | 2010-05-27 05:45:51 +0000 | [diff] [blame] | 326 | Diag(PragmaLoc, diag::warn_pragma_pack_show) << "mac68k"; |
| 327 | else |
| 328 | Diag(PragmaLoc, diag::warn_pragma_pack_show) << AlignmentVal; |
Chris Lattner | 2eccbc1 | 2009-02-17 00:57:29 +0000 | [diff] [blame] | 329 | } |
Denis Zobnin | 10c4f45 | 2016-04-29 18:17:40 +0000 | [diff] [blame] | 330 | // MSDN, C/C++ Preprocessor Reference > Pragma Directives > pack: |
| 331 | // "#pragma pack(pop, identifier, n) is undefined" |
| 332 | if (Action & Sema::PSK_Pop) { |
| 333 | if (Alignment && !SlotLabel.empty()) |
Alexander Kornienko | 2a8c18d | 2018-04-06 15:14:32 +0000 | [diff] [blame] | 334 | Diag(PragmaLoc, diag::warn_pragma_pack_pop_identifier_and_alignment); |
Denis Zobnin | 10c4f45 | 2016-04-29 18:17:40 +0000 | [diff] [blame] | 335 | if (PackStack.Stack.empty()) |
| 336 | Diag(PragmaLoc, diag::warn_pragma_pop_failed) << "pack" << "stack empty"; |
| 337 | } |
| 338 | |
| 339 | PackStack.Act(PragmaLoc, Action, SlotLabel, AlignmentVal); |
Chris Lattner | 2eccbc1 | 2009-02-17 00:57:29 +0000 | [diff] [blame] | 340 | } |
| 341 | |
Alex Lorenz | 45b4014 | 2017-07-28 14:41:21 +0000 | [diff] [blame] | 342 | void Sema::DiagnoseNonDefaultPragmaPack(PragmaPackDiagnoseKind Kind, |
| 343 | SourceLocation IncludeLoc) { |
| 344 | if (Kind == PragmaPackDiagnoseKind::NonDefaultStateAtInclude) { |
| 345 | SourceLocation PrevLocation = PackStack.CurrentPragmaLocation; |
| 346 | // Warn about non-default alignment at #includes (without redundant |
| 347 | // warnings for the same directive in nested includes). |
| 348 | // The warning is delayed until the end of the file to avoid warnings |
| 349 | // for files that don't have any records that are affected by the modified |
| 350 | // alignment. |
| 351 | bool HasNonDefaultValue = |
| 352 | PackStack.hasValue() && |
| 353 | (PackIncludeStack.empty() || |
| 354 | PackIncludeStack.back().CurrentPragmaLocation != PrevLocation); |
| 355 | PackIncludeStack.push_back( |
| 356 | {PackStack.CurrentValue, |
| 357 | PackStack.hasValue() ? PrevLocation : SourceLocation(), |
| 358 | HasNonDefaultValue, /*ShouldWarnOnInclude*/ false}); |
| 359 | return; |
| 360 | } |
| 361 | |
| 362 | assert(Kind == PragmaPackDiagnoseKind::ChangedStateAtExit && "invalid kind"); |
| 363 | PackIncludeState PrevPackState = PackIncludeStack.pop_back_val(); |
| 364 | if (PrevPackState.ShouldWarnOnInclude) { |
| 365 | // Emit the delayed non-default alignment at #include warning. |
| 366 | Diag(IncludeLoc, diag::warn_pragma_pack_non_default_at_include); |
| 367 | Diag(PrevPackState.CurrentPragmaLocation, diag::note_pragma_pack_here); |
| 368 | } |
| 369 | // Warn about modified alignment after #includes. |
| 370 | if (PrevPackState.CurrentValue != PackStack.CurrentValue) { |
| 371 | Diag(IncludeLoc, diag::warn_pragma_pack_modified_after_include); |
| 372 | Diag(PackStack.CurrentPragmaLocation, diag::note_pragma_pack_here); |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | void Sema::DiagnoseUnterminatedPragmaPack() { |
| 377 | if (PackStack.Stack.empty()) |
| 378 | return; |
Alex Lorenz | a1479d7 | 2017-07-31 13:37:50 +0000 | [diff] [blame] | 379 | bool IsInnermost = true; |
| 380 | for (const auto &StackSlot : llvm::reverse(PackStack.Stack)) { |
Alex Lorenz | 45b4014 | 2017-07-28 14:41:21 +0000 | [diff] [blame] | 381 | Diag(StackSlot.PragmaPushLocation, diag::warn_pragma_pack_no_pop_eof); |
Alex Lorenz | a1479d7 | 2017-07-31 13:37:50 +0000 | [diff] [blame] | 382 | // The user might have already reset the alignment, so suggest replacing |
| 383 | // the reset with a pop. |
| 384 | if (IsInnermost && PackStack.CurrentValue == PackStack.DefaultValue) { |
| 385 | DiagnosticBuilder DB = Diag(PackStack.CurrentPragmaLocation, |
| 386 | diag::note_pragma_pack_pop_instead_reset); |
| 387 | SourceLocation FixItLoc = Lexer::findLocationAfterToken( |
| 388 | PackStack.CurrentPragmaLocation, tok::l_paren, SourceMgr, LangOpts, |
| 389 | /*SkipTrailing=*/false); |
| 390 | if (FixItLoc.isValid()) |
| 391 | DB << FixItHint::CreateInsertion(FixItLoc, "pop"); |
| 392 | } |
| 393 | IsInnermost = false; |
| 394 | } |
Alex Lorenz | 45b4014 | 2017-07-28 14:41:21 +0000 | [diff] [blame] | 395 | } |
| 396 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 397 | void Sema::ActOnPragmaMSStruct(PragmaMSStructKind Kind) { |
Fariborz Jahanian | 743dda4 | 2011-04-25 18:49:15 +0000 | [diff] [blame] | 398 | MSStructPragmaOn = (Kind == PMSST_ON); |
| 399 | } |
| 400 | |
Nico Weber | 6622029 | 2016-03-02 17:28:48 +0000 | [diff] [blame] | 401 | void Sema::ActOnPragmaMSComment(SourceLocation CommentLoc, |
| 402 | PragmaMSCommentKind Kind, StringRef Arg) { |
| 403 | auto *PCD = PragmaCommentDecl::Create( |
| 404 | Context, Context.getTranslationUnitDecl(), CommentLoc, Kind, Arg); |
| 405 | Context.getTranslationUnitDecl()->addDecl(PCD); |
| 406 | Consumer.HandleTopLevelDecl(DeclGroupRef(PCD)); |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 407 | } |
| 408 | |
Nico Weber | cbbaeb1 | 2016-03-02 19:28:54 +0000 | [diff] [blame] | 409 | void Sema::ActOnPragmaDetectMismatch(SourceLocation Loc, StringRef Name, |
| 410 | StringRef Value) { |
| 411 | auto *PDMD = PragmaDetectMismatchDecl::Create( |
| 412 | Context, Context.getTranslationUnitDecl(), Loc, Name, Value); |
| 413 | Context.getTranslationUnitDecl()->addDecl(PDMD); |
| 414 | Consumer.HandleTopLevelDecl(DeclGroupRef(PDMD)); |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 415 | } |
| 416 | |
Melanie Blower | f5360d4 | 2020-05-01 10:32:06 -0700 | [diff] [blame] | 417 | void Sema::ActOnPragmaFloatControl(SourceLocation Loc, |
| 418 | PragmaMsStackAction Action, |
| 419 | PragmaFloatControlKind Value) { |
Melanie Blower | defd43a | 2020-06-26 08:45:12 -0700 | [diff] [blame^] | 420 | auto NewValue = FpPragmaStack.CurrentValue; |
| 421 | FPOptions NewFPFeatures(NewValue); |
Melanie Blower | f5360d4 | 2020-05-01 10:32:06 -0700 | [diff] [blame] | 422 | if ((Action == PSK_Push_Set || Action == PSK_Push || Action == PSK_Pop) && |
Melanie Blower | 7f2db99 | 2020-05-08 08:05:34 -0700 | [diff] [blame] | 423 | !(CurContext->isTranslationUnit()) && !CurContext->isNamespace()) { |
| 424 | // Push and pop can only occur at file or namespace scope. |
Melanie Blower | f5360d4 | 2020-05-01 10:32:06 -0700 | [diff] [blame] | 425 | Diag(Loc, diag::err_pragma_fc_pp_scope); |
| 426 | return; |
| 427 | } |
| 428 | switch (Value) { |
| 429 | default: |
| 430 | llvm_unreachable("invalid pragma float_control kind"); |
| 431 | case PFC_Precise: |
Melanie Blower | defd43a | 2020-06-26 08:45:12 -0700 | [diff] [blame^] | 432 | CurFPFeatures.setFPPreciseEnabled(true); |
| 433 | NewValue = CurFPFeatures.getAsOpaqueInt(); |
Melanie Blower | f5360d4 | 2020-05-01 10:32:06 -0700 | [diff] [blame] | 434 | FpPragmaStack.Act(Loc, Action, StringRef(), NewValue); |
| 435 | break; |
| 436 | case PFC_NoPrecise: |
Melanie Blower | defd43a | 2020-06-26 08:45:12 -0700 | [diff] [blame^] | 437 | if (CurFPFeatures.getExceptionMode() == LangOptions::FPE_Strict) |
Melanie Blower | f5360d4 | 2020-05-01 10:32:06 -0700 | [diff] [blame] | 438 | Diag(Loc, diag::err_pragma_fc_noprecise_requires_noexcept); |
Melanie Blower | defd43a | 2020-06-26 08:45:12 -0700 | [diff] [blame^] | 439 | else if (CurFPFeatures.allowFEnvAccess()) |
Melanie Blower | f5360d4 | 2020-05-01 10:32:06 -0700 | [diff] [blame] | 440 | Diag(Loc, diag::err_pragma_fc_noprecise_requires_nofenv); |
| 441 | else |
Melanie Blower | defd43a | 2020-06-26 08:45:12 -0700 | [diff] [blame^] | 442 | CurFPFeatures.setFPPreciseEnabled(false); |
| 443 | NewValue = CurFPFeatures.getAsOpaqueInt(); |
Melanie Blower | f5360d4 | 2020-05-01 10:32:06 -0700 | [diff] [blame] | 444 | FpPragmaStack.Act(Loc, Action, StringRef(), NewValue); |
| 445 | break; |
| 446 | case PFC_Except: |
| 447 | if (!isPreciseFPEnabled()) |
| 448 | Diag(Loc, diag::err_pragma_fc_except_requires_precise); |
| 449 | else |
Melanie Blower | defd43a | 2020-06-26 08:45:12 -0700 | [diff] [blame^] | 450 | CurFPFeatures.setExceptionMode(LangOptions::FPE_Strict); |
| 451 | NewValue = CurFPFeatures.getAsOpaqueInt(); |
Melanie Blower | f5360d4 | 2020-05-01 10:32:06 -0700 | [diff] [blame] | 452 | FpPragmaStack.Act(Loc, Action, StringRef(), NewValue); |
| 453 | break; |
| 454 | case PFC_NoExcept: |
Melanie Blower | defd43a | 2020-06-26 08:45:12 -0700 | [diff] [blame^] | 455 | CurFPFeatures.setExceptionMode(LangOptions::FPE_Ignore); |
| 456 | NewValue = CurFPFeatures.getAsOpaqueInt(); |
Melanie Blower | f5360d4 | 2020-05-01 10:32:06 -0700 | [diff] [blame] | 457 | FpPragmaStack.Act(Loc, Action, StringRef(), NewValue); |
| 458 | break; |
| 459 | case PFC_Push: |
Melanie Blower | defd43a | 2020-06-26 08:45:12 -0700 | [diff] [blame^] | 460 | if (FpPragmaStack.Stack.empty()) { |
| 461 | FpPragmaStack.Act(Loc, Sema::PSK_Set, StringRef(), |
| 462 | CurFPFeatures.getAsOpaqueInt()); |
| 463 | } |
Melanie Blower | 7b8e306 | 2020-05-14 05:56:35 -0700 | [diff] [blame] | 464 | FpPragmaStack.Act(Loc, Sema::PSK_Push_Set, StringRef(), |
| 465 | NewFPFeatures.getAsOpaqueInt()); |
Melanie Blower | f5360d4 | 2020-05-01 10:32:06 -0700 | [diff] [blame] | 466 | break; |
| 467 | case PFC_Pop: |
| 468 | if (FpPragmaStack.Stack.empty()) { |
| 469 | Diag(Loc, diag::warn_pragma_pop_failed) << "float_control" |
| 470 | << "stack empty"; |
| 471 | return; |
| 472 | } |
| 473 | FpPragmaStack.Act(Loc, Action, StringRef(), NewFPFeatures.getAsOpaqueInt()); |
| 474 | NewValue = FpPragmaStack.CurrentValue; |
Melanie Blower | defd43a | 2020-06-26 08:45:12 -0700 | [diff] [blame^] | 475 | CurFPFeatures.getFromOpaqueInt(NewValue); |
Melanie Blower | f5360d4 | 2020-05-01 10:32:06 -0700 | [diff] [blame] | 476 | break; |
| 477 | } |
| 478 | } |
| 479 | |
David Majnemer | 4bb0980 | 2014-02-10 19:50:15 +0000 | [diff] [blame] | 480 | void Sema::ActOnPragmaMSPointersToMembers( |
David Majnemer | 86c318f | 2014-02-11 21:05:00 +0000 | [diff] [blame] | 481 | LangOptions::PragmaMSPointersToMembersKind RepresentationMethod, |
David Majnemer | 4bb0980 | 2014-02-10 19:50:15 +0000 | [diff] [blame] | 482 | SourceLocation PragmaLoc) { |
| 483 | MSPointerToMemberRepresentationMethod = RepresentationMethod; |
| 484 | ImplicitMSInheritanceAttrLoc = PragmaLoc; |
| 485 | } |
| 486 | |
Denis Zobnin | 2290dac | 2016-04-29 11:27:00 +0000 | [diff] [blame] | 487 | void Sema::ActOnPragmaMSVtorDisp(PragmaMsStackAction Action, |
Reid Kleckner | c0dca6d | 2014-02-12 23:50:26 +0000 | [diff] [blame] | 488 | SourceLocation PragmaLoc, |
Reid Kleckner | 2692eb0 | 2019-11-22 14:55:49 -0800 | [diff] [blame] | 489 | MSVtorDispMode Mode) { |
Denis Zobnin | 2290dac | 2016-04-29 11:27:00 +0000 | [diff] [blame] | 490 | if (Action & PSK_Pop && VtorDispStack.Stack.empty()) |
| 491 | Diag(PragmaLoc, diag::warn_pragma_pop_failed) << "vtordisp" |
| 492 | << "stack empty"; |
| 493 | VtorDispStack.Act(PragmaLoc, Action, StringRef(), Mode); |
Reid Kleckner | c0dca6d | 2014-02-12 23:50:26 +0000 | [diff] [blame] | 494 | } |
| 495 | |
Warren Hunt | c3b1896 | 2014-04-08 22:30:47 +0000 | [diff] [blame] | 496 | template<typename ValueType> |
| 497 | void Sema::PragmaStack<ValueType>::Act(SourceLocation PragmaLocation, |
| 498 | PragmaMsStackAction Action, |
| 499 | llvm::StringRef StackSlotLabel, |
| 500 | ValueType Value) { |
| 501 | if (Action == PSK_Reset) { |
Denis Zobnin | 2290dac | 2016-04-29 11:27:00 +0000 | [diff] [blame] | 502 | CurrentValue = DefaultValue; |
Alex Lorenz | 7d7e1e0 | 2017-03-31 15:36:21 +0000 | [diff] [blame] | 503 | CurrentPragmaLocation = PragmaLocation; |
Warren Hunt | c3b1896 | 2014-04-08 22:30:47 +0000 | [diff] [blame] | 504 | return; |
| 505 | } |
| 506 | if (Action & PSK_Push) |
Alex Lorenz | 45b4014 | 2017-07-28 14:41:21 +0000 | [diff] [blame] | 507 | Stack.emplace_back(StackSlotLabel, CurrentValue, CurrentPragmaLocation, |
| 508 | PragmaLocation); |
Warren Hunt | c3b1896 | 2014-04-08 22:30:47 +0000 | [diff] [blame] | 509 | else if (Action & PSK_Pop) { |
| 510 | if (!StackSlotLabel.empty()) { |
| 511 | // If we've got a label, try to find it and jump there. |
David Majnemer | f7e3609 | 2016-06-23 00:15:04 +0000 | [diff] [blame] | 512 | auto I = llvm::find_if(llvm::reverse(Stack), [&](const Slot &x) { |
| 513 | return x.StackSlotLabel == StackSlotLabel; |
| 514 | }); |
Warren Hunt | c3b1896 | 2014-04-08 22:30:47 +0000 | [diff] [blame] | 515 | // If we found the label so pop from there. |
| 516 | if (I != Stack.rend()) { |
| 517 | CurrentValue = I->Value; |
| 518 | CurrentPragmaLocation = I->PragmaLocation; |
| 519 | Stack.erase(std::prev(I.base()), Stack.end()); |
| 520 | } |
| 521 | } else if (!Stack.empty()) { |
George Burgess IV | a75c71d | 2018-05-26 02:29:14 +0000 | [diff] [blame] | 522 | // We do not have a label, just pop the last entry. |
Warren Hunt | c3b1896 | 2014-04-08 22:30:47 +0000 | [diff] [blame] | 523 | CurrentValue = Stack.back().Value; |
| 524 | CurrentPragmaLocation = Stack.back().PragmaLocation; |
| 525 | Stack.pop_back(); |
| 526 | } |
| 527 | } |
| 528 | if (Action & PSK_Set) { |
| 529 | CurrentValue = Value; |
| 530 | CurrentPragmaLocation = PragmaLocation; |
| 531 | } |
| 532 | } |
| 533 | |
Craig Topper | bf3e327 | 2014-08-30 16:55:52 +0000 | [diff] [blame] | 534 | bool Sema::UnifySection(StringRef SectionName, |
Warren Hunt | c3b1896 | 2014-04-08 22:30:47 +0000 | [diff] [blame] | 535 | int SectionFlags, |
| 536 | DeclaratorDecl *Decl) { |
Lucas Prates | 9d39df0 | 2020-04-21 17:25:18 +0100 | [diff] [blame] | 537 | SourceLocation PragmaLocation; |
| 538 | if (auto A = Decl->getAttr<SectionAttr>()) |
| 539 | if (A->isImplicit()) |
| 540 | PragmaLocation = A->getLocation(); |
| 541 | auto SectionIt = Context.SectionInfos.find(SectionName); |
| 542 | if (SectionIt == Context.SectionInfos.end()) { |
Hans Wennborg | 899ded9 | 2014-10-16 20:52:46 +0000 | [diff] [blame] | 543 | Context.SectionInfos[SectionName] = |
Lucas Prates | 9d39df0 | 2020-04-21 17:25:18 +0100 | [diff] [blame] | 544 | ASTContext::SectionInfo(Decl, PragmaLocation, SectionFlags); |
Warren Hunt | c3b1896 | 2014-04-08 22:30:47 +0000 | [diff] [blame] | 545 | return false; |
| 546 | } |
| 547 | // A pre-declared section takes precedence w/o diagnostic. |
Lucas Prates | 9d39df0 | 2020-04-21 17:25:18 +0100 | [diff] [blame] | 548 | const auto &Section = SectionIt->second; |
| 549 | if (Section.SectionFlags == SectionFlags || |
| 550 | ((SectionFlags & ASTContext::PSF_Implicit) && |
| 551 | !(Section.SectionFlags & ASTContext::PSF_Implicit))) |
Warren Hunt | c3b1896 | 2014-04-08 22:30:47 +0000 | [diff] [blame] | 552 | return false; |
Lucas Prates | 9d39df0 | 2020-04-21 17:25:18 +0100 | [diff] [blame] | 553 | Diag(Decl->getLocation(), diag::err_section_conflict) << Decl << Section; |
| 554 | if (Section.Decl) |
| 555 | Diag(Section.Decl->getLocation(), diag::note_declared_at) |
| 556 | << Section.Decl->getName(); |
| 557 | if (PragmaLocation.isValid()) |
| 558 | Diag(PragmaLocation, diag::note_pragma_entered_here); |
| 559 | if (Section.PragmaSectionLocation.isValid()) |
| 560 | Diag(Section.PragmaSectionLocation, diag::note_pragma_entered_here); |
Ehsan Akhgari | 0b51060 | 2014-09-22 19:46:39 +0000 | [diff] [blame] | 561 | return true; |
Warren Hunt | c3b1896 | 2014-04-08 22:30:47 +0000 | [diff] [blame] | 562 | } |
| 563 | |
Craig Topper | bf3e327 | 2014-08-30 16:55:52 +0000 | [diff] [blame] | 564 | bool Sema::UnifySection(StringRef SectionName, |
Warren Hunt | c3b1896 | 2014-04-08 22:30:47 +0000 | [diff] [blame] | 565 | int SectionFlags, |
| 566 | SourceLocation PragmaSectionLocation) { |
Lucas Prates | 9d39df0 | 2020-04-21 17:25:18 +0100 | [diff] [blame] | 567 | auto SectionIt = Context.SectionInfos.find(SectionName); |
| 568 | if (SectionIt != Context.SectionInfos.end()) { |
| 569 | const auto &Section = SectionIt->second; |
| 570 | if (Section.SectionFlags == SectionFlags) |
Warren Hunt | c3b1896 | 2014-04-08 22:30:47 +0000 | [diff] [blame] | 571 | return false; |
Lucas Prates | 9d39df0 | 2020-04-21 17:25:18 +0100 | [diff] [blame] | 572 | if (!(Section.SectionFlags & ASTContext::PSF_Implicit)) { |
Warren Hunt | c3b1896 | 2014-04-08 22:30:47 +0000 | [diff] [blame] | 573 | Diag(PragmaSectionLocation, diag::err_section_conflict) |
Lucas Prates | 9d39df0 | 2020-04-21 17:25:18 +0100 | [diff] [blame] | 574 | << "this" << Section; |
| 575 | if (Section.Decl) |
| 576 | Diag(Section.Decl->getLocation(), diag::note_declared_at) |
| 577 | << Section.Decl->getName(); |
| 578 | if (Section.PragmaSectionLocation.isValid()) |
| 579 | Diag(Section.PragmaSectionLocation, diag::note_pragma_entered_here); |
Warren Hunt | c3b1896 | 2014-04-08 22:30:47 +0000 | [diff] [blame] | 580 | return true; |
| 581 | } |
| 582 | } |
Hans Wennborg | 899ded9 | 2014-10-16 20:52:46 +0000 | [diff] [blame] | 583 | Context.SectionInfos[SectionName] = |
| 584 | ASTContext::SectionInfo(nullptr, PragmaSectionLocation, SectionFlags); |
Warren Hunt | c3b1896 | 2014-04-08 22:30:47 +0000 | [diff] [blame] | 585 | return false; |
| 586 | } |
| 587 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 588 | /// Called on well formed \#pragma bss_seg(). |
Warren Hunt | c3b1896 | 2014-04-08 22:30:47 +0000 | [diff] [blame] | 589 | void Sema::ActOnPragmaMSSeg(SourceLocation PragmaLocation, |
| 590 | PragmaMsStackAction Action, |
| 591 | llvm::StringRef StackSlotLabel, |
| 592 | StringLiteral *SegmentName, |
| 593 | llvm::StringRef PragmaName) { |
| 594 | PragmaStack<StringLiteral *> *Stack = |
| 595 | llvm::StringSwitch<PragmaStack<StringLiteral *> *>(PragmaName) |
| 596 | .Case("data_seg", &DataSegStack) |
| 597 | .Case("bss_seg", &BSSSegStack) |
| 598 | .Case("const_seg", &ConstSegStack) |
| 599 | .Case("code_seg", &CodeSegStack); |
| 600 | if (Action & PSK_Pop && Stack->Stack.empty()) |
| 601 | Diag(PragmaLocation, diag::warn_pragma_pop_failed) << PragmaName |
| 602 | << "stack empty"; |
Nico Weber | 9801621 | 2019-07-09 00:02:23 +0000 | [diff] [blame] | 603 | if (SegmentName) { |
| 604 | if (!checkSectionName(SegmentName->getBeginLoc(), SegmentName->getString())) |
| 605 | return; |
| 606 | |
| 607 | if (SegmentName->getString() == ".drectve" && |
| 608 | Context.getTargetInfo().getCXXABI().isMicrosoft()) |
| 609 | Diag(PragmaLocation, diag::warn_attribute_section_drectve) << PragmaName; |
| 610 | } |
| 611 | |
Warren Hunt | c3b1896 | 2014-04-08 22:30:47 +0000 | [diff] [blame] | 612 | Stack->Act(PragmaLocation, Action, StackSlotLabel, SegmentName); |
| 613 | } |
| 614 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 615 | /// Called on well formed \#pragma bss_seg(). |
Warren Hunt | c3b1896 | 2014-04-08 22:30:47 +0000 | [diff] [blame] | 616 | void Sema::ActOnPragmaMSSection(SourceLocation PragmaLocation, |
| 617 | int SectionFlags, StringLiteral *SegmentName) { |
| 618 | UnifySection(SegmentName->getString(), SectionFlags, PragmaLocation); |
| 619 | } |
| 620 | |
Reid Kleckner | 1a711b1 | 2014-07-22 00:53:05 +0000 | [diff] [blame] | 621 | void Sema::ActOnPragmaMSInitSeg(SourceLocation PragmaLocation, |
| 622 | StringLiteral *SegmentName) { |
| 623 | // There's no stack to maintain, so we just have a current section. When we |
| 624 | // see the default section, reset our current section back to null so we stop |
| 625 | // tacking on unnecessary attributes. |
| 626 | CurInitSeg = SegmentName->getString() == ".CRT$XCU" ? nullptr : SegmentName; |
| 627 | CurInitSegLoc = PragmaLocation; |
| 628 | } |
| 629 | |
Argyrios Kyrtzidis | ee56962 | 2011-01-17 18:58:44 +0000 | [diff] [blame] | 630 | void Sema::ActOnPragmaUnused(const Token &IdTok, Scope *curScope, |
| 631 | SourceLocation PragmaLoc) { |
Ted Kremenek | fd14fad | 2009-03-23 22:28:25 +0000 | [diff] [blame] | 632 | |
Argyrios Kyrtzidis | ee56962 | 2011-01-17 18:58:44 +0000 | [diff] [blame] | 633 | IdentifierInfo *Name = IdTok.getIdentifierInfo(); |
| 634 | LookupResult Lookup(*this, Name, IdTok.getLocation(), LookupOrdinaryName); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 635 | LookupParsedName(Lookup, curScope, nullptr, true); |
Ted Kremenek | fd14fad | 2009-03-23 22:28:25 +0000 | [diff] [blame] | 636 | |
Argyrios Kyrtzidis | ee56962 | 2011-01-17 18:58:44 +0000 | [diff] [blame] | 637 | if (Lookup.empty()) { |
| 638 | Diag(PragmaLoc, diag::warn_pragma_unused_undeclared_var) |
| 639 | << Name << SourceRange(IdTok.getLocation()); |
| 640 | return; |
Ted Kremenek | fd14fad | 2009-03-23 22:28:25 +0000 | [diff] [blame] | 641 | } |
Argyrios Kyrtzidis | ee56962 | 2011-01-17 18:58:44 +0000 | [diff] [blame] | 642 | |
| 643 | VarDecl *VD = Lookup.getAsSingle<VarDecl>(); |
Argyrios Kyrtzidis | ff115a2 | 2011-01-27 18:16:48 +0000 | [diff] [blame] | 644 | if (!VD) { |
| 645 | Diag(PragmaLoc, diag::warn_pragma_unused_expected_var_arg) |
Argyrios Kyrtzidis | ee56962 | 2011-01-17 18:58:44 +0000 | [diff] [blame] | 646 | << Name << SourceRange(IdTok.getLocation()); |
| 647 | return; |
| 648 | } |
| 649 | |
| 650 | // Warn if this was used before being marked unused. |
| 651 | if (VD->isUsed()) |
| 652 | Diag(PragmaLoc, diag::warn_used_but_marked_unused) << Name; |
| 653 | |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 654 | VD->addAttr(UnusedAttr::CreateImplicit(Context, IdTok.getLocation(), |
| 655 | AttributeCommonInfo::AS_Pragma, |
| 656 | UnusedAttr::GNU_unused)); |
Ted Kremenek | fd14fad | 2009-03-23 22:28:25 +0000 | [diff] [blame] | 657 | } |
Eli Friedman | 570024a | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 658 | |
John McCall | 32f5fe1 | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 659 | void Sema::AddCFAuditedAttribute(Decl *D) { |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 660 | IdentifierInfo *Ident; |
| 661 | SourceLocation Loc; |
| 662 | std::tie(Ident, Loc) = PP.getPragmaARCCFCodeAuditedInfo(); |
John McCall | 32f5fe1 | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 663 | if (!Loc.isValid()) return; |
| 664 | |
| 665 | // Don't add a redundant or conflicting attribute. |
| 666 | if (D->hasAttr<CFAuditedTransferAttr>() || |
| 667 | D->hasAttr<CFUnknownTransferAttr>()) |
| 668 | return; |
| 669 | |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 670 | AttributeCommonInfo Info(Ident, SourceRange(Loc), |
| 671 | AttributeCommonInfo::AS_Pragma); |
| 672 | D->addAttr(CFAuditedTransferAttr::CreateImplicit(Context, Info)); |
John McCall | 32f5fe1 | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 673 | } |
| 674 | |
Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 675 | namespace { |
| 676 | |
| 677 | Optional<attr::SubjectMatchRule> |
| 678 | getParentAttrMatcherRule(attr::SubjectMatchRule Rule) { |
| 679 | using namespace attr; |
| 680 | switch (Rule) { |
| 681 | default: |
| 682 | return None; |
| 683 | #define ATTR_MATCH_RULE(Value, Spelling, IsAbstract) |
| 684 | #define ATTR_MATCH_SUB_RULE(Value, Spelling, IsAbstract, Parent, IsNegated) \ |
| 685 | case Value: \ |
| 686 | return Parent; |
| 687 | #include "clang/Basic/AttrSubMatchRulesList.inc" |
| 688 | } |
| 689 | } |
| 690 | |
| 691 | bool isNegatedAttrMatcherSubRule(attr::SubjectMatchRule Rule) { |
| 692 | using namespace attr; |
| 693 | switch (Rule) { |
| 694 | default: |
| 695 | return false; |
| 696 | #define ATTR_MATCH_RULE(Value, Spelling, IsAbstract) |
| 697 | #define ATTR_MATCH_SUB_RULE(Value, Spelling, IsAbstract, Parent, IsNegated) \ |
| 698 | case Value: \ |
| 699 | return IsNegated; |
| 700 | #include "clang/Basic/AttrSubMatchRulesList.inc" |
| 701 | } |
| 702 | } |
| 703 | |
| 704 | CharSourceRange replacementRangeForListElement(const Sema &S, |
| 705 | SourceRange Range) { |
| 706 | // Make sure that the ',' is removed as well. |
| 707 | SourceLocation AfterCommaLoc = Lexer::findLocationAfterToken( |
| 708 | Range.getEnd(), tok::comma, S.getSourceManager(), S.getLangOpts(), |
| 709 | /*SkipTrailingWhitespaceAndNewLine=*/false); |
| 710 | if (AfterCommaLoc.isValid()) |
| 711 | return CharSourceRange::getCharRange(Range.getBegin(), AfterCommaLoc); |
| 712 | else |
| 713 | return CharSourceRange::getTokenRange(Range); |
| 714 | } |
| 715 | |
| 716 | std::string |
| 717 | attrMatcherRuleListToString(ArrayRef<attr::SubjectMatchRule> Rules) { |
| 718 | std::string Result; |
| 719 | llvm::raw_string_ostream OS(Result); |
| 720 | for (const auto &I : llvm::enumerate(Rules)) { |
| 721 | if (I.index()) |
| 722 | OS << (I.index() == Rules.size() - 1 ? ", and " : ", "); |
| 723 | OS << "'" << attr::getSubjectMatchRuleSpelling(I.value()) << "'"; |
| 724 | } |
| 725 | return OS.str(); |
| 726 | } |
| 727 | |
| 728 | } // end anonymous namespace |
| 729 | |
Erik Pilkington | 7d18094 | 2018-10-29 17:38:42 +0000 | [diff] [blame] | 730 | void Sema::ActOnPragmaAttributeAttribute( |
| 731 | ParsedAttr &Attribute, SourceLocation PragmaLoc, |
| 732 | attr::ParsedSubjectMatchRuleSet Rules) { |
Alex Lorenz | 3cfe9d5 | 2019-01-24 19:14:39 +0000 | [diff] [blame] | 733 | Attribute.setIsPragmaClangAttribute(); |
Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 734 | SmallVector<attr::SubjectMatchRule, 4> SubjectMatchRules; |
| 735 | // Gather the subject match rules that are supported by the attribute. |
| 736 | SmallVector<std::pair<attr::SubjectMatchRule, bool>, 4> |
| 737 | StrictSubjectMatchRuleSet; |
| 738 | Attribute.getMatchRules(LangOpts, StrictSubjectMatchRuleSet); |
| 739 | |
| 740 | // Figure out which subject matching rules are valid. |
| 741 | if (StrictSubjectMatchRuleSet.empty()) { |
| 742 | // Check for contradicting match rules. Contradicting match rules are |
| 743 | // either: |
| 744 | // - a top-level rule and one of its sub-rules. E.g. variable and |
| 745 | // variable(is_parameter). |
| 746 | // - a sub-rule and a sibling that's negated. E.g. |
| 747 | // variable(is_thread_local) and variable(unless(is_parameter)) |
Alex Lorenz | 26b4765 | 2017-04-18 20:54:23 +0000 | [diff] [blame] | 748 | llvm::SmallDenseMap<int, std::pair<int, SourceRange>, 2> |
Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 749 | RulesToFirstSpecifiedNegatedSubRule; |
| 750 | for (const auto &Rule : Rules) { |
Alex Lorenz | 26b4765 | 2017-04-18 20:54:23 +0000 | [diff] [blame] | 751 | attr::SubjectMatchRule MatchRule = attr::SubjectMatchRule(Rule.first); |
Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 752 | Optional<attr::SubjectMatchRule> ParentRule = |
Alex Lorenz | 26b4765 | 2017-04-18 20:54:23 +0000 | [diff] [blame] | 753 | getParentAttrMatcherRule(MatchRule); |
Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 754 | if (!ParentRule) |
| 755 | continue; |
| 756 | auto It = Rules.find(*ParentRule); |
| 757 | if (It != Rules.end()) { |
| 758 | // A sub-rule contradicts a parent rule. |
| 759 | Diag(Rule.second.getBegin(), |
| 760 | diag::err_pragma_attribute_matcher_subrule_contradicts_rule) |
Alex Lorenz | 26b4765 | 2017-04-18 20:54:23 +0000 | [diff] [blame] | 761 | << attr::getSubjectMatchRuleSpelling(MatchRule) |
Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 762 | << attr::getSubjectMatchRuleSpelling(*ParentRule) << It->second |
| 763 | << FixItHint::CreateRemoval( |
| 764 | replacementRangeForListElement(*this, Rule.second)); |
| 765 | // Keep going without removing this rule as it won't change the set of |
| 766 | // declarations that receive the attribute. |
| 767 | continue; |
| 768 | } |
Alex Lorenz | 26b4765 | 2017-04-18 20:54:23 +0000 | [diff] [blame] | 769 | if (isNegatedAttrMatcherSubRule(MatchRule)) |
Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 770 | RulesToFirstSpecifiedNegatedSubRule.insert( |
| 771 | std::make_pair(*ParentRule, Rule)); |
| 772 | } |
| 773 | bool IgnoreNegatedSubRules = false; |
| 774 | for (const auto &Rule : Rules) { |
Alex Lorenz | 26b4765 | 2017-04-18 20:54:23 +0000 | [diff] [blame] | 775 | attr::SubjectMatchRule MatchRule = attr::SubjectMatchRule(Rule.first); |
Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 776 | Optional<attr::SubjectMatchRule> ParentRule = |
Alex Lorenz | 26b4765 | 2017-04-18 20:54:23 +0000 | [diff] [blame] | 777 | getParentAttrMatcherRule(MatchRule); |
Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 778 | if (!ParentRule) |
| 779 | continue; |
| 780 | auto It = RulesToFirstSpecifiedNegatedSubRule.find(*ParentRule); |
| 781 | if (It != RulesToFirstSpecifiedNegatedSubRule.end() && |
| 782 | It->second != Rule) { |
| 783 | // Negated sub-rule contradicts another sub-rule. |
| 784 | Diag( |
| 785 | It->second.second.getBegin(), |
| 786 | diag:: |
| 787 | err_pragma_attribute_matcher_negated_subrule_contradicts_subrule) |
Alex Lorenz | 26b4765 | 2017-04-18 20:54:23 +0000 | [diff] [blame] | 788 | << attr::getSubjectMatchRuleSpelling( |
| 789 | attr::SubjectMatchRule(It->second.first)) |
| 790 | << attr::getSubjectMatchRuleSpelling(MatchRule) << Rule.second |
Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 791 | << FixItHint::CreateRemoval( |
| 792 | replacementRangeForListElement(*this, It->second.second)); |
| 793 | // Keep going but ignore all of the negated sub-rules. |
| 794 | IgnoreNegatedSubRules = true; |
| 795 | RulesToFirstSpecifiedNegatedSubRule.erase(It); |
| 796 | } |
| 797 | } |
| 798 | |
| 799 | if (!IgnoreNegatedSubRules) { |
| 800 | for (const auto &Rule : Rules) |
Alex Lorenz | 26b4765 | 2017-04-18 20:54:23 +0000 | [diff] [blame] | 801 | SubjectMatchRules.push_back(attr::SubjectMatchRule(Rule.first)); |
Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 802 | } else { |
| 803 | for (const auto &Rule : Rules) { |
Alex Lorenz | 26b4765 | 2017-04-18 20:54:23 +0000 | [diff] [blame] | 804 | if (!isNegatedAttrMatcherSubRule(attr::SubjectMatchRule(Rule.first))) |
| 805 | SubjectMatchRules.push_back(attr::SubjectMatchRule(Rule.first)); |
Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 806 | } |
| 807 | } |
| 808 | Rules.clear(); |
| 809 | } else { |
| 810 | for (const auto &Rule : StrictSubjectMatchRuleSet) { |
| 811 | if (Rules.erase(Rule.first)) { |
| 812 | // Add the rule to the set of attribute receivers only if it's supported |
| 813 | // in the current language mode. |
| 814 | if (Rule.second) |
| 815 | SubjectMatchRules.push_back(Rule.first); |
| 816 | } |
| 817 | } |
| 818 | } |
| 819 | |
| 820 | if (!Rules.empty()) { |
| 821 | auto Diagnostic = |
| 822 | Diag(PragmaLoc, diag::err_pragma_attribute_invalid_matchers) |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 823 | << Attribute; |
Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 824 | SmallVector<attr::SubjectMatchRule, 2> ExtraRules; |
| 825 | for (const auto &Rule : Rules) { |
Alex Lorenz | 26b4765 | 2017-04-18 20:54:23 +0000 | [diff] [blame] | 826 | ExtraRules.push_back(attr::SubjectMatchRule(Rule.first)); |
Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 827 | Diagnostic << FixItHint::CreateRemoval( |
| 828 | replacementRangeForListElement(*this, Rule.second)); |
| 829 | } |
| 830 | Diagnostic << attrMatcherRuleListToString(ExtraRules); |
| 831 | } |
| 832 | |
Erik Pilkington | 7d18094 | 2018-10-29 17:38:42 +0000 | [diff] [blame] | 833 | if (PragmaAttributeStack.empty()) { |
| 834 | Diag(PragmaLoc, diag::err_pragma_attr_attr_no_push); |
| 835 | return; |
| 836 | } |
| 837 | |
| 838 | PragmaAttributeStack.back().Entries.push_back( |
Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 839 | {PragmaLoc, &Attribute, std::move(SubjectMatchRules), /*IsUsed=*/false}); |
| 840 | } |
| 841 | |
Erik Pilkington | 0876cae | 2018-12-20 22:32:04 +0000 | [diff] [blame] | 842 | void Sema::ActOnPragmaAttributeEmptyPush(SourceLocation PragmaLoc, |
| 843 | const IdentifierInfo *Namespace) { |
Erik Pilkington | 7d18094 | 2018-10-29 17:38:42 +0000 | [diff] [blame] | 844 | PragmaAttributeStack.emplace_back(); |
| 845 | PragmaAttributeStack.back().Loc = PragmaLoc; |
Erik Pilkington | 0876cae | 2018-12-20 22:32:04 +0000 | [diff] [blame] | 846 | PragmaAttributeStack.back().Namespace = Namespace; |
Erik Pilkington | 7d18094 | 2018-10-29 17:38:42 +0000 | [diff] [blame] | 847 | } |
| 848 | |
Erik Pilkington | 0876cae | 2018-12-20 22:32:04 +0000 | [diff] [blame] | 849 | void Sema::ActOnPragmaAttributePop(SourceLocation PragmaLoc, |
| 850 | const IdentifierInfo *Namespace) { |
Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 851 | if (PragmaAttributeStack.empty()) { |
Erik Pilkington | 0876cae | 2018-12-20 22:32:04 +0000 | [diff] [blame] | 852 | Diag(PragmaLoc, diag::err_pragma_attribute_stack_mismatch) << 1; |
Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 853 | return; |
| 854 | } |
Erik Pilkington | 7d18094 | 2018-10-29 17:38:42 +0000 | [diff] [blame] | 855 | |
Erik Pilkington | 0876cae | 2018-12-20 22:32:04 +0000 | [diff] [blame] | 856 | // Dig back through the stack trying to find the most recently pushed group |
| 857 | // that in Namespace. Note that this works fine if no namespace is present, |
| 858 | // think of push/pops without namespaces as having an implicit "nullptr" |
| 859 | // namespace. |
| 860 | for (size_t Index = PragmaAttributeStack.size(); Index;) { |
| 861 | --Index; |
| 862 | if (PragmaAttributeStack[Index].Namespace == Namespace) { |
| 863 | for (const PragmaAttributeEntry &Entry : |
| 864 | PragmaAttributeStack[Index].Entries) { |
| 865 | if (!Entry.IsUsed) { |
| 866 | assert(Entry.Attribute && "Expected an attribute"); |
| 867 | Diag(Entry.Attribute->getLoc(), diag::warn_pragma_attribute_unused) |
| 868 | << *Entry.Attribute; |
| 869 | Diag(PragmaLoc, diag::note_pragma_attribute_region_ends_here); |
| 870 | } |
| 871 | } |
| 872 | PragmaAttributeStack.erase(PragmaAttributeStack.begin() + Index); |
| 873 | return; |
Erik Pilkington | 7d18094 | 2018-10-29 17:38:42 +0000 | [diff] [blame] | 874 | } |
Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 875 | } |
Erik Pilkington | 7d18094 | 2018-10-29 17:38:42 +0000 | [diff] [blame] | 876 | |
Erik Pilkington | 0876cae | 2018-12-20 22:32:04 +0000 | [diff] [blame] | 877 | if (Namespace) |
| 878 | Diag(PragmaLoc, diag::err_pragma_attribute_stack_mismatch) |
| 879 | << 0 << Namespace->getName(); |
| 880 | else |
| 881 | Diag(PragmaLoc, diag::err_pragma_attribute_stack_mismatch) << 1; |
Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 882 | } |
| 883 | |
| 884 | void Sema::AddPragmaAttributes(Scope *S, Decl *D) { |
| 885 | if (PragmaAttributeStack.empty()) |
| 886 | return; |
Erik Pilkington | 7d18094 | 2018-10-29 17:38:42 +0000 | [diff] [blame] | 887 | for (auto &Group : PragmaAttributeStack) { |
| 888 | for (auto &Entry : Group.Entries) { |
| 889 | ParsedAttr *Attribute = Entry.Attribute; |
| 890 | assert(Attribute && "Expected an attribute"); |
Alex Lorenz | 3cfe9d5 | 2019-01-24 19:14:39 +0000 | [diff] [blame] | 891 | assert(Attribute->isPragmaClangAttribute() && |
| 892 | "expected #pragma clang attribute"); |
Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 893 | |
Erik Pilkington | 7d18094 | 2018-10-29 17:38:42 +0000 | [diff] [blame] | 894 | // Ensure that the attribute can be applied to the given declaration. |
| 895 | bool Applies = false; |
| 896 | for (const auto &Rule : Entry.MatchRules) { |
| 897 | if (Attribute->appliesToDecl(D, Rule)) { |
| 898 | Applies = true; |
| 899 | break; |
| 900 | } |
Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 901 | } |
Erik Pilkington | 7d18094 | 2018-10-29 17:38:42 +0000 | [diff] [blame] | 902 | if (!Applies) |
| 903 | continue; |
| 904 | Entry.IsUsed = true; |
| 905 | PragmaAttributeCurrentTargetDecl = D; |
| 906 | ParsedAttributesView Attrs; |
| 907 | Attrs.addAtEnd(Attribute); |
| 908 | ProcessDeclAttributeList(S, D, Attrs); |
| 909 | PragmaAttributeCurrentTargetDecl = nullptr; |
Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 910 | } |
Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 911 | } |
| 912 | } |
| 913 | |
| 914 | void Sema::PrintPragmaAttributeInstantiationPoint() { |
| 915 | assert(PragmaAttributeCurrentTargetDecl && "Expected an active declaration"); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 916 | Diags.Report(PragmaAttributeCurrentTargetDecl->getBeginLoc(), |
Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 917 | diag::note_pragma_attribute_applied_decl_here); |
| 918 | } |
| 919 | |
| 920 | void Sema::DiagnoseUnterminatedPragmaAttribute() { |
| 921 | if (PragmaAttributeStack.empty()) |
| 922 | return; |
| 923 | Diag(PragmaAttributeStack.back().Loc, diag::err_pragma_attribute_no_pop_eof); |
| 924 | } |
| 925 | |
Dario Domizioli | 13a0a38 | 2014-05-23 12:13:25 +0000 | [diff] [blame] | 926 | void Sema::ActOnPragmaOptimize(bool On, SourceLocation PragmaLoc) { |
| 927 | if(On) |
| 928 | OptimizeOffPragmaLocation = SourceLocation(); |
| 929 | else |
| 930 | OptimizeOffPragmaLocation = PragmaLoc; |
| 931 | } |
| 932 | |
| 933 | void Sema::AddRangeBasedOptnone(FunctionDecl *FD) { |
| 934 | // In the future, check other pragmas if they're implemented (e.g. pragma |
| 935 | // optimize 0 will probably map to this functionality too). |
| 936 | if(OptimizeOffPragmaLocation.isValid()) |
| 937 | AddOptnoneAttributeIfNoConflicts(FD, OptimizeOffPragmaLocation); |
| 938 | } |
| 939 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 940 | void Sema::AddOptnoneAttributeIfNoConflicts(FunctionDecl *FD, |
Dario Domizioli | 13a0a38 | 2014-05-23 12:13:25 +0000 | [diff] [blame] | 941 | SourceLocation Loc) { |
| 942 | // Don't add a conflicting attribute. No diagnostic is needed. |
| 943 | if (FD->hasAttr<MinSizeAttr>() || FD->hasAttr<AlwaysInlineAttr>()) |
| 944 | return; |
| 945 | |
| 946 | // Add attributes only if required. Optnone requires noinline as well, but if |
| 947 | // either is already present then don't bother adding them. |
| 948 | if (!FD->hasAttr<OptimizeNoneAttr>()) |
| 949 | FD->addAttr(OptimizeNoneAttr::CreateImplicit(Context, Loc)); |
| 950 | if (!FD->hasAttr<NoInlineAttr>()) |
| 951 | FD->addAttr(NoInlineAttr::CreateImplicit(Context, Loc)); |
| 952 | } |
| 953 | |
John McCall | 2faf32c | 2010-12-10 02:59:44 +0000 | [diff] [blame] | 954 | typedef std::vector<std::pair<unsigned, SourceLocation> > VisStack; |
Alp Toker | ceb95c4 | 2014-03-02 03:20:16 +0000 | [diff] [blame] | 955 | enum : unsigned { NoVisibility = ~0U }; |
Eli Friedman | 570024a | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 956 | |
| 957 | void Sema::AddPushedVisibilityAttribute(Decl *D) { |
| 958 | if (!VisContext) |
| 959 | return; |
| 960 | |
Rafael Espindola | 54606d5 | 2012-12-25 07:31:49 +0000 | [diff] [blame] | 961 | NamedDecl *ND = dyn_cast<NamedDecl>(D); |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 962 | if (ND && ND->getExplicitVisibility(NamedDecl::VisibilityForValue)) |
Eli Friedman | 570024a | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 963 | return; |
| 964 | |
| 965 | VisStack *Stack = static_cast<VisStack*>(VisContext); |
John McCall | 2faf32c | 2010-12-10 02:59:44 +0000 | [diff] [blame] | 966 | unsigned rawType = Stack->back().first; |
| 967 | if (rawType == NoVisibility) return; |
| 968 | |
| 969 | VisibilityAttr::VisibilityType type |
| 970 | = (VisibilityAttr::VisibilityType) rawType; |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 971 | SourceLocation loc = Stack->back().second; |
Eli Friedman | 570024a | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 972 | |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 973 | D->addAttr(VisibilityAttr::CreateImplicit(Context, type, loc)); |
Eli Friedman | 570024a | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 974 | } |
| 975 | |
| 976 | /// FreeVisContext - Deallocate and null out VisContext. |
| 977 | void Sema::FreeVisContext() { |
| 978 | delete static_cast<VisStack*>(VisContext); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 979 | VisContext = nullptr; |
Eli Friedman | 570024a | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 980 | } |
| 981 | |
John McCall | 2faf32c | 2010-12-10 02:59:44 +0000 | [diff] [blame] | 982 | static void PushPragmaVisibility(Sema &S, unsigned type, SourceLocation loc) { |
John McCall | b1be523 | 2010-08-26 09:15:37 +0000 | [diff] [blame] | 983 | // Put visibility on stack. |
| 984 | if (!S.VisContext) |
| 985 | S.VisContext = new VisStack; |
| 986 | |
| 987 | VisStack *Stack = static_cast<VisStack*>(S.VisContext); |
| 988 | Stack->push_back(std::make_pair(type, loc)); |
| 989 | } |
| 990 | |
Rafael Espindola | de15baf | 2012-01-21 05:43:40 +0000 | [diff] [blame] | 991 | void Sema::ActOnPragmaVisibility(const IdentifierInfo* VisType, |
Eli Friedman | 570024a | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 992 | SourceLocation PragmaLoc) { |
Rafael Espindola | de15baf | 2012-01-21 05:43:40 +0000 | [diff] [blame] | 993 | if (VisType) { |
Eli Friedman | 570024a | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 994 | // Compute visibility to use. |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 995 | VisibilityAttr::VisibilityType T; |
| 996 | if (!VisibilityAttr::ConvertStrToVisibilityType(VisType->getName(), T)) { |
| 997 | Diag(PragmaLoc, diag::warn_attribute_unknown_visibility) << VisType; |
Eli Friedman | 570024a | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 998 | return; |
| 999 | } |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 1000 | PushPragmaVisibility(*this, T, PragmaLoc); |
Eli Friedman | 570024a | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 1001 | } else { |
Rafael Espindola | 6d65d7b | 2012-02-01 23:24:59 +0000 | [diff] [blame] | 1002 | PopPragmaVisibility(false, PragmaLoc); |
Eli Friedman | 570024a | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 1003 | } |
| 1004 | } |
| 1005 | |
Melanie Blower | defd43a | 2020-06-26 08:45:12 -0700 | [diff] [blame^] | 1006 | void Sema::ActOnPragmaFPContract(LangOptions::FPModeKind FPC) { |
Adam Nemet | 60d3264 | 2017-04-04 21:18:36 +0000 | [diff] [blame] | 1007 | switch (FPC) { |
Melanie Blower | c355bec | 2020-05-04 10:48:12 -0700 | [diff] [blame] | 1008 | case LangOptions::FPM_On: |
Melanie Blower | defd43a | 2020-06-26 08:45:12 -0700 | [diff] [blame^] | 1009 | CurFPFeatures.setAllowFPContractWithinStatement(); |
Peter Collingbourne | 564c0fa | 2011-02-14 01:42:35 +0000 | [diff] [blame] | 1010 | break; |
Melanie Blower | c355bec | 2020-05-04 10:48:12 -0700 | [diff] [blame] | 1011 | case LangOptions::FPM_Fast: |
Melanie Blower | defd43a | 2020-06-26 08:45:12 -0700 | [diff] [blame^] | 1012 | CurFPFeatures.setAllowFPContractAcrossStatement(); |
Peter Collingbourne | 564c0fa | 2011-02-14 01:42:35 +0000 | [diff] [blame] | 1013 | break; |
Melanie Blower | c355bec | 2020-05-04 10:48:12 -0700 | [diff] [blame] | 1014 | case LangOptions::FPM_Off: |
Melanie Blower | defd43a | 2020-06-26 08:45:12 -0700 | [diff] [blame^] | 1015 | CurFPFeatures.setDisallowFPContract(); |
Peter Collingbourne | 564c0fa | 2011-02-14 01:42:35 +0000 | [diff] [blame] | 1016 | break; |
| 1017 | } |
| 1018 | } |
| 1019 | |
Melanie Blower | defd43a | 2020-06-26 08:45:12 -0700 | [diff] [blame^] | 1020 | void Sema::ActOnPragmaFPReassociate(bool IsEnabled) { |
| 1021 | CurFPFeatures.setAllowAssociativeMath(IsEnabled); |
Melanie Blower | c355bec | 2020-05-04 10:48:12 -0700 | [diff] [blame] | 1022 | } |
| 1023 | |
Melanie Blower | defd43a | 2020-06-26 08:45:12 -0700 | [diff] [blame^] | 1024 | void Sema::setRoundingMode(llvm::RoundingMode FPR) { |
| 1025 | CurFPFeatures.setRoundingMode(FPR); |
Serge Pavlov | 4aea70e | 2019-08-07 23:31:26 +0700 | [diff] [blame] | 1026 | } |
| 1027 | |
Melanie Blower | defd43a | 2020-06-26 08:45:12 -0700 | [diff] [blame^] | 1028 | void Sema::setExceptionMode(LangOptions::FPExceptionModeKind FPE) { |
| 1029 | CurFPFeatures.setExceptionMode(FPE); |
Serge Pavlov | 4aea70e | 2019-08-07 23:31:26 +0700 | [diff] [blame] | 1030 | } |
| 1031 | |
Melanie Blower | c355bec | 2020-05-04 10:48:12 -0700 | [diff] [blame] | 1032 | void Sema::ActOnPragmaFEnvAccess(SourceLocation Loc, bool IsEnabled) { |
| 1033 | if (IsEnabled) { |
Melanie Blower | f5360d4 | 2020-05-01 10:32:06 -0700 | [diff] [blame] | 1034 | // Verify Microsoft restriction: |
| 1035 | // You can't enable fenv_access unless precise semantics are enabled. |
| 1036 | // Precise semantics can be enabled either by the float_control |
| 1037 | // pragma, or by using the /fp:precise or /fp:strict compiler options |
| 1038 | if (!isPreciseFPEnabled()) |
| 1039 | Diag(Loc, diag::err_pragma_fenv_requires_precise); |
Melanie Blower | defd43a | 2020-06-26 08:45:12 -0700 | [diff] [blame^] | 1040 | CurFPFeatures.setAllowFEnvAccess(); |
Melanie Blower | c355bec | 2020-05-04 10:48:12 -0700 | [diff] [blame] | 1041 | } else |
Melanie Blower | defd43a | 2020-06-26 08:45:12 -0700 | [diff] [blame^] | 1042 | CurFPFeatures.setDisallowFEnvAccess(); |
Kevin P. Neal | 2c0bc8b | 2018-08-14 17:06:56 +0000 | [diff] [blame] | 1043 | } |
| 1044 | |
Rafael Espindola | 6d65d7b | 2012-02-01 23:24:59 +0000 | [diff] [blame] | 1045 | void Sema::PushNamespaceVisibilityAttr(const VisibilityAttr *Attr, |
| 1046 | SourceLocation Loc) { |
John McCall | 2faf32c | 2010-12-10 02:59:44 +0000 | [diff] [blame] | 1047 | // Visibility calculations will consider the namespace's visibility. |
| 1048 | // Here we just want to note that we're in a visibility context |
| 1049 | // which overrides any enclosing #pragma context, but doesn't itself |
| 1050 | // contribute visibility. |
Rafael Espindola | 6d65d7b | 2012-02-01 23:24:59 +0000 | [diff] [blame] | 1051 | PushPragmaVisibility(*this, NoVisibility, Loc); |
Eli Friedman | 570024a | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 1052 | } |
| 1053 | |
Rafael Espindola | 6d65d7b | 2012-02-01 23:24:59 +0000 | [diff] [blame] | 1054 | void Sema::PopPragmaVisibility(bool IsNamespaceEnd, SourceLocation EndLoc) { |
| 1055 | if (!VisContext) { |
| 1056 | Diag(EndLoc, diag::err_pragma_pop_visibility_mismatch); |
| 1057 | return; |
Eli Friedman | 570024a | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 1058 | } |
Rafael Espindola | 6d65d7b | 2012-02-01 23:24:59 +0000 | [diff] [blame] | 1059 | |
| 1060 | // Pop visibility from stack |
| 1061 | VisStack *Stack = static_cast<VisStack*>(VisContext); |
| 1062 | |
| 1063 | const std::pair<unsigned, SourceLocation> *Back = &Stack->back(); |
| 1064 | bool StartsWithPragma = Back->first != NoVisibility; |
| 1065 | if (StartsWithPragma && IsNamespaceEnd) { |
| 1066 | Diag(Back->second, diag::err_pragma_push_visibility_mismatch); |
| 1067 | Diag(EndLoc, diag::note_surrounding_namespace_ends_here); |
| 1068 | |
| 1069 | // For better error recovery, eat all pushes inside the namespace. |
| 1070 | do { |
| 1071 | Stack->pop_back(); |
| 1072 | Back = &Stack->back(); |
| 1073 | StartsWithPragma = Back->first != NoVisibility; |
| 1074 | } while (StartsWithPragma); |
| 1075 | } else if (!StartsWithPragma && !IsNamespaceEnd) { |
| 1076 | Diag(EndLoc, diag::err_pragma_pop_visibility_mismatch); |
| 1077 | Diag(Back->second, diag::note_surrounding_namespace_starts_here); |
| 1078 | return; |
| 1079 | } |
| 1080 | |
| 1081 | Stack->pop_back(); |
| 1082 | // To simplify the implementation, never keep around an empty stack. |
| 1083 | if (Stack->empty()) |
| 1084 | FreeVisContext(); |
Eli Friedman | 570024a | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 1085 | } |