use of mrmathami.cia.cpp.CppException in project Cpp4CIA by thanhminhmr.
the class PreprocessorBuilder method build.
@Nonnull
public static char[] build(@Nonnull Path projectRootPath, @Nonnull List<Path> projectFiles, @Nonnull List<Path> includePaths, boolean isReadable) throws CppException {
try {
final Preprocessor preprocessor = new Preprocessor(EMPTY_PREPROCESSOR_LISTENER);
preprocessor.addFeatures(FEATURE_LIST);
preprocessor.setSystemIncludePath(includePaths);
final StringBuilder builder = new StringBuilder();
for (final Path sourceFile : includeList(projectFiles, includePaths)) {
builder.append("#include \"").append(projectRootPath.relativize(sourceFile)).append("\"\n");
}
final Path virtualFile = projectRootPath.resolve(UUID.randomUUID() + ".virtual_file");
preprocessor.addInput(new InputLexerSource(new StringReader(builder.toString()), virtualFile));
// =====
final StringBuilder fileContent = new StringBuilder();
if (isReadable) {
readablePreprocessor(preprocessor, fileContent);
} else {
fastPreprocessor(preprocessor, fileContent);
}
// =====
char[] content = new char[fileContent.length()];
fileContent.getChars(0, content.length, content, 0);
return content;
} catch (IOException | LexerException e) {
throw new CppException("Cannot preprocess the source code!", e);
}
}
use of mrmathami.cia.cpp.CppException in project Cpp4CIA by thanhminhmr.
the class VersionBuilder method build.
@Nonnull
public static ProjectVersion build(@Nonnull String versionName, @Nonnull Path projectRoot, @Nonnull List<Path> projectFiles, @Nonnull List<Path> includePaths, @Nonnull Map<DependencyType, Double> dependencyTypeWeightMap) throws CppException {
try {
final List<Path> projectFileList = createPathList(projectFiles);
final List<Path> externalIncludePaths = createPathList(includePaths);
final List<Path> internalIncludePaths = createInternalIncludePaths(projectFileList);
final List<Path> includePathList = combinePathList(externalIncludePaths, internalIncludePaths);
final Path projectRootPath = projectRoot.toRealPath(LinkOption.NOFOLLOW_LINKS);
final char[] fileContentCharArray = PreprocessorBuilder.build(projectRootPath, projectFileList, includePathList, false);
final IASTTranslationUnit translationUnit = TranslationUnitBuilder.build(fileContentCharArray);
final RootNode root = AstBuilder.build(translationUnit);
final List<String> projectFilePaths = createRelativePathStrings(projectFileList, projectRootPath);
final List<String> projectIncludePaths = createRelativePathStrings(externalIncludePaths, projectRootPath);
final DependencyType[] types = DependencyType.values();
final double[] typeWeights = new double[types.length];
for (final DependencyType type : types) typeWeights[type.ordinal()] = dependencyTypeWeightMap.get(type);
final double[] weights = calculateWeights(typeWeights, root);
return new ProjectVersion(versionName, projectFilePaths, projectIncludePaths, root, typeWeights, weights);
} catch (IOException e) {
throw new CppException("Error when trying to build project!", e);
}
}
use of mrmathami.cia.cpp.CppException in project Cpp4CIA by thanhminhmr.
the class TranslationUnitBuilder method createFileIncludesList.
@Nonnull
static List<Pair<Path, Set<Path>>> createFileIncludesList(@Nonnull List<Path> projectFiles, @Nonnull List<Path> includePaths) throws CppException {
final List<Pair<Path, Set<Path>>> includeList = new ArrayList<>(projectFiles.size());
final Map<Path, Path> projectFileMap = new HashMap<>(projectFiles.size());
for (final Path projectFile : projectFiles) projectFileMap.put(projectFile, projectFile);
for (final Path projectFile : projectFiles) {
try (final CharArrayWriter writer = new CharArrayWriter()) {
try (final Reader reader = EncodingDetector.createReader(Files.newInputStream(projectFile))) {
reader.transferTo(writer);
}
final IASTTranslationUnit translationUnit = GPP_LANGUAGE.getASTTranslationUnit(FileContent.create(projectFile.toString(), writer.toCharArray()), SCANNER_INFO, EMPTY_PROVIDER, null, ILanguage.OPTION_NO_IMAGE_LOCATIONS | ILanguage.OPTION_SKIP_FUNCTION_BODIES | ILanguage.OPTION_SKIP_TRIVIAL_EXPRESSIONS_IN_AGGREGATE_INITIALIZERS, LOG_SERVICE);
final Path currentFolder = projectFile.getParent();
final Set<Path> includeSet = new HashSet<>();
for (final IASTPreprocessorIncludeStatement includeDirective : translationUnit.getIncludeDirectives()) {
final String includeFileName = includeDirective.getName().toString();
if (!includeDirective.isSystemInclude()) {
final Path includeFile = currentFolder.resolve(includeFileName).normalize();
final Path normalizedIncludeFile = projectFileMap.get(includeFile);
if (normalizedIncludeFile != null) {
includeSet.add(normalizedIncludeFile);
continue;
}
}
for (final Path includePath : includePaths) {
final Path includeFile = includePath.resolve(includeFileName).normalize();
final Path normalizedIncludeFile = projectFileMap.get(includeFile);
if (normalizedIncludeFile != null) {
includeSet.add(normalizedIncludeFile);
break;
}
}
}
includeList.add(Pair.immutableOf(projectFile, includeSet));
} catch (CoreException e) {
throw new CppException("Cannot create TranslationUnit!", e);
} catch (IOException e) {
throw new CppException("Cannot read project file!", e);
}
}
return includeList;
}
Aggregations