c.s.tools.ide.analysis.modernize/src/com/sun/tools/ide/analysis/modernize/utils/AnalyticsTools.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)
ilia@18415
     1
/*
ilia@18415
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
ilia@18415
     3
 *
ilia@18415
     4
 * Copyright (c) 2017 Oracle and/or its affiliates. All rights reserved.
ilia@18415
     5
 *
ilia@18415
     6
 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
ilia@18415
     7
 * Other names may be trademarks of their respective owners.
ilia@18415
     8
 *
ilia@18415
     9
 * The contents of this file are subject to the terms of either the GNU
ilia@18415
    10
 * General Public License Version 2 only ("GPL") or the Common
ilia@18415
    11
 * Development and Distribution License("CDDL") (collectively, the
ilia@18415
    12
 * "License"). You may not use this file except in compliance with the
ilia@18415
    13
 * License. You can obtain a copy of the License at
ilia@18415
    14
 * http://www.netbeans.org/cddl-gplv2.html
ilia@18415
    15
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
ilia@18415
    16
 * specific language governing permissions and limitations under the
ilia@18415
    17
 * License.  When distributing the software, include this License Header
ilia@18415
    18
 * Notice in each file and include the License file at
ilia@18415
    19
 * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
ilia@18415
    20
 * particular file as subject to the "Classpath" exception as provided
ilia@18415
    21
 * by Oracle in the GPL Version 2 section of the License file that
ilia@18415
    22
 * accompanied this code. If applicable, add the following below the
ilia@18415
    23
 * License Header, with the fields enclosed by brackets [] replaced by
ilia@18415
    24
 * your own identifying information:
ilia@18415
    25
 * "Portions Copyrighted [year] [name of copyright owner]"
ilia@18415
    26
 *
ilia@18415
    27
 * If you wish your version of this file to be governed by only the CDDL
ilia@18415
    28
 * or only the GPL Version 2, indicate your decision by adding
ilia@18415
    29
 * "[Contributor] elects to include this software in this distribution
ilia@18415
    30
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
ilia@18415
    31
 * single choice of license, a recipient has the option to distribute
ilia@18415
    32
 * your version of this file under either the CDDL, the GPL Version 2 or
ilia@18415
    33
 * to extend the choice of license to its licensees as provided above.
ilia@18415
    34
 * However, if you add GPL Version 2 code and therefore, elected the GPL
ilia@18415
    35
 * Version 2 license, then the option applies only if the new code is
ilia@18415
    36
 * made subject to such option by the copyright holder.
ilia@18415
    37
 *
ilia@18415
    38
 * Contributor(s): Ilia Gromov
ilia@18415
    39
 */
ilia@18415
    40
package com.sun.tools.ide.analysis.modernize.utils;
ilia@18415
    41
ilia@18415
    42
import com.sun.tools.ide.analysis.modernize.impl.ModernizeErrorProvider;
ilia@18415
    43
import java.util.ArrayList;
ilia@18415
    44
import java.util.List;
ilia@18415
    45
import org.netbeans.api.project.FileOwnerQuery;
ilia@18415
    46
import org.netbeans.api.project.Project;
ilia@18415
    47
import org.netbeans.modules.analysis.spi.Analyzer;
ilia@18415
    48
import org.netbeans.modules.cnd.analysis.api.AnalyzerResponse;
ilia@18415
    49
import org.netbeans.modules.cnd.api.model.CsmFile;
ilia@18415
    50
import org.netbeans.modules.cnd.api.model.syntaxerr.CsmErrorInfo;
ilia@18415
    51
import org.netbeans.modules.cnd.api.model.syntaxerr.CsmErrorProvider;
ilia@18415
    52
import org.netbeans.modules.cnd.api.project.NativeFileItem;
ilia@18415
    53
import org.netbeans.modules.cnd.api.remote.RemoteProject;
ilia@18415
    54
import org.netbeans.modules.cnd.api.toolchain.CompilerSet;
ilia@18415
    55
import org.netbeans.modules.cnd.api.toolchain.PredefinedToolKind;
ilia@18415
    56
import org.netbeans.modules.cnd.api.toolchain.Tool;
ilia@18415
    57
import org.netbeans.modules.cnd.makeproject.api.configurations.ConfigurationDescriptorProvider;
ilia@18415
    58
import org.netbeans.modules.cnd.makeproject.api.configurations.Item;
ilia@18415
    59
