This (proto) patch addresses bug 6037 http://llvm.org/bugs/show_bug.cgi?id=6037
A request for a class definition to warn about inaccessible direct base classes.
I've added a check in Sema::AttachBaseSpecifiers to iterate over the direct
base array looking for ambiguous conversions to the individual bases. The
output is something like:
6037.cc:8:15: warning: direct base 'A' is inaccessible due to ambiguity:
struct C -> struct B -> struct A
struct C -> struct A
struct C : B, A {
^
which matches the form of error for an ambiguous base conversion itself.
This patch triggers on a pile of testcases, which I've not yet fixed, because
I'd like some feedback on this approach first -- is there perhaps a better way
to do this checking?
Also, as this is a warning, I presume there should be some way to disable it.
How is that achieved in the clang framework? (I'm not sure if the now failing
tests should be fixed by disabling the warning, for instance).
nathan
Index: src/tools/clang/include/clang/Basic/DiagnosticSemaKinds.td
===================================================================
--- src/tools/clang/include/clang/Basic/DiagnosticSemaKinds.td (revision 223479)
+++ src/tools/clang/include/clang/Basic/DiagnosticSemaKinds.td (working copy)
@@ -6331,6 +6331,8 @@ def err_base_class_has_flexible_array_me
def err_incomplete_base_class : Error<"base class has incomplete type">;
def err_duplicate_base_class : Error<
"base class %0 specified more than once as a direct base class">;
+def warn_inaccessible_base_class : Warning<
+ "direct base %0 is inaccessible due to ambiguity:%1">;
// FIXME: better way to display derivation? Pass entire thing into diagclient?
def err_ambiguous_derived_to_base_conv : Error<
"ambiguous conversion from derived class %0 to base class %1:%2">;
Index: src/tools/clang/lib/Sema/SemaDeclCXX.cpp
===================================================================
--- src/tools/clang/lib/Sema/SemaDeclCXX.cpp (revision 223479)
+++ src/tools/clang/lib/Sema/SemaDeclCXX.cpp (working copy)
@@ -1601,10 +1601,23 @@ bool Sema::AttachBaseSpecifiers(CXXRecor
// Attach the remaining base class specifiers to the derived class.
Class->setBases(Bases, NumGoodBases);
- // Delete the remaining (good) base class specifiers, since their
- // data has been copied into the CXXRecordDecl.
- for (unsigned idx = 0; idx < NumGoodBases; ++idx)
+ for (unsigned idx = 0; idx < NumGoodBases; ++idx) {
+
+ // Check whether this direct base is inaccessible due to ambiguity
+ QualType BaseType = Bases[idx]->getType();
+ CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true,
+ /*DetectVirtual=*/true);
+ (void)IsDerivedFrom(Context.getTagDeclType(Class), BaseType, Paths);
+ if (Paths.isAmbiguous(Context.getCanonicalType(BaseType)
+ .getUnqualifiedType ()))
+ Diag(Bases[idx]->getLocStart (), diag::warn_inaccessible_base_class)
+ << BaseType << getAmbiguousPathsDisplayString (Paths)
+ << Bases[idx]->getSourceRange();
+
+ // Delete the base class specifier, since its data has been copied
+ // into the CXXRecordDecl.
Context.Deallocate(Bases[idx]);
+ }
return Invalid;
}
_______________________________________________
cfe-commits mailing list
[email protected]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits