Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 1 | //===--- SemaAttr.cpp - Semantic Analysis for Attributes ------------------===// |
Chris Lattner | 5a0c351 | 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 | 574aa40 | 2009-02-17 01:09:29 +0000 | [diff] [blame] | 10 | // This file implements semantic analysis for non-trivial attributes and |
| 11 | // pragmas. |
Chris Lattner | 5a0c351 | 2009-02-17 00:57:29 +0000 | [diff] [blame] | 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
John McCall | 2d88708 | 2010-08-25 22:03:47 +0000 | [diff] [blame] | 15 | #include "clang/Sema/SemaInternal.h" |
Reid Kleckner | 3190ca9 | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 16 | #include "clang/AST/ASTConsumer.h" |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 17 | #include "clang/AST/Attr.h" |
Chris Lattner | 5a0c351 | 2009-02-17 00:57:29 +0000 | [diff] [blame] | 18 | #include "clang/AST/Expr.h" |
Daniel Dunbar | 613fd67 | 2010-05-27 00:35:16 +0000 | [diff] [blame] | 19 | #include "clang/Basic/TargetInfo.h" |
| 20 | #include "clang/Lex/Preprocessor.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 21 | #include "clang/Sema/Lookup.h" |
Chris Lattner | 5a0c351 | 2009-02-17 00:57:29 +0000 | [diff] [blame] | 22 | using namespace clang; |
| 23 | |
Chris Lattner | 574aa40 | 2009-02-17 01:09:29 +0000 | [diff] [blame] | 24 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | ea75a82 | 2010-05-27 00:04:40 +0000 | [diff] [blame] | 25 | // Pragma 'pack' and 'options align' |
Chris Lattner | 574aa40 | 2009-02-17 01:09:29 +0000 | [diff] [blame] | 26 | //===----------------------------------------------------------------------===// |
| 27 | |
| 28 | namespace { |
Daniel Dunbar | ae2232b | 2010-05-27 02:25:27 +0000 | [diff] [blame] | 29 | struct PackStackEntry { |
Daniel Dunbar | c6082fe | 2010-05-27 05:45:51 +0000 | [diff] [blame] | 30 | // We just use a sentinel to represent when the stack is set to mac68k |
| 31 | // alignment. |
| 32 | static const unsigned kMac68kAlignmentSentinel = ~0U; |
| 33 | |
Daniel Dunbar | ae2232b | 2010-05-27 02:25:27 +0000 | [diff] [blame] | 34 | unsigned Alignment; |
| 35 | IdentifierInfo *Name; |
| 36 | }; |
| 37 | |
Chris Lattner | 574aa40 | 2009-02-17 01:09:29 +0000 | [diff] [blame] | 38 | /// PragmaPackStack - Simple class to wrap the stack used by #pragma |
| 39 | /// pack. |
| 40 | class PragmaPackStack { |
Daniel Dunbar | ae2232b | 2010-05-27 02:25:27 +0000 | [diff] [blame] | 41 | typedef std::vector<PackStackEntry> stack_ty; |
Chris Lattner | 574aa40 | 2009-02-17 01:09:29 +0000 | [diff] [blame] | 42 | |
| 43 | /// Alignment - The current user specified alignment. |
| 44 | unsigned Alignment; |
| 45 | |
| 46 | /// Stack - Entries in the #pragma pack stack, consisting of saved |
| 47 | /// alignments and optional names. |
| 48 | stack_ty Stack; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 49 | |
| 50 | public: |
Chris Lattner | 574aa40 | 2009-02-17 01:09:29 +0000 | [diff] [blame] | 51 | PragmaPackStack() : Alignment(0) {} |
| 52 | |
| 53 | void setAlignment(unsigned A) { Alignment = A; } |
| 54 | unsigned getAlignment() { return Alignment; } |
| 55 | |
| 56 | /// push - Push the current alignment onto the stack, optionally |
| 57 | /// using the given \arg Name for the record, if non-zero. |
| 58 | void push(IdentifierInfo *Name) { |
Daniel Dunbar | ae2232b | 2010-05-27 02:25:27 +0000 | [diff] [blame] | 59 | PackStackEntry PSE = { Alignment, Name }; |
| 60 | Stack.push_back(PSE); |
Chris Lattner | 574aa40 | 2009-02-17 01:09:29 +0000 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | /// pop - Pop a record from the stack and restore the current |
| 64 | /// alignment to the previous value. If \arg Name is non-zero then |
| 65 | /// the first such named record is popped, otherwise the top record |
| 66 | /// is popped. Returns true if the pop succeeded. |
Daniel Dunbar | ddc6ff6 | 2010-07-16 04:54:16 +0000 | [diff] [blame] | 67 | bool pop(IdentifierInfo *Name, bool IsReset); |
Chris Lattner | 574aa40 | 2009-02-17 01:09:29 +0000 | [diff] [blame] | 68 | }; |
| 69 | } // end anonymous namespace. |
| 70 | |
Daniel Dunbar | ddc6ff6 | 2010-07-16 04:54:16 +0000 | [diff] [blame] | 71 | bool PragmaPackStack::pop(IdentifierInfo *Name, bool IsReset) { |
Chris Lattner | 574aa40 | 2009-02-17 01:09:29 +0000 | [diff] [blame] | 72 | // If name is empty just pop top. |
| 73 | if (!Name) { |
Daniel Dunbar | ddc6ff6 | 2010-07-16 04:54:16 +0000 | [diff] [blame] | 74 | // An empty stack is a special case... |
| 75 | if (Stack.empty()) { |
| 76 | // If this isn't a reset, it is always an error. |
| 77 | if (!IsReset) |
| 78 | return false; |
| 79 | |
| 80 | // Otherwise, it is an error only if some alignment has been set. |
| 81 | if (!Alignment) |
| 82 | return false; |
| 83 | |
| 84 | // Otherwise, reset to the default alignment. |
| 85 | Alignment = 0; |
| 86 | } else { |
| 87 | Alignment = Stack.back().Alignment; |
| 88 | Stack.pop_back(); |
| 89 | } |
| 90 | |
Chris Lattner | 574aa40 | 2009-02-17 01:09:29 +0000 | [diff] [blame] | 91 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 92 | } |
| 93 | |
Chris Lattner | 574aa40 | 2009-02-17 01:09:29 +0000 | [diff] [blame] | 94 | // Otherwise, find the named record. |
| 95 | for (unsigned i = Stack.size(); i != 0; ) { |
| 96 | --i; |
Daniel Dunbar | ae2232b | 2010-05-27 02:25:27 +0000 | [diff] [blame] | 97 | if (Stack[i].Name == Name) { |
Chris Lattner | 574aa40 | 2009-02-17 01:09:29 +0000 | [diff] [blame] | 98 | // Found it, pop up to and including this record. |
Daniel Dunbar | ae2232b | 2010-05-27 02:25:27 +0000 | [diff] [blame] | 99 | Alignment = Stack[i].Alignment; |
Chris Lattner | 574aa40 | 2009-02-17 01:09:29 +0000 | [diff] [blame] | 100 | Stack.erase(Stack.begin() + i, Stack.end()); |
| 101 | return true; |
| 102 | } |
| 103 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 104 | |
Chris Lattner | 574aa40 | 2009-02-17 01:09:29 +0000 | [diff] [blame] | 105 | return false; |
| 106 | } |
| 107 | |
| 108 | |
| 109 | /// FreePackedContext - Deallocate and null out PackContext. |
| 110 | void Sema::FreePackedContext() { |
| 111 | delete static_cast<PragmaPackStack*>(PackContext); |
| 112 | PackContext = 0; |
| 113 | } |
| 114 | |
Daniel Dunbar | 9f21f89 | 2010-05-27 01:53:40 +0000 | [diff] [blame] | 115 | void Sema::AddAlignmentAttributesForRecord(RecordDecl *RD) { |
| 116 | // If there is no pack context, we don't need any attributes. |
| 117 | if (!PackContext) |
| 118 | return; |
| 119 | |
| 120 | PragmaPackStack *Stack = static_cast<PragmaPackStack*>(PackContext); |
| 121 | |
| 122 | // Otherwise, check to see if we need a max field alignment attribute. |
Daniel Dunbar | c6082fe | 2010-05-27 05:45:51 +0000 | [diff] [blame] | 123 | if (unsigned Alignment = Stack->getAlignment()) { |
| 124 | if (Alignment == PackStackEntry::kMac68kAlignmentSentinel) |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 125 | RD->addAttr(::new (Context) AlignMac68kAttr(SourceLocation(), Context)); |
Daniel Dunbar | c6082fe | 2010-05-27 05:45:51 +0000 | [diff] [blame] | 126 | else |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 127 | RD->addAttr(::new (Context) MaxFieldAlignmentAttr(SourceLocation(), |
| 128 | Context, |
| 129 | Alignment * 8)); |
Daniel Dunbar | c6082fe | 2010-05-27 05:45:51 +0000 | [diff] [blame] | 130 | } |
Chris Lattner | 574aa40 | 2009-02-17 01:09:29 +0000 | [diff] [blame] | 131 | } |
| 132 | |
Fariborz Jahanian | c1a0a73 | 2011-04-26 17:54:40 +0000 | [diff] [blame] | 133 | void Sema::AddMsStructLayoutForRecord(RecordDecl *RD) { |
| 134 | if (!MSStructPragmaOn) |
| 135 | return; |
| 136 | RD->addAttr(::new (Context) MsStructAttr(SourceLocation(), Context)); |
| 137 | } |
| 138 | |
Daniel Dunbar | ea75a82 | 2010-05-27 00:04:40 +0000 | [diff] [blame] | 139 | void Sema::ActOnPragmaOptionsAlign(PragmaOptionsAlignKind Kind, |
Eli Friedman | 9595c7e | 2012-10-04 02:36:51 +0000 | [diff] [blame] | 140 | SourceLocation PragmaLoc) { |
Daniel Dunbar | ea75a82 | 2010-05-27 00:04:40 +0000 | [diff] [blame] | 141 | if (PackContext == 0) |
| 142 | PackContext = new PragmaPackStack(); |
| 143 | |
| 144 | PragmaPackStack *Context = static_cast<PragmaPackStack*>(PackContext); |
| 145 | |
Daniel Dunbar | ea75a82 | 2010-05-27 00:04:40 +0000 | [diff] [blame] | 146 | switch (Kind) { |
Daniel Dunbar | 638e7cf | 2010-05-27 18:42:09 +0000 | [diff] [blame] | 147 | // For all targets we support native and natural are the same. |
| 148 | // |
| 149 | // FIXME: This is not true on Darwin/PPC. |
| 150 | case POAK_Native: |
Daniel Dunbar | 450f793 | 2010-05-28 19:43:33 +0000 | [diff] [blame] | 151 | case POAK_Power: |
Daniel Dunbar | d6b305d | 2010-05-28 20:08:00 +0000 | [diff] [blame] | 152 | case POAK_Natural: |
Daniel Dunbar | 450f793 | 2010-05-28 19:43:33 +0000 | [diff] [blame] | 153 | Context->push(0); |
| 154 | Context->setAlignment(0); |
| 155 | break; |
| 156 | |
Daniel Dunbar | 6f73914 | 2010-05-27 18:42:17 +0000 | [diff] [blame] | 157 | // Note that '#pragma options align=packed' is not equivalent to attribute |
| 158 | // packed, it has a different precedence relative to attribute aligned. |
| 159 | case POAK_Packed: |
| 160 | Context->push(0); |
| 161 | Context->setAlignment(1); |
| 162 | break; |
| 163 | |
Daniel Dunbar | 613fd67 | 2010-05-27 00:35:16 +0000 | [diff] [blame] | 164 | case POAK_Mac68k: |
| 165 | // Check if the target supports this. |
| 166 | if (!PP.getTargetInfo().hasAlignMac68kSupport()) { |
| 167 | Diag(PragmaLoc, diag::err_pragma_options_align_mac68k_target_unsupported); |
| 168 | return; |
Daniel Dunbar | 613fd67 | 2010-05-27 00:35:16 +0000 | [diff] [blame] | 169 | } |
Daniel Dunbar | c6082fe | 2010-05-27 05:45:51 +0000 | [diff] [blame] | 170 | Context->push(0); |
| 171 | Context->setAlignment(PackStackEntry::kMac68kAlignmentSentinel); |
Daniel Dunbar | 613fd67 | 2010-05-27 00:35:16 +0000 | [diff] [blame] | 172 | break; |
| 173 | |
Eli Friedman | 9595c7e | 2012-10-04 02:36:51 +0000 | [diff] [blame] | 174 | case POAK_Reset: |
| 175 | // Reset just pops the top of the stack, or resets the current alignment to |
| 176 | // default. |
| 177 | if (!Context->pop(0, /*IsReset=*/true)) { |
| 178 | Diag(PragmaLoc, diag::warn_pragma_options_align_reset_failed) |
| 179 | << "stack empty"; |
| 180 | } |
Daniel Dunbar | ea75a82 | 2010-05-27 00:04:40 +0000 | [diff] [blame] | 181 | break; |
| 182 | } |
| 183 | } |
| 184 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 185 | void Sema::ActOnPragmaPack(PragmaPackKind Kind, IdentifierInfo *Name, |
Richard Trieu | f81e5a9 | 2011-09-09 02:00:50 +0000 | [diff] [blame] | 186 | Expr *alignment, SourceLocation PragmaLoc, |
Chris Lattner | 5a0c351 | 2009-02-17 00:57:29 +0000 | [diff] [blame] | 187 | SourceLocation LParenLoc, SourceLocation RParenLoc) { |
| 188 | Expr *Alignment = static_cast<Expr *>(alignment); |
| 189 | |
| 190 | // If specified then alignment must be a "small" power of two. |
| 191 | unsigned AlignmentVal = 0; |
| 192 | if (Alignment) { |
| 193 | llvm::APSInt Val; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 194 | |
Daniel Dunbar | 79cd116 | 2009-03-06 20:45:54 +0000 | [diff] [blame] | 195 | // pack(0) is like pack(), which just works out since that is what |
| 196 | // we use 0 for in PackAttr. |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 197 | if (Alignment->isTypeDependent() || |
| 198 | Alignment->isValueDependent() || |
| 199 | !Alignment->isIntegerConstantExpr(Val, Context) || |
Daniel Dunbar | 79cd116 | 2009-03-06 20:45:54 +0000 | [diff] [blame] | 200 | !(Val == 0 || Val.isPowerOf2()) || |
Chris Lattner | 5a0c351 | 2009-02-17 00:57:29 +0000 | [diff] [blame] | 201 | Val.getZExtValue() > 16) { |
| 202 | Diag(PragmaLoc, diag::warn_pragma_pack_invalid_alignment); |
Chris Lattner | 5a0c351 | 2009-02-17 00:57:29 +0000 | [diff] [blame] | 203 | return; // Ignore |
| 204 | } |
| 205 | |
| 206 | AlignmentVal = (unsigned) Val.getZExtValue(); |
| 207 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 208 | |
Chris Lattner | 574aa40 | 2009-02-17 01:09:29 +0000 | [diff] [blame] | 209 | if (PackContext == 0) |
| 210 | PackContext = new PragmaPackStack(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 211 | |
Chris Lattner | 574aa40 | 2009-02-17 01:09:29 +0000 | [diff] [blame] | 212 | PragmaPackStack *Context = static_cast<PragmaPackStack*>(PackContext); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 213 | |
Chris Lattner | 5a0c351 | 2009-02-17 00:57:29 +0000 | [diff] [blame] | 214 | switch (Kind) { |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 215 | case Sema::PPK_Default: // pack([n]) |
Chris Lattner | 574aa40 | 2009-02-17 01:09:29 +0000 | [diff] [blame] | 216 | Context->setAlignment(AlignmentVal); |
Chris Lattner | 5a0c351 | 2009-02-17 00:57:29 +0000 | [diff] [blame] | 217 | break; |
| 218 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 219 | case Sema::PPK_Show: // pack(show) |
Chris Lattner | 5a0c351 | 2009-02-17 00:57:29 +0000 | [diff] [blame] | 220 | // Show the current alignment, making sure to show the right value |
| 221 | // for the default. |
Chris Lattner | 574aa40 | 2009-02-17 01:09:29 +0000 | [diff] [blame] | 222 | AlignmentVal = Context->getAlignment(); |
Chris Lattner | 5a0c351 | 2009-02-17 00:57:29 +0000 | [diff] [blame] | 223 | // FIXME: This should come from the target. |
| 224 | if (AlignmentVal == 0) |
| 225 | AlignmentVal = 8; |
Daniel Dunbar | c6082fe | 2010-05-27 05:45:51 +0000 | [diff] [blame] | 226 | if (AlignmentVal == PackStackEntry::kMac68kAlignmentSentinel) |
| 227 | Diag(PragmaLoc, diag::warn_pragma_pack_show) << "mac68k"; |
| 228 | else |
| 229 | Diag(PragmaLoc, diag::warn_pragma_pack_show) << AlignmentVal; |
Chris Lattner | 5a0c351 | 2009-02-17 00:57:29 +0000 | [diff] [blame] | 230 | break; |
| 231 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 232 | case Sema::PPK_Push: // pack(push [, id] [, [n]) |
Chris Lattner | 574aa40 | 2009-02-17 01:09:29 +0000 | [diff] [blame] | 233 | Context->push(Name); |
Chris Lattner | 5a0c351 | 2009-02-17 00:57:29 +0000 | [diff] [blame] | 234 | // Set the new alignment if specified. |
| 235 | if (Alignment) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 236 | Context->setAlignment(AlignmentVal); |
Chris Lattner | 5a0c351 | 2009-02-17 00:57:29 +0000 | [diff] [blame] | 237 | break; |
| 238 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 239 | case Sema::PPK_Pop: // pack(pop [, id] [, n]) |
Chris Lattner | 5a0c351 | 2009-02-17 00:57:29 +0000 | [diff] [blame] | 240 | // MSDN, C/C++ Preprocessor Reference > Pragma Directives > pack: |
| 241 | // "#pragma pack(pop, identifier, n) is undefined" |
| 242 | if (Alignment && Name) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 243 | Diag(PragmaLoc, diag::warn_pragma_pack_pop_identifer_and_alignment); |
| 244 | |
Chris Lattner | 5a0c351 | 2009-02-17 00:57:29 +0000 | [diff] [blame] | 245 | // Do the pop. |
Daniel Dunbar | ddc6ff6 | 2010-07-16 04:54:16 +0000 | [diff] [blame] | 246 | if (!Context->pop(Name, /*IsReset=*/false)) { |
Chris Lattner | 5a0c351 | 2009-02-17 00:57:29 +0000 | [diff] [blame] | 247 | // If a name was specified then failure indicates the name |
| 248 | // wasn't found. Otherwise failure indicates the stack was |
| 249 | // empty. |
| 250 | Diag(PragmaLoc, diag::warn_pragma_pack_pop_failed) |
| 251 | << (Name ? "no record matching name" : "stack empty"); |
| 252 | |
| 253 | // FIXME: Warn about popping named records as MSVC does. |
| 254 | } else { |
| 255 | // Pop succeeded, set the new alignment if specified. |
| 256 | if (Alignment) |
Chris Lattner | 574aa40 | 2009-02-17 01:09:29 +0000 | [diff] [blame] | 257 | Context->setAlignment(AlignmentVal); |
Chris Lattner | 5a0c351 | 2009-02-17 00:57:29 +0000 | [diff] [blame] | 258 | } |
| 259 | break; |
Chris Lattner | 5a0c351 | 2009-02-17 00:57:29 +0000 | [diff] [blame] | 260 | } |
| 261 | } |
| 262 | |
Fariborz Jahanian | 62c9258 | 2011-04-25 18:49:15 +0000 | [diff] [blame] | 263 | void Sema::ActOnPragmaMSStruct(PragmaMSStructKind Kind) { |
| 264 | MSStructPragmaOn = (Kind == PMSST_ON); |
| 265 | } |
| 266 | |
Reid Kleckner | 3190ca9 | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 267 | void Sema::ActOnPragmaMSComment(PragmaMSCommentKind Kind, llvm::StringRef Arg) { |
| 268 | // FIXME: Serialize this. |
| 269 | switch (Kind) { |
| 270 | case PCK_Unknown: |
| 271 | llvm_unreachable("unexpected pragma comment kind"); |
| 272 | case PCK_Linker: |
| 273 | Consumer.HandleLinkerOptionPragma(Arg); |
| 274 | return; |
Aaron Ballman | 89735b9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 275 | case PCK_Lib: |
Reid Kleckner | 3190ca9 | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 276 | Consumer.HandleDependentLibrary(Arg); |
| 277 | return; |
Reid Kleckner | 3190ca9 | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 278 | case PCK_Compiler: |
| 279 | case PCK_ExeStr: |
| 280 | case PCK_User: |
| 281 | return; // We ignore all of these. |
| 282 | } |
| 283 | llvm_unreachable("invalid pragma comment kind"); |
| 284 | } |
| 285 | |
Aaron Ballman | a7ff62f | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 286 | void Sema::ActOnPragmaDetectMismatch(llvm::StringRef Name, |
| 287 | llvm::StringRef Value) { |
| 288 | // FIXME: Serialize this. |
| 289 | Consumer.HandleDetectMismatch(Name, Value); |
| 290 | } |
| 291 | |
Argyrios Kyrtzidis | b918d0f | 2011-01-17 18:58:44 +0000 | [diff] [blame] | 292 | void Sema::ActOnPragmaUnused(const Token &IdTok, Scope *curScope, |
| 293 | SourceLocation PragmaLoc) { |
Ted Kremenek | 4726d03 | 2009-03-23 22:28:25 +0000 | [diff] [blame] | 294 | |
Argyrios Kyrtzidis | b918d0f | 2011-01-17 18:58:44 +0000 | [diff] [blame] | 295 | IdentifierInfo *Name = IdTok.getIdentifierInfo(); |
| 296 | LookupResult Lookup(*this, Name, IdTok.getLocation(), LookupOrdinaryName); |
| 297 | LookupParsedName(Lookup, curScope, NULL, true); |
Ted Kremenek | 4726d03 | 2009-03-23 22:28:25 +0000 | [diff] [blame] | 298 | |
Argyrios Kyrtzidis | b918d0f | 2011-01-17 18:58:44 +0000 | [diff] [blame] | 299 | if (Lookup.empty()) { |
| 300 | Diag(PragmaLoc, diag::warn_pragma_unused_undeclared_var) |
| 301 | << Name << SourceRange(IdTok.getLocation()); |
| 302 | return; |
Ted Kremenek | 4726d03 | 2009-03-23 22:28:25 +0000 | [diff] [blame] | 303 | } |
Argyrios Kyrtzidis | b918d0f | 2011-01-17 18:58:44 +0000 | [diff] [blame] | 304 | |
| 305 | VarDecl *VD = Lookup.getAsSingle<VarDecl>(); |
Argyrios Kyrtzidis | 2a5c45b | 2011-01-27 18:16:48 +0000 | [diff] [blame] | 306 | if (!VD) { |
| 307 | Diag(PragmaLoc, diag::warn_pragma_unused_expected_var_arg) |
Argyrios Kyrtzidis | b918d0f | 2011-01-17 18:58:44 +0000 | [diff] [blame] | 308 | << Name << SourceRange(IdTok.getLocation()); |
| 309 | return; |
| 310 | } |
| 311 | |
| 312 | // Warn if this was used before being marked unused. |
| 313 | if (VD->isUsed()) |
| 314 | Diag(PragmaLoc, diag::warn_used_but_marked_unused) << Name; |
| 315 | |
| 316 | VD->addAttr(::new (Context) UnusedAttr(IdTok.getLocation(), Context)); |
Ted Kremenek | 4726d03 | 2009-03-23 22:28:25 +0000 | [diff] [blame] | 317 | } |
Eli Friedman | aa8b0d1 | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 318 | |
John McCall | 8dfac0b | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 319 | void Sema::AddCFAuditedAttribute(Decl *D) { |
| 320 | SourceLocation Loc = PP.getPragmaARCCFCodeAuditedLoc(); |
| 321 | if (!Loc.isValid()) return; |
| 322 | |
| 323 | // Don't add a redundant or conflicting attribute. |
| 324 | if (D->hasAttr<CFAuditedTransferAttr>() || |
| 325 | D->hasAttr<CFUnknownTransferAttr>()) |
| 326 | return; |
| 327 | |
| 328 | D->addAttr(::new (Context) CFAuditedTransferAttr(Loc, Context)); |
| 329 | } |
| 330 | |
John McCall | 90f1450 | 2010-12-10 02:59:44 +0000 | [diff] [blame] | 331 | typedef std::vector<std::pair<unsigned, SourceLocation> > VisStack; |
| 332 | enum { NoVisibility = (unsigned) -1 }; |
Eli Friedman | aa8b0d1 | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 333 | |
| 334 | void Sema::AddPushedVisibilityAttribute(Decl *D) { |
| 335 | if (!VisContext) |
| 336 | return; |
| 337 | |
Rafael Espindola | 140aadf | 2012-12-25 07:31:49 +0000 | [diff] [blame] | 338 | NamedDecl *ND = dyn_cast<NamedDecl>(D); |
John McCall | d4c3d66 | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 339 | if (ND && ND->getExplicitVisibility(NamedDecl::VisibilityForValue)) |
Eli Friedman | aa8b0d1 | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 340 | return; |
| 341 | |
| 342 | VisStack *Stack = static_cast<VisStack*>(VisContext); |
John McCall | 90f1450 | 2010-12-10 02:59:44 +0000 | [diff] [blame] | 343 | unsigned rawType = Stack->back().first; |
| 344 | if (rawType == NoVisibility) return; |
| 345 | |
| 346 | VisibilityAttr::VisibilityType type |
| 347 | = (VisibilityAttr::VisibilityType) rawType; |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 348 | SourceLocation loc = Stack->back().second; |
Eli Friedman | aa8b0d1 | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 349 | |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 350 | D->addAttr(::new (Context) VisibilityAttr(loc, Context, type)); |
Eli Friedman | aa8b0d1 | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | /// FreeVisContext - Deallocate and null out VisContext. |
| 354 | void Sema::FreeVisContext() { |
| 355 | delete static_cast<VisStack*>(VisContext); |
| 356 | VisContext = 0; |
| 357 | } |
| 358 | |
John McCall | 90f1450 | 2010-12-10 02:59:44 +0000 | [diff] [blame] | 359 | static void PushPragmaVisibility(Sema &S, unsigned type, SourceLocation loc) { |
John McCall | ea31864 | 2010-08-26 09:15:37 +0000 | [diff] [blame] | 360 | // Put visibility on stack. |
| 361 | if (!S.VisContext) |
| 362 | S.VisContext = new VisStack; |
| 363 | |
| 364 | VisStack *Stack = static_cast<VisStack*>(S.VisContext); |
| 365 | Stack->push_back(std::make_pair(type, loc)); |
| 366 | } |
| 367 | |
Rafael Espindola | 3fc809d | 2012-01-21 05:43:40 +0000 | [diff] [blame] | 368 | void Sema::ActOnPragmaVisibility(const IdentifierInfo* VisType, |
Eli Friedman | aa8b0d1 | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 369 | SourceLocation PragmaLoc) { |
Rafael Espindola | 3fc809d | 2012-01-21 05:43:40 +0000 | [diff] [blame] | 370 | if (VisType) { |
Eli Friedman | aa8b0d1 | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 371 | // Compute visibility to use. |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 372 | VisibilityAttr::VisibilityType type; |
Eli Friedman | aa8b0d1 | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 373 | if (VisType->isStr("default")) |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 374 | type = VisibilityAttr::Default; |
Eli Friedman | aa8b0d1 | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 375 | else if (VisType->isStr("hidden")) |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 376 | type = VisibilityAttr::Hidden; |
Eli Friedman | aa8b0d1 | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 377 | else if (VisType->isStr("internal")) |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 378 | type = VisibilityAttr::Hidden; // FIXME |
Eli Friedman | aa8b0d1 | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 379 | else if (VisType->isStr("protected")) |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 380 | type = VisibilityAttr::Protected; |
Eli Friedman | aa8b0d1 | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 381 | else { |
| 382 | Diag(PragmaLoc, diag::warn_attribute_unknown_visibility) << |
| 383 | VisType->getName(); |
| 384 | return; |
| 385 | } |
John McCall | ea31864 | 2010-08-26 09:15:37 +0000 | [diff] [blame] | 386 | PushPragmaVisibility(*this, type, PragmaLoc); |
Eli Friedman | aa8b0d1 | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 387 | } else { |
Rafael Espindola | 20039ae | 2012-02-01 23:24:59 +0000 | [diff] [blame] | 388 | PopPragmaVisibility(false, PragmaLoc); |
Eli Friedman | aa8b0d1 | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 389 | } |
| 390 | } |
| 391 | |
Peter Collingbourne | 321b817 | 2011-02-14 01:42:35 +0000 | [diff] [blame] | 392 | void Sema::ActOnPragmaFPContract(tok::OnOffSwitch OOS) { |
| 393 | switch (OOS) { |
| 394 | case tok::OOS_ON: |
| 395 | FPFeatures.fp_contract = 1; |
| 396 | break; |
| 397 | case tok::OOS_OFF: |
| 398 | FPFeatures.fp_contract = 0; |
| 399 | break; |
| 400 | case tok::OOS_DEFAULT: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 401 | FPFeatures.fp_contract = getLangOpts().DefaultFPContract; |
Peter Collingbourne | 321b817 | 2011-02-14 01:42:35 +0000 | [diff] [blame] | 402 | break; |
| 403 | } |
| 404 | } |
| 405 | |
Rafael Espindola | 20039ae | 2012-02-01 23:24:59 +0000 | [diff] [blame] | 406 | void Sema::PushNamespaceVisibilityAttr(const VisibilityAttr *Attr, |
| 407 | SourceLocation Loc) { |
John McCall | 90f1450 | 2010-12-10 02:59:44 +0000 | [diff] [blame] | 408 | // Visibility calculations will consider the namespace's visibility. |
| 409 | // Here we just want to note that we're in a visibility context |
| 410 | // which overrides any enclosing #pragma context, but doesn't itself |
| 411 | // contribute visibility. |
Rafael Espindola | 20039ae | 2012-02-01 23:24:59 +0000 | [diff] [blame] | 412 | PushPragmaVisibility(*this, NoVisibility, Loc); |
Eli Friedman | aa8b0d1 | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 413 | } |
| 414 | |
Rafael Espindola | 20039ae | 2012-02-01 23:24:59 +0000 | [diff] [blame] | 415 | void Sema::PopPragmaVisibility(bool IsNamespaceEnd, SourceLocation EndLoc) { |
| 416 | if (!VisContext) { |
| 417 | Diag(EndLoc, diag::err_pragma_pop_visibility_mismatch); |
| 418 | return; |
Eli Friedman | aa8b0d1 | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 419 | } |
Rafael Espindola | 20039ae | 2012-02-01 23:24:59 +0000 | [diff] [blame] | 420 | |
| 421 | // Pop visibility from stack |
| 422 | VisStack *Stack = static_cast<VisStack*>(VisContext); |
| 423 | |
| 424 | const std::pair<unsigned, SourceLocation> *Back = &Stack->back(); |
| 425 | bool StartsWithPragma = Back->first != NoVisibility; |
| 426 | if (StartsWithPragma && IsNamespaceEnd) { |
| 427 | Diag(Back->second, diag::err_pragma_push_visibility_mismatch); |
| 428 | Diag(EndLoc, diag::note_surrounding_namespace_ends_here); |
| 429 | |
| 430 | // For better error recovery, eat all pushes inside the namespace. |
| 431 | do { |
| 432 | Stack->pop_back(); |
| 433 | Back = &Stack->back(); |
| 434 | StartsWithPragma = Back->first != NoVisibility; |
| 435 | } while (StartsWithPragma); |
| 436 | } else if (!StartsWithPragma && !IsNamespaceEnd) { |
| 437 | Diag(EndLoc, diag::err_pragma_pop_visibility_mismatch); |
| 438 | Diag(Back->second, diag::note_surrounding_namespace_starts_here); |
| 439 | return; |
| 440 | } |
| 441 | |
| 442 | Stack->pop_back(); |
| 443 | // To simplify the implementation, never keep around an empty stack. |
| 444 | if (Stack->empty()) |
| 445 | FreeVisContext(); |
Eli Friedman | aa8b0d1 | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 446 | } |