Skip to content

Commit

Permalink
Restored documented behaviour of loadIncludes (#182)
Browse files Browse the repository at this point in the history
  • Loading branch information
oxisto authored Jul 30, 2020
1 parent 3c6c4e0 commit 220b27e
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 20 deletions.
13 changes: 6 additions & 7 deletions src/main/java/de/fraunhofer/aisec/cpg/frontends/Handler.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,14 @@ public S handle(T ctx) {
return null;
}

// If we do not want to load includes into the CPG and the current fileLocation is an include
// file ->
// If we do not want to load includes into the CPG and the current fileLocation was included
if (!this.lang.config.loadIncludes && ctx instanceof ASTNode) {
ASTNode astNode = (ASTNode) ctx;
for (String inclPath : this.lang.config.includePaths) {
if (astNode.getContainingFilename().startsWith(inclPath)) {
log.debug("Skip parsing include file" + astNode.getContainingFilename());
return null;
}

if (astNode.getFileLocation() != null
&& astNode.getFileLocation().getContextInclusionStatement() != null) {
log.debug("Skip parsing include file" + astNode.getContainingFilename());
return null;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,20 +259,14 @@ public TranslationUnitDeclaration parse(File file) throws TranslationException {

DefaultLogService log = new DefaultLogService();

IncludeFileContentProvider includeProvider;
if (config.loadIncludes) {
includeProvider = includeFileContentProvider;
} else {
includeProvider = IncludeFileContentProvider.getEmptyFilesProvider();
}

int opts = ILanguage.OPTION_PARSE_INACTIVE_CODE; // | ILanguage.OPTION_ADD_COMMENTS;

try {
Benchmark bench = new Benchmark(this.getClass(), "Parsing sourcefile");
IASTTranslationUnit translationUnit =
GPPLanguage.getDefault()
.getASTTranslationUnit(content, scannerInfo, includeProvider, null, opts, log);
.getASTTranslationUnit(
content, scannerInfo, includeFileContentProvider, null, opts, log);
bench.stop();

bench = new Benchmark(this.getClass(), "Transform to CPG");
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/de/fraunhofer/aisec/cpg/graph/Expression.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ public void updatePossibleSubtypes(Set<Type> types) {

@Override
public void setType(Type type, HasType root) {
//TODO Document this method. It is called very often (potentially for each AST node) and performs less than optimal.
// TODO Document this method. It is called very often (potentially for each AST node) and
// performs less than optimal.
if (type == null || root == this) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import java.util.stream.Collectors;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;

/** The top most declaration, representing a translation unit, for example a file. */
public class TranslationUnitDeclaration extends Declaration {
Expand All @@ -53,9 +54,25 @@ public class TranslationUnitDeclaration extends Declaration {
@NonNull
private List<Declaration> namespaces = new ArrayList<>();

// TODO Fragile! May easily throw ClassCastException. Use visitor instead.
public <T> T getDeclarationAs(int i, Class<T> clazz) {
return clazz.cast(this.declarations.get(i));
/**
* Returns the i-th declaration as a specific class, if it can be cast
*
* @param i the index
* @param clazz the class
* @param <T> the type of the class
* @return the declaration or null, if it the declaration can not be cast to the class
*/
@Nullable
public <T extends Declaration> T getDeclarationAs(int i, Class<T> clazz) {
Declaration declaration = this.declarations.get(i);

if (declaration == null) {
return null;
}

return declaration.getClass().isAssignableFrom(clazz)
? clazz.cast(this.declarations.get(i))
: null;
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/de/fraunhofer/aisec/cpg/graph/TypeManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,8 @@ private Optional<Type> rewrapType(
}

private Set<Type> unwrapTypes(Collection<Type> types, WrapState wrapState) {
// TODO Performance: This method is called very often (for each setType()) and does four iterations over "types". Reduce number of iterations.
// TODO Performance: This method is called very often (for each setType()) and does four
// iterations over "types". Reduce number of iterations.
Set<Type> original = new HashSet<>(types);
Set<Type> unwrappedTypes = new HashSet<>();
int depth = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,4 +243,24 @@ void testIncludeBothLists() throws Exception {
.equals(
new File("src/test/resources/another-include.h").getAbsolutePath())));
}

@Test
void testLoadIncludes() throws Exception {
File file = new File("src/test/resources/include.cpp");
List<TranslationUnitDeclaration> translationUnitDeclarations =
analyzeWithBuilder(
TranslationConfiguration.builder()
.sourceLocations(List.of(file))
.topLevel(file.getParentFile())
.loadIncludes(false)
.debugParser(true)
.failOnError(true));

assertNotNull(translationUnitDeclarations);

// first one should NOT be a class (since it is defined in the header)
RecordDeclaration recordDeclaration =
translationUnitDeclarations.get(0).getDeclarationAs(0, RecordDeclaration.class);
assertNull(recordDeclaration);
}
}

0 comments on commit 220b27e

Please sign in to comment.