blob: 3bc620b89ba9d11daed92e9907aa51788f7b26ac [file] [log] [blame]
Richard Smithc202b282012-04-14 00:33:13 +00001//===--- 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"
Richard Smithc202b282012-04-14 00:33:13 +000015#include "clang/AST/ASTContext.h"
16#include "clang/Basic/SourceManager.h"
17#include "clang/Sema/DelayedDiagnostic.h"
18#include "clang/Sema/Lookup.h"
Richard Smith84837d52012-05-03 18:27:39 +000019#include "clang/Sema/ScopeInfo.h"
Richard Smithc202b282012-04-14 00:33:13 +000020#include "llvm/ADT/StringExtras.h"
Richard Smith84837d52012-05-03 18:27:39 +000021
Richard Smithc202b282012-04-14 00:33:13 +000022using namespace clang;
23using namespace sema;
24
Richard Smith84837d52012-05-03 18:27:39 +000025static Attr *handleFallThroughAttr(Sema &S, Stmt *St, const AttributeList &A,
26 SourceRange Range) {
27 if (!isa<NullStmt>(St)) {
28 S.Diag(A.getRange().getBegin(), diag::err_fallthrough_attr_wrong_target)
29 << St->getLocStart();
30 if (isa<SwitchCase>(St)) {
Alp Tokerb6cc5922014-05-03 03:45:55 +000031 SourceLocation L = S.getLocForEndOfToken(Range.getEnd());
Richard Smith84837d52012-05-03 18:27:39 +000032 S.Diag(L, diag::note_fallthrough_insert_semi_fixit)
33 << FixItHint::CreateInsertion(L, ";");
34 }
Craig Topperc3ec1492014-05-26 06:22:03 +000035 return nullptr;
Richard Smith84837d52012-05-03 18:27:39 +000036 }
37 if (S.getCurFunction()->SwitchStack.empty()) {
38 S.Diag(A.getRange().getBegin(), diag::err_fallthrough_attr_outside_switch);
Craig Topperc3ec1492014-05-26 06:22:03 +000039 return nullptr;
Richard Smith84837d52012-05-03 18:27:39 +000040 }
Aaron Ballman36a53502014-01-16 13:03:14 +000041 return ::new (S.Context) FallThroughAttr(A.getRange(), S.Context,
42 A.getAttributeSpellingListIndex());
Richard Smith84837d52012-05-03 18:27:39 +000043}
Richard Smithc202b282012-04-14 00:33:13 +000044
Richard Smith84837d52012-05-03 18:27:39 +000045
46static Attr *ProcessStmtAttribute(Sema &S, Stmt *St, const AttributeList &A,
47 SourceRange Range) {
Richard Smithc202b282012-04-14 00:33:13 +000048 switch (A.getKind()) {
Michael Han23214e52012-10-03 01:56:22 +000049 case AttributeList::UnknownAttribute:
50 S.Diag(A.getLoc(), A.isDeclspecAttribute() ?
51 diag::warn_unhandled_ms_attribute_ignored :
52 diag::warn_unknown_attribute_ignored) << A.getName();
Craig Topperc3ec1492014-05-26 06:22:03 +000053 return nullptr;
Alexis Hunt3bc72c12012-06-19 23:57:03 +000054 case AttributeList::AT_FallThrough:
Richard Smith84837d52012-05-03 18:27:39 +000055 return handleFallThroughAttr(S, St, A, Range);
Richard Smithc202b282012-04-14 00:33:13 +000056 default:
Michael Han23214e52012-10-03 01:56:22 +000057 // if we're here, then we parsed a known attribute, but didn't recognize
58 // it as a statement attribute => it is declaration attribute
Richard Smith4c96e992013-02-19 23:47:15 +000059 S.Diag(A.getRange().getBegin(), diag::err_attribute_invalid_on_stmt)
60 << A.getName() << St->getLocStart();
Craig Topperc3ec1492014-05-26 06:22:03 +000061 return nullptr;
Richard Smithc202b282012-04-14 00:33:13 +000062 }
63}
64
65StmtResult Sema::ProcessStmtAttributes(Stmt *S, AttributeList *AttrList,
66 SourceRange Range) {
Alexander Kornienko20f6fc62012-07-09 10:04:07 +000067 SmallVector<const Attr*, 8> Attrs;
Richard Smithc202b282012-04-14 00:33:13 +000068 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Richard Smith84837d52012-05-03 18:27:39 +000069 if (Attr *a = ProcessStmtAttribute(*this, S, *l, Range))
Richard Smithc202b282012-04-14 00:33:13 +000070 Attrs.push_back(a);
71 }
72
73 if (Attrs.empty())
74 return S;
75
76 return ActOnAttributedStmt(Range.getBegin(), Attrs, S);
77}