import org.netbeans.modules.cnd.makeproject.api.configurations.MakeConfigurationDescriptor;
ilia@18415
    60
import org.netbeans.modules.cnd.spi.toolchain.ToolchainProject;
ilia@18415
    61
import org.netbeans.modules.cnd.utils.MIMENames;
ilia@18415
    62
import org.netbeans.modules.nativeexecution.api.ExecutionEnvironment;
ilia@18415
    63
import org.openide.filesystems.FileObject;
ilia@18415
    64
import org.openide.util.Lookup;
ilia@18415
    65
ilia@18415
    66
/**
ilia@18415
    67
 *
ilia@18415
    68
 * @author Ilia Gromov
ilia@18415
    69
 */
ilia@18415
    70
public class AnalyticsTools {
ilia@18415
    71
ilia@18415
    72
    public static ExecutionEnvironment detectEnvironment(Analyzer.Context context) {
ilia@18415
    73
        if (context.getScope() == null) {
ilia@18415
    74
            return null;
ilia@18415
    75
        }
ilia@18415
    76
        for (FileObject fo : context.getScope().getFiles()) {
ilia@18415
    77
            String mimeType = fo.getMIMEType();
ilia@18415
    78
            if (MIMENames.isHeaderOrCppOrC(mimeType)) {
ilia@18415
    79
                Project project = FileOwnerQuery.getOwner(fo);
ilia@18415
    80
                RemoteProject info = project.getLookup().lookup(RemoteProject.class);
ilia@18415
    81
                if (info != null) {
ilia@18415
    82
                    ExecutionEnvironment dh = info.getDevelopmentHost();
ilia@18415
    83
                    if (dh != null) {
ilia@18415
    84
                        return dh;
ilia@18415
    85
                    }
ilia@18415
    86
                }
ilia@18415
    87
            }
ilia@18415
    88
        }
ilia@18415
    89
        return null;
ilia@18415
    90
    }
ilia@18415
    91
ilia@18415
    92
    public static CompilerSet toolchain(Lookup.Provider project) {
ilia@18415
    93
        ToolchainProject toolchain = project.getLookup().lookup(ToolchainProject.class);
ilia@18415
    94
        if (toolchain != null) {
ilia@18415
    95
            return toolchain.getCompilerSet();
ilia@18415
    96
        }
ilia@18415
    97
        return null;
ilia@18415
    98
    }
ilia@18415
    99
ilia@18415
   100
    public static MakeConfigurationDescriptor getConfigurationDescriptor(CsmFile file, Lookup.Provider project) {
ilia@18415
   101
        if (file != null) {
ilia@18415
   102
            ConfigurationDescriptorProvider pdp = project.getLookup().lookup(ConfigurationDescriptorProvider.class);
ilia@18415
   103
            if (pdp != null) {
ilia@18415
   104
                MakeConfigurationDescriptor makeConfigurationDescriptor = pdp.getConfigurationDescriptor();
ilia@18415
   105
                if (makeConfigurationDescriptor != null) {
ilia@18415
   106
                    return makeConfigurationDescriptor;
ilia@18415
   107
                }
ilia@18415
   108
            }
ilia@18415
   109
        }
ilia@18415
   110
ilia@18415
   111
        return null;
ilia@18415
   112
    }
ilia@18415
   113
ilia@18415
   114
    public static Item findItem(CsmFile file, Lookup.Provider project) {
ilia@18415
   115
        return getConfigurationDescriptor(file, project).findProjectItemByPath(file.getAbsolutePath().toString());
ilia@18415
   116
    }
ilia@18415
   117
ilia@18415
   118
    public static void fatalError(AnalyzerResponse.AnalyzerSeverity severity, String id, String message, CsmFile file, CsmErrorProvider.Response response) {
ilia@18415
   119
        CsmErrorInfo fatal = new ModernizeErrorProvider.FatalErrorInfo(id, message);
ilia@18415
   120
        if (response instanceof AnalyzerResponse) {
ilia@18415
   121
            AnalyzerResponse ar = (AnalyzerResponse) response;
ilia@18415
   122
            ar.addError(severity, message, file.getFileObject(), fatal); //NOI18N
ilia@18415
   123
        } else {
ilia@18415
   124
            response.addError(fatal); //NOI18N
ilia@18415
   125
        }
ilia@18415
   126
    }
ilia@18415
   127
ilia@18415
   128
    public static String useTool(Item item, Lookup.Provider project) {
ilia@18415
   129
        ToolchainProject toolchain = project.getLookup().lookup(ToolchainProject.class);
ilia@18415
   130
        if (toolchain != null) {
ilia@18415
   131
            CompilerSet set = toolchain.getCompilerSet();
ilia@18415
   132
            if (set != null) {
ilia@18415
   133
                if (set.getCompilerFlavor().isSunStudioCompiler()) {
ilia@18415
   134
                    if (item.getLanguage() == NativeFileItem.Language.C) {
ilia@18415
   135
                        return set.findTool(PredefinedToolKind.CCompiler).getPath();
ilia@18415
   136
                    } else {
ilia@18415
   137
                        return set.findTool(PredefinedToolKind.CCCompiler).getPath();
ilia@18415
   138
                    }
ilia@18415
   139
                }
ilia@18415
   140
            }
ilia@18415
   141
        }
ilia@18415
   142
        return null;
ilia@18415
   143
    }
ilia@18415
   144
ilia@18415
   145
    public static Tool compiler(Item item, Lookup.Provider project) {
ilia@18415
   146
        CompilerSet set = toolchain(project);
ilia@18415
   147
        if (set != null) {
ilia@18415
   148
            if (item.getLanguage() == NativeFileItem.Language.C) {
ilia@18415
   149
                return set.findTool(PredefinedToolKind.CCompiler);
ilia@18415
   150
            } else {
ilia@18415
   151
                return set.findTool(PredefinedToolKind.CCCompiler);
ilia@18415
   152
            }
ilia@18415
   153
        }
ilia@18415
   154
        return null;
ilia@18415
   155
    }
ilia@18415
   156
ilia@18415
   157
    public static List<String> scanCommandLine(String line) {
ilia@18415
   158
        List<String> res = new ArrayList<String>();
ilia@18415
   159
        int i = 0;
ilia@18415
   160
        StringBuilder current = new StringBuilder();
ilia@18415
   161
        boolean isSingleQuoteMode = false;
ilia@18415
   162
        boolean isDoubleQuoteMode = false;
ilia@18415
   163
        while (i < line.length()) {
ilia@18415
   164
            char c = line.charAt(i);
ilia@18415
   165
            i++;
ilia@18415
   166
            switch (c) {
ilia@18415
   167
                case '\'': // NOI18N
ilia@18415
   168
                    if (isSingleQuoteMode) {
ilia@18415
   169
                        isSingleQuoteMode = false;
ilia@18415
   170
                    } else if (!isDoubleQuoteMode) {
ilia@18415
   171
                        isSingleQuoteMode = true;
ilia@18415
   172
                    }
ilia@18415
   173
                    current.append(c);
ilia@18415
   174
                    break;
ilia@18415
   175
                case '\"': // NOI18N
ilia@18415
   176
                    if (isDoubleQuoteMode) {
ilia@18415
   177
                        isDoubleQuoteMode = false;
ilia@18415
   178
                    } else if (!isSingleQuoteMode) {
ilia@18415
   179
                        isDoubleQuoteMode = true;
ilia@18415
   180
                    }
ilia@18415
   181
                    current.append(c);
ilia@18415
   182
                    break;
ilia@18415
   183
                case ' ': // NOI18N
ilia@18415
   184
                case '\t': // NOI18N
ilia@18415
   185
                case '\n': // NOI18N
ilia@18415
   186
                case '\r': // NOI18N
ilia@18415
   187
                    if (isSingleQuoteMode || isDoubleQuoteMode) {
ilia@18415
   188
                        current.append(c);
ilia@18415
   189
                        break;
ilia@18415
   190
                    } else {
ilia@18415
   191
                        if (current.length() > 0) {
ilia@18415
   192
                            res.add(current.toString());
ilia@18415
   193
                            current.setLength(0);
ilia@18415
   194
                        }
ilia@18415
   195
                    }
ilia@18415
   196
                    break;
ilia@18415
   197
                default:
ilia@18415
   198
                    current.append(c);
ilia@18415
   199
                    break;
ilia@18415
   200
            }
ilia@18415
   201
        }
ilia@18415
   202
        if (current.length() > 0) {
ilia@18415
   203
            res.add(current.toString());
ilia@18415
   204
        }
ilia@18415
   205
        return res;
ilia@18415
   206
    }
ilia@18415
   207
ilia@18415
   208
}