-
Notifications
You must be signed in to change notification settings - Fork 365
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add CxxPublicApiVisitor which counts documented and undocumented public
API items. Add UndocumentedApiCheck that creates issue for undocumented API.
- Loading branch information
Showing
21 changed files
with
1,451 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
cxx-checks/src/main/java/org/sonar/cxx/checks/UndocumentedApiCheck.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/* | ||
* Sonar C++ Plugin (Community) | ||
* Copyright (C) 2011 Waleri Enns and CONTACT Software GmbH | ||
* dev@sonar.codehaus.org | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 3 of the License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public | ||
* License along with this program; if not, write to the Free Software | ||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 | ||
*/ | ||
package org.sonar.cxx.checks; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.sonar.check.Priority; | ||
import org.sonar.check.Rule; | ||
import org.sonar.cxx.visitors.AbstractCxxPublicApiVisitor; | ||
|
||
import com.sonar.sslr.api.AstNode; | ||
import com.sonar.sslr.api.Grammar; | ||
import com.sonar.sslr.api.Token; | ||
|
||
/** | ||
* Check that generates issue for undocumented API items.<br> | ||
* Following items are counted as public API: | ||
* <ul> | ||
* <li>classes/structures</li> | ||
* <li>class members (public and protected)</li> | ||
* <li>structure members</li> | ||
* <li>enumerations</li> | ||
* <li>enumeration values</li> | ||
* <li>typedefs</li> | ||
* <li>functions</li> | ||
* <li>variables</li> | ||
* </ul> | ||
* <p> | ||
* Public API items are considered documented if they have Doxygen comments.<br> | ||
* Function arguments are not counted since they can be documented in function | ||
* documentation and this visitor does not parse Doxygen comments.<br> | ||
* This visitor should be applied only on header files.<br> | ||
* Currently, no filtering is applied using preprocessing directive.<br> | ||
* <p> | ||
* Limitation: only "in front of the declaration" comments are considered. | ||
* | ||
* @see <a href="http://www.stack.nl/~dimitri/doxygen/manual/docblocks.html"> | ||
* Doxygen Manual: Documenting the code</a> | ||
* | ||
* @author Ludovic Cintrat | ||
* | ||
* @param <GRAMMAR> | ||
*/ | ||
@Rule(key = "UndocumentedApi", | ||
description = "All public APIs should be documented", | ||
priority = Priority.MINOR) | ||
public class UndocumentedApiCheck extends AbstractCxxPublicApiVisitor<Grammar> { | ||
|
||
private static final Logger LOG = LoggerFactory | ||
.getLogger("UndocumentedApiCheck"); | ||
|
||
private static final List<String> DEFAULT_NAME_SUFFIX = Arrays.asList(".h", | ||
".hh", ".hpp", ".H"); | ||
|
||
public UndocumentedApiCheck() { | ||
super(); | ||
withHeaderFileSuffixes(DEFAULT_NAME_SUFFIX); | ||
} | ||
|
||
@Override | ||
protected void onPublicApi(AstNode node, String id, List<Token> comments) { | ||
boolean commented = !comments.isEmpty(); | ||
|
||
LOG.debug("node: " + node.getType() + " line: " + node.getTokenLine() | ||
+ " id: '" + id + "' documented: " + commented); | ||
|
||
if (!commented) { | ||
getContext().createLineViolation(this, "Undocumented API: " + id, | ||
node); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
cxx-checks/src/main/resources/org/sonar/l10n/cxx/rules/cxx/UndocumentedApi.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<p> | ||
Public APIs have to be documented in order to be used by customers.<br> | ||
APIs documentation is typically generated using a tool, such as <a href="http://www.stack.nl/~dimitri/doxygen/index.html">Doxygen</a>. | ||
</p> | ||
|
||
<p>The following code illustrates a well documented class:</p> | ||
|
||
<pre> | ||
/** | ||
class documentation | ||
/* | ||
class DocumentedClass { | ||
public: | ||
int commentedVar; ///< documentation | ||
|
||
/** | ||
apiMethod documentation | ||
*/ | ||
void apiMethod(); | ||
|
||
private: | ||
void notInApiMethod(); | ||
}; | ||
</pre> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
cxx-checks/src/test/java/org/sonar/cxx/checks/UndocumentedApiCheckTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* Sonar C++ Plugin (Community) | ||
* Copyright (C) 2011 Waleri Enns and CONTACT Software GmbH | ||
* dev@sonar.codehaus.org | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 3 of the License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public | ||
* License along with this program; if not, write to the Free Software | ||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 | ||
*/ | ||
package org.sonar.cxx.checks; | ||
|
||
import java.io.File; | ||
|
||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.sonar.cxx.CxxAstScanner; | ||
import org.sonar.squid.api.CheckMessage; | ||
import org.sonar.squid.api.SourceFile; | ||
|
||
import static org.fest.assertions.Assertions.assertThat; | ||
|
||
import com.sonar.sslr.squid.checks.CheckMessagesVerifierRule; | ||
|
||
public class UndocumentedApiCheckTest { | ||
|
||
@Rule | ||
public CheckMessagesVerifierRule checkMessagesVerifier = new CheckMessagesVerifierRule(); | ||
|
||
@SuppressWarnings("unchecked") | ||
@Test | ||
public void detected() { | ||
SourceFile file = CxxAstScanner.scanSingleFile(new File( | ||
"src/test/resources/checks/UndocumentedApiCheck/no_doc.h"), new UndocumentedApiCheck()); | ||
|
||
checkMessagesVerifier.verify(file.getCheckMessages()).next().atLine(6) | ||
.next().atLine(11).next().atLine(13).next().atLine(14).next() | ||
.atLine(17).next().atLine(19).next().atLine(21).next() | ||
.atLine(23).next().atLine(26).next().atLine(48).next() | ||
.atLine(52).next().atLine(53).next().atLine(56).next() | ||
.atLine(58).next().atLine(60).next().atLine(63).next() | ||
.atLine(65).next().atLine(68).next().atLine(74).next().atLine(77); | ||
|
||
for (CheckMessage msg : file.getCheckMessages()) { | ||
assertThat(msg.formatDefaultMessage()).isNotEmpty(); | ||
} | ||
} | ||
} |
79 changes: 79 additions & 0 deletions
79
cxx-checks/src/test/resources/checks/UndocumentedApiCheck/no_doc.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
#define TEST_MACRO() \ | ||
macro | ||
|
||
#define EXPORT | ||
|
||
class testClass | ||
{ | ||
void defaultMethod(); | ||
|
||
public: | ||
void publicMethod(); | ||
|
||
enum classEnum { | ||
classEnumValue | ||
} | ||
|
||
enumVar; | ||
|
||
EXPORT int publicAttribute; | ||
|
||
int inlineCommentedAttr; | ||
|
||
void inlinePublicMethod(); | ||
|
||
protected: | ||
virtual void protectedMethod(); | ||
|
||
private: | ||
void privateMethod(); | ||
|
||
enum privateEnum { | ||
privateEnumVal | ||
}; | ||
|
||
struct privateStruct { | ||
int privateStructField; | ||
}; | ||
|
||
class privateClass { | ||
int i; | ||
}; | ||
|
||
union privateUnion { | ||
int u; | ||
}; | ||
|
||
public: | ||
int inlineCommentedLastAttr; | ||
}; | ||
|
||
|
||
struct testStruct { | ||
int testField; | ||
}; | ||
|
||
extern int globalVar; | ||
|
||
void testFunction(); | ||
|
||
enum emptyEnum | ||
{}; | ||
|
||
enum testEnum | ||
{ | ||
enum_val | ||
}; | ||
|
||
union testUnion | ||
{ | ||
|
||
}; | ||
|
||
template<T> | ||
struct tmplStruct | ||
{}; | ||
|
||
void func() { | ||
for (int i = 0; i < 10; i++) {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.