Skip to content

Commit 3d362cf

Browse files
committed
clang/sema: disallow more than one 'onweship_takes' with different classes
1 parent aee553e commit 3d362cf

File tree

3 files changed

+18
-0
lines changed

3 files changed

+18
-0
lines changed

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3328,6 +3328,10 @@ def err_ownership_returns_index_mismatch : Error<
33283328
"'ownership_returns' attribute index does not match; here it is %0">;
33293329
def note_ownership_returns_index_mismatch : Note<
33303330
"declared with index %0 here">;
3331+
def err_ownership_takes_class_mismatch : Error<
3332+
"'ownership_takes' attribute class does not match; here it is '%0'">;
3333+
def note_ownership_takes_class_mismatch : Note<
3334+
"declared with class '%0' here">;
33313335
def err_format_strftime_third_parameter : Error<
33323336
"strftime format attribute requires 3rd parameter to be 0">;
33333337
def err_format_attribute_not : Error<"format argument not a string type">;

clang/lib/Sema/SemaDeclAttr.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1537,6 +1537,16 @@ static void handleOwnershipAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
15371537
<< Idx.getSourceIndex() << Ex->getSourceRange();
15381538
return;
15391539
}
1540+
} else if (K == OwnershipAttr::Takes &&
1541+
I->getOwnKind() == OwnershipAttr::Takes) {
1542+
if (I->getModule()->getName() != ModuleName) {
1543+
S.Diag(I->getLocation(), diag::err_ownership_takes_class_mismatch)
1544+
<< I->getModule()->getName();
1545+
S.Diag(AL.getLoc(), diag::note_ownership_takes_class_mismatch)
1546+
<< ModuleName << Ex->getSourceRange();
1547+
1548+
return;
1549+
}
15401550
}
15411551
}
15421552
OwnershipArgs.push_back(Idx);

clang/test/Sema/attr-ownership.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,7 @@ void f15(int, int)
2424
void f16(int *i, int *j) __attribute__((ownership_holds(foo, 1))) __attribute__((ownership_holds(foo, 1))); // OK, same index
2525
void f17(void*) __attribute__((ownership_takes(__, 1)));
2626
void f18() __attribute__((ownership_takes(foo, 1))); // expected-warning {{'ownership_takes' attribute only applies to non-K&R-style functions}}
27+
28+
int f19(void *)
29+
__attribute__((ownership_takes(foo, 1))) // expected-error {{'ownership_takes' attribute class does not match; here it is 'foo'}}
30+
__attribute__((ownership_takes(foo1, 1))); // expected-note {{declared with class 'foo1' here}}

0 commit comments

Comments
 (0)