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" |
| 16 | #include "clang/AST/Expr.h" |
| 17 | using namespace clang; |
| 18 | |
Chris Lattner | 574aa40 | 2009-02-17 01:09:29 +0000 | [diff] [blame] | 19 | //===----------------------------------------------------------------------===// |
| 20 | // Pragma Packed |
| 21 | //===----------------------------------------------------------------------===// |
| 22 | |
| 23 | namespace { |
| 24 | /// PragmaPackStack - Simple class to wrap the stack used by #pragma |
| 25 | /// pack. |
| 26 | class PragmaPackStack { |
| 27 | typedef std::vector< std::pair<unsigned, IdentifierInfo*> > stack_ty; |
| 28 | |
| 29 | /// Alignment - The current user specified alignment. |
| 30 | unsigned Alignment; |
| 31 | |
| 32 | /// Stack - Entries in the #pragma pack stack, consisting of saved |
| 33 | /// alignments and optional names. |
| 34 | stack_ty Stack; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 35 | |
| 36 | public: |
Chris Lattner | 574aa40 | 2009-02-17 01:09:29 +0000 | [diff] [blame] | 37 | PragmaPackStack() : Alignment(0) {} |
| 38 | |
| 39 | void setAlignment(unsigned A) { Alignment = A; } |
| 40 | unsigned getAlignment() { return Alignment; } |
| 41 | |
| 42 | /// push - Push the current alignment onto the stack, optionally |
| 43 | /// using the given \arg Name for the record, if non-zero. |
| 44 | void push(IdentifierInfo *Name) { |
| 45 | Stack.push_back(std::make_pair(Alignment, Name)); |
| 46 | } |
| 47 | |
| 48 | /// pop - Pop a record from the stack and restore the current |
| 49 | /// alignment to the previous value. If \arg Name is non-zero then |
| 50 | /// the first such named record is popped, otherwise the top record |
| 51 | /// is popped. Returns true if the pop succeeded. |
| 52 | bool pop(IdentifierInfo *Name); |
| 53 | }; |
| 54 | } // end anonymous namespace. |
| 55 | |
| 56 | bool PragmaPackStack::pop(IdentifierInfo *Name) { |
| 57 | if (Stack.empty()) |
| 58 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 59 | |
Chris Lattner | 574aa40 | 2009-02-17 01:09:29 +0000 | [diff] [blame] | 60 | // If name is empty just pop top. |
| 61 | if (!Name) { |
| 62 | Alignment = Stack.back().first; |
| 63 | Stack.pop_back(); |
| 64 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 65 | } |
| 66 | |
Chris Lattner | 574aa40 | 2009-02-17 01:09:29 +0000 | [diff] [blame] | 67 | // Otherwise, find the named record. |
| 68 | for (unsigned i = Stack.size(); i != 0; ) { |
| 69 | --i; |
| 70 | if (Stack[i].second == Name) { |
| 71 | // Found it, pop up to and including this record. |
| 72 | Alignment = Stack[i].first; |
| 73 | Stack.erase(Stack.begin() + i, Stack.end()); |
| 74 | return true; |
| 75 | } |
| 76 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 77 | |
Chris Lattner | 574aa40 | 2009-02-17 01:09:29 +0000 | [diff] [blame] | 78 | return false; |
| 79 | } |
| 80 | |
| 81 | |
| 82 | /// FreePackedContext - Deallocate and null out PackContext. |
| 83 | void Sema::FreePackedContext() { |
| 84 | delete static_cast<PragmaPackStack*>(PackContext); |
| 85 | PackContext = 0; |
| 86 | } |
| 87 | |
| 88 | /// getPragmaPackAlignment() - Return the current alignment as specified by |
| 89 | /// the current #pragma pack directive, or 0 if none is currently active. |
| 90 | unsigned Sema::getPragmaPackAlignment() const { |
| 91 | if (PackContext) |
| 92 | return static_cast<PragmaPackStack*>(PackContext)->getAlignment(); |
| 93 | return 0; |
| 94 | } |
| 95 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 96 | void Sema::ActOnPragmaPack(PragmaPackKind Kind, IdentifierInfo *Name, |
| 97 | ExprTy *alignment, SourceLocation PragmaLoc, |
Chris Lattner | 5a0c351 | 2009-02-17 00:57:29 +0000 | [diff] [blame] | 98 | SourceLocation LParenLoc, SourceLocation RParenLoc) { |
| 99 | Expr *Alignment = static_cast<Expr *>(alignment); |
| 100 | |
| 101 | // If specified then alignment must be a "small" power of two. |
| 102 | unsigned AlignmentVal = 0; |
| 103 | if (Alignment) { |
| 104 | llvm::APSInt Val; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 105 | |
Daniel Dunbar | 79cd116 | 2009-03-06 20:45:54 +0000 | [diff] [blame] | 106 | // pack(0) is like pack(), which just works out since that is what |
| 107 | // we use 0 for in PackAttr. |
Chris Lattner | 5a0c351 | 2009-02-17 00:57:29 +0000 | [diff] [blame] | 108 | if (!Alignment->isIntegerConstantExpr(Val, Context) || |
Daniel Dunbar | 79cd116 | 2009-03-06 20:45:54 +0000 | [diff] [blame] | 109 | !(Val == 0 || Val.isPowerOf2()) || |
Chris Lattner | 5a0c351 | 2009-02-17 00:57:29 +0000 | [diff] [blame] | 110 | Val.getZExtValue() > 16) { |
| 111 | Diag(PragmaLoc, diag::warn_pragma_pack_invalid_alignment); |
| 112 | Alignment->Destroy(Context); |
| 113 | return; // Ignore |
| 114 | } |
| 115 | |
| 116 | AlignmentVal = (unsigned) Val.getZExtValue(); |
| 117 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 118 | |
Chris Lattner | 574aa40 | 2009-02-17 01:09:29 +0000 | [diff] [blame] | 119 | if (PackContext == 0) |
| 120 | PackContext = new PragmaPackStack(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 121 | |
Chris Lattner | 574aa40 | 2009-02-17 01:09:29 +0000 | [diff] [blame] | 122 | PragmaPackStack *Context = static_cast<PragmaPackStack*>(PackContext); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 123 | |
Chris Lattner | 5a0c351 | 2009-02-17 00:57:29 +0000 | [diff] [blame] | 124 | switch (Kind) { |
| 125 | case Action::PPK_Default: // pack([n]) |
Chris Lattner | 574aa40 | 2009-02-17 01:09:29 +0000 | [diff] [blame] | 126 | Context->setAlignment(AlignmentVal); |
Chris Lattner | 5a0c351 | 2009-02-17 00:57:29 +0000 | [diff] [blame] | 127 | break; |
| 128 | |
| 129 | case Action::PPK_Show: // pack(show) |
| 130 | // Show the current alignment, making sure to show the right value |
| 131 | // for the default. |
Chris Lattner | 574aa40 | 2009-02-17 01:09:29 +0000 | [diff] [blame] | 132 | AlignmentVal = Context->getAlignment(); |
Chris Lattner | 5a0c351 | 2009-02-17 00:57:29 +0000 | [diff] [blame] | 133 | // FIXME: This should come from the target. |
| 134 | if (AlignmentVal == 0) |
| 135 | AlignmentVal = 8; |
| 136 | Diag(PragmaLoc, diag::warn_pragma_pack_show) << AlignmentVal; |
| 137 | break; |
| 138 | |
| 139 | case Action::PPK_Push: // pack(push [, id] [, [n]) |
Chris Lattner | 574aa40 | 2009-02-17 01:09:29 +0000 | [diff] [blame] | 140 | Context->push(Name); |
Chris Lattner | 5a0c351 | 2009-02-17 00:57:29 +0000 | [diff] [blame] | 141 | // Set the new alignment if specified. |
| 142 | if (Alignment) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 143 | Context->setAlignment(AlignmentVal); |
Chris Lattner | 5a0c351 | 2009-02-17 00:57:29 +0000 | [diff] [blame] | 144 | break; |
| 145 | |
| 146 | case Action::PPK_Pop: // pack(pop [, id] [, n]) |
| 147 | // MSDN, C/C++ Preprocessor Reference > Pragma Directives > pack: |
| 148 | // "#pragma pack(pop, identifier, n) is undefined" |
| 149 | if (Alignment && Name) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 150 | Diag(PragmaLoc, diag::warn_pragma_pack_pop_identifer_and_alignment); |
| 151 | |
Chris Lattner | 5a0c351 | 2009-02-17 00:57:29 +0000 | [diff] [blame] | 152 | // Do the pop. |
Chris Lattner | 574aa40 | 2009-02-17 01:09:29 +0000 | [diff] [blame] | 153 | if (!Context->pop(Name)) { |
Chris Lattner | 5a0c351 | 2009-02-17 00:57:29 +0000 | [diff] [blame] | 154 | // If a name was specified then failure indicates the name |
| 155 | // wasn't found. Otherwise failure indicates the stack was |
| 156 | // empty. |
| 157 | Diag(PragmaLoc, diag::warn_pragma_pack_pop_failed) |
| 158 | << (Name ? "no record matching name" : "stack empty"); |
| 159 | |
| 160 | // FIXME: Warn about popping named records as MSVC does. |
| 161 | } else { |
| 162 | // Pop succeeded, set the new alignment if specified. |
| 163 | if (Alignment) |
Chris Lattner | 574aa40 | 2009-02-17 01:09:29 +0000 | [diff] [blame] | 164 | Context->setAlignment(AlignmentVal); |
Chris Lattner | 5a0c351 | 2009-02-17 00:57:29 +0000 | [diff] [blame] | 165 | } |
| 166 | break; |
| 167 | |
| 168 | default: |
| 169 | assert(0 && "Invalid #pragma pack kind."); |
| 170 | } |
| 171 | } |
| 172 | |
Ted Kremenek | 7a02a37 | 2009-08-03 23:24:57 +0000 | [diff] [blame] | 173 | void Sema::ActOnPragmaUnused(const Token *Identifiers, unsigned NumIdentifiers, |
| 174 | Scope *curScope, |
Ted Kremenek | 4726d03 | 2009-03-23 22:28:25 +0000 | [diff] [blame] | 175 | SourceLocation PragmaLoc, |
| 176 | SourceLocation LParenLoc, |
| 177 | SourceLocation RParenLoc) { |
Ted Kremenek | 4726d03 | 2009-03-23 22:28:25 +0000 | [diff] [blame] | 178 | |
Ted Kremenek | 7a02a37 | 2009-08-03 23:24:57 +0000 | [diff] [blame] | 179 | for (unsigned i = 0; i < NumIdentifiers; ++i) { |
| 180 | const Token &Tok = Identifiers[i]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 181 | IdentifierInfo *Name = Tok.getIdentifierInfo(); |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame^] | 182 | LookupResult Lookup(*this, Name, Tok.getLocation(), LookupOrdinaryName); |
| 183 | LookupParsedName(Lookup, curScope, NULL, true); |
Ted Kremenek | 4726d03 | 2009-03-23 22:28:25 +0000 | [diff] [blame] | 184 | |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 185 | NamedDecl *ND = Lookup.getAsSingleDecl(Context); |
Ted Kremenek | 7a02a37 | 2009-08-03 23:24:57 +0000 | [diff] [blame] | 186 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 187 | if (!ND) { |
Ted Kremenek | 7a02a37 | 2009-08-03 23:24:57 +0000 | [diff] [blame] | 188 | Diag(PragmaLoc, diag::warn_pragma_unused_undeclared_var) |
| 189 | << Name << SourceRange(Tok.getLocation()); |
| 190 | continue; |
Ted Kremenek | 4726d03 | 2009-03-23 22:28:25 +0000 | [diff] [blame] | 191 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 192 | |
Ted Kremenek | 7a02a37 | 2009-08-03 23:24:57 +0000 | [diff] [blame] | 193 | if (!isa<VarDecl>(ND) || !cast<VarDecl>(ND)->hasLocalStorage()) { |
| 194 | Diag(PragmaLoc, diag::warn_pragma_unused_expected_localvar) |
| 195 | << Name << SourceRange(Tok.getLocation()); |
| 196 | continue; |
| 197 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 198 | |
Ted Kremenek | 7a02a37 | 2009-08-03 23:24:57 +0000 | [diff] [blame] | 199 | ND->addAttr(::new (Context) UnusedAttr()); |
Ted Kremenek | 4726d03 | 2009-03-23 22:28:25 +0000 | [diff] [blame] | 200 | } |
| 201 | } |