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