Home | History | Annotate | Download | only in Sema
      1 //===--- SemaStmtAttr.cpp - Statement Attribute Handling ------------------===//
      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 //
     10 //  This file implements stmt-related attribute processing.
     11 //
     12 //===----------------------------------------------------------------------===//
     13 
     14 #include "clang/Sema/SemaInternal.h"
     15 #include "TargetAttributesSema.h"
     16 #include "clang/AST/ASTContext.h"
     17 #include "clang/Basic/SourceManager.h"
     18 #include "clang/Lex/Lexer.h"
     19 #include "clang/Sema/DelayedDiagnostic.h"
     20 #include "clang/Sema/Lookup.h"
     21 #include "clang/Sema/ScopeInfo.h"
     22 #include "llvm/ADT/StringExtras.h"
     23 
     24 using namespace clang;
     25 using namespace sema;
     26 
     27 static Attr *handleFallThroughAttr(Sema &S, Stmt *St, const AttributeList &A,
     28                                    SourceRange Range) {
     29   if (!isa<NullStmt>(St)) {
     30     S.Diag(A.getRange().getBegin(), diag::err_fallthrough_attr_wrong_target)
     31         << St->getLocStart();
     32     if (isa<SwitchCase>(St)) {
     33       SourceLocation L = Lexer::getLocForEndOfToken(Range.getEnd(), 0,
     34                                   S.getSourceManager(), S.getLangOpts());
     35       S.Diag(L, diag::note_fallthrough_insert_semi_fixit)
     36           << FixItHint::CreateInsertion(L, ";");
     37     }
     38     return 0;
     39   }
     40   if (S.getCurFunction()->SwitchStack.empty()) {
     41     S.Diag(A.getRange().getBegin(), diag::err_fallthrough_attr_outside_switch);
     42     return 0;
     43   }
     44   return ::new (S.Context) FallThroughAttr(A.getRange(), S.Context);
     45 }
     46 
     47 
     48 static Attr *ProcessStmtAttribute(Sema &S, Stmt *St, const AttributeList &A,
     49                                   SourceRange Range) {
     50   switch (A.getKind()) {
     51   case AttributeList::AT_FallThrough:
     52     return handleFallThroughAttr(S, St, A, Range);
     53   default:
     54     // if we're here, then we parsed an attribute, but didn't recognize it as a
     55     // statement attribute => it is declaration attribute
     56     S.Diag(A.getRange().getBegin(), diag::warn_attribute_invalid_on_stmt)
     57         << A.getName()->getName() << St->getLocStart();
     58     return 0;
     59   }
     60 }
     61 
     62 StmtResult Sema::ProcessStmtAttributes(Stmt *S, AttributeList *AttrList,
     63                                        SourceRange Range) {
     64   SmallVector<const Attr*, 8> Attrs;
     65   for (const AttributeList* l = AttrList; l; l = l->getNext()) {
     66     if (Attr *a = ProcessStmtAttribute(*this, S, *l, Range))
     67       Attrs.push_back(a);
     68   }
     69 
     70   if (Attrs.empty())
     71     return S;
     72 
     73   return ActOnAttributedStmt(Range.getBegin(), Attrs, S);
     74 }
     75