pmd-cpp: checkstyle, formatting

This commit is contained in:
Andreas Dangel
2016-12-02 09:18:02 +01:00
parent 9a39c3ec02
commit edcd4e5ccb
10 changed files with 83 additions and 97 deletions

View File

@ -1,6 +1,7 @@
/**
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.cpd;
import java.util.Properties;
@ -11,18 +12,22 @@ import java.util.Properties;
public class CPPLanguage extends AbstractLanguage {
/**
* Creates a new instance of {@link CPPLanguage} with the default extensions for c/c++ files.
* Creates a new instance of {@link CPPLanguage} with the default extensions
* for c/c++ files.
*/
public CPPLanguage() {
super("C++", "cpp", new CPPTokenizer(), ".h", ".hpp", ".hxx", ".c", ".cpp", ".cxx", ".cc", ".C");
}
/* (non-Javadoc)
* @see net.sourceforge.pmd.cpd.AbstractLanguage#setProperties(java.util.Properties)
/*
* (non-Javadoc)
*
* @see net.sourceforge.pmd.cpd.AbstractLanguage#setProperties(java.util.
* Properties)
*/
@Override
public void setProperties(Properties properties) {
super.setProperties(properties);
((CPPTokenizer)getTokenizer()).setProperties(properties);
((CPPTokenizer) getTokenizer()).setProperties(properties);
}
}

View File

