Checkstyle/whitespace fixes

This commit is contained in:
Andreas Dangel
2014-10-18 13:31:14 +02:00
parent cc24717980
commit 53048546ba
9 changed files with 168 additions and 125 deletions

View File

@ -3,8 +3,15 @@
*/
package net.sourceforge.pmd.cpd;
/**
* Defines the Language module for C/C++
*/
public class CPPLanguage extends AbstractLanguage {
public CPPLanguage() {
super("C++", "cpp", new CPPTokenizer(), ".h", ".hpp", ".hxx",".c", ".cpp", ".cxx", ".cc", ".C");
}
/**
* 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");
}
}

View File

@ -14,30 +14,34 @@ import net.sourceforge.pmd.lang.cpp.ast.Token;
import org.apache.commons.io.IOUtils;
/**
* The C++ tokenizer.
*/
public class CPPTokenizer implements Tokenizer {
public void tokenize(SourceCode sourceCode, Tokens tokenEntries) {
StringBuilder buffer = sourceCode.getCodeBuffer();
StringReader reader = null;
try {
LanguageVersionHandler languageVersionHandler = LanguageRegistry.getLanguage(CppLanguageModule.NAME).getDefaultVersion().getLanguageVersionHandler();
reader = new StringReader(buffer.toString());
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));
currentToken = (Token) tokenManager.getNextToken();
}
tokenEntries.add(TokenEntry.getEOF());
System.err.println("Added " + sourceCode.getFileName());
} catch (TokenMgrError err) {
err.printStackTrace();
System.err.println("Skipping " + sourceCode.getFileName() + " due to parse error");
tokenEntries.add(TokenEntry.getEOF());
} finally {
IOUtils.closeQuietly(reader);
}
}
@Override
public void tokenize(SourceCode sourceCode, Tokens tokenEntries) {
StringBuilder buffer = sourceCode.getCodeBuffer();
StringReader reader = null;
try {
LanguageVersionHandler languageVersionHandler = LanguageRegistry.getLanguage(CppLanguageModule.NAME)
.getDefaultVersion().getLanguageVersionHandler();
reader = new StringReader(buffer.toString());
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));
currentToken = (Token) tokenManager.getNextToken();
}
tokenEntries.add(TokenEntry.getEOF());
System.err.println("Added " + sourceCode.getFileName());
} catch (TokenMgrError err) {
err.printStackTrace();
System.err.println("Skipping " + sourceCode.getFileName() + " due to parse error");
tokenEntries.add(TokenEntry.getEOF());
} finally {
IOUtils.closeQuietly(reader);
}
}
}

View File

@ -21,58 +21,63 @@ import java.io.Reader;
* continuation character</a>.
*/
public class ContinuationReader extends Reader {
private static final int EOF = -1;
private static final char BACKSLASH = '\\';
private static final char CARRIAGE_RETURN = '\n';
private static final char LINE_FEED = '\r';
private static final int EOF = -1;
private static final char BACKSLASH = '\\';
private static final char CARRIAGE_RETURN = '\n';
private static final char LINE_FEED = '\r';
protected final PushbackReader in;
/** the original stream is wrapped in this pushback reader. */
protected final PushbackReader in;
public ContinuationReader(Reader in) {
this.in = new PushbackReader(in, 2);
}
/**
* Creates a new {@link ContinuationReader} which filters the given reader.
* @param in the given reader
*/
public ContinuationReader(Reader in) {
this.in = new PushbackReader(in, 2);
}
@Override
public int read(char[] cbuf, int off, int len) throws IOException {
int count = 0;
while (count < len) {
int c1 = in.read();
if (c1 == EOF) {
break;
} else if (c1 == BACKSLASH) {
int c2 = in.read();
if (c2 == EOF) {
// No match
} else if (c2 == CARRIAGE_RETURN) {
// Match: backslash, carriage return
continue;
} else if (c2 == LINE_FEED) {
int c3 = in.read();
if (c3 == EOF) {
// No match
in.unread(c2);
} else if (c3 == CARRIAGE_RETURN) {
// Match: backslash, line feed, carriage return
continue;
} else {
// No match
in.unread(c3);
in.unread(c2);
}
} else {
// No match
in.unread(c2);
}
}
cbuf[off + count] = (char) c1;
count++;
}
@Override
public int read(char[] cbuf, int off, int len) throws IOException {
int count = 0;
while (count < len) {
int c1 = in.read();
if (c1 == EOF) {
break;
} else if (c1 == BACKSLASH) {
int c2 = in.read();
if (c2 == EOF) {
// No match
} else if (c2 == CARRIAGE_RETURN) {
// Match: backslash, carriage return
continue;
} else if (c2 == LINE_FEED) {
int c3 = in.read();
if (c3 == EOF) {
// No match
in.unread(c2);
} else if (c3 == CARRIAGE_RETURN) {
// Match: backslash, line feed, carriage return
continue;
} else {
// No match
in.unread(c3);
in.unread(c2);
}
} else {
// No match
in.unread(c2);
}
}
cbuf[off + count] = (char) c1;
count++;
}
return count > 0 ? count : -1;
}
return count > 0 ? count : -1;
}
@Override
public void close() throws IOException {
in.close();
}
@Override
public void close() throws IOException {
in.close();
}
}

