1193326Sed//===--- SemaAttr.cpp - Semantic Analysis for Attributes ------------------===// 2193326Sed// 3193326Sed// The LLVM Compiler Infrastructure 4193326Sed// 5193326Sed// This file is distributed under the University of Illinois Open Source 6193326Sed// License. See LICENSE.TXT for details. 7193326Sed// 8193326Sed//===----------------------------------------------------------------------===// 9193326Sed// 10193326Sed// This file implements semantic analysis for non-trivial attributes and 11193326Sed// pragmas. 12193326Sed// 13193326Sed//===----------------------------------------------------------------------===// 14193326Sed 15212904Sdim#include "clang/Sema/SemaInternal.h" 16212904Sdim#include "clang/AST/Attr.h" 17193326Sed#include "clang/AST/Expr.h" 18208600Srdivacky#include "clang/Basic/TargetInfo.h" 19208600Srdivacky#include "clang/Lex/Preprocessor.h" 20249423Sdim#include "clang/Sema/Lookup.h" 21193326Sedusing namespace clang; 22193326Sed 23193326Sed//===----------------------------------------------------------------------===// 24208600Srdivacky// Pragma 'pack' and 'options align' 25193326Sed//===----------------------------------------------------------------------===// 26193326Sed 27193326Sednamespace { 28208600Srdivacky struct PackStackEntry { 29208600Srdivacky // We just use a sentinel to represent when the stack is set to mac68k 30208600Srdivacky // alignment. 31208600Srdivacky static const unsigned kMac68kAlignmentSentinel = ~0U; 32208600Srdivacky 33208600Srdivacky unsigned Alignment; 34208600Srdivacky IdentifierInfo *Name; 35208600Srdivacky }; 36208600Srdivacky 37193326Sed /// PragmaPackStack - Simple class to wrap the stack used by #pragma 38193326Sed /// pack. 39193326Sed class PragmaPackStack { 40208600Srdivacky typedef std::vector<PackStackEntry> stack_ty; 41193326Sed 42193326Sed /// Alignment - The current user specified alignment. 43193326Sed unsigned Alignment; 44193326Sed 45193326Sed /// Stack - Entries in the #pragma pack stack, consisting of saved 46193326Sed /// alignments and optional names. 47193326Sed stack_ty Stack; 48198092Srdivacky 49198092Srdivacky public: 50193326Sed PragmaPackStack() : Alignment(0) {} 51193326Sed 52193326Sed void setAlignment(unsigned A) { Alignment = A; } 53193326Sed unsigned getAlignment() { return Alignment; } 54193326Sed 55193326Sed /// push - Push the current alignment onto the stack, optionally 56193326Sed /// using the given \arg Name for the record, if non-zero. 57193326Sed void push(IdentifierInfo *Name) { 58208600Srdivacky PackStackEntry PSE = { Alignment, Name }; 59208600Srdivacky Stack.push_back(PSE); 60193326Sed } 61193326Sed 62193326Sed /// pop - Pop a record from the stack and restore the current 63193326Sed /// alignment to the previous value. If \arg Name is non-zero then 64193326Sed /// the first such named record is popped, otherwise the top record 65193326Sed /// is popped. Returns true if the pop succeeded. 66212904Sdim bool pop(IdentifierInfo *Name, bool IsReset); 67193326Sed }; 68193326Sed} // end anonymous namespace. 69193326Sed 70212904Sdimbool PragmaPackStack::pop(IdentifierInfo *Name, bool IsReset) { 71193326Sed // If name is empty just pop top. 72193326Sed if (!Name) { 73212904Sdim // An empty stack is a special case... 74212904Sdim if (Stack.empty()) { 75212904Sdim // If this isn't a reset, it is always an error. 76212904Sdim if (!IsReset) 77212904Sdim return false; 78212904Sdim 79212904Sdim // Otherwise, it is an error only if some alignment has been set. 80212904Sdim if (!Alignment) 81212904Sdim return false; 82212904Sdim 83212904Sdim // Otherwise, reset to the default alignment. 84212904Sdim Alignment = 0; 85212904Sdim } else { 86212904Sdim Alignment = Stack.back().Alignment; 87212904Sdim Stack.pop_back(); 88212904Sdim } 89212904Sdim 90193326Sed return true; 91198092Srdivacky } 92198092Srdivacky 93193326Sed // Otherwise, find the named record. 94193326Sed for (unsigned i = Stack.size(); i != 0; ) { 95193326Sed --i; 96208600Srdivacky if (Stack[i].Name == Name) { 97193326Sed // Found it, pop up to and including this record. 98208600Srdivacky Alignment = Stack[i].Alignment; 99193326Sed Stack.erase(Stack.begin() + i, Stack.end()); 100193326Sed return true; 101193326Sed } 102193326Sed } 103198092Srdivacky 104193326Sed return false; 105193326Sed} 106193326Sed 107193326Sed 108193326Sed/// FreePackedContext - Deallocate and null out PackContext. 109193326Sedvoid Sema::FreePackedContext() { 110193326Sed delete static_cast<PragmaPackStack*>(PackContext); 111193326Sed PackContext = 0; 112193326Sed} 113193326Sed 114208600Srdivackyvoid Sema::AddAlignmentAttributesForRecord(RecordDecl *RD) { 115208600Srdivacky // If there is no pack context, we don't need any attributes. 116208600Srdivacky if (!PackContext) 117208600Srdivacky return; 118208600Srdivacky 119208600Srdivacky PragmaPackStack *Stack = static_cast<PragmaPackStack*>(PackContext); 120208600Srdivacky 121208600Srdivacky // Otherwise, check to see if we need a max field alignment attribute. 122208600Srdivacky if (unsigned Alignment = Stack->getAlignment()) { 123208600Srdivacky if (Alignment == PackStackEntry::kMac68kAlignmentSentinel) 124212904Sdim RD->addAttr(::new (Context) AlignMac68kAttr(SourceLocation(), Context)); 125208600Srdivacky else 126212904Sdim RD->addAttr(::new (Context) MaxFieldAlignmentAttr(SourceLocation(), 127212904Sdim Context, 128212904Sdim Alignment * 8)); 129208600Srdivacky } 130193326Sed} 131193326Sed 132221345Sdimvoid Sema::AddMsStructLayoutForRecord(RecordDecl *RD) { 133221345Sdim if (!MSStructPragmaOn) 134221345Sdim return; 135221345Sdim RD->addAttr(::new (Context) MsStructAttr(SourceLocation(), Context)); 136221345Sdim} 137221345Sdim 138208600Srdivackyvoid Sema::ActOnPragmaOptionsAlign(PragmaOptionsAlignKind Kind, 139243830Sdim SourceLocation PragmaLoc) { 140208600Srdivacky if (PackContext == 0) 141208600Srdivacky PackContext = new PragmaPackStack(); 142208600Srdivacky 143208600Srdivacky PragmaPackStack *Context = static_cast<PragmaPackStack*>(PackContext); 144208600Srdivacky 145208600Srdivacky switch (Kind) { 146210299Sed // For all targets we support native and natural are the same. 147210299Sed // 148210299Sed // FIXME: This is not true on Darwin/PPC. 149210299Sed case POAK_Native: 150210299Sed case POAK_Power: 151208600Srdivacky case POAK_Natural: 152208600Srdivacky Context->push(0); 153208600Srdivacky Context->setAlignment(0); 154208600Srdivacky break; 155208600Srdivacky 156210299Sed // Note that '#pragma options align=packed' is not equivalent to attribute 157210299Sed // packed, it has a different precedence relative to attribute aligned. 158210299Sed case POAK_Packed: 159210299Sed Context->push(0); 160210299Sed Context->setAlignment(1); 161210299Sed break; 162210299Sed 163208600Srdivacky case POAK_Mac68k: 164208600Srdivacky // Check if the target supports this. 165208600Srdivacky if (!PP.getTargetInfo().hasAlignMac68kSupport()) { 166208600Srdivacky Diag(PragmaLoc, diag::err_pragma_options_align_mac68k_target_unsupported); 167208600Srdivacky return; 168208600Srdivacky } 169208600Srdivacky Context->push(0); 170208600Srdivacky Context->setAlignment(PackStackEntry::kMac68kAlignmentSentinel); 171208600Srdivacky break; 172208600Srdivacky 173243830Sdim case POAK_Reset: 174243830Sdim // Reset just pops the top of the stack, or resets the current alignment to 175243830Sdim // default. 176243830Sdim if (!Context->pop(0, /*IsReset=*/true)) { 177243830Sdim Diag(PragmaLoc, diag::warn_pragma_options_align_reset_failed) 178243830Sdim << "stack empty"; 179243830Sdim } 180208600Srdivacky break; 181208600Srdivacky } 182208600Srdivacky} 183208600Srdivacky 184198092Srdivackyvoid Sema::ActOnPragmaPack(PragmaPackKind Kind, IdentifierInfo *Name, 185226633Sdim Expr *alignment, SourceLocation PragmaLoc, 186193326Sed SourceLocation LParenLoc, SourceLocation RParenLoc) { 187193326Sed Expr *Alignment = static_cast<Expr *>(alignment); 188193326Sed 189193326Sed // If specified then alignment must be a "small" power of two. 190193326Sed unsigned AlignmentVal = 0; 191193326Sed if (Alignment) { 192193326Sed llvm::APSInt Val; 193198092Srdivacky 194193326Sed // pack(0) is like pack(), which just works out since that is what 195193326Sed // we use 0 for in PackAttr. 196208600Srdivacky if (Alignment->isTypeDependent() || 197208600Srdivacky Alignment->isValueDependent() || 198208600Srdivacky !Alignment->isIntegerConstantExpr(Val, Context) || 199193326Sed !(Val == 0 || Val.isPowerOf2()) || 200193326Sed Val.getZExtValue() > 16) { 201193326Sed Diag(PragmaLoc, diag::warn_pragma_pack_invalid_alignment); 202193326Sed return; // Ignore 203193326Sed } 204193326Sed 205193326Sed AlignmentVal = (unsigned) Val.getZExtValue(); 206193326Sed } 207198092Srdivacky 208193326Sed if (PackContext == 0) 209193326Sed PackContext = new PragmaPackStack(); 210198092Srdivacky 211193326Sed PragmaPackStack *Context = static_cast<PragmaPackStack*>(PackContext); 212198092Srdivacky 213193326Sed switch (Kind) { 214212904Sdim case Sema::PPK_Default: // pack([n]) 215193326Sed Context->setAlignment(AlignmentVal); 216193326Sed break; 217193326Sed 218212904Sdim case Sema::PPK_Show: // pack(show) 219193326Sed // Show the current alignment, making sure to show the right value 220193326Sed // for the default. 221193326Sed AlignmentVal = Context->getAlignment(); 222193326Sed // FIXME: This should come from the target. 223193326Sed if (AlignmentVal == 0) 224193326Sed AlignmentVal = 8; 225208600Srdivacky if (AlignmentVal == PackStackEntry::kMac68kAlignmentSentinel) 226208600Srdivacky Diag(PragmaLoc, diag::warn_pragma_pack_show) << "mac68k"; 227208600Srdivacky else 228208600Srdivacky Diag(PragmaLoc, diag::warn_pragma_pack_show) << AlignmentVal; 229193326Sed break; 230193326Sed 231212904Sdim case Sema::PPK_Push: // pack(push [, id] [, [n]) 232193326Sed Context->push(Name); 233193326Sed // Set the new alignment if specified. 234193326Sed if (Alignment) 235198092Srdivacky Context->setAlignment(AlignmentVal); 236193326Sed break; 237193326Sed 238212904Sdim case Sema::PPK_Pop: // pack(pop [, id] [, n]) 239193326Sed // MSDN, C/C++ Preprocessor Reference > Pragma Directives > pack: 240193326Sed // "#pragma pack(pop, identifier, n) is undefined" 241193326Sed if (Alignment && Name) 242198092Srdivacky Diag(PragmaLoc, diag::warn_pragma_pack_pop_identifer_and_alignment); 243198092Srdivacky 244193326Sed // Do the pop. 245212904Sdim if (!Context->pop(Name, /*IsReset=*/false)) { 246193326Sed // If a name was specified then failure indicates the name 247193326Sed // wasn't found. Otherwise failure indicates the stack was 248193326Sed // empty. 249193326Sed Diag(PragmaLoc, diag::warn_pragma_pack_pop_failed) 250193326Sed << (Name ? "no record matching name" : "stack empty"); 251193326Sed 252193326Sed // FIXME: Warn about popping named records as MSVC does. 253193326Sed } else { 254193326Sed // Pop succeeded, set the new alignment if specified. 255193326Sed if (Alignment) 256193326Sed Context->setAlignment(AlignmentVal); 257193326Sed } 258193326Sed break; 259193326Sed } 260193326Sed} 261193326Sed 262221345Sdimvoid Sema::ActOnPragmaMSStruct(PragmaMSStructKind Kind) { 263221345Sdim MSStructPragmaOn = (Kind == PMSST_ON); 264221345Sdim} 265221345Sdim 266218893Sdimvoid Sema::ActOnPragmaUnused(const Token &IdTok, Scope *curScope, 267218893Sdim SourceLocation PragmaLoc) { 268198092Srdivacky 269218893Sdim IdentifierInfo *Name = IdTok.getIdentifierInfo(); 270218893Sdim LookupResult Lookup(*this, Name, IdTok.getLocation(), LookupOrdinaryName); 271218893Sdim LookupParsedName(Lookup, curScope, NULL, true); 272198092Srdivacky 273218893Sdim if (Lookup.empty()) { 274218893Sdim Diag(PragmaLoc, diag::warn_pragma_unused_undeclared_var) 275218893Sdim << Name << SourceRange(IdTok.getLocation()); 276218893Sdim return; 277218893Sdim } 278193326Sed 279218893Sdim VarDecl *VD = Lookup.getAsSingle<VarDecl>(); 280218893Sdim if (!VD) { 281218893Sdim Diag(PragmaLoc, diag::warn_pragma_unused_expected_var_arg) 282218893Sdim << Name << SourceRange(IdTok.getLocation()); 283218893Sdim return; 284218893Sdim } 285193326Sed 286218893Sdim // Warn if this was used before being marked unused. 287218893Sdim if (VD->isUsed()) 288218893Sdim Diag(PragmaLoc, diag::warn_used_but_marked_unused) << Name; 289218893Sdim 290218893Sdim VD->addAttr(::new (Context) UnusedAttr(IdTok.getLocation(), Context)); 291193326Sed} 292212904Sdim 293226633Sdimvoid Sema::AddCFAuditedAttribute(Decl *D) { 294226633Sdim SourceLocation Loc = PP.getPragmaARCCFCodeAuditedLoc(); 295226633Sdim if (!Loc.isValid()) return; 296226633Sdim 297226633Sdim // Don't add a redundant or conflicting attribute. 298226633Sdim if (D->hasAttr<CFAuditedTransferAttr>() || 299226633Sdim D->hasAttr<CFUnknownTransferAttr>()) 300226633Sdim return; 301226633Sdim 302226633Sdim D->addAttr(::new (Context) CFAuditedTransferAttr(Loc, Context)); 303226633Sdim} 304226633Sdim 305218893Sdimtypedef std::vector<std::pair<unsigned, SourceLocation> > VisStack; 306218893Sdimenum { NoVisibility = (unsigned) -1 }; 307212904Sdim 308212904Sdimvoid Sema::AddPushedVisibilityAttribute(Decl *D) { 309212904Sdim if (!VisContext) 310212904Sdim return; 311212904Sdim 312249423Sdim NamedDecl *ND = dyn_cast<NamedDecl>(D); 313249423Sdim if (ND && ND->getExplicitVisibility(NamedDecl::VisibilityForValue)) 314212904Sdim return; 315212904Sdim 316212904Sdim VisStack *Stack = static_cast<VisStack*>(VisContext); 317218893Sdim unsigned rawType = Stack->back().first; 318218893Sdim if (rawType == NoVisibility) return; 319218893Sdim 320218893Sdim VisibilityAttr::VisibilityType type 321218893Sdim = (VisibilityAttr::VisibilityType) rawType; 322212904Sdim SourceLocation loc = Stack->back().second; 323212904Sdim 324212904Sdim D->addAttr(::new (Context) VisibilityAttr(loc, Context, type)); 325212904Sdim} 326212904Sdim 327212904Sdim/// FreeVisContext - Deallocate and null out VisContext. 328212904Sdimvoid Sema::FreeVisContext() { 329212904Sdim delete static_cast<VisStack*>(VisContext); 330212904Sdim VisContext = 0; 331212904Sdim} 332212904Sdim 333218893Sdimstatic void PushPragmaVisibility(Sema &S, unsigned type, SourceLocation loc) { 334212904Sdim // Put visibility on stack. 335212904Sdim if (!S.VisContext) 336212904Sdim S.VisContext = new VisStack; 337212904Sdim 338212904Sdim VisStack *Stack = static_cast<VisStack*>(S.VisContext); 339212904Sdim Stack->push_back(std::make_pair(type, loc)); 340212904Sdim} 341212904Sdim 342234353Sdimvoid Sema::ActOnPragmaVisibility(const IdentifierInfo* VisType, 343212904Sdim SourceLocation PragmaLoc) { 344234353Sdim if (VisType) { 345212904Sdim // Compute visibility to use. 346212904Sdim VisibilityAttr::VisibilityType type; 347212904Sdim if (VisType->isStr("default")) 348212904Sdim type = VisibilityAttr::Default; 349212904Sdim else if (VisType->isStr("hidden")) 350212904Sdim type = VisibilityAttr::Hidden; 351212904Sdim else if (VisType->isStr("internal")) 352212904Sdim type = VisibilityAttr::Hidden; // FIXME 353212904Sdim else if (VisType->isStr("protected")) 354212904Sdim type = VisibilityAttr::Protected; 355212904Sdim else { 356212904Sdim Diag(PragmaLoc, diag::warn_attribute_unknown_visibility) << 357212904Sdim VisType->getName(); 358212904Sdim return; 359212904Sdim } 360212904Sdim PushPragmaVisibility(*this, type, PragmaLoc); 361212904Sdim } else { 362234353Sdim PopPragmaVisibility(false, PragmaLoc); 363212904Sdim } 364212904Sdim} 365212904Sdim 366218893Sdimvoid Sema::ActOnPragmaFPContract(tok::OnOffSwitch OOS) { 367218893Sdim switch (OOS) { 368218893Sdim case tok::OOS_ON: 369218893Sdim FPFeatures.fp_contract = 1; 370218893Sdim break; 371218893Sdim case tok::OOS_OFF: 372218893Sdim FPFeatures.fp_contract = 0; 373218893Sdim break; 374218893Sdim case tok::OOS_DEFAULT: 375234353Sdim FPFeatures.fp_contract = getLangOpts().DefaultFPContract; 376218893Sdim break; 377218893Sdim } 378212904Sdim} 379212904Sdim 380234353Sdimvoid Sema::PushNamespaceVisibilityAttr(const VisibilityAttr *Attr, 381234353Sdim SourceLocation Loc) { 382218893Sdim // Visibility calculations will consider the namespace's visibility. 383218893Sdim // Here we just want to note that we're in a visibility context 384218893Sdim // which overrides any enclosing #pragma context, but doesn't itself 385218893Sdim // contribute visibility. 386234353Sdim PushPragmaVisibility(*this, NoVisibility, Loc); 387218893Sdim} 388218893Sdim 389234353Sdimvoid Sema::PopPragmaVisibility(bool IsNamespaceEnd, SourceLocation EndLoc) { 390234353Sdim if (!VisContext) { 391234353Sdim Diag(EndLoc, diag::err_pragma_pop_visibility_mismatch); 392234353Sdim return; 393234353Sdim } 394212904Sdim 395234353Sdim // Pop visibility from stack 396234353Sdim VisStack *Stack = static_cast<VisStack*>(VisContext); 397234353Sdim 398234353Sdim const std::pair<unsigned, SourceLocation> *Back = &Stack->back(); 399234353Sdim bool StartsWithPragma = Back->first != NoVisibility; 400234353Sdim if (StartsWithPragma && IsNamespaceEnd) { 401234353Sdim Diag(Back->second, diag::err_pragma_push_visibility_mismatch); 402234353Sdim Diag(EndLoc, diag::note_surrounding_namespace_ends_here); 403234353Sdim 404234353Sdim // For better error recovery, eat all pushes inside the namespace. 405234353Sdim do { 406234353Sdim Stack->pop_back(); 407234353Sdim Back = &Stack->back(); 408234353Sdim StartsWithPragma = Back->first != NoVisibility; 409234353Sdim } while (StartsWithPragma); 410234353Sdim } else if (!StartsWithPragma && !IsNamespaceEnd) { 411234353Sdim Diag(EndLoc, diag::err_pragma_pop_visibility_mismatch); 412234353Sdim Diag(Back->second, diag::note_surrounding_namespace_starts_here); 413234353Sdim return; 414212904Sdim } 415234353Sdim 416234353Sdim Stack->pop_back(); 417234353Sdim // To simplify the implementation, never keep around an empty stack. 418234353Sdim if (Stack->empty()) 419234353Sdim FreeVisContext(); 420212904Sdim} 421