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 | } |
Chris Lattner | 31180bb | 2009-02-17 01:09:29 +0000 | [diff] [blame] | 64 | } |
| 65 | |
Fariborz Jahanian | 6b4e26b | 2011-04-26 17:54:40 +0000 | [diff] [blame] | 66 | void Sema::AddMsStructLayoutForRecord(RecordDecl *RD) { |
Reid Kleckner | c0dca6d | 2014-02-12 23:50:26 +0000 | [diff] [blame] | 67 | if (MSStructPragmaOn) |
David Majnemer | 8ab003a | 2015-02-02 19:30:52 +0000 | [diff] [blame] | 68 | RD->addAttr(MSStructAttr::CreateImplicit(Context)); |
Reid Kleckner | c0dca6d | 2014-02-12 23:50:26 +0000 | [diff] [blame] | 69 | |
| 70 | // FIXME: We should merge AddAlignmentAttributesForRecord with |
| 71 | // AddMsStructLayoutForRecord into AddPragmaAttributesForRecord, which takes |
| 72 | // all active pragmas and applies them as attributes to class definitions. |
Denis Zobnin | 2290dac | 2016-04-29 11:27:00 +0000 | [diff] [blame] | 73 | if (VtorDispStack.CurrentValue != getLangOpts().VtorDispMode) |
Reid Kleckner | c0dca6d | 2014-02-12 23:50:26 +0000 | [diff] [blame] | 74 | RD->addAttr( |
Denis Zobnin | 2290dac | 2016-04-29 11:27:00 +0000 | [diff] [blame] | 75 | MSVtorDispAttr::CreateImplicit(Context, VtorDispStack.CurrentValue)); |
Fariborz Jahanian | 6b4e26b | 2011-04-26 17:54:40 +0000 | [diff] [blame] | 76 | } |
| 77 | |
Daniel Dunbar | 69dac58 | 2010-05-27 00:04:40 +0000 | [diff] [blame] | 78 | void Sema::ActOnPragmaOptionsAlign(PragmaOptionsAlignKind Kind, |
Eli Friedman | 68be164 | 2012-10-04 02:36:51 +0000 | [diff] [blame] | 79 | SourceLocation PragmaLoc) { |
Denis Zobnin | 10c4f45 | 2016-04-29 18:17:40 +0000 | [diff] [blame] | 80 | PragmaMsStackAction Action = Sema::PSK_Reset; |
Denis Zobnin | 3f287c2 | 2016-04-29 22:50:16 +0000 | [diff] [blame] | 81 | unsigned Alignment = 0; |
Daniel Dunbar | 69dac58 | 2010-05-27 00:04:40 +0000 | [diff] [blame] | 82 | switch (Kind) { |
Daniel Dunbar | 663e809 | 2010-05-27 18:42:09 +0000 | [diff] [blame] | 83 | // For all targets we support native and natural are the same. |
| 84 | // |
| 85 | // FIXME: This is not true on Darwin/PPC. |
| 86 | case POAK_Native: |
Daniel Dunbar | 5794c6f | 2010-05-28 19:43:33 +0000 | [diff] [blame] | 87 | case POAK_Power: |
Daniel Dunbar | a688566 | 2010-05-28 20:08:00 +0000 | [diff] [blame] | 88 | case POAK_Natural: |
Denis Zobnin | 10c4f45 | 2016-04-29 18:17:40 +0000 | [diff] [blame] | 89 | Action = Sema::PSK_Push_Set; |
| 90 | Alignment = 0; |
Daniel Dunbar | 5794c6f | 2010-05-28 19:43:33 +0000 | [diff] [blame] | 91 | break; |
| 92 | |
Daniel Dunbar | 9c84d4a | 2010-05-27 18:42:17 +0000 | [diff] [blame] | 93 | // Note that '#pragma options align=packed' is not equivalent to attribute |
| 94 | // packed, it has a different precedence relative to attribute aligned. |
| 95 | case POAK_Packed: |
Denis Zobnin | 10c4f45 | 2016-04-29 18:17:40 +0000 | [diff] [blame] | 96 | Action = Sema::PSK_Push_Set; |
| 97 | Alignment = 1; |
Daniel Dunbar | 9c84d4a | 2010-05-27 18:42:17 +0000 | [diff] [blame] | 98 | break; |
| 99 | |
Daniel Dunbar | bd60652 | 2010-05-27 00:35:16 +0000 | [diff] [blame] | 100 | case POAK_Mac68k: |
| 101 | // Check if the target supports this. |
Alp Toker | b6cc592 | 2014-05-03 03:45:55 +0000 | [diff] [blame] | 102 | if (!this->Context.getTargetInfo().hasAlignMac68kSupport()) { |
Daniel Dunbar | bd60652 | 2010-05-27 00:35:16 +0000 | [diff] [blame] | 103 | Diag(PragmaLoc, diag::err_pragma_options_align_mac68k_target_unsupported); |
| 104 | return; |
Daniel Dunbar | bd60652 | 2010-05-27 00:35:16 +0000 | [diff] [blame] | 105 | } |
Denis Zobnin | 10c4f45 | 2016-04-29 18:17:40 +0000 | [diff] [blame] | 106 | Action = Sema::PSK_Push_Set; |
| 107 | Alignment = Sema::kMac68kAlignmentSentinel; |
Daniel Dunbar | bd60652 | 2010-05-27 00:35:16 +0000 | [diff] [blame] | 108 | break; |
| 109 | |
Eli Friedman | 68be164 | 2012-10-04 02:36:51 +0000 | [diff] [blame] | 110 | case POAK_Reset: |
| 111 | // Reset just pops the top of the stack, or resets the current alignment to |
| 112 | // default. |
Denis Zobnin | 10c4f45 | 2016-04-29 18:17:40 +0000 | [diff] [blame] | 113 | Action = Sema::PSK_Pop; |
| 114 | if (PackStack.Stack.empty()) { |
| 115 | if (PackStack.CurrentValue) { |
| 116 | Action = Sema::PSK_Reset; |
| 117 | } else { |
| 118 | Diag(PragmaLoc, diag::warn_pragma_options_align_reset_failed) |
| 119 | << "stack empty"; |
| 120 | return; |
| 121 | } |
Eli Friedman | 68be164 | 2012-10-04 02:36:51 +0000 | [diff] [blame] | 122 | } |
Daniel Dunbar | 69dac58 | 2010-05-27 00:04:40 +0000 | [diff] [blame] | 123 | break; |
| 124 | } |
Denis Zobnin | 10c4f45 | 2016-04-29 18:17:40 +0000 | [diff] [blame] | 125 | |
| 126 | PackStack.Act(PragmaLoc, Action, StringRef(), Alignment); |
Daniel Dunbar | 69dac58 | 2010-05-27 00:04:40 +0000 | [diff] [blame] | 127 | } |
| 128 | |
Denis Zobnin | 10c4f45 | 2016-04-29 18:17:40 +0000 | [diff] [blame] | 129 | void Sema::ActOnPragmaPack(SourceLocation PragmaLoc, PragmaMsStackAction Action, |
| 130 | StringRef SlotLabel, Expr *alignment) { |
Chris Lattner | 2eccbc1 | 2009-02-17 00:57:29 +0000 | [diff] [blame] | 131 | Expr *Alignment = static_cast<Expr *>(alignment); |
| 132 | |
| 133 | // If specified then alignment must be a "small" power of two. |
| 134 | unsigned AlignmentVal = 0; |
| 135 | if (Alignment) { |
| 136 | llvm::APSInt Val; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 137 | |
Daniel Dunbar | e03c610 | 2009-03-06 20:45:54 +0000 | [diff] [blame] | 138 | // pack(0) is like pack(), which just works out since that is what |
| 139 | // we use 0 for in PackAttr. |
Douglas Gregor | bdb604a | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 140 | if (Alignment->isTypeDependent() || |
| 141 | Alignment->isValueDependent() || |
| 142 | !Alignment->isIntegerConstantExpr(Val, Context) || |
Daniel Dunbar | e03c610 | 2009-03-06 20:45:54 +0000 | [diff] [blame] | 143 | !(Val == 0 || Val.isPowerOf2()) || |
Chris Lattner | 2eccbc1 | 2009-02-17 00:57:29 +0000 | [diff] [blame] | 144 | Val.getZExtValue() > 16) { |
| 145 | Diag(PragmaLoc, diag::warn_pragma_pack_invalid_alignment); |
Chris Lattner | 2eccbc1 | 2009-02-17 00:57:29 +0000 | [diff] [blame] | 146 | return; // Ignore |
| 147 | } |
| 148 | |
| 149 | AlignmentVal = (unsigned) Val.getZExtValue(); |
| 150 | } |
Denis Zobnin | 10c4f45 | 2016-04-29 18:17:40 +0000 | [diff] [blame] | 151 | if (Action == Sema::PSK_Show) { |
Chris Lattner | 2eccbc1 | 2009-02-17 00:57:29 +0000 | [diff] [blame] | 152 | // Show the current alignment, making sure to show the right value |
| 153 | // for the default. |
Chris Lattner | 2eccbc1 | 2009-02-17 00:57:29 +0000 | [diff] [blame] | 154 | // FIXME: This should come from the target. |
Denis Zobnin | 10c4f45 | 2016-04-29 18:17:40 +0000 | [diff] [blame] | 155 | AlignmentVal = PackStack.CurrentValue; |
Chris Lattner | 2eccbc1 | 2009-02-17 00:57:29 +0000 | [diff] [blame] | 156 | if (AlignmentVal == 0) |
| 157 | AlignmentVal = 8; |
Denis Zobnin | 10c4f45 | 2016-04-29 18:17:40 +0000 | [diff] [blame] | 158 | if (AlignmentVal == Sema::kMac68kAlignmentSentinel) |
Daniel Dunbar | 6da1098 | 2010-05-27 05:45:51 +0000 | [diff] [blame] | 159 | Diag(PragmaLoc, diag::warn_pragma_pack_show) << "mac68k"; |
| 160 | else |
| 161 | Diag(PragmaLoc, diag::warn_pragma_pack_show) << AlignmentVal; |
Chris Lattner | 2eccbc1 | 2009-02-17 00:57:29 +0000 | [diff] [blame] | 162 | } |
Denis Zobnin | 10c4f45 | 2016-04-29 18:17:40 +0000 | [diff] [blame] | 163 | // MSDN, C/C++ Preprocessor Reference > Pragma Directives > pack: |
| 164 | // "#pragma pack(pop, identifier, n) is undefined" |
| 165 | if (Action & Sema::PSK_Pop) { |
| 166 | if (Alignment && !SlotLabel.empty()) |
| 167 | Diag(PragmaLoc, diag::warn_pragma_pack_pop_identifer_and_alignment); |
| 168 | if (PackStack.Stack.empty()) |
| 169 | Diag(PragmaLoc, diag::warn_pragma_pop_failed) << "pack" << "stack empty"; |
| 170 | } |
| 171 | |
| 172 | PackStack.Act(PragmaLoc, Action, SlotLabel, AlignmentVal); |
Chris Lattner | 2eccbc1 | 2009-02-17 00:57:29 +0000 | [diff] [blame] | 173 | } |
| 174 | |
Fariborz Jahanian | 743dda4 | 2011-04-25 18:49:15 +0000 | [diff] [blame] | 175 | void Sema::ActOnPragmaMSStruct(PragmaMSStructKind Kind) { |
| 176 | MSStructPragmaOn = (Kind == PMSST_ON); |
| 177 | } |
| 178 | |
Nico Weber | 6622029 | 2016-03-02 17:28:48 +0000 | [diff] [blame] | 179 | void Sema::ActOnPragmaMSComment(SourceLocation CommentLoc, |
| 180 | PragmaMSCommentKind Kind, StringRef Arg) { |
| 181 | auto *PCD = PragmaCommentDecl::Create( |
| 182 | Context, Context.getTranslationUnitDecl(), CommentLoc, Kind, Arg); |
| 183 | Context.getTranslationUnitDecl()->addDecl(PCD); |
| 184 | Consumer.HandleTopLevelDecl(DeclGroupRef(PCD)); |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 185 | } |
| 186 | |
Nico Weber | cbbaeb1 | 2016-03-02 19:28:54 +0000 | [diff] [blame] | 187 | void Sema::ActOnPragmaDetectMismatch(SourceLocation Loc, StringRef Name, |
| 188 | StringRef Value) { |
| 189 | auto *PDMD = PragmaDetectMismatchDecl::Create( |
| 190 | Context, Context.getTranslationUnitDecl(), Loc, Name, Value); |
| 191 | Context.getTranslationUnitDecl()->addDecl(PDMD); |
| 192 | Consumer.HandleTopLevelDecl(DeclGroupRef(PDMD)); |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 193 | } |
| 194 | |
David Majnemer | 4bb0980 | 2014-02-10 19:50:15 +0000 | [diff] [blame] | 195 | void Sema::ActOnPragmaMSPointersToMembers( |
David Majnemer | 86c318f | 2014-02-11 21:05:00 +0000 | [diff] [blame] | 196 | LangOptions::PragmaMSPointersToMembersKind RepresentationMethod, |
David Majnemer | 4bb0980 | 2014-02-10 19:50:15 +0000 | [diff] [blame] | 197 | SourceLocation PragmaLoc) { |
| 198 | MSPointerToMemberRepresentationMethod = RepresentationMethod; |
| 199 | ImplicitMSInheritanceAttrLoc = PragmaLoc; |
| 200 | } |
| 201 | |
Denis Zobnin | 2290dac | 2016-04-29 11:27:00 +0000 | [diff] [blame] | 202 | void Sema::ActOnPragmaMSVtorDisp(PragmaMsStackAction Action, |
Reid Kleckner | c0dca6d | 2014-02-12 23:50:26 +0000 | [diff] [blame] | 203 | SourceLocation PragmaLoc, |
| 204 | MSVtorDispAttr::Mode Mode) { |
Denis Zobnin | 2290dac | 2016-04-29 11:27:00 +0000 | [diff] [blame] | 205 | if (Action & PSK_Pop && VtorDispStack.Stack.empty()) |
| 206 | Diag(PragmaLoc, diag::warn_pragma_pop_failed) << "vtordisp" |
| 207 | << "stack empty"; |
| 208 | VtorDispStack.Act(PragmaLoc, Action, StringRef(), Mode); |
Reid Kleckner | c0dca6d | 2014-02-12 23:50:26 +0000 | [diff] [blame] | 209 | } |
| 210 | |
Warren Hunt | c3b1896 | 2014-04-08 22:30:47 +0000 | [diff] [blame] | 211 | template<typename ValueType> |
| 212 | void Sema::PragmaStack<ValueType>::Act(SourceLocation PragmaLocation, |
| 213 | PragmaMsStackAction Action, |
| 214 | llvm::StringRef StackSlotLabel, |
| 215 | ValueType Value) { |
| 216 | if (Action == PSK_Reset) { |
Denis Zobnin | 2290dac | 2016-04-29 11:27:00 +0000 | [diff] [blame] | 217 | CurrentValue = DefaultValue; |
Warren Hunt | c3b1896 | 2014-04-08 22:30:47 +0000 | [diff] [blame] | 218 | return; |
| 219 | } |
| 220 | if (Action & PSK_Push) |
| 221 | Stack.push_back(Slot(StackSlotLabel, CurrentValue, CurrentPragmaLocation)); |
| 222 | else if (Action & PSK_Pop) { |
| 223 | if (!StackSlotLabel.empty()) { |
| 224 | // 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] | 225 | auto I = llvm::find_if(llvm::reverse(Stack), [&](const Slot &x) { |
| 226 | return x.StackSlotLabel == StackSlotLabel; |
| 227 | }); |
Warren Hunt | c3b1896 | 2014-04-08 22:30:47 +0000 | [diff] [blame] | 228 | // If we found the label so pop from there. |
| 229 | if (I != Stack.rend()) { |
| 230 | CurrentValue = I->Value; |
| 231 | CurrentPragmaLocation = I->PragmaLocation; |
| 232 | Stack.erase(std::prev(I.base()), Stack.end()); |
| 233 | } |
| 234 | } else if (!Stack.empty()) { |
| 235 | // We don't have a label, just pop the last entry. |
| 236 | CurrentValue = Stack.back().Value; |
| 237 | CurrentPragmaLocation = Stack.back().PragmaLocation; |
| 238 | Stack.pop_back(); |
| 239 | } |
| 240 | } |
| 241 | if (Action & PSK_Set) { |
| 242 | CurrentValue = Value; |
| 243 | CurrentPragmaLocation = PragmaLocation; |
| 244 | } |
| 245 | } |
| 246 | |
Craig Topper | bf3e327 | 2014-08-30 16:55:52 +0000 | [diff] [blame] | 247 | bool Sema::UnifySection(StringRef SectionName, |
Warren Hunt | c3b1896 | 2014-04-08 22:30:47 +0000 | [diff] [blame] | 248 | int SectionFlags, |
| 249 | DeclaratorDecl *Decl) { |
Hans Wennborg | 899ded9 | 2014-10-16 20:52:46 +0000 | [diff] [blame] | 250 | auto Section = Context.SectionInfos.find(SectionName); |
| 251 | if (Section == Context.SectionInfos.end()) { |
| 252 | Context.SectionInfos[SectionName] = |
| 253 | ASTContext::SectionInfo(Decl, SourceLocation(), SectionFlags); |
Warren Hunt | c3b1896 | 2014-04-08 22:30:47 +0000 | [diff] [blame] | 254 | return false; |
| 255 | } |
| 256 | // A pre-declared section takes precedence w/o diagnostic. |
| 257 | if (Section->second.SectionFlags == SectionFlags || |
Hans Wennborg | 899ded9 | 2014-10-16 20:52:46 +0000 | [diff] [blame] | 258 | !(Section->second.SectionFlags & ASTContext::PSF_Implicit)) |
Warren Hunt | c3b1896 | 2014-04-08 22:30:47 +0000 | [diff] [blame] | 259 | return false; |
| 260 | auto OtherDecl = Section->second.Decl; |
| 261 | Diag(Decl->getLocation(), diag::err_section_conflict) |
| 262 | << Decl << OtherDecl; |
| 263 | Diag(OtherDecl->getLocation(), diag::note_declared_at) |
| 264 | << OtherDecl->getName(); |
| 265 | if (auto A = Decl->getAttr<SectionAttr>()) |
| 266 | if (A->isImplicit()) |
| 267 | Diag(A->getLocation(), diag::note_pragma_entered_here); |
| 268 | if (auto A = OtherDecl->getAttr<SectionAttr>()) |
| 269 | if (A->isImplicit()) |
| 270 | Diag(A->getLocation(), diag::note_pragma_entered_here); |
Ehsan Akhgari | 0b51060 | 2014-09-22 19:46:39 +0000 | [diff] [blame] | 271 | return true; |
Warren Hunt | c3b1896 | 2014-04-08 22:30:47 +0000 | [diff] [blame] | 272 | } |
| 273 | |
Craig Topper | bf3e327 | 2014-08-30 16:55:52 +0000 | [diff] [blame] | 274 | bool Sema::UnifySection(StringRef SectionName, |
Warren Hunt | c3b1896 | 2014-04-08 22:30:47 +0000 | [diff] [blame] | 275 | int SectionFlags, |
| 276 | SourceLocation PragmaSectionLocation) { |
Hans Wennborg | 899ded9 | 2014-10-16 20:52:46 +0000 | [diff] [blame] | 277 | auto Section = Context.SectionInfos.find(SectionName); |
| 278 | if (Section != Context.SectionInfos.end()) { |
Warren Hunt | c3b1896 | 2014-04-08 22:30:47 +0000 | [diff] [blame] | 279 | if (Section->second.SectionFlags == SectionFlags) |
| 280 | return false; |
Hans Wennborg | 899ded9 | 2014-10-16 20:52:46 +0000 | [diff] [blame] | 281 | if (!(Section->second.SectionFlags & ASTContext::PSF_Implicit)) { |
Warren Hunt | c3b1896 | 2014-04-08 22:30:47 +0000 | [diff] [blame] | 282 | Diag(PragmaSectionLocation, diag::err_section_conflict) |
| 283 | << "this" << "a prior #pragma section"; |
| 284 | Diag(Section->second.PragmaSectionLocation, |
| 285 | diag::note_pragma_entered_here); |
| 286 | return true; |
| 287 | } |
| 288 | } |
Hans Wennborg | 899ded9 | 2014-10-16 20:52:46 +0000 | [diff] [blame] | 289 | Context.SectionInfos[SectionName] = |
| 290 | ASTContext::SectionInfo(nullptr, PragmaSectionLocation, SectionFlags); |
Warren Hunt | c3b1896 | 2014-04-08 22:30:47 +0000 | [diff] [blame] | 291 | return false; |
| 292 | } |
| 293 | |
| 294 | /// \brief Called on well formed \#pragma bss_seg(). |
| 295 | void Sema::ActOnPragmaMSSeg(SourceLocation PragmaLocation, |
| 296 | PragmaMsStackAction Action, |
| 297 | llvm::StringRef StackSlotLabel, |
| 298 | StringLiteral *SegmentName, |
| 299 | llvm::StringRef PragmaName) { |
| 300 | PragmaStack<StringLiteral *> *Stack = |
| 301 | llvm::StringSwitch<PragmaStack<StringLiteral *> *>(PragmaName) |
| 302 | .Case("data_seg", &DataSegStack) |
| 303 | .Case("bss_seg", &BSSSegStack) |
| 304 | .Case("const_seg", &ConstSegStack) |
| 305 | .Case("code_seg", &CodeSegStack); |
| 306 | if (Action & PSK_Pop && Stack->Stack.empty()) |
| 307 | Diag(PragmaLocation, diag::warn_pragma_pop_failed) << PragmaName |
| 308 | << "stack empty"; |
Reid Kleckner | 2a13322 | 2015-03-04 23:39:17 +0000 | [diff] [blame] | 309 | if (SegmentName && |
| 310 | !checkSectionName(SegmentName->getLocStart(), SegmentName->getString())) |
| 311 | return; |
Warren Hunt | c3b1896 | 2014-04-08 22:30:47 +0000 | [diff] [blame] | 312 | Stack->Act(PragmaLocation, Action, StackSlotLabel, SegmentName); |
| 313 | } |
| 314 | |
| 315 | /// \brief Called on well formed \#pragma bss_seg(). |
| 316 | void Sema::ActOnPragmaMSSection(SourceLocation PragmaLocation, |
| 317 | int SectionFlags, StringLiteral *SegmentName) { |
| 318 | UnifySection(SegmentName->getString(), SectionFlags, PragmaLocation); |
| 319 | } |
| 320 | |
Reid Kleckner | 1a711b1 | 2014-07-22 00:53:05 +0000 | [diff] [blame] | 321 | void Sema::ActOnPragmaMSInitSeg(SourceLocation PragmaLocation, |
| 322 | StringLiteral *SegmentName) { |
| 323 | // There's no stack to maintain, so we just have a current section. When we |
| 324 | // see the default section, reset our current section back to null so we stop |
| 325 | // tacking on unnecessary attributes. |
| 326 | CurInitSeg = SegmentName->getString() == ".CRT$XCU" ? nullptr : SegmentName; |
| 327 | CurInitSegLoc = PragmaLocation; |
| 328 | } |
| 329 | |
Argyrios Kyrtzidis | ee56962 | 2011-01-17 18:58:44 +0000 | [diff] [blame] | 330 | void Sema::ActOnPragmaUnused(const Token &IdTok, Scope *curScope, |
| 331 | SourceLocation PragmaLoc) { |
Ted Kremenek | fd14fad | 2009-03-23 22:28:25 +0000 | [diff] [blame] | 332 | |
Argyrios Kyrtzidis | ee56962 | 2011-01-17 18:58:44 +0000 | [diff] [blame] | 333 | IdentifierInfo *Name = IdTok.getIdentifierInfo(); |
| 334 | LookupResult Lookup(*this, Name, IdTok.getLocation(), LookupOrdinaryName); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 335 | LookupParsedName(Lookup, curScope, nullptr, true); |
Ted Kremenek | fd14fad | 2009-03-23 22:28:25 +0000 | [diff] [blame] | 336 | |
Argyrios Kyrtzidis | ee56962 | 2011-01-17 18:58:44 +0000 | [diff] [blame] | 337 | if (Lookup.empty()) { |
| 338 | Diag(PragmaLoc, diag::warn_pragma_unused_undeclared_var) |
| 339 | << Name << SourceRange(IdTok.getLocation()); |
| 340 | return; |
Ted Kremenek | fd14fad | 2009-03-23 22:28:25 +0000 | [diff] [blame] | 341 | } |
Argyrios Kyrtzidis | ee56962 | 2011-01-17 18:58:44 +0000 | [diff] [blame] | 342 | |
| 343 | VarDecl *VD = Lookup.getAsSingle<VarDecl>(); |
Argyrios Kyrtzidis | ff115a2 | 2011-01-27 18:16:48 +0000 | [diff] [blame] | 344 | if (!VD) { |
| 345 | Diag(PragmaLoc, diag::warn_pragma_unused_expected_var_arg) |
Argyrios Kyrtzidis | ee56962 | 2011-01-17 18:58:44 +0000 | [diff] [blame] | 346 | << Name << SourceRange(IdTok.getLocation()); |
| 347 | return; |
| 348 | } |
| 349 | |
| 350 | // Warn if this was used before being marked unused. |
| 351 | if (VD->isUsed()) |
| 352 | Diag(PragmaLoc, diag::warn_used_but_marked_unused) << Name; |
| 353 | |
Aaron Ballman | 0bcd6c1 | 2016-03-09 16:48:08 +0000 | [diff] [blame] | 354 | VD->addAttr(UnusedAttr::CreateImplicit(Context, UnusedAttr::GNU_unused, |
| 355 | IdTok.getLocation())); |
Ted Kremenek | fd14fad | 2009-03-23 22:28:25 +0000 | [diff] [blame] | 356 | } |
Eli Friedman | 570024a | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 357 | |
John McCall | 32f5fe1 | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 358 | void Sema::AddCFAuditedAttribute(Decl *D) { |
| 359 | SourceLocation Loc = PP.getPragmaARCCFCodeAuditedLoc(); |
| 360 | if (!Loc.isValid()) return; |
| 361 | |
| 362 | // Don't add a redundant or conflicting attribute. |
| 363 | if (D->hasAttr<CFAuditedTransferAttr>() || |
| 364 | D->hasAttr<CFUnknownTransferAttr>()) |
| 365 | return; |
| 366 | |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 367 | D->addAttr(CFAuditedTransferAttr::CreateImplicit(Context, Loc)); |
John McCall | 32f5fe1 | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 368 | } |
| 369 | |
Dario Domizioli | 13a0a38 | 2014-05-23 12:13:25 +0000 | [diff] [blame] | 370 | void Sema::ActOnPragmaOptimize(bool On, SourceLocation PragmaLoc) { |
| 371 | if(On) |
| 372 | OptimizeOffPragmaLocation = SourceLocation(); |
| 373 | else |
| 374 | OptimizeOffPragmaLocation = PragmaLoc; |
| 375 | } |
| 376 | |
| 377 | void Sema::AddRangeBasedOptnone(FunctionDecl *FD) { |
| 378 | // In the future, check other pragmas if they're implemented (e.g. pragma |
| 379 | // optimize 0 will probably map to this functionality too). |
| 380 | if(OptimizeOffPragmaLocation.isValid()) |
| 381 | AddOptnoneAttributeIfNoConflicts(FD, OptimizeOffPragmaLocation); |
| 382 | } |
| 383 | |
| 384 | void Sema::AddOptnoneAttributeIfNoConflicts(FunctionDecl *FD, |
| 385 | SourceLocation Loc) { |
| 386 | // Don't add a conflicting attribute. No diagnostic is needed. |
| 387 | if (FD->hasAttr<MinSizeAttr>() || FD->hasAttr<AlwaysInlineAttr>()) |
| 388 | return; |
| 389 | |
| 390 | // Add attributes only if required. Optnone requires noinline as well, but if |
| 391 | // either is already present then don't bother adding them. |
| 392 | if (!FD->hasAttr<OptimizeNoneAttr>()) |
| 393 | FD->addAttr(OptimizeNoneAttr::CreateImplicit(Context, Loc)); |
| 394 | if (!FD->hasAttr<NoInlineAttr>()) |
| 395 | FD->addAttr(NoInlineAttr::CreateImplicit(Context, Loc)); |
| 396 | } |
| 397 | |
John McCall | 2faf32c | 2010-12-10 02:59:44 +0000 | [diff] [blame] | 398 | typedef std::vector<std::pair<unsigned, SourceLocation> > VisStack; |
Alp Toker | ceb95c4 | 2014-03-02 03:20:16 +0000 | [diff] [blame] | 399 | enum : unsigned { NoVisibility = ~0U }; |
Eli Friedman | 570024a | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 400 | |
| 401 | void Sema::AddPushedVisibilityAttribute(Decl *D) { |
| 402 | if (!VisContext) |
| 403 | return; |
| 404 | |
Rafael Espindola | 54606d5 | 2012-12-25 07:31:49 +0000 | [diff] [blame] | 405 | NamedDecl *ND = dyn_cast<NamedDecl>(D); |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 406 | if (ND && ND->getExplicitVisibility(NamedDecl::VisibilityForValue)) |
Eli Friedman | 570024a | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 407 | return; |
| 408 | |
| 409 | VisStack *Stack = static_cast<VisStack*>(VisContext); |
John McCall | 2faf32c | 2010-12-10 02:59:44 +0000 | [diff] [blame] | 410 | unsigned rawType = Stack->back().first; |
| 411 | if (rawType == NoVisibility) return; |
| 412 | |
| 413 | VisibilityAttr::VisibilityType type |
| 414 | = (VisibilityAttr::VisibilityType) rawType; |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 415 | SourceLocation loc = Stack->back().second; |
Eli Friedman | 570024a | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 416 | |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 417 | D->addAttr(VisibilityAttr::CreateImplicit(Context, type, loc)); |
Eli Friedman | 570024a | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | /// FreeVisContext - Deallocate and null out VisContext. |
| 421 | void Sema::FreeVisContext() { |
| 422 | delete static_cast<VisStack*>(VisContext); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 423 | VisContext = nullptr; |
Eli Friedman | 570024a | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 424 | } |
| 425 | |
John McCall | 2faf32c | 2010-12-10 02:59:44 +0000 | [diff] [blame] | 426 | static void PushPragmaVisibility(Sema &S, unsigned type, SourceLocation loc) { |
John McCall | b1be523 | 2010-08-26 09:15:37 +0000 | [diff] [blame] | 427 | // Put visibility on stack. |
| 428 | if (!S.VisContext) |
| 429 | S.VisContext = new VisStack; |
| 430 | |
| 431 | VisStack *Stack = static_cast<VisStack*>(S.VisContext); |
| 432 | Stack->push_back(std::make_pair(type, loc)); |
| 433 | } |
| 434 | |
Rafael Espindola | de15baf | 2012-01-21 05:43:40 +0000 | [diff] [blame] | 435 | void Sema::ActOnPragmaVisibility(const IdentifierInfo* VisType, |
Eli Friedman | 570024a | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 436 | SourceLocation PragmaLoc) { |
Rafael Espindola | de15baf | 2012-01-21 05:43:40 +0000 | [diff] [blame] | 437 | if (VisType) { |
Eli Friedman | 570024a | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 438 | // Compute visibility to use. |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 439 | VisibilityAttr::VisibilityType T; |
| 440 | if (!VisibilityAttr::ConvertStrToVisibilityType(VisType->getName(), T)) { |
| 441 | Diag(PragmaLoc, diag::warn_attribute_unknown_visibility) << VisType; |
Eli Friedman | 570024a | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 442 | return; |
| 443 | } |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 444 | PushPragmaVisibility(*this, T, PragmaLoc); |
Eli Friedman | 570024a | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 445 | } else { |
Rafael Espindola | 6d65d7b | 2012-02-01 23:24:59 +0000 | [diff] [blame] | 446 | PopPragmaVisibility(false, PragmaLoc); |
Eli Friedman | 570024a | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 447 | } |
| 448 | } |
| 449 | |
Peter Collingbourne | 564c0fa | 2011-02-14 01:42:35 +0000 | [diff] [blame] | 450 | void Sema::ActOnPragmaFPContract(tok::OnOffSwitch OOS) { |
| 451 | switch (OOS) { |
| 452 | case tok::OOS_ON: |
| 453 | FPFeatures.fp_contract = 1; |
| 454 | break; |
| 455 | case tok::OOS_OFF: |
| 456 | FPFeatures.fp_contract = 0; |
| 457 | break; |
| 458 | case tok::OOS_DEFAULT: |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 459 | FPFeatures.fp_contract = getLangOpts().DefaultFPContract; |
Peter Collingbourne | 564c0fa | 2011-02-14 01:42:35 +0000 | [diff] [blame] | 460 | break; |
| 461 | } |
| 462 | } |
| 463 | |
Rafael Espindola | 6d65d7b | 2012-02-01 23:24:59 +0000 | [diff] [blame] | 464 | void Sema::PushNamespaceVisibilityAttr(const VisibilityAttr *Attr, |
| 465 | SourceLocation Loc) { |
John McCall | 2faf32c | 2010-12-10 02:59:44 +0000 | [diff] [blame] | 466 | // Visibility calculations will consider the namespace's visibility. |
| 467 | // Here we just want to note that we're in a visibility context |
| 468 | // which overrides any enclosing #pragma context, but doesn't itself |
| 469 | // contribute visibility. |
Rafael Espindola | 6d65d7b | 2012-02-01 23:24:59 +0000 | [diff] [blame] | 470 | PushPragmaVisibility(*this, NoVisibility, Loc); |
Eli Friedman | 570024a | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 471 | } |
| 472 | |
Rafael Espindola | 6d65d7b | 2012-02-01 23:24:59 +0000 | [diff] [blame] | 473 | void Sema::PopPragmaVisibility(bool IsNamespaceEnd, SourceLocation EndLoc) { |
| 474 | if (!VisContext) { |
| 475 | Diag(EndLoc, diag::err_pragma_pop_visibility_mismatch); |
| 476 | return; |
Eli Friedman | 570024a | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 477 | } |
Rafael Espindola | 6d65d7b | 2012-02-01 23:24:59 +0000 | [diff] [blame] | 478 | |
| 479 | // Pop visibility from stack |
| 480 | VisStack *Stack = static_cast<VisStack*>(VisContext); |
| 481 | |
| 482 | const std::pair<unsigned, SourceLocation> *Back = &Stack->back(); |
| 483 | bool StartsWithPragma = Back->first != NoVisibility; |
| 484 | if (StartsWithPragma && IsNamespaceEnd) { |
| 485 | Diag(Back->second, diag::err_pragma_push_visibility_mismatch); |
| 486 | Diag(EndLoc, diag::note_surrounding_namespace_ends_here); |
| 487 | |
| 488 | // For better error recovery, eat all pushes inside the namespace. |
| 489 | do { |
| 490 | Stack->pop_back(); |
| 491 | Back = &Stack->back(); |
| 492 | StartsWithPragma = Back->first != NoVisibility; |
| 493 | } while (StartsWithPragma); |
| 494 | } else if (!StartsWithPragma && !IsNamespaceEnd) { |
| 495 | Diag(EndLoc, diag::err_pragma_pop_visibility_mismatch); |
| 496 | Diag(Back->second, diag::note_surrounding_namespace_starts_here); |
| 497 | return; |
| 498 | } |
| 499 | |
| 500 | Stack->pop_back(); |
| 501 | // To simplify the implementation, never keep around an empty stack. |
| 502 | if (Stack->empty()) |
| 503 | FreeVisContext(); |
Eli Friedman | 570024a | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 504 | } |