From bb94a88d0f08ec09dfcedc39bce4a3dd28cb8f6d Mon Sep 17 00:00:00 2001 From: Ludovic Cintrat Date: Sun, 13 Mar 2016 18:39:28 +0100 Subject: [PATCH] Remove methods marked with "override" from public API Fix #761 --- .../visitors/AbstractCxxPublicApiVisitor.java | 22 +++++++++++++++++++ .../src/test/resources/metrics/public_api.h | 4 +++- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/cxx-squid/src/main/java/org/sonar/cxx/visitors/AbstractCxxPublicApiVisitor.java b/cxx-squid/src/main/java/org/sonar/cxx/visitors/AbstractCxxPublicApiVisitor.java index 08e3efaa8b..22b2d73ee7 100644 --- a/cxx-squid/src/main/java/org/sonar/cxx/visitors/AbstractCxxPublicApiVisitor.java +++ b/cxx-squid/src/main/java/org/sonar/cxx/visitors/AbstractCxxPublicApiVisitor.java @@ -88,6 +88,8 @@ abstract protected void onPublicApi(AstNode node, String id, private static final String UNNAMED_CLASSIFIER_ID = ""; private static final String UNNAMED_ENUM_ID = ""; + private static final String TOKEN_OVERRIDE = "override"; + public interface PublicApiHandler { void onPublicApi(AstNode node, String id, List comments); @@ -418,6 +420,20 @@ private boolean isDefaultOrDeleteFunctionBody(AstNode functionBodyNode) { return defaultOrDelete; } + private boolean isOverriddenMethod(AstNode memberDeclarator) { + List modifiers = memberDeclarator.getDescendants(CxxGrammarImpl.cliFunctionModifier); + + for (AstNode modifier : modifiers) { + AstNode modifierId = modifier.getFirstChild(); + + if (TOKEN_OVERRIDE.equals(modifierId.getTokenValue())) { + return true; + } + } + + return false; + } + /** * Find documentation node, associated documentation, identifier of a * public member declarator and visit it as a public API. @@ -430,6 +446,12 @@ private void visitMemberDeclarator(AstNode node) { CxxGrammarImpl.templateDeclaration, CxxGrammarImpl.classSpecifier); + if (isOverriddenMethod(node)) { + // assume that ancestor method is documented + // and do not count as public API + return; + } + AstNode docNode; if (container == null diff --git a/cxx-squid/src/test/resources/metrics/public_api.h b/cxx-squid/src/test/resources/metrics/public_api.h index 2e956fcd55..c5c7492b03 100644 --- a/cxx-squid/src/test/resources/metrics/public_api.h +++ b/cxx-squid/src/test/resources/metrics/public_api.h @@ -56,7 +56,9 @@ class testClass protectedMethod doc */ virtual void protectedMethod(); - + + virtual void overriddenMethod() override; // no doc is OK, it could come from ancestor + /** protectedStruct doc */