can produce import relation of c/c++

This commit is contained in:
Gang ZHANG 2018-10-29 21:47:00 +08:00
parent 73ed56149b
commit fbda0152af
12 changed files with 56 additions and 25 deletions

View File

@ -26,10 +26,13 @@ public class FileEntity extends ContainerEntity{
* @param importedName could be className, package Name in JAVA
* could be file in C/C++
*/
public void addImport(String importedName) {
public void addImport(String importedName, boolean useFileAsImportedKey) {
String lastName = importedName;
if (lastName.indexOf(".") > 0)
if (useFileAsImportedKey) {
;
}else if (lastName.indexOf(".") > 0) {
lastName = lastName.substring(lastName.lastIndexOf(".")+1);
}
importedNames.put(lastName, importedName);
}
public String getImport(String lastName) {

View File

@ -64,8 +64,15 @@ public class HandlerContext {
entityStack.pop();
}
public void foundNewImport(String importedName) {
currentFileEntity.addImport(importedName);
/**
*
* @param importedName
* @param usedFilenameAsKey - to identify whether the importedName is a file name
* in (C/C++), the value should be true due to it is a filename
* in Java, the value should be false due to it is a package or class name
*/
public void foundNewImport(String importedName, boolean usedFilenameAsKey) {
currentFileEntity.addImport(importedName,usedFilenameAsKey);
}
public TypeEntity currentType() {

View File

@ -76,7 +76,7 @@ public class CdtCppEntitiesListener extends ASTVisitor {
IASTPreprocessorStatement[] i = tu.getAllPreprocessorStatements();
preprocessorHandler.handlePreprocessors(tu.getAllPreprocessorStatements(),this.filePath);
for (String incl:preprocessorHandler.getIncludedFullPathNames()) {
context.foundNewImport(incl);
context.foundNewImport(incl,true);
}
return super.visit(tu);
}

View File

@ -25,6 +25,7 @@ import org.eclipse.cdt.internal.core.parser.scanner.InternalFileContent;
import depends.entity.repo.EntityRepo;
import depends.extractor.cpp.CppFileParser;
import depends.util.FileUtil;
public class CdtCppFileParser extends CppFileParser {
@ -36,7 +37,7 @@ public class CdtCppFileParser extends CppFileParser {
}
@Override
public void parse() throws IOException {
CdtCppEntitiesListener bridge = new CdtCppEntitiesListener(fileFullPath, entityRepo, includeSearchPath );
CdtCppEntitiesListener bridge = new CdtCppEntitiesListener(FileUtil.uniqFilePath(fileFullPath), entityRepo, includeSearchPath );
IASTTranslationUnit translationUnit = (new CDTParser()).parse(this.fileFullPath);
translationUnit.accept(bridge);
}

View File

@ -44,4 +44,7 @@ public class GlobalIncludeMap {
if (matcher.matches()) return true;
return false;
}
public List<String> get(String fileName) {
return includesMap.get(fileName);
}
}

View File

@ -1,5 +1,7 @@
package depends.extractor.cpp.cdt;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
@ -7,6 +9,8 @@ import org.eclipse.cdt.core.dom.ast.IASTPreprocessorIncludeStatement;
import org.eclipse.cdt.core.dom.ast.IASTPreprocessorStatement;
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
import depends.util.FileUtil;
public class PreprocessorHandler {
GlobalIncludeMap includeMap = GlobalIncludeMap.INSTANCE;
private String fileName;
@ -16,6 +20,8 @@ public class PreprocessorHandler {
includedFullPathNames= new ArrayList<>();
if (!includeMap.contains(fileName))
processIncludes(statements);
else
includedFullPathNames = includeMap.get(fileName);
}
@ -30,7 +36,7 @@ public class PreprocessorHandler {
continue;
}
this.includedFullPathNames.add(incl.getPath());
this.includedFullPathNames.add(FileUtil.uniqFilePath(incl.getPath()));
String explandedPath = includeMap.add(fileName,incl.getPath());
if (!includeMap.contains(explandedPath)) {
IASTTranslationUnit translationUnit = (new CDTParser()).parse(explandedPath);

View File

@ -61,7 +61,7 @@ public class JavaEntitiesListener extends JavaParserBaseListener {
// Import
@Override
public void enterImportDeclaration(ImportDeclarationContext ctx) {
context.foundNewImport(ctx.qualifiedName().getText());
context.foundNewImport(ctx.qualifiedName().getText(),false);
super.enterImportDeclaration(ctx);
}

View File

@ -26,8 +26,7 @@ public class DotDataBuilder {
}
writer.println("digraph");
writer.println("{");
Map<Integer, Map<Integer, Map<String, Integer>>> finalRes = matrix.getRelations();
addRelations(writer,finalRes);
addRelations(writer,matrix.getRelations());
writer.println("}");
writer.close();
return true;
@ -37,13 +36,12 @@ public class DotDataBuilder {
}
}
private void addRelations(PrintWriter writer, Map<Integer, Map<Integer, Map<String, Integer>>> finalRes) {
for (Map.Entry<Integer, Map<Integer, Map<String, Integer>>> entry1 : finalRes.entrySet()) {
int src = entry1.getKey();
Map<Integer, Map<String, Integer>> values1 = entry1.getValue();
for (Map.Entry<Integer, Map<String, Integer>> entry2: values1.entrySet()) {
int dst = entry2.getKey();
private void addRelations(PrintWriter writer, Map<Integer, Map<Integer, Map<String, Integer>>> relations) {
for (Map.Entry<Integer, Map<Integer, Map<String, Integer>>> relation : relations.entrySet()) {
int src = relation.getKey();
Map<Integer, Map<String, Integer>> dependencyType = relation.getValue();
for (Map.Entry<Integer, Map<String, Integer>> to: dependencyType.entrySet()) {
int dst = to.getKey();
writer.println("\t"+src + " -> " + dst + ";");
}
}

View File

@ -0,0 +1,15 @@
package depends.util;
import java.io.File;
import java.io.IOException;
public class FileUtil {
public static String uniqFilePath(String filePath) {
try {
File f = new File(filePath);
filePath = f.getCanonicalPath();
} catch (IOException e) {
}
return filePath;
}
}

View File

@ -24,7 +24,7 @@ public class EntityRepoExpendsImportsOfPackageTest {
visitor.foundNewPackage(packageName);
visitor.startFile("/tmp/thefile.java");
visitor.foundNewImport(packageName);
visitor.foundNewImport(packageName,false);
entityRepo.resolveAllBindings();
@ -47,7 +47,7 @@ public class EntityRepoExpendsImportsOfPackageTest {
context.foundNewPackage(packageName);
context.startFile("/tmp/thefile.java");
context.foundNewImport(packageName+".ClassA");
context.foundNewImport(packageName+".ClassA",false);
entityRepo.resolveAllBindings();

View File

@ -1,3 +1 @@
#ifndef __A_H
#define __A_H
#endif
#include "C.h"

View File

@ -1,2 +1,2 @@
#inclde "A.h"
#inclde <A.h>
#include "A.h"
#include <A.h>