c.s.tools.ide.analysis.modernize/src/com/sun/tools/ide/analysis/modernize/utils/AnalyticsTools.java
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/c.s.tools.ide.analysis.modernize/src/com/sun/tools/ide/analysis/modernize/utils/AnalyticsTools.java Wed Jun 07 20:23:29 2017 +0300
1.3 @@ -0,0 +1,208 @@
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 com.sun.tools.ide.analysis.modernize.utils;
1.44 +
1.45 +import com.sun.tools.ide.analysis.modernize.impl.ModernizeErrorProvider;
1.46 +import java.util.ArrayList;
1.47 +import java.util.List;
1.48 +import org.netbeans.api.project.FileOwnerQuery;
1.49 +import org.netbeans.api.project.Project;
1.50 +import org.netbeans.modules.analysis.spi.Analyzer;
1.51 +import org.netbeans.modules.cnd.analysis.api.AnalyzerResponse;
1.52 +import org.netbeans.modules.cnd.api.model.CsmFile;
1.53 +import org.netbeans.modules.cnd.api.model.syntaxerr.CsmErrorInfo;
1.54 +import org.netbeans.modules.cnd.api.model.syntaxerr.CsmErrorProvider;
1.55 +import org.netbeans.modules.cnd.api.project.NativeFileItem;
1.56 +import org.netbeans.modules.cnd.api.remote.RemoteProject;
1.57 +import org.netbeans.modules.cnd.api.toolchain.CompilerSet;
1.58 +import org.netbeans.modules.cnd.api.toolchain.PredefinedToolKind;
1.59 +import org.netbeans.modules.cnd.api.toolchain.Tool;
1.60 +import org.netbeans.modules.cnd.makeproject.api.configurations.ConfigurationDescriptorProvider;
1.61 +import org.netbeans.modules.cnd.makeproject.api.configurations.Item;
1.62 +import org.netbeans.modules.cnd.makeproject.api.configurations.MakeConfigurationDescriptor;
1.63 +import org.netbeans.modules.cnd.spi.toolchain.ToolchainProject;
1.64 +import org.netbeans.modules.cnd.utils.MIMENames;
1.65 +import org.netbeans.modules.nativeexecution.api.ExecutionEnvironment;
1.66 +import org.openide.filesystems.FileObject;
1.67 +import org.openide.util.Lookup;
1.68 +
1.69 +/**
1.70 + *
1.71 + * @author Ilia Gromov
1.72 + */
1.73 +public class AnalyticsTools {
1.74 +
1.75 + public static ExecutionEnvironment detectEnvironment(Analyzer.Context context) {
1.76 + if (context.getScope() == null) {
1.77 + return null;
1.78 + }
1.79 + for (FileObject fo : context.getScope().getFiles()) {
1.80 + String mimeType = fo.getMIMEType();
1.81 + if (MIMENames.isHeaderOrCppOrC(mimeType)) {
1.82 + Project project = FileOwnerQuery.getOwner(fo);
1.83 + RemoteProject info = project.getLookup().lookup(RemoteProject.class);
1.84 + if (info != null) {
1.85 + ExecutionEnvironment dh = info.getDevelopmentHost();
1.86 + if (dh != null) {
1.87 + return dh;
1.88 + }
1.89 + }
1.90 + }
1.91 + }
1.92 + return null;
1.93 + }
1.94 +
1.95 + public static CompilerSet toolchain(Lookup.Provider project) {
1.96 + ToolchainProject toolchain = project.getLookup().lookup(ToolchainProject.class);
1.97 + if (toolchain != null) {
1.98 + return toolchain.getCompilerSet();
1.99 + }
1.100 + return null;
1.101 + }
1.102 +
1.103 + public static MakeConfigurationDescriptor getConfigurationDescriptor(CsmFile file, Lookup.Provider project) {
1.104 + if (file != null) {
1.105 + ConfigurationDescriptorProvider pdp = project.getLookup().lookup(ConfigurationDescriptorProvider.class);
1.106 + if (pdp != null) {
1.107 + MakeConfigurationDescriptor makeConfigurationDescriptor = pdp.getConfigurationDescriptor();
1.108 + if (makeConfigurationDescriptor != null) {
1.109 + return makeConfigurationDescriptor;
1.110 + }
1.111 + }
1.112 + }
1.113 +
1.114 + return null;
1.115 + }
1.116 +
1.117 + public static Item findItem(CsmFile file, Lookup.Provider project) {
1.118 + return getConfigurationDescriptor(file, project).findProjectItemByPath(file.getAbsolutePath().toString());
1.119 + }
1.120 +
1.121 + public static void fatalError(AnalyzerResponse.AnalyzerSeverity severity, String id, String message, CsmFile file, CsmErrorProvider.Response response) {
1.122 + CsmErrorInfo fatal = new ModernizeErrorProvider.FatalErrorInfo(id, message);
1.123 + if (response instanceof AnalyzerResponse) {
1.124 + AnalyzerResponse ar = (AnalyzerResponse) response;
1.125 + ar.addError(severity, message, file.getFileObject(), fatal); //NOI18N
1.126 + } else {
1.127 + response.addError(fatal); //NOI18N
1.128 + }
1.129 + }
1.130 +
1.131 + public static String useTool(Item item, Lookup.Provider project) {
1.132 + ToolchainProject toolchain = project.getLookup().lookup(ToolchainProject.class);
1.133 + if (toolchain != null) {
1.134 + CompilerSet set = toolchain.getCompilerSet();
1.135 + if (set != null) {
1.136 + if (set.getCompilerFlavor().isSunStudioCompiler()) {
1.137 + if (item.getLanguage() == NativeFileItem.Language.C) {
1.138 + return set.findTool(PredefinedToolKind.CCompiler).getPath();
1.139 + } else {
1.140 + return set.findTool(PredefinedToolKind.CCCompiler).getPath();
1.141 + }
1.142 + }
1.143 + }
1.144 + }
1.145 + return null;
1.146 + }
1.147 +
1.148 + public static Tool compiler(Item item, Lookup.Provider project) {
1.149 + CompilerSet set = toolchain(project);
1.150 + if (set != null) {
1.151 + if (item.getLanguage() == NativeFileItem.Language.C) {
1.152 + return set.findTool(PredefinedToolKind.CCompiler);
1.153 + } else {
1.154 + return set.findTool(PredefinedToolKind.CCCompiler);
1.155 + }
1.156 + }
1.157 + return null;
1.158 + }
1.159 +
1.160 + public static List<String> scanCommandLine(String line) {
1.161 + List<String> res = new ArrayList<String>();
1.162 + int i = 0;
1.163 + StringBuilder current = new StringBuilder();
1.164 + boolean isSingleQuoteMode = false;
1.165 + boolean isDoubleQuoteMode = false;
1.166 + while (i < line.length()) {
1.167 + char c = line.charAt(i);
1.168 + i++;
1.169 + switch (c) {
1.170 + case '\'': // NOI18N
1.171 + if (isSingleQuoteMode) {
1.172 + isSingleQuoteMode = false;
1.173 + } else if (!isDoubleQuoteMode) {
1.174 + isSingleQuoteMode = true;
1.175 + }
1.176 + current.append(c);
1.177 + break;
1.178 + case '\"': // NOI18N
1.179 + if (isDoubleQuoteMode) {
1.180 + isDoubleQuoteMode = false;
1.181 + } else if (!isSingleQuoteMode) {
1.182 + isDoubleQuoteMode = true;
1.183 + }
1.184 + current.append(c);
1.185 + break;
1.186 + case ' ': // NOI18N
1.187 + case '\t': // NOI18N
1.188 + case '\n': // NOI18N
1.189 + case '\r': // NOI18N
1.190 + if (isSingleQuoteMode || isDoubleQuoteMode) {
1.191 + current.append(c);
1.192 + break;
1.193 + } else {
1.194 + if (current.length() > 0) {
1.195 + res.add(current.toString());
1.196 + current.setLength(0);
1.197 + }
1.198 + }
1.199 + break;
1.200 + default:
1.201 + current.append(c);
1.202 + break;
1.203 + }
1.204 + }
1.205 + if (current.length() > 0) {
1.206 + res.add(current.toString());
1.207 + }
1.208 + return res;
1.209 + }
1.210 +
1.211 +}