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 5e67219906..19fb9da62d 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 @@ -384,10 +384,41 @@ private void visitMemberDeclaration(AstNode memberDeclaration) { private void visitFunctionDefinition(AstNode functionDef) { if (isPublicApiMember(functionDef)) { + // filter out deleted and defaulted methods + AstNode functionBodyNode = functionDef + .getFirstChild(CxxGrammarImpl.functionBody); + + if (functionBodyNode != null) { + if (isDefaultOrDeleteFunctionBody(functionBodyNode)) { + return; + } + } + visitMemberDeclarator(functionDef); } } + private boolean isDefaultOrDeleteFunctionBody(AstNode functionBodyNode) { + boolean defaultOrDelete = false; + List functionBody = functionBodyNode.getChildren(); + + // look for exact sub AST + if (functionBody.size() == 3) { + if (functionBody.get(0).is(CxxPunctuator.ASSIGN) + && functionBody.get(2).is(CxxPunctuator.SEMICOLON)) { + + AstNode bodyType = functionBody.get(1); + + if (bodyType.is(CxxKeyword.DELETE) + || bodyType.is(CxxKeyword.DEFAULT)) { + defaultOrDelete = true; + } + } + } + + return defaultOrDelete; + } + /** * Find documentation node, associated documentation, * identifier of a public member declarator and visit it diff --git a/cxx-squid/src/test/java/org/sonar/cxx/CxxPublicApiVisitorTest.java b/cxx-squid/src/test/java/org/sonar/cxx/CxxPublicApiVisitorTest.java index f36207aa6e..7d0697102c 100644 --- a/cxx-squid/src/test/java/org/sonar/cxx/CxxPublicApiVisitorTest.java +++ b/cxx-squid/src/test/java/org/sonar/cxx/CxxPublicApiVisitorTest.java @@ -123,12 +123,12 @@ public void doxygen_example() { @Test public void to_delete() { - testFile("src/test/resources/metrics/public_api.h", 40, 0, true); + testFile("src/test/resources/metrics/public_api.h", 41, 0, true); } @Test public void no_doc() { - testFile("src/test/resources/metrics/no_doc.h", 21, 21, true); + testFile("src/test/resources/metrics/no_doc.h", 22, 22, true); } @Test @@ -175,6 +175,7 @@ public void onPublicApi(AstNode node, String id, final Map expectedIdCommentMap = new HashMap(); + expectedIdCommentMap.put("publicDefinedMethod", "publicDefinedMethod"); expectedIdCommentMap.put("aliasDeclaration", "aliasDeclaration"); expectedIdCommentMap.put("publicMethod", "publicMethod"); expectedIdCommentMap.put("testStruct", "testStruct"); diff --git a/cxx-squid/src/test/resources/metrics/no_doc.h b/cxx-squid/src/test/resources/metrics/no_doc.h index 0294bf7092..0e3391f6a6 100644 --- a/cxx-squid/src/test/resources/metrics/no_doc.h +++ b/cxx-squid/src/test/resources/metrics/no_doc.h @@ -23,6 +23,14 @@ class testClass int inlineCommentedAttr; void inlinePublicMethod(); + + // ignore deleted methods + A(A const&) = delete; + + // ignore defaulted methods + A& operator=(A const&) = default; + + void publicDefinedMethod() { } protected: virtual void protectedMethod(); diff --git a/cxx-squid/src/test/resources/metrics/public_api.h b/cxx-squid/src/test/resources/metrics/public_api.h index 9699d9861f..8f55fb0668 100644 --- a/cxx-squid/src/test/resources/metrics/public_api.h +++ b/cxx-squid/src/test/resources/metrics/public_api.h @@ -40,6 +40,17 @@ class testClass template friend S& operator<<(S&, A const&); friend class friendClass; + + // ignore deleted methods + A(A const&) = delete; + + // ignore defaulted methods + A& operator=(A const&) = default; + + /** + * publicDefinedMethod comment + */ + void publicDefinedMethod() { } protected: /** protectedMethod doc