can produce import relation of c/c++
This commit is contained in:
parent
73ed56149b
commit
fbda0152af
|
@ -26,10 +26,13 @@ public class FileEntity extends ContainerEntity{
|
||||||
* @param importedName could be className, package Name in JAVA
|
* @param importedName could be className, package Name in JAVA
|
||||||
* could be file in C/C++
|
* could be file in C/C++
|
||||||
*/
|
*/
|
||||||
public void addImport(String importedName) {
|
public void addImport(String importedName, boolean useFileAsImportedKey) {
|
||||||
String lastName = importedName;
|
String lastName = importedName;
|
||||||
if (lastName.indexOf(".") > 0)
|
if (useFileAsImportedKey) {
|
||||||
lastName = lastName.substring(lastName.lastIndexOf(".")+1);
|
;
|
||||||
|
}else if (lastName.indexOf(".") > 0) {
|
||||||
|
lastName = lastName.substring(lastName.lastIndexOf(".")+1);
|
||||||
|
}
|
||||||
importedNames.put(lastName, importedName);
|
importedNames.put(lastName, importedName);
|
||||||
}
|
}
|
||||||
public String getImport(String lastName) {
|
public String getImport(String lastName) {
|
||||||
|
|
|
@ -64,8 +64,15 @@ public class HandlerContext {
|
||||||
entityStack.pop();
|
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() {
|
public TypeEntity currentType() {
|
||||||
|
|
|
@ -76,7 +76,7 @@ public class CdtCppEntitiesListener extends ASTVisitor {
|
||||||
IASTPreprocessorStatement[] i = tu.getAllPreprocessorStatements();
|
IASTPreprocessorStatement[] i = tu.getAllPreprocessorStatements();
|
||||||
preprocessorHandler.handlePreprocessors(tu.getAllPreprocessorStatements(),this.filePath);
|
preprocessorHandler.handlePreprocessors(tu.getAllPreprocessorStatements(),this.filePath);
|
||||||
for (String incl:preprocessorHandler.getIncludedFullPathNames()) {
|
for (String incl:preprocessorHandler.getIncludedFullPathNames()) {
|
||||||
context.foundNewImport(incl);
|
context.foundNewImport(incl,true);
|
||||||
}
|
}
|
||||||
return super.visit(tu);
|
return super.visit(tu);
|
||||||
}
|
}
|
||||||
|
|
|
@ -25,6 +25,7 @@ import org.eclipse.cdt.internal.core.parser.scanner.InternalFileContent;
|
||||||
|
|
||||||
import depends.entity.repo.EntityRepo;
|
import depends.entity.repo.EntityRepo;
|
||||||
import depends.extractor.cpp.CppFileParser;
|
import depends.extractor.cpp.CppFileParser;
|
||||||
|
import depends.util.FileUtil;
|
||||||
|
|
||||||
public class CdtCppFileParser extends CppFileParser {
|
public class CdtCppFileParser extends CppFileParser {
|
||||||
|
|
||||||
|
@ -36,7 +37,7 @@ public class CdtCppFileParser extends CppFileParser {
|
||||||
}
|
}
|
||||||
@Override
|
@Override
|
||||||
public void parse() throws IOException {
|
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);
|
IASTTranslationUnit translationUnit = (new CDTParser()).parse(this.fileFullPath);
|
||||||
translationUnit.accept(bridge);
|
translationUnit.accept(bridge);
|
||||||
}
|
}
|
||||||
|
|
|
@ -44,4 +44,7 @@ public class GlobalIncludeMap {
|
||||||
if (matcher.matches()) return true;
|
if (matcher.matches()) return true;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
public List<String> get(String fileName) {
|
||||||
|
return includesMap.get(fileName);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
package depends.extractor.cpp.cdt;
|
package depends.extractor.cpp.cdt;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
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.IASTPreprocessorStatement;
|
||||||
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
|
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
|
||||||
|
|
||||||
|
import depends.util.FileUtil;
|
||||||
|
|
||||||
public class PreprocessorHandler {
|
public class PreprocessorHandler {
|
||||||
GlobalIncludeMap includeMap = GlobalIncludeMap.INSTANCE;
|
GlobalIncludeMap includeMap = GlobalIncludeMap.INSTANCE;
|
||||||
private String fileName;
|
private String fileName;
|
||||||
|
@ -16,6 +20,8 @@ public class PreprocessorHandler {
|
||||||
includedFullPathNames= new ArrayList<>();
|
includedFullPathNames= new ArrayList<>();
|
||||||
if (!includeMap.contains(fileName))
|
if (!includeMap.contains(fileName))
|
||||||
processIncludes(statements);
|
processIncludes(statements);
|
||||||
|
else
|
||||||
|
includedFullPathNames = includeMap.get(fileName);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -30,7 +36,7 @@ public class PreprocessorHandler {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.includedFullPathNames.add(incl.getPath());
|
this.includedFullPathNames.add(FileUtil.uniqFilePath(incl.getPath()));
|
||||||
String explandedPath = includeMap.add(fileName,incl.getPath());
|
String explandedPath = includeMap.add(fileName,incl.getPath());
|
||||||
if (!includeMap.contains(explandedPath)) {
|
if (!includeMap.contains(explandedPath)) {
|
||||||
IASTTranslationUnit translationUnit = (new CDTParser()).parse(explandedPath);
|
IASTTranslationUnit translationUnit = (new CDTParser()).parse(explandedPath);
|
||||||
|
|
|
@ -61,7 +61,7 @@ public class JavaEntitiesListener extends JavaParserBaseListener {
|
||||||
// Import
|
// Import
|
||||||
@Override
|
@Override
|
||||||
public void enterImportDeclaration(ImportDeclarationContext ctx) {
|
public void enterImportDeclaration(ImportDeclarationContext ctx) {
|
||||||
context.foundNewImport(ctx.qualifiedName().getText());
|
context.foundNewImport(ctx.qualifiedName().getText(),false);
|
||||||
super.enterImportDeclaration(ctx);
|
super.enterImportDeclaration(ctx);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -26,8 +26,7 @@ public class DotDataBuilder {
|
||||||
}
|
}
|
||||||
writer.println("digraph");
|
writer.println("digraph");
|
||||||
writer.println("{");
|
writer.println("{");
|
||||||
Map<Integer, Map<Integer, Map<String, Integer>>> finalRes = matrix.getRelations();
|
addRelations(writer,matrix.getRelations());
|
||||||
addRelations(writer,finalRes);
|
|
||||||
writer.println("}");
|
writer.println("}");
|
||||||
writer.close();
|
writer.close();
|
||||||
return true;
|
return true;
|
||||||
|
@ -37,13 +36,12 @@ public class DotDataBuilder {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void addRelations(PrintWriter writer, Map<Integer, Map<Integer, Map<String, Integer>>> finalRes) {
|
private void addRelations(PrintWriter writer, Map<Integer, Map<Integer, Map<String, Integer>>> relations) {
|
||||||
for (Map.Entry<Integer, Map<Integer, Map<String, Integer>>> entry1 : finalRes.entrySet()) {
|
for (Map.Entry<Integer, Map<Integer, Map<String, Integer>>> relation : relations.entrySet()) {
|
||||||
int src = entry1.getKey();
|
int src = relation.getKey();
|
||||||
|
Map<Integer, Map<String, Integer>> dependencyType = relation.getValue();
|
||||||
Map<Integer, Map<String, Integer>> values1 = entry1.getValue();
|
for (Map.Entry<Integer, Map<String, Integer>> to: dependencyType.entrySet()) {
|
||||||
for (Map.Entry<Integer, Map<String, Integer>> entry2: values1.entrySet()) {
|
int dst = to.getKey();
|
||||||
int dst = entry2.getKey();
|
|
||||||
writer.println("\t"+src + " -> " + dst + ";");
|
writer.println("\t"+src + " -> " + dst + ";");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -24,7 +24,7 @@ public class EntityRepoExpendsImportsOfPackageTest {
|
||||||
visitor.foundNewPackage(packageName);
|
visitor.foundNewPackage(packageName);
|
||||||
|
|
||||||
visitor.startFile("/tmp/thefile.java");
|
visitor.startFile("/tmp/thefile.java");
|
||||||
visitor.foundNewImport(packageName);
|
visitor.foundNewImport(packageName,false);
|
||||||
|
|
||||||
entityRepo.resolveAllBindings();
|
entityRepo.resolveAllBindings();
|
||||||
|
|
||||||
|
@ -47,7 +47,7 @@ public class EntityRepoExpendsImportsOfPackageTest {
|
||||||
context.foundNewPackage(packageName);
|
context.foundNewPackage(packageName);
|
||||||
|
|
||||||
context.startFile("/tmp/thefile.java");
|
context.startFile("/tmp/thefile.java");
|
||||||
context.foundNewImport(packageName+".ClassA");
|
context.foundNewImport(packageName+".ClassA",false);
|
||||||
|
|
||||||
entityRepo.resolveAllBindings();
|
entityRepo.resolveAllBindings();
|
||||||
|
|
||||||
|
|
|
@ -1,3 +1 @@
|
||||||
#ifndef __A_H
|
#include "C.h"
|
||||||
#define __A_H
|
|
||||||
#endif
|
|
||||||
|
|
|
@ -1,2 +1,2 @@
|
||||||
#inclde "A.h"
|
#include "A.h"
|
||||||
#inclde <A.h>
|
#include <A.h>
|
||||||
|
|
Loading…
Reference in New Issue