Search in sources :

Example 1 with CppException

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);
    }
}
Also used : Path(java.nio.file.Path) InputLexerSource(org.anarres.cpp.InputLexerSource) CppException(mrmathami.cia.cpp.CppException) StringReader(java.io.StringReader) Preprocessor(org.anarres.cpp.Preprocessor) IOException(java.io.IOException) LexerException(org.anarres.cpp.LexerException) Nonnull(mrmathami.annotations.Nonnull)

Example 2 with CppException

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);
    }
}
Also used : Path(java.nio.file.Path) RootNode(mrmathami.cia.cpp.ast.RootNode) CppException(mrmathami.cia.cpp.CppException) IOException(java.io.IOException) IASTTranslationUnit(org.eclipse.cdt.core.dom.ast.IASTTranslationUnit) DependencyType(mrmathami.cia.cpp.ast.DependencyType) Nonnull(mrmathami.annotations.Nonnull)

Example 3 with CppException

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;
}
Also used : Path(java.nio.file.Path) CppException(mrmathami.cia.cpp.CppException) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Reader(java.io.Reader) IOException(java.io.IOException) CharArrayWriter(java.io.CharArrayWriter) CoreException(org.eclipse.core.runtime.CoreException) IASTTranslationUnit(org.eclipse.cdt.core.dom.ast.IASTTranslationUnit) IASTPreprocessorIncludeStatement(org.eclipse.cdt.core.dom.ast.IASTPreprocessorIncludeStatement) Pair(mrmathami.utils.Pair) HashSet(java.util.HashSet) Nonnull(mrmathami.annotations.Nonnull)

Aggregations

IOException (java.io.IOException)3 Path (java.nio.file.Path)3 Nonnull (mrmathami.annotations.Nonnull)3 CppException (mrmathami.cia.cpp.CppException)3 IASTTranslationUnit (org.eclipse.cdt.core.dom.ast.IASTTranslationUnit)2 CharArrayWriter (java.io.CharArrayWriter)1 Reader (java.io.Reader)1 StringReader (java.io.StringReader)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 DependencyType (mrmathami.cia.cpp.ast.DependencyType)1 RootNode (mrmathami.cia.cpp.ast.RootNode)1 Pair (mrmathami.utils.Pair)1 InputLexerSource (org.anarres.cpp.InputLexerSource)1 LexerException (org.anarres.cpp.LexerException)1 Preprocessor (org.anarres.cpp.Preprocessor)1 IASTPreprocessorIncludeStatement (org.eclipse.cdt.core.dom.ast.IASTPreprocessorIncludeStatement)1 CoreException (org.eclipse.core.runtime.CoreException)1