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