@ -1,6 +1,7 @@
/**
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.cpd;
import java.io.BufferedReader;
@ -9,6 +10,8 @@ import java.io.Reader;
import java.io.StringReader;
import java.util.Properties;
import org.apache.commons.io.IOUtils;
import net.sourceforge.pmd.PMD;
import net.sourceforge.pmd.lang.LanguageRegistry;
import net.sourceforge.pmd.lang.LanguageVersionHandler;
@ -18,8 +21,6 @@ import net.sourceforge.pmd.lang.cpp.CppLanguageModule;
import net.sourceforge.pmd.lang.cpp.ast.Token;
import net.sourceforge.pmd.util.IOUtil;
import org.apache.commons.io.IOUtils;
/**
* The C++ tokenizer.
*/
@ -31,7 +32,9 @@ public class CPPTokenizer implements Tokenizer {
/**
* Sets the possible options for the C++ tokenizer.
* @param properties the properties
*
* @param properties
* the properties
* @see #OPTION_SKIP_BLOCKS
* @see #OPTION_SKIP_BLOCKS_PATTERN
*/
@ -58,8 +61,9 @@ public class CPPTokenizer implements Tokenizer {
.getDefaultVersion().getLanguageVersionHandler();
reader = new StringReader(maybeSkipBlocks(buffer.toString()));
reader = IOUtil.skipBOM(reader);
TokenManager tokenManager = languageVersionHandler.getParser(
languageVersionHandler.getDefaultParserOptions()).getTokenManager(sourceCode.getFileName(), reader);
TokenManager tokenManager = languageVersionHandler
.getParser(languageVersionHandler.getDefaultParserOptions())
.getTokenManager(sourceCode.getFileName(), reader);
Token currentToken = (Token) tokenManager.getNextToken();
while (currentToken.image.length() > 0) {
tokenEntries.add(new TokenEntry(currentToken.image, sourceCode.getFileName(), currentToken.beginLine));
@ -98,7 +102,8 @@ public class CPPTokenizer implements Tokenizer {
if (!skip) {
filtered.append(line);
}
filtered.append(PMD.EOL); // always add a new line to keep the line-numbering
// always add a new line to keep the line-numbering
filtered.append(PMD.EOL);
}
return filtered.toString();
}

View File

@ -1,6 +1,7 @@
/**
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.lang.cpp;
import java.io.IOException;
@ -31,7 +32,9 @@ public class ContinuationReader extends Reader {
/**
* Creates a new {@link ContinuationReader} which filters the given reader.
* @param in the given reader
*
* @param in
* the given reader
*/
public ContinuationReader(Reader in) {
this.in = new PushbackReader(in, 2);

View File

@ -1,6 +1,7 @@
/**
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.lang.cpp;
import net.sourceforge.pmd.lang.AbstractLanguageVersionHandler;

View File

@ -1,6 +1,7 @@
/**
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.lang.cpp;
import net.sourceforge.pmd.lang.BaseLanguageModule;
@ -16,7 +17,8 @@ public class CppLanguageModule extends BaseLanguageModule {
public static final String TERSE_NAME = "cpp";
/**
* Creates a new instance of {@link CppLanguageModule} with the default file extensions for C++.
* Creates a new instance of {@link CppLanguageModule} with the default file
* extensions for C++.
*/
public CppLanguageModule() {
super(NAME, null, TERSE_NAME, null, "h", "c", "cpp", "cxx", "cc", "C");

View File

@ -1,6 +1,7 @@
/**
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.lang.cpp;
import java.io.Reader;
@ -20,7 +21,9 @@ public class CppParser extends AbstractParser {
/**
* Creates a new C++ Parser.
* @param parserOptions the options
*
* @param parserOptions
* the options
*/
public CppParser(ParserOptions parserOptions) {
super(parserOptions);

View File

@ -1,6 +1,7 @@
/**
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.lang.cpp;
import java.io.Reader;
@ -17,7 +18,9 @@ public class CppTokenManager implements TokenManager {
/**
* Creates a new C++ Token Manager from the given source code.
* @param source the source code
*
* @param source
* the source code
*/
public CppTokenManager(Reader source) {
tokenManager = new CppParserTokenManager(new SimpleCharStream(new ContinuationReader(source)));

View File

@ -1,17 +1,18 @@
/**
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd;
import java.util.Arrays;
import java.util.Collection;
import org.junit.runners.Parameterized.Parameters;
import net.sourceforge.pmd.lang.LanguageRegistry;
import net.sourceforge.pmd.lang.LanguageVersion;
import net.sourceforge.pmd.lang.cpp.CppLanguageModule;
import org.junit.runners.Parameterized.Parameters;
public class LanguageVersionTest extends AbstractLanguageVersionTest {
public LanguageVersionTest(String name, String terseName, String version, LanguageVersion expected) {
@ -21,7 +22,12 @@ public class LanguageVersionTest extends AbstractLanguageVersionTest {
@Parameters
public static Collection<Object[]> data() {
return Arrays.asList(new Object[][] {
{ CppLanguageModule.NAME, CppLanguageModule.TERSE_NAME, "", LanguageRegistry.getLanguage(CppLanguageModule.NAME).getDefaultVersion() }
});
{
CppLanguageModule.NAME,
CppLanguageModule.TERSE_NAME,
"",
LanguageRegistry.getLanguage(CppLanguageModule.NAME).getDefaultVersion(),
},
});
}
}

View File

@ -1,6 +1,7 @@
/**
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.cpd;
import static org.junit.Assert.assertEquals;
@ -8,11 +9,11 @@ import static org.junit.Assert.assertTrue;
import java.util.Properties;
import net.sourceforge.pmd.PMD;
import org.apache.commons.io.IOUtils;
import org.junit.Test;
import net.sourceforge.pmd.PMD;
public class CPPTokenizerTest {
@Test
@ -24,25 +25,16 @@ public class CPPTokenizerTest {
@Test
public void testUnicodeSupport() {
String code = "\ufeff" +
"#include <iostream>\n" +
"#include <string>\n" +
"\n" +
"// example\n" +
"\n" +
"int main()\n" +
"{\n" +
" std::string text(\"ąęćśźńó\");\n" +
" std::cout << text;\n" +
" return 0;\n" +
"}\n";
String code = "\ufeff" + "#include <iostream>\n" + "#include <string>\n" + "\n" + "// example\n" + "\n"
+ "int main()\n" + "{\n" + " std::string text(\"ąęćśźńó\");\n" + " std::cout << text;\n"
+ " return 0;\n" + "}\n";
Tokens tokens = parse(code);
assertTrue(TokenEntry.getEOF() != tokens.getTokens().get(0));
assertEquals(24, tokens.size());
}
@Test
public void testMultiLineMacros() throws Throwable {
public void testMultiLineMacros() {
Tokens tokens = parse(TEST1);
assertEquals(7, tokens.size());
}
@ -64,14 +56,14 @@ public class CPPTokenizerTest {
@Test
public void testContinuation_IntraToken() {
Tokens tokens = parse(TEST5);
Tokens tokens = parse(TEST5);
assertEquals(7, tokens.size());
}
@Test
public void testContinuation_InterToken() {
Tokens tokens = parse(TEST6);
assertEquals(17, tokens.size());
Tokens tokens = parse(TEST6);
assertEquals(17, tokens.size());
}
@Test
@ -96,7 +88,7 @@ public class CPPTokenizerTest {
}
@Test
//ASM code containing the '@' character
// ASM code containing the '@' character
public void testAsmWithAtSign() {
Tokens tokens = parse(TEST7);
assertEquals(22, tokens.size());
@ -124,17 +116,13 @@ public class CPPTokenizerTest {
Tokens tokens = parse("szPath = m_sdcacheDir + _T(\"\\ oMedia\");" + PMD.EOL);
assertEquals(10, tokens.size());
}
@Test
public void testRawStringLiteral() {
String code =
"const char* const KDefaultConfig = R\"(\n" +
" [Sinks.1]\n" +
" Destination=Console\n" +
" AutoFlush=true\n" +
" Format=\"[%TimeStamp%] %ThreadId% %QueryIdHigh% %QueryIdLow% %LoggerFile%:%Line% (%Severity%) - %Message%\"\n" +
" Filter=\"%Severity% >= WRN\"\n" +
")\";\n";
String code = "const char* const KDefaultConfig = R\"(\n" + " [Sinks.1]\n" + " Destination=Console\n"
+ " AutoFlush=true\n"
+ " Format=\"[%TimeStamp%] %ThreadId% %QueryIdHigh% %QueryIdLow% %LoggerFile%:%Line% (%Severity%) - %Message%\"\n"
+ " Filter=\"%Severity% >= WRN\"\n" + ")\";\n";
Tokens tokens = parse(code);
assertTrue(TokenEntry.getEOF() != tokens.getTokens().get(0));
assertEquals(9, tokens.size());
@ -143,9 +131,11 @@ public class CPPTokenizerTest {
private Tokens parse(String snippet) {
return parse(snippet, false);
}
private Tokens parse(String snippet, boolean skipBlocks) {
return parse(snippet, skipBlocks, null);
}
private Tokens parse(String snippet, boolean skipBlocks, String skipPattern) {
Properties properties = new Properties();
properties.setProperty(Tokenizer.OPTION_SKIP_BLOCKS, Boolean.toString(skipBlocks));
@ -162,59 +152,26 @@ public class CPPTokenizerTest {
return tokens;
}
private static final String TEST1 =
"#define FOO a +\\" + PMD.EOL +
" b +\\" + PMD.EOL +
" c +\\" + PMD.EOL +
" d +\\" + PMD.EOL +
" e +\\" + PMD.EOL +
" f +\\" + PMD.EOL +
" g" + PMD.EOL +
" void main() {}";
private static final String TEST1 = "#define FOO a +\\" + PMD.EOL + " b +\\" + PMD.EOL
+ " c +\\" + PMD.EOL + " d +\\" + PMD.EOL + " e +\\" + PMD.EOL
+ " f +\\" + PMD.EOL + " g" + PMD.EOL + " void main() {}";
private static final String TEST2 =
" void main() { int x$y = 42; }";
private static final String TEST2 = " void main() { int x$y = 42; }";
private static final String TEST3 =
" void main() { int $x = 42; }";
private static final String TEST3 = " void main() { int $x = 42; }";
private static final String TEST4 =
" void main() { char x = L'a'; }";
private static final String TEST5 =
"v\\" + PMD.EOL +
"o\\" + PMD.EOL +
"i\\" + PMD.EOL +
"d\\" + PMD.EOL +
" \\" + PMD.EOL +
"m\\" + PMD.EOL +
"a\\" + PMD.EOL +
"i\\" + PMD.EOL +
"n\\" + PMD.EOL +
"(\\" + PMD.EOL +
")\\" + PMD.EOL +
" \\" + PMD.EOL +
"{\\" + PMD.EOL +
" \\" + PMD.EOL +
"}\\" + PMD.EOL;
private static final String TEST6 =
"#include <iostream>" + PMD.EOL +
PMD.EOL +
"int main()" + PMD.EOL +
"{" + PMD.EOL +
" std::cout << \"Hello, \" \\" + PMD.EOL +
" \"world!\\n\";" + PMD.EOL +
" return 0;" + PMD.EOL +
"}";
private static final String TEST4 = " void main() { char x = L'a'; }";
private static final String TEST7 =
"asm void eSPI_boot()" + PMD.EOL +
"{" + PMD.EOL +
" // setup stack pointer" + PMD.EOL +
" lis r1, _stack_addr@h" + PMD.EOL +
" ori r1, r1, _stack_addr@l" + PMD.EOL +
"}";
private static final String TEST5 = "v\\" + PMD.EOL + "o\\" + PMD.EOL + "i\\" + PMD.EOL + "d\\" + PMD.EOL + " \\"
+ PMD.EOL + "m\\" + PMD.EOL + "a\\" + PMD.EOL + "i\\" + PMD.EOL + "n\\" + PMD.EOL + "(\\" + PMD.EOL + ")\\"
+ PMD.EOL + " \\" + PMD.EOL + "{\\" + PMD.EOL + " \\" + PMD.EOL + "}\\" + PMD.EOL;
private static final String TEST6 = "#include <iostream>" + PMD.EOL + PMD.EOL + "int main()" + PMD.EOL + "{"
+ PMD.EOL + " std::cout << \"Hello, \" \\" + PMD.EOL + " \"world!\\n\";" + PMD.EOL
+ " return 0;" + PMD.EOL + "}";
private static final String TEST7 = "asm void eSPI_boot()" + PMD.EOL + "{" + PMD.EOL + " // setup stack pointer"
+ PMD.EOL + " lis r1, _stack_addr@h" + PMD.EOL + " ori r1, r1, _stack_addr@l" + PMD.EOL + "}";
public static junit.framework.Test suite() {
return new junit.framework.JUnit4TestAdapter(CPPTokenizerTest.class);

View File

@ -1,6 +1,7 @@
/**
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.lang.cpp;
import static org.junit.Assert.assertEquals;