c.s.tools.ide.analysis.modernize/src/com/sun/tools/ide/analysis/modernize/impl/DiagnosticsTool.java
author Ilia Gromov <ilia@netbeans.org>
Wed, 07 Jun 2017 20:23:29 +0300
branchrelease82
changeset 18423 b9d9af239a0c
permissions -rw-r--r--
Fixing #270763 - Move clang-tidy integration to nb contrib
* * *
Fixing #270763 - Move clang-tidy integration to nb contrib - move wrapper
* * *
Fixing #270763 - Move clang-tidy integration to nb contrib - sign nbm
* * *
Fixing #270763 - Move clang-tidy integration to nb contrib - move tests
* * *
Fixing #270763 - Move clang-tidy integration to nb contrib - data for a new test
* * *
Fixed #270839 - [clang-tidy] Group checks in Editor hints
(transplanted from 35b6125ef00c470655dac6673075f5c12ec74593)
     1 /*
     2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     3  *
     4  * Copyright (c) 2017 Oracle and/or its affiliates. All rights reserved.
     5  *
     6  * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
     7  * Other names may be trademarks of their respective owners.
     8  *
     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]"
    26  *
    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.
    37  *
    38  * Contributor(s): Ilia Gromov
    39  */
    40 package com.sun.tools.ide.analysis.modernize.impl;
    41 
    42 import com.sun.tools.ide.analysis.modernize.utils.AnalyticsTools;
    43 import java.io.BufferedReader;
    44 import java.io.File;
    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;
    76 
    77 /**
    78  *
    79  * @author Ilia Gromov
    80  */
    81 public class DiagnosticsTool {
    82 
    83     public static final Logger LOG = Logger.getLogger("ide.analysis.tidy"); //NOI18N
    84     public static final String CACHE_ID = "TIDY-CACHE"; //NOI18N
    85 
    86     public static final int STATUS_OK = 0;
    87     public static final int STATUS_IO_ERROR = -1;
    88 
    89     public static List<CodeAudit> getAudits(String path, ExecutionEnvironment env, AuditPreferences preferences) {
    90         List<CodeAudit> res = new ArrayList<>();
    91 
    92         ProcessUtils.ExitStatus status = ProcessUtils.execute(env, path, new String[]{"-checks=*", "-list-checks", "dummy", "--"}); // NOI18N
    93 
    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
    98 
    99             FileObject disabledChecksFolder = FileUtil.getConfigFile("Analysis/Clang-Tidy/Disabled_Default"); // NOI18N
   100 
   101             List<String> disabledChecks = new ArrayList<>();
   102             for (FileObject fileObject : disabledChecksFolder.getChildren()) {
   103                 disabledChecks.add(fileObject.getName());
   104             }
   105 
   106             for (String check : checks) {
   107                 check = check.trim();
   108                 ModernizeCodeAudit myCodeAudit = new ModernizeCodeAudit(
   109                         check,
   110                         check,
   111                         check,
   112                         "warning", //NOI18N
   113                         !disabledChecks.contains(check),
   114                         preferences);
   115                 res.add(myCodeAudit);
   116             }
   117         }
   118 
   119         return res;
   120     }
   121 
   122     private final ExecutionEnvironment execEnv;
   123     private final Item item;
   124     private final MakeProject project;
   125     private final String binaryPath;
   126 
   127     private StringBuilder buf;
   128 
   129     public DiagnosticsTool(ExecutionEnvironment execEnv, Item item, MakeProject project, String binaryPath) {
   130         this.execEnv = execEnv;
   131         this.item = item;
   132         this.project = project;
   133         this.binaryPath = binaryPath;
   134     }
   135 
   136     /**
   137      * @return clang-tidy's exit code or negative code if some other problem has
   138      * occurred.
   139      */
   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?
   142 
   143         final String directoryMacro = "xDIRx"; //NOI18N
   144 
   145         File tmpDir = null;
   146         try {
   147             List<String> args = new ArrayList<String>();
   148 
   149             HostInfo hostInfo = HostInfoUtils.getHostInfo(execEnv);
   150 
   151             try {
   152                 if (execEnv.isRemote()) {
   153                     tmpDir = Files.createTempDirectory(hostInfo.getTempDirFile().toPath(), "tidy").toFile();  //NOI18N
   154                 } else {
   155                     tmpDir = Files.createTempDirectory("tidy").toFile();  //NOI18N
   156                 }
   157             } catch (IOException ex) {
   158                 return -1;
   159             }
   160 
   161             Path tmpFile = Files.createTempFile(tmpDir.toPath(), null, null);
   162 
   163             args.add("-header-filter=.*"); //NOI18N
   164             StringBuilder sb = new StringBuilder("-checks=");// NOI18N
   165             for (String check : checks) {
   166                 sb.append(check);
   167                 sb.append(","); // NOI18N
   168             }
   169             args.add(sb.toString().substring(0, sb.length() - 1));
   170 
   171             args.add("-export-fixes=" + tmpFile); // NOI18N
   172             int directoryIdx = args.size() - 1;
   173 
   174             Collection<? extends CsmFile> startFiles = Collections.EMPTY_LIST;
   175             args.add(csmFile.getFileObject().getPath());
   176 
   177             args.add("--");  //NOI18N
   178             args.addAll(getAdditionalFlags(csmFile));
   179 
   180             List<String> copy = new ArrayList<String>(args);
   181 
   182             copy.set(directoryIdx, copy.get(directoryIdx).replace(directoryMacro, tmpDir.getAbsolutePath()));
   183 
   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(),
   186                     execEnv,
   187                     binaryPath,
   188                     copy.toArray(new String[copy.size()]));
   189 
   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;
   195             } else {
   196                 LOG.log(Level.FINEST, "{0}", arrayAsString(binaryPath, copy));
   197                 LOG.finest(executeStatus.getOutputString());
   198                 LOG.finest(executeStatus.getErrorString());
   199             }
   200 
   201             File[] listFiles = tmpDir.listFiles();
   202 
   203             if (listFiles.length != 1) {
   204                 return STATUS_IO_ERROR;
   205             }
   206 
   207             File yamlFile = listFiles[0];
   208 
   209             BufferedReader in = new BufferedReader(new FileReader(yamlFile));
   210             buf = new StringBuilder();
   211             while (true) {
   212                 String s = in.readLine();
   213                 if (s == null) {
   214                     break;
   215                 }
   216                 buf.append(s).append('\n'); //NOI18N
   217             }
   218 
   219         } finally {
   220             try {
   221                 tmpDir.delete();
   222             } catch (Exception x) {
   223 
   224             }
   225         }
   226 
   227         return STATUS_OK;
   228     }
   229 
   230     private List<String> getAdditionalFlags(CsmFile csmFile) {
   231         List<String> all = new ArrayList<String>();
   232 
   233         Tool tool = AnalyticsTools.compiler(item, project);
   234 
   235         if (tool instanceof AbstractCompiler) {
   236             AbstractCompiler compiler = (AbstractCompiler) tool;
   237 
   238             for (String systemIncludeDirectory : compiler.getSystemIncludeDirectories()) {
   239                 all.add("-I" + systemIncludeDirectory); //NOI18N
   240             }
   241 
   242             MakeConfiguration activeConfiguration = AnalyticsTools.getConfigurationDescriptor(csmFile, project).getActiveConfiguration();
   243 
   244             String options = "";
   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()) {
   255                         case CPP98:
   256                             options += " -std=c++98"; //NOI18N
   257                             break;
   258                         case CPP11:
   259                             options += " -std=c++11"; //NOI18N
   260                             break;
   261                         case CPP14:
   262                             options += " -std=c++14"; //NOI18N
   263                             break;
   264                     }
   265                 }
   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()) {
   273                         case C11:
   274                             options += " -std=c11"; //NOI18N
   275                             break;
   276                         case C89:
   277                             options += " -std=c89"; //NOI18N
   278                             break;
   279                         case C99:
   280                             options += " -std=c99"; //NOI18N
   281                             break;
   282                     }
   283                 }
   284             }
   285 
   286             List<String> optionsList = AnalyticsTools.scanCommandLine(MakeProjectOptionsFormat.reformatWhitespaces(options));
   287             all.addAll(optionsList);
   288         }
   289 
   290         return all;
   291     }
   292 
   293     public String getYamlAsString() {
   294         if (buf == null) {
   295             throw new IllegalStateException();
   296         }
   297         return buf.toString();
   298     }
   299 
   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
   304         }
   305         return sb.toString();
   306     }
   307 }