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. |
Denis Zobnin | 2290dac | 2016-04-29 11:27:00 +0000 | [diff] [blame] | 83 | if (VtorDispStack.CurrentValue != getLangOpts().VtorDispMode) |
Reid Kleckner | c0dca6d | 2014-02-12 23:50:26 +0000 | [diff] [blame] | 84 | RD->addAttr( |
Denis Zobnin | 2290dac | 2016-04-29 11:27:00 +0000 | [diff] [blame] | 85 | MSVtorDispAttr::CreateImplicit(Context, VtorDispStack.CurrentValue)); |
Fariborz Jahanian | 6b4e26b | 2011-04-26 17:54:40 +0000 | [diff] [blame] | 86 | } |
| 87 | |
Daniel Dunbar | 69dac58 | 2010-05-27 00:04:40 +0000 | [diff] [blame] | 88 | void Sema::ActOnPragmaOptionsAlign(PragmaOptionsAlignKind Kind, |
Eli Friedman | 68be164 | 2012-10-04 02:36:51 +0000 | [diff] [blame] | 89 | SourceLocation PragmaLoc) { |
Denis Zobnin | 10c4f45 | 2016-04-29 18:17:40 +0000 | [diff] [blame] | 90 | PragmaMsStackAction Action = Sema::PSK_Reset; |
Denis Zobnin | 3f287c2 | 2016-04-29 22:50:16 +0000 | [diff] [blame] | 91 | unsigned Alignment = 0; |
Daniel Dunbar | 69dac58 | 2010-05-27 00:04:40 +0000 | [diff] [blame] | 92 | switch (Kind) { |
Daniel Dunbar | 663e809 | 2010-05-27 18:42:09 +0000 | [diff] [blame] | 93 | // For all targets we support native and natural are the same. |
| 94 | // |
| 95 | // FIXME: This is not true on Darwin/PPC. |
| 96 | case POAK_Native: |
Daniel Dunbar | 5794c6f | 2010-05-28 19:43:33 +0000 | [diff] [blame] | 97 | case POAK_Power: |
Daniel Dunbar | a688566 | 2010-05-28 20:08:00 +0000 | [diff] [blame] | 98 | case POAK_Natural: |
Denis Zobnin | 10c4f45 | 2016-04-29 18:17:40 +0000 | [diff] [blame] | 99 | Action = Sema::PSK_Push_Set; |
| 100 | Alignment = 0; |
Daniel Dunbar | 5794c6f | 2010-05-28 19:43:33 +0000 | [diff] [blame] | 101 | break; |
| 102 | |
Daniel Dunbar | 9c84d4a | 2010-05-27 18:42:17 +0000 | [diff] [blame] | 103 | // Note that '#pragma options align=packed' is not equivalent to attribute |
| 104 | // packed, it has a different precedence relative to attribute aligned. |
| 105 | case POAK_Packed: |
Denis Zobnin | 10c4f45 | 2016-04-29 18:17:40 +0000 | [diff] [blame] | 106 | Action = Sema::PSK_Push_Set; |
| 107 | Alignment = 1; |
Daniel Dunbar | 9c84d4a | 2010-05-27 18:42:17 +0000 | [diff] [blame] | 108 | break; |
| 109 | |
Daniel Dunbar | bd60652 | 2010-05-27 00:35:16 +0000 | [diff] [blame] | 110 | case POAK_Mac68k: |
| 111 | // Check if the target supports this. |
Alp Toker | b6cc592 | 2014-05-03 03:45:55 +0000 | [diff] [blame] | 112 | if (!this->Context.getTargetInfo().hasAlignMac68kSupport()) { |
Daniel Dunbar | bd60652 | 2010-05-27 00:35:16 +0000 | [diff] [blame] | 113 | Diag(PragmaLoc, diag::err_pragma_options_align_mac68k_target_unsupported); |
| 114 | return; |
Daniel Dunbar | bd60652 | 2010-05-27 00:35:16 +0000 | [diff] [blame] | 115 | } |
Denis Zobnin | 10c4f45 | 2016-04-29 18:17:40 +0000 | [diff] [blame] | 116 | Action = Sema::PSK_Push_Set; |
| 117 | Alignment = Sema::kMac68kAlignmentSentinel; |
Daniel Dunbar | bd60652 | 2010-05-27 00:35:16 +0000 | [diff] [blame] | 118 | break; |
| 119 | |
Eli Friedman | 68be164 | 2012-10-04 02:36:51 +0000 | [diff] [blame] | 120 | case POAK_Reset: |
| 121 | // Reset just pops the top of the stack, or resets the current alignment to |
| 122 | // default. |
Denis Zobnin | 10c4f45 | 2016-04-29 18:17:40 +0000 | [diff] [blame] | 123 | Action = Sema::PSK_Pop; |
| 124 | if (PackStack.Stack.empty()) { |
| 125 | if (PackStack.CurrentValue) { |
| 126 | Action = Sema::PSK_Reset; |
| 127 | } else { |
| 128 | Diag(PragmaLoc, diag::warn_pragma_options_align_reset_failed) |
| 129 | << "stack empty"; |
| 130 | return; |
| 131 | } |
Eli Friedman | 68be164 | 2012-10-04 02:36:51 +0000 | [diff] [blame] | 132 | } |
Daniel Dunbar | 69dac58 | 2010-05-27 00:04:40 +0000 | [diff] [blame] | 133 | break; |
| 134 | } |
Denis Zobnin | 10c4f45 | 2016-04-29 18:17:40 +0000 | [diff] [blame] | 135 | |
| 136 | PackStack.Act(PragmaLoc, Action, StringRef(), Alignment); |
Daniel Dunbar | 69dac58 | 2010-05-27 00:04:40 +0000 | [diff] [blame] | 137 | } |
| 138 | |
Javed Absar | 2a67c9e | 2017-06-05 10:11:57 +0000 | [diff] [blame] | 139 | void Sema::ActOnPragmaClangSection(SourceLocation PragmaLoc, PragmaClangSectionAction Action, |
| 140 | PragmaClangSectionKind SecKind, StringRef SecName) { |
| 141 | PragmaClangSection *CSec; |
| 142 | switch (SecKind) { |
| 143 | case PragmaClangSectionKind::PCSK_BSS: |
| 144 | CSec = &PragmaClangBSSSection; |
| 145 | break; |
| 146 | case PragmaClangSectionKind::PCSK_Data: |
| 147 | CSec = &PragmaClangDataSection; |
| 148 | break; |
| 149 | case PragmaClangSectionKind::PCSK_Rodata: |
| 150 | CSec = &PragmaClangRodataSection; |
| 151 | break; |
| 152 | case PragmaClangSectionKind::PCSK_Text: |
| 153 | CSec = &PragmaClangTextSection; |
| 154 | break; |
| 155 | default: |
| 156 | llvm_unreachable("invalid clang section kind"); |
| 157 | } |
| 158 | |
| 159 | if (Action == PragmaClangSectionAction::PCSA_Clear) { |
| 160 | CSec->Valid = false; |
| 161 | return; |
| 162 | } |
| 163 | |
| 164 | CSec->Valid = true; |
| 165 | CSec->SectionName = SecName; |
| 166 | CSec->PragmaLocation = PragmaLoc; |
| 167 | } |
| 168 | |
Denis Zobnin | 10c4f45 | 2016-04-29 18:17:40 +0000 | [diff] [blame] | 169 | void Sema::ActOnPragmaPack(SourceLocation PragmaLoc, PragmaMsStackAction Action, |
| 170 | StringRef SlotLabel, Expr *alignment) { |
Chris Lattner | 2eccbc1 | 2009-02-17 00:57:29 +0000 | [diff] [blame] | 171 | Expr *Alignment = static_cast<Expr *>(alignment); |
| 172 | |
| 173 | // If specified then alignment must be a "small" power of two. |
| 174 | unsigned AlignmentVal = 0; |
| 175 | if (Alignment) { |
| 176 | llvm::APSInt Val; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 177 | |
Daniel Dunbar | e03c610 | 2009-03-06 20:45:54 +0000 | [diff] [blame] | 178 | // pack(0) is like pack(), which just works out since that is what |
| 179 | // we use 0 for in PackAttr. |
Douglas Gregor | bdb604a | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 180 | if (Alignment->isTypeDependent() || |
| 181 | Alignment->isValueDependent() || |
| 182 | !Alignment->isIntegerConstantExpr(Val, Context) || |
Daniel Dunbar | e03c610 | 2009-03-06 20:45:54 +0000 | [diff] [blame] | 183 | !(Val == 0 || Val.isPowerOf2()) || |
Chris Lattner | 2eccbc1 | 2009-02-17 00:57:29 +0000 | [diff] [blame] | 184 | Val.getZExtValue() > 16) { |
| 185 | Diag(PragmaLoc, diag::warn_pragma_pack_invalid_alignment); |
Chris Lattner | 2eccbc1 | 2009-02-17 00:57:29 +0000 | [diff] [blame] | 186 | return; // Ignore |
| 187 | } |
| 188 | |
| 189 | AlignmentVal = (unsigned) Val.getZExtValue(); |
| 190 | } |
Denis Zobnin | 10c4f45 | 2016-04-29 18:17:40 +0000 | [diff] [blame] | 191 | if (Action == Sema::PSK_Show) { |
Chris Lattner | 2eccbc1 | 2009-02-17 00:57:29 +0000 | [diff] [blame] | 192 | // Show the current alignment, making sure to show the right value |
| 193 | // for the default. |
Chris Lattner | 2eccbc1 | 2009-02-17 00:57:29 +0000 | [diff] [blame] | 194 | // FIXME: This should come from the target. |
Denis Zobnin | 10c4f45 | 2016-04-29 18:17:40 +0000 | [diff] [blame] | 195 | AlignmentVal = PackStack.CurrentValue; |
Chris Lattner | 2eccbc1 | 2009-02-17 00:57:29 +0000 | [diff] [blame] | 196 | if (AlignmentVal == 0) |
| 197 | AlignmentVal = 8; |
Denis Zobnin | 10c4f45 | 2016-04-29 18:17:40 +0000 | [diff] [blame] | 198 | if (AlignmentVal == Sema::kMac68kAlignmentSentinel) |
Daniel Dunbar | 6da1098 | 2010-05-27 05:45:51 +0000 | [diff] [blame] | 199 | Diag(PragmaLoc, diag::warn_pragma_pack_show) << "mac68k"; |
| 200 | else |
| 201 | Diag(PragmaLoc, diag::warn_pragma_pack_show) << AlignmentVal; |
Chris Lattner | 2eccbc1 | 2009-02-17 00:57:29 +0000 | [diff] [blame] | 202 | } |
Denis Zobnin | 10c4f45 | 2016-04-29 18:17:40 +0000 | [diff] [blame] | 203 | // MSDN, C/C++ Preprocessor Reference > Pragma Directives > pack: |
| 204 | // "#pragma pack(pop, identifier, n) is undefined" |
| 205 | if (Action & Sema::PSK_Pop) { |
| 206 | if (Alignment && !SlotLabel.empty()) |
Alexander Kornienko | 2a8c18d | 2018-04-06 15:14:32 +0000 | [diff] [blame] | 207 | Diag(PragmaLoc, diag::warn_pragma_pack_pop_identifier_and_alignment); |
Denis Zobnin | 10c4f45 | 2016-04-29 18:17:40 +0000 | [diff] [blame] | 208 | if (PackStack.Stack.empty()) |
| 209 | Diag(PragmaLoc, diag::warn_pragma_pop_failed) << "pack" << "stack empty"; |
| 210 | } |
| 211 | |
| 212 | PackStack.Act(PragmaLoc, Action, SlotLabel, AlignmentVal); |
Chris Lattner | 2eccbc1 | 2009-02-17 00:57:29 +0000 | [diff] [blame] | 213 | } |
| 214 | |
Alex Lorenz | 45b4014 | 2017-07-28 14:41:21 +0000 | [diff] [blame] | 215 | void Sema::DiagnoseNonDefaultPragmaPack(PragmaPackDiagnoseKind Kind, |
| 216 | SourceLocation IncludeLoc) { |
| 217 | if (Kind == PragmaPackDiagnoseKind::NonDefaultStateAtInclude) { |
| 218 | SourceLocation PrevLocation = PackStack.CurrentPragmaLocation; |
| 219 | // Warn about non-default alignment at #includes (without redundant |
| 220 | // warnings for the same directive in nested includes). |
| 221 | // The warning is delayed until the end of the file to avoid warnings |
| 222 | // for files that don't have any records that are affected by the modified |
| 223 | // alignment. |
| 224 | bool HasNonDefaultValue = |
| 225 | PackStack.hasValue() && |
| 226 | (PackIncludeStack.empty() || |
| 227 | PackIncludeStack.back().CurrentPragmaLocation != PrevLocation); |
| 228 | PackIncludeStack.push_back( |
| 229 | {PackStack.CurrentValue, |
| 230 | PackStack.hasValue() ? PrevLocation : SourceLocation(), |
| 231 | HasNonDefaultValue, /*ShouldWarnOnInclude*/ false}); |
| 232 | return; |
| 233 | } |
| 234 | |
| 235 | assert(Kind == PragmaPackDiagnoseKind::ChangedStateAtExit && "invalid kind"); |
| 236 | PackIncludeState PrevPackState = PackIncludeStack.pop_back_val(); |
| 237 | if (PrevPackState.ShouldWarnOnInclude) { |
| 238 | // Emit the delayed non-default alignment at #include warning. |
| 239 | Diag(IncludeLoc, diag::warn_pragma_pack_non_default_at_include); |
| 240 | Diag(PrevPackState.CurrentPragmaLocation, diag::note_pragma_pack_here); |
| 241 | } |
| 242 | // Warn about modified alignment after #includes. |
| 243 | if (PrevPackState.CurrentValue != PackStack.CurrentValue) { |
| 244 | Diag(IncludeLoc, diag::warn_pragma_pack_modified_after_include); |
| 245 | Diag(PackStack.CurrentPragmaLocation, diag::note_pragma_pack_here); |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | void Sema::DiagnoseUnterminatedPragmaPack() { |
| 250 | if (PackStack.Stack.empty()) |
| 251 | return; |
Alex Lorenz | a1479d7 | 2017-07-31 13:37:50 +0000 | [diff] [blame] | 252 | bool IsInnermost = true; |
| 253 | for (const auto &StackSlot : llvm::reverse(PackStack.Stack)) { |
Alex Lorenz | 45b4014 | 2017-07-28 14:41:21 +0000 | [diff] [blame] | 254 | Diag(StackSlot.PragmaPushLocation, diag::warn_pragma_pack_no_pop_eof); |
Alex Lorenz | a1479d7 | 2017-07-31 13:37:50 +0000 | [diff] [blame] | 255 | // The user might have already reset the alignment, so suggest replacing |
| 256 | // the reset with a pop. |
| 257 | if (IsInnermost && PackStack.CurrentValue == PackStack.DefaultValue) { |
| 258 | DiagnosticBuilder DB = Diag(PackStack.CurrentPragmaLocation, |
| 259 | diag::note_pragma_pack_pop_instead_reset); |
| 260 | SourceLocation FixItLoc = Lexer::findLocationAfterToken( |
| 261 | PackStack.CurrentPragmaLocation, tok::l_paren, SourceMgr, LangOpts, |
| 262 | /*SkipTrailing=*/false); |
| 263 | if (FixItLoc.isValid()) |
| 264 | DB << FixItHint::CreateInsertion(FixItLoc, "pop"); |
| 265 | } |
| 266 | IsInnermost = false; |
| 267 | } |
Alex Lorenz | 45b4014 | 2017-07-28 14:41:21 +0000 | [diff] [blame] | 268 | } |
| 269 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 270 | void Sema::ActOnPragmaMSStruct(PragmaMSStructKind Kind) { |
Fariborz Jahanian | 743dda4 | 2011-04-25 18:49:15 +0000 | [diff] [blame] | 271 | MSStructPragmaOn = (Kind == PMSST_ON); |
| 272 | } |
| 273 | |
Nico Weber | 6622029 | 2016-03-02 17:28:48 +0000 | [diff] [blame] | 274 | void Sema::ActOnPragmaMSComment(SourceLocation CommentLoc, |
| 275 | PragmaMSCommentKind Kind, StringRef Arg) { |
| 276 | auto *PCD = PragmaCommentDecl::Create( |
| 277 | Context, Context.getTranslationUnitDecl(), CommentLoc, Kind, Arg); |
| 278 | Context.getTranslationUnitDecl()->addDecl(PCD); |
| 279 | Consumer.HandleTopLevelDecl(DeclGroupRef(PCD)); |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 280 | } |
| 281 | |
Nico Weber | cbbaeb1 | 2016-03-02 19:28:54 +0000 | [diff] [blame] | 282 | void Sema::ActOnPragmaDetectMismatch(SourceLocation Loc, StringRef Name, |
| 283 | StringRef Value) { |
| 284 | auto *PDMD = PragmaDetectMismatchDecl::Create( |
| 285 | Context, Context.getTranslationUnitDecl(), Loc, Name, Value); |
| 286 | Context.getTranslationUnitDecl()->addDecl(PDMD); |
| 287 | Consumer.HandleTopLevelDecl(DeclGroupRef(PDMD)); |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 288 | } |
| 289 | |
David Majnemer | 4bb0980 | 2014-02-10 19:50:15 +0000 | [diff] [blame] | 290 | void Sema::ActOnPragmaMSPointersToMembers( |
David Majnemer | 86c318f | 2014-02-11 21:05:00 +0000 | [diff] [blame] | 291 | LangOptions::PragmaMSPointersToMembersKind RepresentationMethod, |
David Majnemer | 4bb0980 | 2014-02-10 19:50:15 +0000 | [diff] [blame] | 292 | SourceLocation PragmaLoc) { |
| 293 | MSPointerToMemberRepresentationMethod = RepresentationMethod; |
| 294 | ImplicitMSInheritanceAttrLoc = PragmaLoc; |
| 295 | } |
| 296 | |
Denis Zobnin | 2290dac | 2016-04-29 11:27:00 +0000 | [diff] [blame] | 297 | void Sema::ActOnPragmaMSVtorDisp(PragmaMsStackAction Action, |
Reid Kleckner | c0dca6d | 2014-02-12 23:50:26 +0000 | [diff] [blame] | 298 | SourceLocation PragmaLoc, |
| 299 | MSVtorDispAttr::Mode Mode) { |
Denis Zobnin | 2290dac | 2016-04-29 11:27:00 +0000 | [diff] [blame] | 300 | if (Action & PSK_Pop && VtorDispStack.Stack.empty()) |
| 301 | Diag(PragmaLoc, diag::warn_pragma_pop_failed) << "vtordisp" |
| 302 | << "stack empty"; |
| 303 | VtorDispStack.Act(PragmaLoc, Action, StringRef(), Mode); |
Reid Kleckner | c0dca6d | 2014-02-12 23:50:26 +0000 | [diff] [blame] | 304 | } |
| 305 | |
Warren Hunt | c3b1896 | 2014-04-08 22:30:47 +0000 | [diff] [blame] | 306 | template<typename ValueType> |
| 307 | void Sema::PragmaStack<ValueType>::Act(SourceLocation PragmaLocation, |
| 308 | PragmaMsStackAction Action, |
| 309 | llvm::StringRef StackSlotLabel, |
| 310 | ValueType Value) { |
| 311 | if (Action == PSK_Reset) { |
Denis Zobnin | 2290dac | 2016-04-29 11:27:00 +0000 | [diff] [blame] | 312 | CurrentValue = DefaultValue; |
Alex Lorenz | 7d7e1e0 | 2017-03-31 15:36:21 +0000 | [diff] [blame] | 313 | CurrentPragmaLocation = PragmaLocation; |
Warren Hunt | c3b1896 | 2014-04-08 22:30:47 +0000 | [diff] [blame] | 314 | return; |
| 315 | } |
| 316 | if (Action & PSK_Push) |
Alex Lorenz | 45b4014 | 2017-07-28 14:41:21 +0000 | [diff] [blame] | 317 | Stack.emplace_back(StackSlotLabel, CurrentValue, CurrentPragmaLocation, |
| 318 | PragmaLocation); |
Warren Hunt | c3b1896 | 2014-04-08 22:30:47 +0000 | [diff] [blame] | 319 | else if (Action & PSK_Pop) { |
| 320 | if (!StackSlotLabel.empty()) { |
| 321 | // 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] | 322 | auto I = llvm::find_if(llvm::reverse(Stack), [&](const Slot &x) { |
| 323 | return x.StackSlotLabel == StackSlotLabel; |
| 324 | }); |
Warren Hunt | c3b1896 | 2014-04-08 22:30:47 +0000 | [diff] [blame] | 325 | // If we found the label so pop from there. |
| 326 | if (I != Stack.rend()) { |
| 327 | CurrentValue = I->Value; |
| 328 | CurrentPragmaLocation = I->PragmaLocation; |
| 329 | Stack.erase(std::prev(I.base()), Stack.end()); |
| 330 | } |
| 331 | } else if (!Stack.empty()) { |
George Burgess IV | a75c71d | 2018-05-26 02:29:14 +0000 | [diff] [blame] | 332 | // We do not have a label, just pop the last entry. |
Warren Hunt | c3b1896 | 2014-04-08 22:30:47 +0000 | [diff] [blame] | 333 | CurrentValue = Stack.back().Value; |
| 334 | CurrentPragmaLocation = Stack.back().PragmaLocation; |
| 335 | Stack.pop_back(); |
| 336 | } |
| 337 | } |
| 338 | if (Action & PSK_Set) { |
| 339 | CurrentValue = Value; |
| 340 | CurrentPragmaLocation = PragmaLocation; |
| 341 | } |
| 342 | } |
| 343 | |
Craig Topper | bf3e327 | 2014-08-30 16:55:52 +0000 | [diff] [blame] | 344 | bool Sema::UnifySection(StringRef SectionName, |
Warren Hunt | c3b1896 | 2014-04-08 22:30:47 +0000 | [diff] [blame] | 345 | int SectionFlags, |
| 346 | DeclaratorDecl *Decl) { |
Hans Wennborg | 899ded9 | 2014-10-16 20:52:46 +0000 | [diff] [blame] | 347 | auto Section = Context.SectionInfos.find(SectionName); |
| 348 | if (Section == Context.SectionInfos.end()) { |
| 349 | Context.SectionInfos[SectionName] = |
| 350 | ASTContext::SectionInfo(Decl, SourceLocation(), SectionFlags); |
Warren Hunt | c3b1896 | 2014-04-08 22:30:47 +0000 | [diff] [blame] | 351 | return false; |
| 352 | } |
| 353 | // A pre-declared section takes precedence w/o diagnostic. |
| 354 | if (Section->second.SectionFlags == SectionFlags || |
Hans Wennborg | 899ded9 | 2014-10-16 20:52:46 +0000 | [diff] [blame] | 355 | !(Section->second.SectionFlags & ASTContext::PSF_Implicit)) |
Warren Hunt | c3b1896 | 2014-04-08 22:30:47 +0000 | [diff] [blame] | 356 | return false; |
| 357 | auto OtherDecl = Section->second.Decl; |
| 358 | Diag(Decl->getLocation(), diag::err_section_conflict) |
| 359 | << Decl << OtherDecl; |
| 360 | Diag(OtherDecl->getLocation(), diag::note_declared_at) |
| 361 | << OtherDecl->getName(); |
| 362 | if (auto A = Decl->getAttr<SectionAttr>()) |
| 363 | if (A->isImplicit()) |
| 364 | Diag(A->getLocation(), diag::note_pragma_entered_here); |
| 365 | if (auto A = OtherDecl->getAttr<SectionAttr>()) |
| 366 | if (A->isImplicit()) |
| 367 | Diag(A->getLocation(), diag::note_pragma_entered_here); |
Ehsan Akhgari | 0b51060 | 2014-09-22 19:46:39 +0000 | [diff] [blame] | 368 | return true; |
Warren Hunt | c3b1896 | 2014-04-08 22:30:47 +0000 | [diff] [blame] | 369 | } |
| 370 | |
Craig Topper | bf3e327 | 2014-08-30 16:55:52 +0000 | [diff] [blame] | 371 | bool Sema::UnifySection(StringRef SectionName, |
Warren Hunt | c3b1896 | 2014-04-08 22:30:47 +0000 | [diff] [blame] | 372 | int SectionFlags, |
| 373 | SourceLocation PragmaSectionLocation) { |
Hans Wennborg | 899ded9 | 2014-10-16 20:52:46 +0000 | [diff] [blame] | 374 | auto Section = Context.SectionInfos.find(SectionName); |
| 375 | if (Section != Context.SectionInfos.end()) { |
Warren Hunt | c3b1896 | 2014-04-08 22:30:47 +0000 | [diff] [blame] | 376 | if (Section->second.SectionFlags == SectionFlags) |
| 377 | return false; |
Hans Wennborg | 899ded9 | 2014-10-16 20:52:46 +0000 | [diff] [blame] | 378 | if (!(Section->second.SectionFlags & ASTContext::PSF_Implicit)) { |
Warren Hunt | c3b1896 | 2014-04-08 22:30:47 +0000 | [diff] [blame] | 379 | Diag(PragmaSectionLocation, diag::err_section_conflict) |
| 380 | << "this" << "a prior #pragma section"; |
| 381 | Diag(Section->second.PragmaSectionLocation, |
| 382 | diag::note_pragma_entered_here); |
| 383 | return true; |
| 384 | } |
| 385 | } |
Hans Wennborg | 899ded9 | 2014-10-16 20:52:46 +0000 | [diff] [blame] | 386 | Context.SectionInfos[SectionName] = |
| 387 | ASTContext::SectionInfo(nullptr, PragmaSectionLocation, SectionFlags); |
Warren Hunt | c3b1896 | 2014-04-08 22:30:47 +0000 | [diff] [blame] | 388 | return false; |
| 389 | } |
| 390 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 391 | /// Called on well formed \#pragma bss_seg(). |
Warren Hunt | c3b1896 | 2014-04-08 22:30:47 +0000 | [diff] [blame] | 392 | void Sema::ActOnPragmaMSSeg(SourceLocation PragmaLocation, |
| 393 | PragmaMsStackAction Action, |
| 394 | llvm::StringRef StackSlotLabel, |
| 395 | StringLiteral *SegmentName, |
| 396 | llvm::StringRef PragmaName) { |
| 397 | PragmaStack<StringLiteral *> *Stack = |
| 398 | llvm::StringSwitch<PragmaStack<StringLiteral *> *>(PragmaName) |
| 399 | .Case("data_seg", &DataSegStack) |
| 400 | .Case("bss_seg", &BSSSegStack) |
| 401 | .Case("const_seg", &ConstSegStack) |
| 402 | .Case("code_seg", &CodeSegStack); |
| 403 | if (Action & PSK_Pop && Stack->Stack.empty()) |
| 404 | Diag(PragmaLocation, diag::warn_pragma_pop_failed) << PragmaName |
| 405 | << "stack empty"; |
Nico Weber | 9801621 | 2019-07-09 00:02:23 +0000 | [diff] [blame^] | 406 | if (SegmentName) { |
| 407 | if (!checkSectionName(SegmentName->getBeginLoc(), SegmentName->getString())) |
| 408 | return; |
| 409 | |
| 410 | if (SegmentName->getString() == ".drectve" && |
| 411 | Context.getTargetInfo().getCXXABI().isMicrosoft()) |
| 412 | Diag(PragmaLocation, diag::warn_attribute_section_drectve) << PragmaName; |
| 413 | } |
| 414 | |
Warren Hunt | c3b1896 | 2014-04-08 22:30:47 +0000 | [diff] [blame] | 415 | Stack->Act(PragmaLocation, Action, StackSlotLabel, SegmentName); |
| 416 | } |
| 417 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 418 | /// Called on well formed \#pragma bss_seg(). |
Warren Hunt | c3b1896 | 2014-04-08 22:30:47 +0000 | [diff] [blame] | 419 | void Sema::ActOnPragmaMSSection(SourceLocation PragmaLocation, |
| 420 | int SectionFlags, StringLiteral *SegmentName) { |
| 421 | UnifySection(SegmentName->getString(), SectionFlags, PragmaLocation); |
| 422 | } |
| 423 | |
Reid Kleckner | 1a711b1 | 2014-07-22 00:53:05 +0000 | [diff] [blame] | 424 | void Sema::ActOnPragmaMSInitSeg(SourceLocation PragmaLocation, |
| 425 | StringLiteral *SegmentName) { |
| 426 | // There's no stack to maintain, so we just have a current section. When we |
| 427 | // see the default section, reset our current section back to null so we stop |
| 428 | // tacking on unnecessary attributes. |
| 429 | CurInitSeg = SegmentName->getString() == ".CRT$XCU" ? nullptr : SegmentName; |
| 430 | CurInitSegLoc = PragmaLocation; |
| 431 | } |
| 432 | |
Argyrios Kyrtzidis | ee56962 | 2011-01-17 18:58:44 +0000 | [diff] [blame] | 433 | void Sema::ActOnPragmaUnused(const Token &IdTok, Scope *curScope, |
| 434 | SourceLocation PragmaLoc) { |
Ted Kremenek | fd14fad | 2009-03-23 22:28:25 +0000 | [diff] [blame] | 435 | |
Argyrios Kyrtzidis | ee56962 | 2011-01-17 18:58:44 +0000 | [diff] [blame] | 436 | IdentifierInfo *Name = IdTok.getIdentifierInfo(); |
| 437 | LookupResult Lookup(*this, Name, IdTok.getLocation(), LookupOrdinaryName); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 438 | LookupParsedName(Lookup, curScope, nullptr, true); |
Ted Kremenek | fd14fad | 2009-03-23 22:28:25 +0000 | [diff] [blame] | 439 | |
Argyrios Kyrtzidis | ee56962 | 2011-01-17 18:58:44 +0000 | [diff] [blame] | 440 | if (Lookup.empty()) { |
| 441 | Diag(PragmaLoc, diag::warn_pragma_unused_undeclared_var) |
| 442 | << Name << SourceRange(IdTok.getLocation()); |
| 443 | return; |
Ted Kremenek | fd14fad | 2009-03-23 22:28:25 +0000 | [diff] [blame] | 444 | } |
Argyrios Kyrtzidis | ee56962 | 2011-01-17 18:58:44 +0000 | [diff] [blame] | 445 | |
| 446 | VarDecl *VD = Lookup.getAsSingle<VarDecl>(); |
Argyrios Kyrtzidis | ff115a2 | 2011-01-27 18:16:48 +0000 | [diff] [blame] | 447 | if (!VD) { |
| 448 | Diag(PragmaLoc, diag::warn_pragma_unused_expected_var_arg) |
Argyrios Kyrtzidis | ee56962 | 2011-01-17 18:58:44 +0000 | [diff] [blame] | 449 | << Name << SourceRange(IdTok.getLocation()); |
| 450 | return; |
| 451 | } |
| 452 | |
| 453 | // Warn if this was used before being marked unused. |
| 454 | if (VD->isUsed()) |
| 455 | Diag(PragmaLoc, diag::warn_used_but_marked_unused) << Name; |
| 456 | |
Aaron Ballman | 0bcd6c1 | 2016-03-09 16:48:08 +0000 | [diff] [blame] | 457 | VD->addAttr(UnusedAttr::CreateImplicit(Context, UnusedAttr::GNU_unused, |
| 458 | IdTok.getLocation())); |
Ted Kremenek | fd14fad | 2009-03-23 22:28:25 +0000 | [diff] [blame] | 459 | } |
Eli Friedman | 570024a | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 460 | |
John McCall | 32f5fe1 | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 461 | void Sema::AddCFAuditedAttribute(Decl *D) { |
| 462 | SourceLocation Loc = PP.getPragmaARCCFCodeAuditedLoc(); |
| 463 | if (!Loc.isValid()) return; |
| 464 | |
| 465 | // Don't add a redundant or conflicting attribute. |
| 466 | if (D->hasAttr<CFAuditedTransferAttr>() || |
| 467 | D->hasAttr<CFUnknownTransferAttr>()) |
| 468 | return; |
| 469 | |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 470 | D->addAttr(CFAuditedTransferAttr::CreateImplicit(Context, Loc)); |
John McCall | 32f5fe1 | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 471 | } |
| 472 | |
Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 473 | namespace { |
| 474 | |
| 475 | Optional<attr::SubjectMatchRule> |
| 476 | getParentAttrMatcherRule(attr::SubjectMatchRule Rule) { |
| 477 | using namespace attr; |
| 478 | switch (Rule) { |
| 479 | default: |
| 480 | return None; |
| 481 | #define ATTR_MATCH_RULE(Value, Spelling, IsAbstract) |
| 482 | #define ATTR_MATCH_SUB_RULE(Value, Spelling, IsAbstract, Parent, IsNegated) \ |
| 483 | case Value: \ |
| 484 | return Parent; |
| 485 | #include "clang/Basic/AttrSubMatchRulesList.inc" |
| 486 | } |
| 487 | } |
| 488 | |
| 489 | bool isNegatedAttrMatcherSubRule(attr::SubjectMatchRule Rule) { |
| 490 | using namespace attr; |
| 491 | switch (Rule) { |
| 492 | default: |
| 493 | return false; |
| 494 | #define ATTR_MATCH_RULE(Value, Spelling, IsAbstract) |
| 495 | #define ATTR_MATCH_SUB_RULE(Value, Spelling, IsAbstract, Parent, IsNegated) \ |
| 496 | case Value: \ |
| 497 | return IsNegated; |
| 498 | #include "clang/Basic/AttrSubMatchRulesList.inc" |
| 499 | } |
| 500 | } |
| 501 | |
| 502 | CharSourceRange replacementRangeForListElement(const Sema &S, |
| 503 | SourceRange Range) { |
| 504 | // Make sure that the ',' is removed as well. |
| 505 | SourceLocation AfterCommaLoc = Lexer::findLocationAfterToken( |
| 506 | Range.getEnd(), tok::comma, S.getSourceManager(), S.getLangOpts(), |
| 507 | /*SkipTrailingWhitespaceAndNewLine=*/false); |
| 508 | if (AfterCommaLoc.isValid()) |
| 509 | return CharSourceRange::getCharRange(Range.getBegin(), AfterCommaLoc); |
| 510 | else |
| 511 | return CharSourceRange::getTokenRange(Range); |
| 512 | } |
| 513 | |
| 514 | std::string |
| 515 | attrMatcherRuleListToString(ArrayRef<attr::SubjectMatchRule> Rules) { |
| 516 | std::string Result; |
| 517 | llvm::raw_string_ostream OS(Result); |
| 518 | for (const auto &I : llvm::enumerate(Rules)) { |
| 519 | if (I.index()) |
| 520 | OS << (I.index() == Rules.size() - 1 ? ", and " : ", "); |
| 521 | OS << "'" << attr::getSubjectMatchRuleSpelling(I.value()) << "'"; |
| 522 | } |
| 523 | return OS.str(); |
| 524 | } |
| 525 | |
| 526 | } // end anonymous namespace |
| 527 | |
Erik Pilkington | 7d18094 | 2018-10-29 17:38:42 +0000 | [diff] [blame] | 528 | void Sema::ActOnPragmaAttributeAttribute( |
| 529 | ParsedAttr &Attribute, SourceLocation PragmaLoc, |
| 530 | attr::ParsedSubjectMatchRuleSet Rules) { |
Alex Lorenz | 3cfe9d5 | 2019-01-24 19:14:39 +0000 | [diff] [blame] | 531 | Attribute.setIsPragmaClangAttribute(); |
Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 532 | SmallVector<attr::SubjectMatchRule, 4> SubjectMatchRules; |
| 533 | // Gather the subject match rules that are supported by the attribute. |
| 534 | SmallVector<std::pair<attr::SubjectMatchRule, bool>, 4> |
| 535 | StrictSubjectMatchRuleSet; |
| 536 | Attribute.getMatchRules(LangOpts, StrictSubjectMatchRuleSet); |
| 537 | |
| 538 | // Figure out which subject matching rules are valid. |
| 539 | if (StrictSubjectMatchRuleSet.empty()) { |
| 540 | // Check for contradicting match rules. Contradicting match rules are |
| 541 | // either: |
| 542 | // - a top-level rule and one of its sub-rules. E.g. variable and |
| 543 | // variable(is_parameter). |
| 544 | // - a sub-rule and a sibling that's negated. E.g. |
| 545 | // variable(is_thread_local) and variable(unless(is_parameter)) |
Alex Lorenz | 26b4765 | 2017-04-18 20:54:23 +0000 | [diff] [blame] | 546 | llvm::SmallDenseMap<int, std::pair<int, SourceRange>, 2> |
Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 547 | RulesToFirstSpecifiedNegatedSubRule; |
| 548 | for (const auto &Rule : Rules) { |
Alex Lorenz | 26b4765 | 2017-04-18 20:54:23 +0000 | [diff] [blame] | 549 | attr::SubjectMatchRule MatchRule = attr::SubjectMatchRule(Rule.first); |
Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 550 | Optional<attr::SubjectMatchRule> ParentRule = |
Alex Lorenz | 26b4765 | 2017-04-18 20:54:23 +0000 | [diff] [blame] | 551 | getParentAttrMatcherRule(MatchRule); |
Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 552 | if (!ParentRule) |
| 553 | continue; |
| 554 | auto It = Rules.find(*ParentRule); |
| 555 | if (It != Rules.end()) { |
| 556 | // A sub-rule contradicts a parent rule. |
| 557 | Diag(Rule.second.getBegin(), |
| 558 | diag::err_pragma_attribute_matcher_subrule_contradicts_rule) |
Alex Lorenz | 26b4765 | 2017-04-18 20:54:23 +0000 | [diff] [blame] | 559 | << attr::getSubjectMatchRuleSpelling(MatchRule) |
Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 560 | << attr::getSubjectMatchRuleSpelling(*ParentRule) << It->second |
| 561 | << FixItHint::CreateRemoval( |
| 562 | replacementRangeForListElement(*this, Rule.second)); |
| 563 | // Keep going without removing this rule as it won't change the set of |
| 564 | // declarations that receive the attribute. |
| 565 | continue; |
| 566 | } |
Alex Lorenz | 26b4765 | 2017-04-18 20:54:23 +0000 | [diff] [blame] | 567 | if (isNegatedAttrMatcherSubRule(MatchRule)) |
Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 568 | RulesToFirstSpecifiedNegatedSubRule.insert( |
| 569 | std::make_pair(*ParentRule, Rule)); |
| 570 | } |
| 571 | bool IgnoreNegatedSubRules = false; |
| 572 | for (const auto &Rule : Rules) { |
Alex Lorenz | 26b4765 | 2017-04-18 20:54:23 +0000 | [diff] [blame] | 573 | attr::SubjectMatchRule MatchRule = attr::SubjectMatchRule(Rule.first); |
Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 574 | Optional<attr::SubjectMatchRule> ParentRule = |
Alex Lorenz | 26b4765 | 2017-04-18 20:54:23 +0000 | [diff] [blame] | 575 | getParentAttrMatcherRule(MatchRule); |
Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 576 | if (!ParentRule) |
| 577 | continue; |
| 578 | auto It = RulesToFirstSpecifiedNegatedSubRule.find(*ParentRule); |
| 579 | if (It != RulesToFirstSpecifiedNegatedSubRule.end() && |
| 580 | It->second != Rule) { |
| 581 | // Negated sub-rule contradicts another sub-rule. |
| 582 | Diag( |
| 583 | It->second.second.getBegin(), |
| 584 | diag:: |
| 585 | err_pragma_attribute_matcher_negated_subrule_contradicts_subrule) |
Alex Lorenz | 26b4765 | 2017-04-18 20:54:23 +0000 | [diff] [blame] | 586 | << attr::getSubjectMatchRuleSpelling( |
| 587 | attr::SubjectMatchRule(It->second.first)) |
| 588 | << attr::getSubjectMatchRuleSpelling(MatchRule) << Rule.second |
Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 589 | << FixItHint::CreateRemoval( |
| 590 | replacementRangeForListElement(*this, It->second.second)); |
| 591 | // Keep going but ignore all of the negated sub-rules. |
| 592 | IgnoreNegatedSubRules = true; |
| 593 | RulesToFirstSpecifiedNegatedSubRule.erase(It); |
| 594 | } |
| 595 | } |
| 596 | |
| 597 | if (!IgnoreNegatedSubRules) { |
| 598 | for (const auto &Rule : Rules) |
Alex Lorenz | 26b4765 | 2017-04-18 20:54:23 +0000 | [diff] [blame] | 599 | SubjectMatchRules.push_back(attr::SubjectMatchRule(Rule.first)); |
Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 600 | } else { |
| 601 | for (const auto &Rule : Rules) { |
Alex Lorenz | 26b4765 | 2017-04-18 20:54:23 +0000 | [diff] [blame] | 602 | if (!isNegatedAttrMatcherSubRule(attr::SubjectMatchRule(Rule.first))) |
| 603 | SubjectMatchRules.push_back(attr::SubjectMatchRule(Rule.first)); |
Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 604 | } |
| 605 | } |
| 606 | Rules.clear(); |
| 607 | } else { |
| 608 | for (const auto &Rule : StrictSubjectMatchRuleSet) { |
| 609 | if (Rules.erase(Rule.first)) { |
| 610 | // Add the rule to the set of attribute receivers only if it's supported |
| 611 | // in the current language mode. |
| 612 | if (Rule.second) |
| 613 | SubjectMatchRules.push_back(Rule.first); |
| 614 | } |
| 615 | } |
| 616 | } |
| 617 | |
| 618 | if (!Rules.empty()) { |
| 619 | auto Diagnostic = |
| 620 | Diag(PragmaLoc, diag::err_pragma_attribute_invalid_matchers) |
| 621 | << Attribute.getName(); |
| 622 | SmallVector<attr::SubjectMatchRule, 2> ExtraRules; |
| 623 | for (const auto &Rule : Rules) { |
Alex Lorenz | 26b4765 | 2017-04-18 20:54:23 +0000 | [diff] [blame] | 624 | ExtraRules.push_back(attr::SubjectMatchRule(Rule.first)); |
Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 625 | Diagnostic << FixItHint::CreateRemoval( |
| 626 | replacementRangeForListElement(*this, Rule.second)); |
| 627 | } |
| 628 | Diagnostic << attrMatcherRuleListToString(ExtraRules); |
| 629 | } |
| 630 | |
Erik Pilkington | 7d18094 | 2018-10-29 17:38:42 +0000 | [diff] [blame] | 631 | if (PragmaAttributeStack.empty()) { |
| 632 | Diag(PragmaLoc, diag::err_pragma_attr_attr_no_push); |
| 633 | return; |
| 634 | } |
| 635 | |
| 636 | PragmaAttributeStack.back().Entries.push_back( |
Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 637 | {PragmaLoc, &Attribute, std::move(SubjectMatchRules), /*IsUsed=*/false}); |
| 638 | } |
| 639 | |
Erik Pilkington | 0876cae | 2018-12-20 22:32:04 +0000 | [diff] [blame] | 640 | void Sema::ActOnPragmaAttributeEmptyPush(SourceLocation PragmaLoc, |
| 641 | const IdentifierInfo *Namespace) { |
Erik Pilkington | 7d18094 | 2018-10-29 17:38:42 +0000 | [diff] [blame] | 642 | PragmaAttributeStack.emplace_back(); |
| 643 | PragmaAttributeStack.back().Loc = PragmaLoc; |
Erik Pilkington | 0876cae | 2018-12-20 22:32:04 +0000 | [diff] [blame] | 644 | PragmaAttributeStack.back().Namespace = Namespace; |
Erik Pilkington | 7d18094 | 2018-10-29 17:38:42 +0000 | [diff] [blame] | 645 | } |
| 646 | |
Erik Pilkington | 0876cae | 2018-12-20 22:32:04 +0000 | [diff] [blame] | 647 | void Sema::ActOnPragmaAttributePop(SourceLocation PragmaLoc, |
| 648 | const IdentifierInfo *Namespace) { |
Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 649 | if (PragmaAttributeStack.empty()) { |
Erik Pilkington | 0876cae | 2018-12-20 22:32:04 +0000 | [diff] [blame] | 650 | Diag(PragmaLoc, diag::err_pragma_attribute_stack_mismatch) << 1; |
Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 651 | return; |
| 652 | } |
Erik Pilkington | 7d18094 | 2018-10-29 17:38:42 +0000 | [diff] [blame] | 653 | |
Erik Pilkington | 0876cae | 2018-12-20 22:32:04 +0000 | [diff] [blame] | 654 | // Dig back through the stack trying to find the most recently pushed group |
| 655 | // that in Namespace. Note that this works fine if no namespace is present, |
| 656 | // think of push/pops without namespaces as having an implicit "nullptr" |
| 657 | // namespace. |
| 658 | for (size_t Index = PragmaAttributeStack.size(); Index;) { |
| 659 | --Index; |
| 660 | if (PragmaAttributeStack[Index].Namespace == Namespace) { |
| 661 | for (const PragmaAttributeEntry &Entry : |
| 662 | PragmaAttributeStack[Index].Entries) { |
| 663 | if (!Entry.IsUsed) { |
| 664 | assert(Entry.Attribute && "Expected an attribute"); |
| 665 | Diag(Entry.Attribute->getLoc(), diag::warn_pragma_attribute_unused) |
| 666 | << *Entry.Attribute; |
| 667 | Diag(PragmaLoc, diag::note_pragma_attribute_region_ends_here); |
| 668 | } |
| 669 | } |
| 670 | PragmaAttributeStack.erase(PragmaAttributeStack.begin() + Index); |
| 671 | return; |
Erik Pilkington | 7d18094 | 2018-10-29 17:38:42 +0000 | [diff] [blame] | 672 | } |
Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 673 | } |
Erik Pilkington | 7d18094 | 2018-10-29 17:38:42 +0000 | [diff] [blame] | 674 | |
Erik Pilkington | 0876cae | 2018-12-20 22:32:04 +0000 | [diff] [blame] | 675 | if (Namespace) |
| 676 | Diag(PragmaLoc, diag::err_pragma_attribute_stack_mismatch) |
| 677 | << 0 << Namespace->getName(); |
| 678 | else |
| 679 | Diag(PragmaLoc, diag::err_pragma_attribute_stack_mismatch) << 1; |
Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 680 | } |
| 681 | |
| 682 | void Sema::AddPragmaAttributes(Scope *S, Decl *D) { |
| 683 | if (PragmaAttributeStack.empty()) |
| 684 | return; |
Erik Pilkington | 7d18094 | 2018-10-29 17:38:42 +0000 | [diff] [blame] | 685 | for (auto &Group : PragmaAttributeStack) { |
| 686 | for (auto &Entry : Group.Entries) { |
| 687 | ParsedAttr *Attribute = Entry.Attribute; |
| 688 | assert(Attribute && "Expected an attribute"); |
Alex Lorenz | 3cfe9d5 | 2019-01-24 19:14:39 +0000 | [diff] [blame] | 689 | assert(Attribute->isPragmaClangAttribute() && |
| 690 | "expected #pragma clang attribute"); |
Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 691 | |
Erik Pilkington | 7d18094 | 2018-10-29 17:38:42 +0000 | [diff] [blame] | 692 | // Ensure that the attribute can be applied to the given declaration. |
| 693 | bool Applies = false; |
| 694 | for (const auto &Rule : Entry.MatchRules) { |
| 695 | if (Attribute->appliesToDecl(D, Rule)) { |
| 696 | Applies = true; |
| 697 | break; |
| 698 | } |
Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 699 | } |
Erik Pilkington | 7d18094 | 2018-10-29 17:38:42 +0000 | [diff] [blame] | 700 | if (!Applies) |
| 701 | continue; |
| 702 | Entry.IsUsed = true; |
| 703 | PragmaAttributeCurrentTargetDecl = D; |
| 704 | ParsedAttributesView Attrs; |
| 705 | Attrs.addAtEnd(Attribute); |
| 706 | ProcessDeclAttributeList(S, D, Attrs); |
| 707 | PragmaAttributeCurrentTargetDecl = nullptr; |
Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 708 | } |
Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 709 | } |
| 710 | } |
| 711 | |
| 712 | void Sema::PrintPragmaAttributeInstantiationPoint() { |
| 713 | assert(PragmaAttributeCurrentTargetDecl && "Expected an active declaration"); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 714 | Diags.Report(PragmaAttributeCurrentTargetDecl->getBeginLoc(), |
Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 715 | diag::note_pragma_attribute_applied_decl_here); |
| 716 | } |
| 717 | |
| 718 | void Sema::DiagnoseUnterminatedPragmaAttribute() { |
| 719 | if (PragmaAttributeStack.empty()) |
| 720 | return; |
| 721 | Diag(PragmaAttributeStack.back().Loc, diag::err_pragma_attribute_no_pop_eof); |
| 722 | } |
| 723 | |
Dario Domizioli | 13a0a38 | 2014-05-23 12:13:25 +0000 | [diff] [blame] | 724 | void Sema::ActOnPragmaOptimize(bool On, SourceLocation PragmaLoc) { |
| 725 | if(On) |
| 726 | OptimizeOffPragmaLocation = SourceLocation(); |
| 727 | else |
| 728 | OptimizeOffPragmaLocation = PragmaLoc; |
| 729 | } |
| 730 | |
| 731 | void Sema::AddRangeBasedOptnone(FunctionDecl *FD) { |
| 732 | // In the future, check other pragmas if they're implemented (e.g. pragma |
| 733 | // optimize 0 will probably map to this functionality too). |
| 734 | if(OptimizeOffPragmaLocation.isValid()) |
| 735 | AddOptnoneAttributeIfNoConflicts(FD, OptimizeOffPragmaLocation); |
| 736 | } |
| 737 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 738 | void Sema::AddOptnoneAttributeIfNoConflicts(FunctionDecl *FD, |
Dario Domizioli | 13a0a38 | 2014-05-23 12:13:25 +0000 | [diff] [blame] | 739 | SourceLocation Loc) { |
| 740 | // Don't add a conflicting attribute. No diagnostic is needed. |
| 741 | if (FD->hasAttr<MinSizeAttr>() || FD->hasAttr<AlwaysInlineAttr>()) |
| 742 | return; |
| 743 | |
| 744 | // Add attributes only if required. Optnone requires noinline as well, but if |
| 745 | // either is already present then don't bother adding them. |
| 746 | if (!FD->hasAttr<OptimizeNoneAttr>()) |
| 747 | FD->addAttr(OptimizeNoneAttr::CreateImplicit(Context, Loc)); |
| 748 | if (!FD->hasAttr<NoInlineAttr>()) |
| 749 | FD->addAttr(NoInlineAttr::CreateImplicit(Context, Loc)); |
| 750 | } |
| 751 | |
John McCall | 2faf32c | 2010-12-10 02:59:44 +0000 | [diff] [blame] | 752 | typedef std::vector<std::pair<unsigned, SourceLocation> > VisStack; |
Alp Toker | ceb95c4 | 2014-03-02 03:20:16 +0000 | [diff] [blame] | 753 | enum : unsigned { NoVisibility = ~0U }; |
Eli Friedman | 570024a | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 754 | |
| 755 | void Sema::AddPushedVisibilityAttribute(Decl *D) { |
| 756 | if (!VisContext) |
| 757 | return; |
| 758 | |
Rafael Espindola | 54606d5 | 2012-12-25 07:31:49 +0000 | [diff] [blame] | 759 | NamedDecl *ND = dyn_cast<NamedDecl>(D); |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 760 | if (ND && ND->getExplicitVisibility(NamedDecl::VisibilityForValue)) |
Eli Friedman | 570024a | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 761 | return; |
| 762 | |
| 763 | VisStack *Stack = static_cast<VisStack*>(VisContext); |
John McCall | 2faf32c | 2010-12-10 02:59:44 +0000 | [diff] [blame] | 764 | unsigned rawType = Stack->back().first; |
| 765 | if (rawType == NoVisibility) return; |
| 766 | |
| 767 | VisibilityAttr::VisibilityType type |
| 768 | = (VisibilityAttr::VisibilityType) rawType; |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 769 | SourceLocation loc = Stack->back().second; |
Eli Friedman | 570024a | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 770 | |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 771 | D->addAttr(VisibilityAttr::CreateImplicit(Context, type, loc)); |
Eli Friedman | 570024a | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 772 | } |
| 773 | |
| 774 | /// FreeVisContext - Deallocate and null out VisContext. |
| 775 | void Sema::FreeVisContext() { |
| 776 | delete static_cast<VisStack*>(VisContext); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 777 | VisContext = nullptr; |
Eli Friedman | 570024a | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 778 | } |
| 779 | |
John McCall | 2faf32c | 2010-12-10 02:59:44 +0000 | [diff] [blame] | 780 | static void PushPragmaVisibility(Sema &S, unsigned type, SourceLocation loc) { |
John McCall | b1be523 | 2010-08-26 09:15:37 +0000 | [diff] [blame] | 781 | // Put visibility on stack. |
| 782 | if (!S.VisContext) |
| 783 | S.VisContext = new VisStack; |
| 784 | |
| 785 | VisStack *Stack = static_cast<VisStack*>(S.VisContext); |
| 786 | Stack->push_back(std::make_pair(type, loc)); |
| 787 | } |
| 788 | |
Rafael Espindola | de15baf | 2012-01-21 05:43:40 +0000 | [diff] [blame] | 789 | void Sema::ActOnPragmaVisibility(const IdentifierInfo* VisType, |
Eli Friedman | 570024a | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 790 | SourceLocation PragmaLoc) { |
Rafael Espindola | de15baf | 2012-01-21 05:43:40 +0000 | [diff] [blame] | 791 | if (VisType) { |
Eli Friedman | 570024a | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 792 | // Compute visibility to use. |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 793 | VisibilityAttr::VisibilityType T; |
| 794 | if (!VisibilityAttr::ConvertStrToVisibilityType(VisType->getName(), T)) { |
| 795 | Diag(PragmaLoc, diag::warn_attribute_unknown_visibility) << VisType; |
Eli Friedman | 570024a | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 796 | return; |
| 797 | } |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 798 | PushPragmaVisibility(*this, T, PragmaLoc); |
Eli Friedman | 570024a | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 799 | } else { |
Rafael Espindola | 6d65d7b | 2012-02-01 23:24:59 +0000 | [diff] [blame] | 800 | PopPragmaVisibility(false, PragmaLoc); |
Eli Friedman | 570024a | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 801 | } |
| 802 | } |
| 803 | |
Adam Nemet | 60d3264 | 2017-04-04 21:18:36 +0000 | [diff] [blame] | 804 | void Sema::ActOnPragmaFPContract(LangOptions::FPContractModeKind FPC) { |
| 805 | switch (FPC) { |
| 806 | case LangOptions::FPC_On: |
Adam Nemet | 049a31d | 2017-03-29 21:54:24 +0000 | [diff] [blame] | 807 | FPFeatures.setAllowFPContractWithinStatement(); |
Peter Collingbourne | 564c0fa | 2011-02-14 01:42:35 +0000 | [diff] [blame] | 808 | break; |
Adam Nemet | 60d3264 | 2017-04-04 21:18:36 +0000 | [diff] [blame] | 809 | case LangOptions::FPC_Fast: |
| 810 | FPFeatures.setAllowFPContractAcrossStatement(); |
Peter Collingbourne | 564c0fa | 2011-02-14 01:42:35 +0000 | [diff] [blame] | 811 | break; |
Adam Nemet | 60d3264 | 2017-04-04 21:18:36 +0000 | [diff] [blame] | 812 | case LangOptions::FPC_Off: |
| 813 | FPFeatures.setDisallowFPContract(); |
Peter Collingbourne | 564c0fa | 2011-02-14 01:42:35 +0000 | [diff] [blame] | 814 | break; |
| 815 | } |
| 816 | } |
| 817 | |
Kevin P. Neal | 2c0bc8b | 2018-08-14 17:06:56 +0000 | [diff] [blame] | 818 | void Sema::ActOnPragmaFEnvAccess(LangOptions::FEnvAccessModeKind FPC) { |
| 819 | switch (FPC) { |
| 820 | case LangOptions::FEA_On: |
| 821 | FPFeatures.setAllowFEnvAccess(); |
| 822 | break; |
| 823 | case LangOptions::FEA_Off: |
| 824 | FPFeatures.setDisallowFEnvAccess(); |
| 825 | break; |
| 826 | } |
| 827 | } |
| 828 | |
| 829 | |
Rafael Espindola | 6d65d7b | 2012-02-01 23:24:59 +0000 | [diff] [blame] | 830 | void Sema::PushNamespaceVisibilityAttr(const VisibilityAttr *Attr, |
| 831 | SourceLocation Loc) { |
John McCall | 2faf32c | 2010-12-10 02:59:44 +0000 | [diff] [blame] | 832 | // Visibility calculations will consider the namespace's visibility. |
| 833 | // Here we just want to note that we're in a visibility context |
| 834 | // which overrides any enclosing #pragma context, but doesn't itself |
| 835 | // contribute visibility. |
Rafael Espindola | 6d65d7b | 2012-02-01 23:24:59 +0000 | [diff] [blame] | 836 | PushPragmaVisibility(*this, NoVisibility, Loc); |
Eli Friedman | 570024a | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 837 | } |
| 838 | |
Rafael Espindola | 6d65d7b | 2012-02-01 23:24:59 +0000 | [diff] [blame] | 839 | void Sema::PopPragmaVisibility(bool IsNamespaceEnd, SourceLocation EndLoc) { |
| 840 | if (!VisContext) { |
| 841 | Diag(EndLoc, diag::err_pragma_pop_visibility_mismatch); |
| 842 | return; |
Eli Friedman | 570024a | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 843 | } |
Rafael Espindola | 6d65d7b | 2012-02-01 23:24:59 +0000 | [diff] [blame] | 844 | |
| 845 | // Pop visibility from stack |
| 846 | VisStack *Stack = static_cast<VisStack*>(VisContext); |
| 847 | |
| 848 | const std::pair<unsigned, SourceLocation> *Back = &Stack->back(); |
| 849 | bool StartsWithPragma = Back->first != NoVisibility; |
| 850 | if (StartsWithPragma && IsNamespaceEnd) { |
| 851 | Diag(Back->second, diag::err_pragma_push_visibility_mismatch); |
| 852 | Diag(EndLoc, diag::note_surrounding_namespace_ends_here); |
| 853 | |
| 854 | // For better error recovery, eat all pushes inside the namespace. |
| 855 | do { |
| 856 | Stack->pop_back(); |
| 857 | Back = &Stack->back(); |
| 858 | StartsWithPragma = Back->first != NoVisibility; |
| 859 | } while (StartsWithPragma); |
| 860 | } else if (!StartsWithPragma && !IsNamespaceEnd) { |
| 861 | Diag(EndLoc, diag::err_pragma_pop_visibility_mismatch); |
| 862 | Diag(Back->second, diag::note_surrounding_namespace_starts_here); |
| 863 | return; |
| 864 | } |
| 865 | |
| 866 | Stack->pop_back(); |
| 867 | // To simplify the implementation, never keep around an empty stack. |
| 868 | if (Stack->empty()) |
| 869 | FreeVisContext(); |
Eli Friedman | 570024a | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 870 | } |