ide.analysis.modernize/src/org/netbeans/modules/ide/analysis/modernize/impl/DiagnosticsTool.java
1.1 --- a/ide.analysis.modernize/src/org/netbeans/modules/ide/analysis/modernize/impl/DiagnosticsTool.java Wed Jun 07 18:50:35 2017 +0300
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,307 +0,0 @@
1.4 -/*
1.5 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
1.6 - *
1.7 - * Copyright (c) 2017 Oracle and/or its affiliates. All rights reserved.
1.8 - *
1.9 - * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
1.10 - * Other names may be trademarks of their respective owners.
1.11 - *
1.12 - * The contents of this file are subject to the terms of either the GNU
1.13 - * General Public License Version 2 only ("GPL") or the Common
1.14 - * Development and Distribution License("CDDL") (collectively, the
1.15 - * "License"). You may not use this file except in compliance with the
1.16 - * License. You can obtain a copy of the License at
1.17 - * http://www.netbeans.org/cddl-gplv2.html
1.18 - * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
1.19 - * specific language governing permissions and limitations under the
1.20 - * License. When distributing the software, include this License Header
1.21 - * Notice in each file and include the License file at
1.22 - * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
1.23 - * particular file as subject to the "Classpath" exception as provided
1.24 - * by Oracle in the GPL Version 2 section of the License file that
1.25 - * accompanied this code. If applicable, add the following below the
1.26 - * License Header, with the fields enclosed by brackets [] replaced by
1.27 - * your own identifying information:
1.28 - * "Portions Copyrighted [year] [name of copyright owner]"
1.29 - *
1.30 - * If you wish your version of this file to be governed by only the CDDL
1.31 - * or only the GPL Version 2, indicate your decision by adding
1.32 - * "[Contributor] elects to include this software in this distribution
1.33 - * under the [CDDL or GPL Version 2] license." If you do not indicate a
1.34 - * single choice of license, a recipient has the option to distribute
1.35 - * your version of this file under either the CDDL, the GPL Version 2 or
1.36 - * to extend the choice of license to its licensees as provided above.
1.37 - * However, if you add GPL Version 2 code and therefore, elected the GPL
1.38 - * Version 2 license, then the option applies only if the new code is
1.39 - * made subject to such option by the copyright holder.
1.40 - *
1.41 - * Contributor(s): Ilia Gromov
1.42 - */
1.43 -package org.netbeans.modules.ide.analysis.modernize.impl;
1.44 -
1.45 -import org.netbeans.modules.ide.analysis.modernize.utils.AnalyticsTools;
1.46 -import java.io.BufferedReader;
1.47 -import java.io.File;
1.48 -import java.io.FileReader;
1.49 -import java.io.IOException;
1.50 -import java.nio.file.Files;
1.51 -import java.nio.file.Path;
1.52 -import java.util.ArrayList;
1.53 -import java.util.Collection;
1.54 -import java.util.Collections;
1.55 -import java.util.List;
1.56 -import java.util.logging.Level;
1.57 -import java.util.logging.Logger;
1.58 -import org.netbeans.modules.cnd.api.model.CsmFile;
1.59 -import org.netbeans.modules.cnd.api.model.syntaxerr.AuditPreferences;
1.60 -import org.netbeans.modules.cnd.api.model.syntaxerr.CodeAudit;
1.61 -import org.netbeans.modules.cnd.api.toolchain.AbstractCompiler;
1.62 -import static org.netbeans.modules.cnd.api.toolchain.PredefinedToolKind.CCCompiler;
1.63 -import static org.netbeans.modules.cnd.api.toolchain.PredefinedToolKind.CCompiler;
1.64 -import org.netbeans.modules.cnd.api.toolchain.Tool;
1.65 -import org.netbeans.modules.cnd.makeproject.api.MakeProject;
1.66 -import org.netbeans.modules.cnd.makeproject.api.configurations.CCCCompilerConfiguration;
1.67 -import org.netbeans.modules.cnd.makeproject.api.configurations.CCCompilerConfiguration;
1.68 -import org.netbeans.modules.cnd.makeproject.api.configurations.CCompilerConfiguration;
1.69 -import org.netbeans.modules.cnd.makeproject.api.configurations.Item;
1.70 -import org.netbeans.modules.cnd.makeproject.api.configurations.MakeConfiguration;
1.71 -import org.netbeans.modules.cnd.makeproject.api.support.MakeProjectOptionsFormat;
1.72 -import org.netbeans.modules.nativeexecution.api.ExecutionEnvironment;
1.73 -import org.netbeans.modules.nativeexecution.api.HostInfo;
1.74 -import org.netbeans.modules.nativeexecution.api.util.ConnectionManager;
1.75 -import org.netbeans.modules.nativeexecution.api.util.HostInfoUtils;
1.76 -import org.netbeans.modules.nativeexecution.api.util.ProcessUtils;
1.77 -import org.openide.filesystems.FileObject;
1.78 -import org.openide.filesystems.FileUtil;
1.79 -
1.80 -/**
1.81 - *
1.82 - * @author Ilia Gromov
1.83 - */
1.84 -public class DiagnosticsTool {
1.85 -
1.86 - public static final Logger LOG = Logger.getLogger("ide.analysis.tidy"); //NOI18N
1.87 - public static final String CACHE_ID = "TIDY-CACHE"; //NOI18N
1.88 -
1.89 - public static final int STATUS_OK = 0;
1.90 - public static final int STATUS_IO_ERROR = -1;
1.91 -
1.92 - public static List<CodeAudit> getAudits(String path, ExecutionEnvironment env, AuditPreferences preferences) {
1.93 - List<CodeAudit> res = new ArrayList<>();
1.94 -
1.95 - ProcessUtils.ExitStatus status = ProcessUtils.execute(env, path, new String[]{"-checks=*", "-list-checks", "dummy", "--"}); // NOI18N
1.96 -
1.97 - if (status.exitCode == 0) {
1.98 - String out = status.getOutputString();
1.99 - out = out.substring(status.getOutputString().indexOf('\n') + 1);
1.100 - String[] checks = out.split("\n"); // NOI18N
1.101 -
1.102 - FileObject disabledChecksFolder = FileUtil.getConfigFile("Analysis/Clang-Tidy/Disabled_Default"); // NOI18N
1.103 -
1.104 - List<String> disabledChecks = new ArrayList<>();
1.105 - for (FileObject fileObject : disabledChecksFolder.getChildren()) {
1.106 - disabledChecks.add(fileObject.getName());
1.107 - }
1.108 -
1.109 - for (String check : checks) {
1.110 - check = check.trim();
1.111 - ModernizeCodeAudit myCodeAudit = new ModernizeCodeAudit(
1.112 - check,
1.113 - check,
1.114 - check,
1.115 - "warning", //NOI18N
1.116 - !disabledChecks.contains(check),
1.117 - preferences);
1.118 - res.add(myCodeAudit);
1.119 - }
1.120 - }
1.121 -
1.122 - return res;
1.123 - }
1.124 -
1.125 - private final ExecutionEnvironment execEnv;
1.126 - private final Item item;
1.127 - private final MakeProject project;
1.128 - private final String binaryPath;
1.129 -
1.130 - private StringBuilder buf;
1.131 -
1.132 - public DiagnosticsTool(ExecutionEnvironment execEnv, Item item, MakeProject project, String binaryPath) {
1.133 - this.execEnv = execEnv;
1.134 - this.item = item;
1.135 - this.project = project;
1.136 - this.binaryPath = binaryPath;
1.137 - }
1.138 -
1.139 - /**
1.140 - * @return clang-tidy's exit code or negative code if some other problem has
1.141 - * occurred.
1.142 - */
1.143 - public int process(Collection<String> checks, CsmFile csmFile, boolean isStartFile) throws ConnectionManager.CancellationException, IOException {
1.144 - // TODO: can we split analyzer (Source -> Inspect) ant editor error providing?
1.145 -
1.146 - final String directoryMacro = "xDIRx"; //NOI18N
1.147 -
1.148 - File tmpDir = null;
1.149 - try {
1.150 - List<String> args = new ArrayList<String>();
1.151 -
1.152 - HostInfo hostInfo = HostInfoUtils.getHostInfo(execEnv);
1.153 -
1.154 - try {
1.155 - if (execEnv.isRemote()) {
1.156 - tmpDir = Files.createTempDirectory(hostInfo.getTempDirFile().toPath(), "tidy").toFile(); //NOI18N
1.157 - } else {
1.158 - tmpDir = Files.createTempDirectory("tidy").toFile(); //NOI18N
1.159 - }
1.160 - } catch (IOException ex) {
1.161 - return -1;
1.162 - }
1.163 -
1.164 - Path tmpFile = Files.createTempFile(tmpDir.toPath(), null, null);
1.165 -
1.166 - args.add("-header-filter=.*"); //NOI18N
1.167 - StringBuilder sb = new StringBuilder("-checks=");// NOI18N
1.168 - for (String check : checks) {
1.169 - sb.append(check);
1.170 - sb.append(","); // NOI18N
1.171 - }
1.172 - args.add(sb.toString().substring(0, sb.length() - 1));
1.173 -
1.174 - args.add("-export-fixes=" + tmpFile); // NOI18N
1.175 - int directoryIdx = args.size() - 1;
1.176 -
1.177 - Collection<? extends CsmFile> startFiles = Collections.EMPTY_LIST;
1.178 - args.add(csmFile.getFileObject().getPath());
1.179 -
1.180 - args.add("--"); //NOI18N
1.181 - args.addAll(getAdditionalFlags(csmFile));
1.182 -
1.183 - List<String> copy = new ArrayList<String>(args);
1.184 -
1.185 - copy.set(directoryIdx, copy.get(directoryIdx).replace(directoryMacro, tmpDir.getAbsolutePath()));
1.186 -
1.187 - // TODO: execute in the ${WORKING_DIR} ? Because we can have relative path includes (-Isrc/libs/abcd)
1.188 - ProcessUtils.ExitStatus executeStatus = ProcessUtils.executeInDir(tmpDir.getAbsolutePath(),
1.189 - execEnv,
1.190 - binaryPath,
1.191 - copy.toArray(new String[copy.size()]));
1.192 -
1.193 - if (executeStatus.exitCode != STATUS_OK) {
1.194 - LOG.log(Level.INFO, "clang-tidy exit code {0}: {1}", new Object[]{executeStatus.exitCode, arrayAsString(binaryPath, copy)});
1.195 - LOG.fine(executeStatus.getOutputString());
1.196 - LOG.fine(executeStatus.getErrorString());
1.197 - return executeStatus.exitCode;
1.198 - } else {
1.199 - LOG.log(Level.FINEST, "{0}", arrayAsString(binaryPath, copy));
1.200 - LOG.finest(executeStatus.getOutputString());
1.201 - LOG.finest(executeStatus.getErrorString());
1.202 - }
1.203 -
1.204 - File[] listFiles = tmpDir.listFiles();
1.205 -
1.206 - if (listFiles.length != 1) {
1.207 - return STATUS_IO_ERROR;
1.208 - }
1.209 -
1.210 - File yamlFile = listFiles[0];
1.211 -
1.212 - BufferedReader in = new BufferedReader(new FileReader(yamlFile));
1.213 - buf = new StringBuilder();
1.214 - while (true) {
1.215 - String s = in.readLine();
1.216 - if (s == null) {
1.217 - break;
1.218 - }
1.219 - buf.append(s).append('\n'); //NOI18N
1.220 - }
1.221 -
1.222 - } finally {
1.223 - try {
1.224 - tmpDir.delete();
1.225 - } catch (Exception x) {
1.226 -
1.227 - }
1.228 - }
1.229 -
1.230 - return STATUS_OK;
1.231 - }
1.232 -
1.233 - private List<String> getAdditionalFlags(CsmFile csmFile) {
1.234 - List<String> all = new ArrayList<String>();
1.235 -
1.236 - Tool tool = AnalyticsTools.compiler(item, project);
1.237 -
1.238 - if (tool instanceof AbstractCompiler) {
1.239 - AbstractCompiler compiler = (AbstractCompiler) tool;
1.240 -
1.241 - for (String systemIncludeDirectory : compiler.getSystemIncludeDirectories()) {
1.242 - all.add("-I" + systemIncludeDirectory); //NOI18N
1.243 - }
1.244 -
1.245 - MakeConfiguration activeConfiguration = AnalyticsTools.getConfigurationDescriptor(csmFile, project).getActiveConfiguration();
1.246 -
1.247 - String options = "";
1.248 -// // compileSingleUnmanage as reference
1.249 -// // don't use getAllOptions - we need not -w flag
1.250 - if (tool.getKind() == CCCompiler) {
1.251 - CCCompilerConfiguration ccc = activeConfiguration.getCCCompilerConfiguration();
1.252 - options += ccc.getPreprocessorOptions(compiler.getCompilerSet());
1.253 - options += ccc.getIncludeDirectoriesOptions(compiler.getCompilerSet());
1.254 -// options += getLibrariesFlags();
1.255 - options += compiler.getCppStandardOptions(ccc.getInheritedCppStandard());
1.256 - if (!options.contains("-std=")) { //NOI18N
1.257 - switch (item.getLanguageFlavor()) {
1.258 - case CPP98:
1.259 - options += " -std=c++98"; //NOI18N
1.260 - break;
1.261 - case CPP11:
1.262 - options += " -std=c++11"; //NOI18N
1.263 - break;
1.264 - case CPP14:
1.265 - options += " -std=c++14"; //NOI18N
1.266 - break;
1.267 - }
1.268 - }
1.269 - } else if (tool.getKind() == CCompiler) {
1.270 - CCompilerConfiguration cc = activeConfiguration.getCCompilerConfiguration();
1.271 - options += cc.getPreprocessorOptions(compiler.getCompilerSet());
1.272 - options += cc.getIncludeDirectoriesOptions(compiler.getCompilerSet());
1.273 - options += compiler.getCStandardOptions(cc.getInheritedCStandard());
1.274 - if (!options.contains("-std=")) { //NOI18N
1.275 - switch (item.getLanguageFlavor()) {
1.276 - case C11:
1.277 - options += " -std=c11"; //NOI18N
1.278 - break;
1.279 - case C89:
1.280 - options += " -std=c89"; //NOI18N
1.281 - break;
1.282 - case C99:
1.283 - options += " -std=c99"; //NOI18N
1.284 - break;
1.285 - }
1.286 - }
1.287 - }
1.288 -
1.289 - List<String> optionsList = AnalyticsTools.scanCommandLine(MakeProjectOptionsFormat.reformatWhitespaces(options));
1.290 - all.addAll(optionsList);
1.291 - }
1.292 -
1.293 - return all;
1.294 - }
1.295 -
1.296 - public String getYamlAsString() {
1.297 - if (buf == null) {
1.298 - throw new IllegalStateException();
1.299 - }
1.300 - return buf.toString();
1.301 - }
1.302 -
1.303 - private static String arrayAsString(String binary, List<String> list) {
1.304 - StringBuilder sb = new StringBuilder(binary).append(" "); //NOI18N
1.305 - for (String string : list) {
1.306 - sb.append(string).append(" "); //NOI18N
1.307 - }
1.308 - return sb.toString();
1.309 - }
1.310 -}