c.s.tools.ide.analysis.modernize/src/com/sun/tools/ide/analysis/modernize/impl/DiagnosticsTool.java
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4 * Copyright (c) 2017 Oracle and/or its affiliates. All rights reserved.
6 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
7 * Other names may be trademarks of their respective owners.
9 * The contents of this file are subject to the terms of either the GNU
10 * General Public License Version 2 only ("GPL") or the Common
11 * Development and Distribution License("CDDL") (collectively, the
12 * "License"). You may not use this file except in compliance with the
13 * License. You can obtain a copy of the License at
14 * http://www.netbeans.org/cddl-gplv2.html
15 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
16 * specific language governing permissions and limitations under the
17 * License. When distributing the software, include this License Header
18 * Notice in each file and include the License file at
19 * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
20 * particular file as subject to the "Classpath" exception as provided
21 * by Oracle in the GPL Version 2 section of the License file that
22 * accompanied this code. If applicable, add the following below the
23 * License Header, with the fields enclosed by brackets [] replaced by
24 * your own identifying information:
25 * "Portions Copyrighted [year] [name of copyright owner]"
27 * If you wish your version of this file to be governed by only the CDDL
28 * or only the GPL Version 2, indicate your decision by adding
29 * "[Contributor] elects to include this software in this distribution
30 * under the [CDDL or GPL Version 2] license." If you do not indicate a
31 * single choice of license, a recipient has the option to distribute
32 * your version of this file under either the CDDL, the GPL Version 2 or
33 * to extend the choice of license to its licensees as provided above.
34 * However, if you add GPL Version 2 code and therefore, elected the GPL
35 * Version 2 license, then the option applies only if the new code is
36 * made subject to such option by the copyright holder.
38 * Contributor(s): Ilia Gromov
40 package com.sun.tools.ide.analysis.modernize.impl;
42 import com.sun.tools.ide.analysis.modernize.utils.AnalyticsTools;
43 import java.io.BufferedReader;
45 import java.io.FileReader;
46 import java.io.IOException;
47 import java.nio.file.Files;
48 import java.nio.file.Path;
49 import java.util.ArrayList;
50 import java.util.Collection;
51 import java.util.Collections;
52 import java.util.List;
53 import java.util.logging.Level;
54 import java.util.logging.Logger;
55 import org.netbeans.modules.cnd.api.model.CsmFile;
56 import org.netbeans.modules.cnd.api.model.syntaxerr.AuditPreferences;
57 import org.netbeans.modules.cnd.api.model.syntaxerr.CodeAudit;
58 import org.netbeans.modules.cnd.api.toolchain.AbstractCompiler;
59 import static org.netbeans.modules.cnd.api.toolchain.PredefinedToolKind.CCCompiler;
60 import static org.netbeans.modules.cnd.api.toolchain.PredefinedToolKind.CCompiler;
61 import org.netbeans.modules.cnd.api.toolchain.Tool;
62 import org.netbeans.modules.cnd.makeproject.api.MakeProject;
63 import org.netbeans.modules.cnd.makeproject.api.configurations.CCCCompilerConfiguration;
64 import org.netbeans.modules.cnd.makeproject.api.configurations.CCCompilerConfiguration;
65 import org.netbeans.modules.cnd.makeproject.api.configurations.CCompilerConfiguration;
66 import org.netbeans.modules.cnd.makeproject.api.configurations.Item;
67 import org.netbeans.modules.cnd.makeproject.api.configurations.MakeConfiguration;
68 import org.netbeans.modules.cnd.makeproject.api.support.MakeProjectOptionsFormat;
69 import org.netbeans.modules.nativeexecution.api.ExecutionEnvironment;
70 import org.netbeans.modules.nativeexecution.api.HostInfo;
71 import org.netbeans.modules.nativeexecution.api.util.ConnectionManager;
72 import org.netbeans.modules.nativeexecution.api.util.HostInfoUtils;
73 import org.netbeans.modules.nativeexecution.api.util.ProcessUtils;
74 import org.openide.filesystems.FileObject;
75 import org.openide.filesystems.FileUtil;
81 public class DiagnosticsTool {
83 public static final Logger LOG = Logger.getLogger("ide.analysis.tidy"); //NOI18N
84 public static final String CACHE_ID = "TIDY-CACHE"; //NOI18N
86 public static final int STATUS_OK = 0;
87 public static final int STATUS_IO_ERROR = -1;
89 public static List<CodeAudit> getAudits(String path, ExecutionEnvironment env, AuditPreferences preferences) {
90 List<CodeAudit> res = new ArrayList<>();
92 ProcessUtils.ExitStatus status = ProcessUtils.execute(env, path, new String[]{"-checks=*", "-list-checks", "dummy", "--"}); // NOI18N
94 if (status.exitCode == 0) {
95 String out = status.getOutputString();
96 out = out.substring(status.getOutputString().indexOf('\n') + 1);
97 String[] checks = out.split("\n"); // NOI18N
99 FileObject disabledChecksFolder = FileUtil.getConfigFile("Analysis/Clang-Tidy/Disabled_Default"); // NOI18N
101 List<String> disabledChecks = new ArrayList<>();
102 for (FileObject fileObject : disabledChecksFolder.getChildren()) {
103 disabledChecks.add(fileObject.getName());
106 for (String check : checks) {
107 check = check.trim();
108 ModernizeCodeAudit myCodeAudit = new ModernizeCodeAudit(
113 !disabledChecks.contains(check),
115 res.add(myCodeAudit);
122 private final ExecutionEnvironment execEnv;
123 private final Item item;
124 private final MakeProject project;
125 private final String binaryPath;
127 private StringBuilder buf;
129 public DiagnosticsTool(ExecutionEnvironment execEnv, Item item, MakeProject project, String binaryPath) {
130 this.execEnv = execEnv;
132 this.project = project;
133 this.binaryPath = binaryPath;
137 * @return clang-tidy's exit code or negative code if some other problem has
140 public int process(Collection<String> checks, CsmFile csmFile, boolean isStartFile) throws ConnectionManager.CancellationException, IOException {
141 // TODO: can we split analyzer (Source -> Inspect) ant editor error providing?
143 final String directoryMacro = "xDIRx"; //NOI18N
147 List<String> args = new ArrayList<String>();
149 HostInfo hostInfo = HostInfoUtils.getHostInfo(execEnv);
152 if (execEnv.isRemote()) {
153 tmpDir = Files.createTempDirectory(hostInfo.getTempDirFile().toPath(), "tidy").toFile(); //NOI18N
155 tmpDir = Files.createTempDirectory("tidy").toFile(); //NOI18N
157 } catch (IOException ex) {
161 Path tmpFile = Files.createTempFile(tmpDir.toPath(), null, null);
163 args.add("-header-filter=.*"); //NOI18N
164 StringBuilder sb = new StringBuilder("-checks=");// NOI18N
165 for (String check : checks) {
167 sb.append(","); // NOI18N
169 args.add(sb.toString().substring(0, sb.length() - 1));
171 args.add("-export-fixes=" + tmpFile); // NOI18N
172 int directoryIdx = args.size() - 1;
174 Collection<? extends CsmFile> startFiles = Collections.EMPTY_LIST;
175 args.add(csmFile.getFileObject().getPath());
177 args.add("--"); //NOI18N
178 args.addAll(getAdditionalFlags(csmFile));
180 List<String> copy = new ArrayList<String>(args);
182 copy.set(directoryIdx, copy.get(directoryIdx).replace(directoryMacro, tmpDir.getAbsolutePath()));
184 // TODO: execute in the ${WORKING_DIR} ? Because we can have relative path includes (-Isrc/libs/abcd)
185 ProcessUtils.ExitStatus executeStatus = ProcessUtils.executeInDir(tmpDir.getAbsolutePath(),
188 copy.toArray(new String[copy.size()]));
190 if (executeStatus.exitCode != STATUS_OK) {
191 LOG.log(Level.INFO, "clang-tidy exit code {0}: {1}", new Object[]{executeStatus.exitCode, arrayAsString(binaryPath, copy)});
192 LOG.fine(executeStatus.getOutputString());
193 LOG.fine(executeStatus.getErrorString());
194 return executeStatus.exitCode;
196 LOG.log(Level.FINEST, "{0}", arrayAsString(binaryPath, copy));
197 LOG.finest(executeStatus.getOutputString());
198 LOG.finest(executeStatus.getErrorString());
201 File[] listFiles = tmpDir.listFiles();
203 if (listFiles.length != 1) {
204 return STATUS_IO_ERROR;
207 File yamlFile = listFiles[0];
209 BufferedReader in = new BufferedReader(new FileReader(yamlFile));
210 buf = new StringBuilder();
212 String s = in.readLine();
216 buf.append(s).append('\n'); //NOI18N
222 } catch (Exception x) {
230 private List<String> getAdditionalFlags(CsmFile csmFile) {
231 List<String> all = new ArrayList<String>();
233 Tool tool = AnalyticsTools.compiler(item, project);
235 if (tool instanceof AbstractCompiler) {
236 AbstractCompiler compiler = (AbstractCompiler) tool;
238 for (String systemIncludeDirectory : compiler.getSystemIncludeDirectories()) {
239 all.add("-I" + systemIncludeDirectory); //NOI18N
242 MakeConfiguration activeConfiguration = AnalyticsTools.getConfigurationDescriptor(csmFile, project).getActiveConfiguration();
245 // // compileSingleUnmanage as reference
246 // // don't use getAllOptions - we need not -w flag
247 if (tool.getKind() == CCCompiler) {
248 CCCompilerConfiguration ccc = activeConfiguration.getCCCompilerConfiguration();
249 options += ccc.getPreprocessorOptions(compiler.getCompilerSet());
250 options += ccc.getIncludeDirectoriesOptions(compiler.getCompilerSet());
251 // options += getLibrariesFlags();
252 options += compiler.getCppStandardOptions(ccc.getInheritedCppStandard());
253 if (!options.contains("-std=")) { //NOI18N
254 switch (item.getLanguageFlavor()) {
256 options += " -std=c++98"; //NOI18N
259 options += " -std=c++11"; //NOI18N
262 options += " -std=c++14"; //NOI18N
266 } else if (tool.getKind() == CCompiler) {
267 CCompilerConfiguration cc = activeConfiguration.getCCompilerConfiguration();
268 options += cc.getPreprocessorOptions(compiler.getCompilerSet());
269 options += cc.getIncludeDirectoriesOptions(compiler.getCompilerSet());
270 options += compiler.getCStandardOptions(cc.getInheritedCStandard());
271 if (!options.contains("-std=")) { //NOI18N
272 switch (item.getLanguageFlavor()) {
274 options += " -std=c11"; //NOI18N
277 options += " -std=c89"; //NOI18N
280 options += " -std=c99"; //NOI18N
286 List<String> optionsList = AnalyticsTools.scanCommandLine(MakeProjectOptionsFormat.reformatWhitespaces(options));
287 all.addAll(optionsList);
293 public String getYamlAsString() {
295 throw new IllegalStateException();
297 return buf.toString();
300 private static String arrayAsString(String binary, List<String> list) {
301 StringBuilder sb = new StringBuilder(binary).append(" "); //NOI18N
302 for (String string : list) {
303 sb.append(string).append(" "); //NOI18N
305 return sb.toString();