View File

@ -13,11 +13,13 @@ import net.sourceforge.pmd.lang.rule.RuleViolationFactory;
*/
public class CppHandler extends AbstractLanguageVersionHandler {
@Override
public RuleViolationFactory getRuleViolationFactory() {
throw new UnsupportedOperationException("getRuleViolationFactory() is not supported for C++");
throw new UnsupportedOperationException("getRuleViolationFactory() is not supported for C++");
}
@Override
public Parser getParser(ParserOptions parserOptions) {
return new CppParser(parserOptions);
return new CppParser(parserOptions);
}
}

View File

@ -1,18 +1,25 @@
/**
* 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;
/**
* Created by christoferdutz on 20.09.14.
* Implementation of the C/C++ Language Module.
*/
public class CppLanguageModule extends BaseLanguageModule {
/** The name, that can be used to display the language in UI. */
public static final String NAME = "C++";
/** The internal name. */
public static final String TERSE_NAME = "cpp";
/**
* 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");
addVersion("", new CppHandler(), true);
}
}

View File

@ -18,25 +18,32 @@ import net.sourceforge.pmd.lang.ast.ParseException;
*/
public class CppParser extends AbstractParser {
/**
* Creates a new C++ Parser.
* @param parserOptions the options
*/
public CppParser(ParserOptions parserOptions) {
super(parserOptions);
super(parserOptions);
}
@Override
public TokenManager createTokenManager(Reader source) {
return new CppTokenManager(source);
return new CppTokenManager(source);
}
@Override
public boolean canParse() {
return false;
return false;
}
@Override
public Node parse(String fileName, Reader source) throws ParseException {
AbstractTokenManager.setFileName(fileName);
throw new UnsupportedOperationException("parse(Reader) is not supported for C++");
AbstractTokenManager.setFileName(fileName);
throw new UnsupportedOperationException("parse(Reader) is not supported for C++");
}
@Override
public Map<Integer, String> getSuppressMap() {
throw new UnsupportedOperationException("getSuppressMap() is not supported for C++");
throw new UnsupportedOperationException("getSuppressMap() is not supported for C++");
}
}

View File

@ -15,15 +15,20 @@ import net.sourceforge.pmd.lang.cpp.ast.CppParserTokenManager;
public class CppTokenManager implements TokenManager {
private final CppParserTokenManager tokenManager;
/**
* Creates a new C++ Token Manager from the given source code.
* @param source the source code
*/
public CppTokenManager(Reader source) {
tokenManager = new CppParserTokenManager(new SimpleCharStream(new ContinuationReader(source)));
tokenManager = new CppParserTokenManager(new SimpleCharStream(new ContinuationReader(source)));
}
public Object getNextToken() {
return tokenManager.getNextToken();
return tokenManager.getNextToken();
}
@Override
public void setFileName(String fileName) {
tokenManager.setFileName(fileName);
CppParserTokenManager.setFileName(fileName);
}
}

View File

@ -1,3 +1,6 @@
/**
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd;
import java.util.Arrays;
@ -16,7 +19,7 @@ public class LanguageVersionTest extends AbstractLanguageVersionTest {
}
@Parameters
public static Collection data() {
public static Collection<Object[]> data() {
return Arrays.asList(new Object[][] {
{ CppLanguageModule.NAME, CppLanguageModule.TERSE_NAME, "", LanguageRegistry.getLanguage(CppLanguageModule.NAME).getDefaultVersion() }
});

View File

@ -1,3 +1,6 @@
/**
* 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;
@ -8,45 +11,45 @@ import java.io.StringReader;
import org.junit.Test;
public class ContinuationReaderTest {
@Test
public void testHappyPath() throws IOException {
assertEquals("empty", "", filter(""));
assertEquals("anything", "anything", filter("anything"));
@Test
public void testHappyPath() throws IOException {
assertEquals("empty", "", filter(""));
assertEquals("anything", "anything", filter("anything"));
assertEquals("partial: BS", "\\", filter("\\"));
assertEquals("partial: BS LF", "\\\r", filter("\\\r"));
assertEquals("full: BS CR", "", filter("\\\n"));
assertEquals("full: BS LF CR", "", filter("\\\r\n"));
assertEquals("partial: BS", "\\", filter("\\"));
assertEquals("partial: BS LF", "\\\r", filter("\\\r"));
assertEquals("full: BS CR", "", filter("\\\n"));
assertEquals("full: BS LF CR", "", filter("\\\r\n"));
assertEquals("partial: BS: prefix", "prefix\\", filter("prefix\\"));
assertEquals("partial: BS LF: prefix", "prefix\\\r", filter("prefix\\\r"));
assertEquals("full: BS CR: prefix", "prefix", filter("prefix\\\n"));
assertEquals("full: BS LF CR: prefix", "prefix", filter("prefix\\\r\n"));
assertEquals("partial: BS: prefix", "prefix\\", filter("prefix\\"));
assertEquals("partial: BS LF: prefix", "prefix\\\r", filter("prefix\\\r"));
assertEquals("full: BS CR: prefix", "prefix", filter("prefix\\\n"));
assertEquals("full: BS LF CR: prefix", "prefix", filter("prefix\\\r\n"));
assertEquals("partial: BS: suffix", "\\suffix", filter("\\suffix"));
assertEquals("partial: BS LF: suffix", "\\\rsuffix", filter("\\\rsuffix"));
assertEquals("full: BS CR: suffix", "suffix", filter("\\\nsuffix"));
assertEquals("full: BS LF CR: suffix", "suffix", filter("\\\r\nsuffix"));
assertEquals("partial: BS: suffix", "\\suffix", filter("\\suffix"));
assertEquals("partial: BS LF: suffix", "\\\rsuffix", filter("\\\rsuffix"));
assertEquals("full: BS CR: suffix", "suffix", filter("\\\nsuffix"));
assertEquals("full: BS LF CR: suffix", "suffix", filter("\\\r\nsuffix"));
assertEquals("partial: BS: prefix, suffix", "prefix\\suffix", filter("prefix\\suffix"));
assertEquals("partial: BS LF: prefix, suffix", "prefix\\\rsuffix", filter("prefix\\\rsuffix"));
assertEquals("full: BS CR: prefix, suffix", "prefixsuffix", filter("prefix\\\nsuffix"));
assertEquals("full: BS LF CR: prefix, suffix", "prefixsuffix", filter("prefix\\\r\nsuffix"));
assertEquals("partial: BS: prefix, suffix", "prefix\\suffix", filter("prefix\\suffix"));
assertEquals("partial: BS LF: prefix, suffix", "prefix\\\rsuffix", filter("prefix\\\rsuffix"));
assertEquals("full: BS CR: prefix, suffix", "prefixsuffix", filter("prefix\\\nsuffix"));
assertEquals("full: BS LF CR: prefix, suffix", "prefixsuffix", filter("prefix\\\r\nsuffix"));
assertEquals("complex mixed", "abc", filter("a\\\r\nb\\\n\\\n\\\r\nc"));
}
assertEquals("complex mixed", "abc", filter("a\\\r\nb\\\n\\\n\\\r\nc"));
}
private static String filter(String s) throws IOException {
ContinuationReader reader = new ContinuationReader(new StringReader(s));
try {
StringBuilder buf = new StringBuilder();
int c;
while ((c = reader.read()) >= 0) {
buf.append((char) c);
}
return buf.toString();
} finally {
reader.close();
}
}
private static String filter(String s) throws IOException {
ContinuationReader reader = new ContinuationReader(new StringReader(s));
try {
StringBuilder buf = new StringBuilder();
int c;
while ((c = reader.read()) >= 0) {
buf.append((char) c);
}
return buf.toString();
} finally {
reader.close();
}
}
}