Chris Lattner | 5a0c351 | 2009-02-17 00:57:29 +0000 | [diff] [blame] | 1 | //===--- SemaAttr.cpp - Semantic Analysis for Attributes ------------------===// |
| 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 | |
| 15 | #include "Sema.h" |
John McCall | 7d384dd | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 16 | #include "Lookup.h" |
Chris Lattner | 5a0c351 | 2009-02-17 00:57:29 +0000 | [diff] [blame] | 17 | #include "clang/AST/Expr.h" |
Daniel Dunbar | 613fd67 | 2010-05-27 00:35:16 +0000 | [diff] [blame] | 18 | #include "clang/Basic/TargetInfo.h" |
| 19 | #include "clang/Lex/Preprocessor.h" |
Chris Lattner | 5a0c351 | 2009-02-17 00:57:29 +0000 | [diff] [blame] | 20 | using namespace clang; |
| 21 | |
Chris Lattner | 574aa40 | 2009-02-17 01:09:29 +0000 | [diff] [blame] | 22 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | ea75a82 | 2010-05-27 00:04:40 +0000 | [diff] [blame] | 23 | // Pragma 'pack' and 'options align' |
Chris Lattner | 574aa40 | 2009-02-17 01:09:29 +0000 | [diff] [blame] | 24 | //===----------------------------------------------------------------------===// |
| 25 | |
| 26 | namespace { |
Daniel Dunbar | ae2232b | 2010-05-27 02:25:27 +0000 | [diff] [blame] | 27 | struct PackStackEntry { |
Daniel Dunbar | c6082fe | 2010-05-27 05:45:51 +0000 | [diff] [blame] | 28 | // We just use a sentinel to represent when the stack is set to mac68k |
| 29 | // alignment. |
| 30 | static const unsigned kMac68kAlignmentSentinel = ~0U; |
| 31 | |
Daniel Dunbar | ae2232b | 2010-05-27 02:25:27 +0000 | [diff] [blame] | 32 | unsigned Alignment; |
| 33 | IdentifierInfo *Name; |
| 34 | }; |
| 35 | |
Chris Lattner | 574aa40 | 2009-02-17 01:09:29 +0000 | [diff] [blame] | 36 | /// PragmaPackStack - Simple class to wrap the stack used by #pragma |
| 37 | /// pack. |
| 38 | class PragmaPackStack { |
Daniel Dunbar | ae2232b | 2010-05-27 02:25:27 +0000 | [diff] [blame] | 39 | typedef std::vector<PackStackEntry> stack_ty; |
Chris Lattner | 574aa40 | 2009-02-17 01:09:29 +0000 | [diff] [blame] | 40 | |
| 41 | /// Alignment - The current user specified alignment. |
| 42 | unsigned Alignment; |
| 43 | |
| 44 | /// Stack - Entries in the #pragma pack stack, consisting of saved |
| 45 | /// alignments and optional names. |
| 46 | stack_ty Stack; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 47 | |
| 48 | public: |
Chris Lattner | 574aa40 | 2009-02-17 01:09:29 +0000 | [diff] [blame] | 49 | PragmaPackStack() : Alignment(0) {} |
| 50 | |
| 51 | void setAlignment(unsigned A) { Alignment = A; } |
| 52 | unsigned getAlignment() { return Alignment; } |
| 53 | |
| 54 | /// push - Push the current alignment onto the stack, optionally |
| 55 | /// using the given \arg Name for the record, if non-zero. |
| 56 | void push(IdentifierInfo *Name) { |
Daniel Dunbar | ae2232b | 2010-05-27 02:25:27 +0000 | [diff] [blame] | 57 | PackStackEntry PSE = { Alignment, Name }; |
| 58 | Stack.push_back(PSE); |
Chris Lattner | 574aa40 | 2009-02-17 01:09:29 +0000 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | /// pop - Pop a record from the stack and restore the current |
| 62 | /// alignment to the previous value. If \arg Name is non-zero then |
| 63 | /// the first such named record is popped, otherwise the top record |
| 64 | /// is popped. Returns true if the pop succeeded. |
| 65 | bool pop(IdentifierInfo *Name); |
| 66 | }; |
| 67 | } // end anonymous namespace. |
| 68 | |
| 69 | bool PragmaPackStack::pop(IdentifierInfo *Name) { |
| 70 | if (Stack.empty()) |
| 71 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 72 | |
Chris Lattner | 574aa40 | 2009-02-17 01:09:29 +0000 | [diff] [blame] | 73 | // If name is empty just pop top. |
| 74 | if (!Name) { |
Daniel Dunbar | ae2232b | 2010-05-27 02:25:27 +0000 | [diff] [blame] | 75 | Alignment = Stack.back().Alignment; |
Chris Lattner | 574aa40 | 2009-02-17 01:09:29 +0000 | [diff] [blame] | 76 | Stack.pop_back(); |
| 77 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 78 | } |
| 79 | |
Chris Lattner | 574aa40 | 2009-02-17 01:09:29 +0000 | [diff] [blame] | 80 | // Otherwise, find the named record. |
| 81 | for (unsigned i = Stack.size(); i != 0; ) { |
| 82 | --i; |
Daniel Dunbar | ae2232b | 2010-05-27 02:25:27 +0000 | [diff] [blame] | 83 | if (Stack[i].Name == Name) { |
Chris Lattner | 574aa40 | 2009-02-17 01:09:29 +0000 | [diff] [blame] | 84 | // Found it, pop up to and including this record. |
Daniel Dunbar | ae2232b | 2010-05-27 02:25:27 +0000 | [diff] [blame] | 85 | Alignment = Stack[i].Alignment; |
Chris Lattner | 574aa40 | 2009-02-17 01:09:29 +0000 | [diff] [blame] | 86 | Stack.erase(Stack.begin() + i, Stack.end()); |
| 87 | return true; |
| 88 | } |
| 89 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 90 | |
Chris Lattner | 574aa40 | 2009-02-17 01:09:29 +0000 | [diff] [blame] | 91 | return false; |
| 92 | } |
| 93 | |
| 94 | |
| 95 | /// FreePackedContext - Deallocate and null out PackContext. |
| 96 | void Sema::FreePackedContext() { |
| 97 | delete static_cast<PragmaPackStack*>(PackContext); |
| 98 | PackContext = 0; |
| 99 | } |
| 100 | |
Daniel Dunbar | 9f21f89 | 2010-05-27 01:53:40 +0000 | [diff] [blame] | 101 | void Sema::AddAlignmentAttributesForRecord(RecordDecl *RD) { |
| 102 | // If there is no pack context, we don't need any attributes. |
| 103 | if (!PackContext) |
| 104 | return; |
| 105 | |
| 106 | PragmaPackStack *Stack = static_cast<PragmaPackStack*>(PackContext); |
| 107 | |
| 108 | // Otherwise, check to see if we need a max field alignment attribute. |
Daniel Dunbar | c6082fe | 2010-05-27 05:45:51 +0000 | [diff] [blame] | 109 | if (unsigned Alignment = Stack->getAlignment()) { |
| 110 | if (Alignment == PackStackEntry::kMac68kAlignmentSentinel) |
| 111 | RD->addAttr(::new (Context) AlignMac68kAttr()); |
| 112 | else |
| 113 | RD->addAttr(::new (Context) MaxFieldAlignmentAttr(Alignment * 8)); |
| 114 | } |
Chris Lattner | 574aa40 | 2009-02-17 01:09:29 +0000 | [diff] [blame] | 115 | } |
| 116 | |
Daniel Dunbar | ea75a82 | 2010-05-27 00:04:40 +0000 | [diff] [blame] | 117 | void Sema::ActOnPragmaOptionsAlign(PragmaOptionsAlignKind Kind, |
| 118 | SourceLocation PragmaLoc, |
| 119 | SourceLocation KindLoc) { |
| 120 | if (PackContext == 0) |
| 121 | PackContext = new PragmaPackStack(); |
| 122 | |
| 123 | PragmaPackStack *Context = static_cast<PragmaPackStack*>(PackContext); |
| 124 | |
| 125 | // Reset just pops the top of the stack. |
| 126 | if (Kind == Action::POAK_Reset) { |
| 127 | // Do the pop. |
| 128 | if (!Context->pop(0)) { |
| 129 | // If a name was specified then failure indicates the name |
| 130 | // wasn't found. Otherwise failure indicates the stack was |
| 131 | // empty. |
| 132 | Diag(PragmaLoc, diag::warn_pragma_options_align_reset_failed) |
| 133 | << "stack empty"; |
| 134 | } |
| 135 | return; |
| 136 | } |
| 137 | |
| 138 | // We don't support #pragma options align=power. |
| 139 | switch (Kind) { |
Daniel Dunbar | 638e7cf | 2010-05-27 18:42:09 +0000 | [diff] [blame^] | 140 | // For all targets we support native and natural are the same. |
| 141 | // |
| 142 | // FIXME: This is not true on Darwin/PPC. |
| 143 | case POAK_Native: |
Daniel Dunbar | ea75a82 | 2010-05-27 00:04:40 +0000 | [diff] [blame] | 144 | case POAK_Natural: |
| 145 | Context->push(0); |
| 146 | Context->setAlignment(0); |
| 147 | break; |
| 148 | |
Daniel Dunbar | 613fd67 | 2010-05-27 00:35:16 +0000 | [diff] [blame] | 149 | case POAK_Mac68k: |
| 150 | // Check if the target supports this. |
| 151 | if (!PP.getTargetInfo().hasAlignMac68kSupport()) { |
| 152 | Diag(PragmaLoc, diag::err_pragma_options_align_mac68k_target_unsupported); |
| 153 | return; |
Daniel Dunbar | 613fd67 | 2010-05-27 00:35:16 +0000 | [diff] [blame] | 154 | } |
Daniel Dunbar | c6082fe | 2010-05-27 05:45:51 +0000 | [diff] [blame] | 155 | Context->push(0); |
| 156 | Context->setAlignment(PackStackEntry::kMac68kAlignmentSentinel); |
Daniel Dunbar | 613fd67 | 2010-05-27 00:35:16 +0000 | [diff] [blame] | 157 | break; |
| 158 | |
Daniel Dunbar | ea75a82 | 2010-05-27 00:04:40 +0000 | [diff] [blame] | 159 | default: |
| 160 | Diag(PragmaLoc, diag::warn_pragma_options_align_unsupported_option) |
| 161 | << KindLoc; |
| 162 | break; |
| 163 | } |
| 164 | } |
| 165 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 166 | void Sema::ActOnPragmaPack(PragmaPackKind Kind, IdentifierInfo *Name, |
| 167 | ExprTy *alignment, SourceLocation PragmaLoc, |
Chris Lattner | 5a0c351 | 2009-02-17 00:57:29 +0000 | [diff] [blame] | 168 | SourceLocation LParenLoc, SourceLocation RParenLoc) { |
| 169 | Expr *Alignment = static_cast<Expr *>(alignment); |
| 170 | |
| 171 | // If specified then alignment must be a "small" power of two. |
| 172 | unsigned AlignmentVal = 0; |
| 173 | if (Alignment) { |
| 174 | llvm::APSInt Val; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 175 | |
Daniel Dunbar | 79cd116 | 2009-03-06 20:45:54 +0000 | [diff] [blame] | 176 | // pack(0) is like pack(), which just works out since that is what |
| 177 | // we use 0 for in PackAttr. |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 178 | if (Alignment->isTypeDependent() || |
| 179 | Alignment->isValueDependent() || |
| 180 | !Alignment->isIntegerConstantExpr(Val, Context) || |
Daniel Dunbar | 79cd116 | 2009-03-06 20:45:54 +0000 | [diff] [blame] | 181 | !(Val == 0 || Val.isPowerOf2()) || |
Chris Lattner | 5a0c351 | 2009-02-17 00:57:29 +0000 | [diff] [blame] | 182 | Val.getZExtValue() > 16) { |
| 183 | Diag(PragmaLoc, diag::warn_pragma_pack_invalid_alignment); |
| 184 | Alignment->Destroy(Context); |
| 185 | return; // Ignore |
| 186 | } |
| 187 | |
| 188 | AlignmentVal = (unsigned) Val.getZExtValue(); |
| 189 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 190 | |
Chris Lattner | 574aa40 | 2009-02-17 01:09:29 +0000 | [diff] [blame] | 191 | if (PackContext == 0) |
| 192 | PackContext = new PragmaPackStack(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 193 | |
Chris Lattner | 574aa40 | 2009-02-17 01:09:29 +0000 | [diff] [blame] | 194 | PragmaPackStack *Context = static_cast<PragmaPackStack*>(PackContext); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 195 | |
Chris Lattner | 5a0c351 | 2009-02-17 00:57:29 +0000 | [diff] [blame] | 196 | switch (Kind) { |
| 197 | case Action::PPK_Default: // pack([n]) |
Chris Lattner | 574aa40 | 2009-02-17 01:09:29 +0000 | [diff] [blame] | 198 | Context->setAlignment(AlignmentVal); |
Chris Lattner | 5a0c351 | 2009-02-17 00:57:29 +0000 | [diff] [blame] | 199 | break; |
| 200 | |
| 201 | case Action::PPK_Show: // pack(show) |
| 202 | // Show the current alignment, making sure to show the right value |
| 203 | // for the default. |
Chris Lattner | 574aa40 | 2009-02-17 01:09:29 +0000 | [diff] [blame] | 204 | AlignmentVal = Context->getAlignment(); |
Chris Lattner | 5a0c351 | 2009-02-17 00:57:29 +0000 | [diff] [blame] | 205 | // FIXME: This should come from the target. |
| 206 | if (AlignmentVal == 0) |
| 207 | AlignmentVal = 8; |
Daniel Dunbar | c6082fe | 2010-05-27 05:45:51 +0000 | [diff] [blame] | 208 | if (AlignmentVal == PackStackEntry::kMac68kAlignmentSentinel) |
| 209 | Diag(PragmaLoc, diag::warn_pragma_pack_show) << "mac68k"; |
| 210 | else |
| 211 | Diag(PragmaLoc, diag::warn_pragma_pack_show) << AlignmentVal; |
Chris Lattner | 5a0c351 | 2009-02-17 00:57:29 +0000 | [diff] [blame] | 212 | break; |
| 213 | |
| 214 | case Action::PPK_Push: // pack(push [, id] [, [n]) |
Chris Lattner | 574aa40 | 2009-02-17 01:09:29 +0000 | [diff] [blame] | 215 | Context->push(Name); |
Chris Lattner | 5a0c351 | 2009-02-17 00:57:29 +0000 | [diff] [blame] | 216 | // Set the new alignment if specified. |
| 217 | if (Alignment) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 218 | Context->setAlignment(AlignmentVal); |
Chris Lattner | 5a0c351 | 2009-02-17 00:57:29 +0000 | [diff] [blame] | 219 | break; |
| 220 | |
| 221 | case Action::PPK_Pop: // pack(pop [, id] [, n]) |
| 222 | // MSDN, C/C++ Preprocessor Reference > Pragma Directives > pack: |
| 223 | // "#pragma pack(pop, identifier, n) is undefined" |
| 224 | if (Alignment && Name) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 225 | Diag(PragmaLoc, diag::warn_pragma_pack_pop_identifer_and_alignment); |
| 226 | |
Chris Lattner | 5a0c351 | 2009-02-17 00:57:29 +0000 | [diff] [blame] | 227 | // Do the pop. |
Chris Lattner | 574aa40 | 2009-02-17 01:09:29 +0000 | [diff] [blame] | 228 | if (!Context->pop(Name)) { |
Chris Lattner | 5a0c351 | 2009-02-17 00:57:29 +0000 | [diff] [blame] | 229 | // If a name was specified then failure indicates the name |
| 230 | // wasn't found. Otherwise failure indicates the stack was |
| 231 | // empty. |
| 232 | Diag(PragmaLoc, diag::warn_pragma_pack_pop_failed) |
| 233 | << (Name ? "no record matching name" : "stack empty"); |
| 234 | |
| 235 | // FIXME: Warn about popping named records as MSVC does. |
| 236 | } else { |
| 237 | // Pop succeeded, set the new alignment if specified. |
| 238 | if (Alignment) |
Chris Lattner | 574aa40 | 2009-02-17 01:09:29 +0000 | [diff] [blame] | 239 | Context->setAlignment(AlignmentVal); |
Chris Lattner | 5a0c351 | 2009-02-17 00:57:29 +0000 | [diff] [blame] | 240 | } |
| 241 | break; |
| 242 | |
| 243 | default: |
| 244 | assert(0 && "Invalid #pragma pack kind."); |
| 245 | } |
| 246 | } |
| 247 | |
Ted Kremenek | 7a02a37 | 2009-08-03 23:24:57 +0000 | [diff] [blame] | 248 | void Sema::ActOnPragmaUnused(const Token *Identifiers, unsigned NumIdentifiers, |
| 249 | Scope *curScope, |
Ted Kremenek | 4726d03 | 2009-03-23 22:28:25 +0000 | [diff] [blame] | 250 | SourceLocation PragmaLoc, |
| 251 | SourceLocation LParenLoc, |
| 252 | SourceLocation RParenLoc) { |
Ted Kremenek | 4726d03 | 2009-03-23 22:28:25 +0000 | [diff] [blame] | 253 | |
Ted Kremenek | 7a02a37 | 2009-08-03 23:24:57 +0000 | [diff] [blame] | 254 | for (unsigned i = 0; i < NumIdentifiers; ++i) { |
| 255 | const Token &Tok = Identifiers[i]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 256 | IdentifierInfo *Name = Tok.getIdentifierInfo(); |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 257 | LookupResult Lookup(*this, Name, Tok.getLocation(), LookupOrdinaryName); |
| 258 | LookupParsedName(Lookup, curScope, NULL, true); |
Ted Kremenek | 4726d03 | 2009-03-23 22:28:25 +0000 | [diff] [blame] | 259 | |
John McCall | 1bcee0a | 2009-12-02 08:25:40 +0000 | [diff] [blame] | 260 | if (Lookup.empty()) { |
Ted Kremenek | 7a02a37 | 2009-08-03 23:24:57 +0000 | [diff] [blame] | 261 | Diag(PragmaLoc, diag::warn_pragma_unused_undeclared_var) |
| 262 | << Name << SourceRange(Tok.getLocation()); |
| 263 | continue; |
Ted Kremenek | 4726d03 | 2009-03-23 22:28:25 +0000 | [diff] [blame] | 264 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 265 | |
John McCall | 1bcee0a | 2009-12-02 08:25:40 +0000 | [diff] [blame] | 266 | VarDecl *VD = Lookup.getAsSingle<VarDecl>(); |
| 267 | if (!VD || !VD->hasLocalStorage()) { |
Ted Kremenek | 7a02a37 | 2009-08-03 23:24:57 +0000 | [diff] [blame] | 268 | Diag(PragmaLoc, diag::warn_pragma_unused_expected_localvar) |
| 269 | << Name << SourceRange(Tok.getLocation()); |
| 270 | continue; |
| 271 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 272 | |
John McCall | 1bcee0a | 2009-12-02 08:25:40 +0000 | [diff] [blame] | 273 | VD->addAttr(::new (Context) UnusedAttr()); |
Ted Kremenek | 4726d03 | 2009-03-23 22:28:25 +0000 | [diff] [blame] | 274 | } |
| 275 | } |