c.s.tools.ide.analysis.modernize/src/com/sun/tools/ide/analysis/modernize/impl/ModernizeAnalyzerImpl.java
author Ilia Gromov <ilia@netbeans.org>
Wed, 07 Jun 2017 20:23:29 +0300
branchrelease82
changeset 18423 b9d9af239a0c
child 18417 853976f2c616
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.options.ClangAnalyzerOptions;
    43 import com.sun.tools.ide.analysis.modernize.resources.BundleUtilities;
    44 import java.io.CharConversionException;
    45 import java.io.File;
    46 import java.util.ArrayList;
    47 import java.util.Arrays;
    48 import java.util.Collection;
    49 import java.util.Collections;
    50 import java.util.List;
    51 import java.util.concurrent.atomic.AtomicBoolean;
    52 import java.util.prefs.Preferences;
    53 import org.netbeans.modules.analysis.spi.Analyzer;
    54 import org.netbeans.modules.cnd.analysis.api.AbstractAnalyzer;
    55 import org.netbeans.modules.cnd.analysis.api.AbstractHintsPanel;
    56 import org.netbeans.modules.cnd.api.model.CsmFile;
    57 import org.netbeans.modules.cnd.api.model.syntaxerr.CodeAudit;
    58 import org.netbeans.modules.cnd.api.model.syntaxerr.CsmErrorInfo;
    59 import org.netbeans.modules.cnd.api.model.syntaxerr.CsmErrorProvider;
    60 import org.netbeans.modules.cnd.modelutil.CsmUtilities;
    61 import org.netbeans.modules.cnd.utils.CndPathUtilities;
    62 import org.netbeans.spi.editor.hints.ErrorDescription;
    63 import org.netbeans.spi.editor.hints.ErrorDescriptionFactory;
    64 import org.netbeans.spi.editor.hints.LazyFixList;
    65 import org.netbeans.spi.editor.hints.Severity;
    66 import org.openide.filesystems.FileObject;
    67 import org.openide.util.ImageUtilities;
    68 import org.openide.util.NbBundle;
    69 import org.openide.util.lookup.ServiceProvider;
    70 import org.openide.xml.XMLUtil;
    71 
    72 public class ModernizeAnalyzerImpl extends AbstractAnalyzer {
    73 
    74     private static final String PREFIX = "tidy-"; //NOI18N
    75 
    76     private ModernizeAnalyzerImpl(Context ctx) {
    77         super(ctx);
    78     }
    79 
    80     @Override
    81     public ModernizeErrorProvider getErrorProvider(Preferences preferences) {
    82         return ModernizeErrorProvider.getInstance();
    83     }
    84 
    85     @Override 
    86     protected boolean isCompileUnitBased() {
    87         return true;
    88     }
    89 
    90     @Override
    91     protected Collection<? extends ErrorDescription> doRunImpl(final FileObject sr, final Context ctx, final CsmErrorProvider provider, final AtomicBoolean cancel) {
    92         final CsmFile csmFile = CsmUtilities.getCsmFile(sr, false, false);
    93         if (csmFile == null) {
    94             return Collections.<ErrorDescription>emptyList();
    95         }
    96         CsmErrorProvider.Request request = new RequestImpl(csmFile, ctx, cancel);
    97         final ArrayList<ErrorDescription> res = new ArrayList<ErrorDescription>();
    98         CsmErrorProvider.Response response = new ResponseImpl(sr, res, cancel);
    99         provider.getErrors(request, response);
   100         return res;
   101     }
   102 
   103     protected static class ResponseImpl extends AbstractResponse {
   104 
   105         public ResponseImpl(FileObject sr, ArrayList<ErrorDescription> res, AtomicBoolean cancel) {
   106             super(sr, res, cancel);
   107         }
   108 
   109         @Override
   110         protected ErrorDescription addErrorImpl(CsmErrorInfo errorInfo, FileObject fo) {
   111             String messages[] = errorInfo.getMessage().split("\n"); //NOI18N
   112             if (messages.length > 0) {
   113                 StringBuilder sb = new StringBuilder();
   114                 if (errorInfo instanceof ModernizeErrorInfo) {
   115                     ModernizeErrorInfo mei = (ModernizeErrorInfo) errorInfo;
   116                     sb.append("<pre>"); //NOI18N
   117                     sb.append("["); //NOI18N
   118                     sb.append(mei.getId());
   119                     sb.append("]"); //NOI18N
   120                     sb.append("\n"); //NOI18N
   121                     List<YamlParser.Replacement> replacements = mei.getDiagnostics().getReplacements();
   122                     if (replacements.isEmpty()) {
   123                         sb.append(CndPathUtilities.getRelativePath(mei.getProject().getProjectRoot(), mei.getDiagnostics().getMessageFilePath())).append(" "); //NOI18N
   124                         sb.append(mei.getDiagnostics().getMessageFileOffset()).append("\n");
   125                     } else {
   126                         for (YamlParser.Replacement replacement : replacements) {
   127                             sb.append("\n"); //NOI18N
   128                             sb.append("Replacement: "); //NOI18N
   129                             sb.append(CndPathUtilities.getRelativePath(mei.getProject().getProjectRoot(), replacement.filePath)).append(" "); //NOI18N
   130                             sb.append(replacement.offset).append("-").append(replacement.offset + replacement.length).append("\n"); //NOI18N
   131                             String attributeValue = replacement.replacementText;
   132                             try {
   133                                 attributeValue = XMLUtil.toAttributeValue(replacement.replacementText);
   134                             } catch (CharConversionException ex) {
   135                             }
   136                             sb.append(attributeValue).append("\n"); //NOI18N
   137                         }
   138                     }
   139                     sb.append(mei.getMessage());
   140                     sb.append("</pre>"); //NOI18N
   141 
   142                     LazyFixList list = new LazyFixListImpl();
   143 
   144                     return ErrorDescriptionFactory.createErrorDescription(PREFIX + mei.getId(), Severity.valueOf(mei.getSeverity().toString().toUpperCase()),
   145                             messages[0], sb.toString(), list, fo, errorInfo.getStartOffset(), errorInfo.getStartOffset());
   146                 }
   147             }
   148             return null;
   149         }
   150     }
   151 
   152     @ServiceProvider(service = AnalyzerFactory.class)
   153     public static final class AnalyzerFactoryImpl extends AnalyzerFactory {
   154 
   155         public AnalyzerFactoryImpl() {
   156             super(ModernizeErrorProvider.NAME,
   157                     NbBundle.getMessage(ModernizeErrorProvider.class, "Modernize_NAME"), //NOI18N
   158                     ImageUtilities.loadImage("com/sun/tools/ide/analysis/modernize/impl/bugs.png")); //NOI18N
   159         }
   160 
   161         @Override
   162         public Iterable<? extends WarningDescription> getWarnings() {
   163             List<WarningDescription> result = new ArrayList<WarningDescription>();
   164             final ModernizeErrorProvider provider = ModernizeErrorProvider.getInstance();
   165             for (CodeAudit audit : provider.getAudits()) {
   166                 result.add(WarningDescription.create(PREFIX + audit.getID(), audit.getName(), ModernizeErrorProvider.NAME, provider.getDisplayName()));
   167             }
   168             String[] fatals = BundleUtilities.getFatalErrors();
   169             for (String id : fatals) { //NOI18N
   170                 if (!id.isEmpty()) {
   171                     result.add(WarningDescription.create(PREFIX + id, BundleUtilities.getDescription(id), ModernizeErrorProvider.NAME, provider.getDisplayName()));
   172                 }
   173             }
   174             return result;
   175         }
   176 
   177         @Override
   178         public CustomizerProvider<Void, AbstractHintsPanel> getCustomizerProvider() {
   179             return new CustomizerProvider<Void, AbstractHintsPanel>() {
   180 
   181                 @Override
   182                 public Void initialize() {
   183                     return null;
   184                 }
   185 
   186                 @Override
   187                 public AbstractHintsPanel createComponent(CustomizerContext<Void, AbstractHintsPanel> context) {
   188                     return AbstractAnalyzer.createComponent(ModernizeErrorProvider.getInstance());
   189                 }
   190             };
   191         }
   192 
   193         @Override
   194         public Collection<? extends MissingPlugin> requiredPlugins(Context context) {
   195 //            ExecutionEnvironment env = detectEnvironment(context);
   196 //            if (env == null) {
   197 //                env = ExecutionEnvironmentFactory.getLocal();
   198 //            }
   199             String installedTool = ClangAnalyzerOptions.getClangAnalyzerPath();
   200             if (installedTool == null || !new File(installedTool).exists()) {
   201                 String codeBase = ClangAnalyzerOptions.getMissingModuleName();
   202                 if (codeBase != null) {
   203                     return Arrays.asList(new MissingPlugin(codeBase, NbBundle.getMessage(ModernizeAnalyzerImpl.class, "Modernize_NAME"))); //NOI18N
   204                 }
   205             }
   206             return Collections.emptyList();
   207         }
   208 
   209         @Override
   210         public Analyzer createAnalyzer(Context context) {
   211             return new ModernizeAnalyzerImpl(context);
   212         }
   213     }
   214 }