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/impl/AnalyzerResponseMerger.java Thu Jun 15 13:26:38 2017 +0300
1.3 @@ -0,0 +1,105 @@
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):
1.42 + */
1.43 +package com.sun.tools.ide.analysis.modernize.impl;
1.44 +
1.45 +import com.sun.tools.ide.analysis.modernize.impl.ModernizeAnalyzerImpl.ModernizeResponse;
1.46 +import java.util.ArrayList;
1.47 +import java.util.HashMap;
1.48 +import java.util.List;
1.49 +import java.util.Map;
1.50 +import java.util.Optional;
1.51 +import java.util.stream.Collectors;
1.52 +import org.netbeans.modules.cnd.analysis.api.AnalyzerResponse;
1.53 +import org.netbeans.spi.editor.hints.ErrorDescription;
1.54 +import org.openide.filesystems.FileObject;
1.55 +
1.56 +/**
1.57 + *
1.58 + * @author Ilia Gromov
1.59 + */
1.60 +public class AnalyzerResponseMerger {
1.61 +
1.62 + private static class ErrorDesc {
1.63 +
1.64 + final ModernizeErrorInfo info;
1.65 + final FileObject fo;
1.66 +
1.67 + public ErrorDesc(ModernizeErrorInfo info, FileObject fo) {
1.68 + this.info = info;
1.69 + this.fo = fo;
1.70 + }
1.71 +
1.72 + public boolean isSame(ErrorDesc o2) {
1.73 + return isSame(this.info, o2.info) && this.fo.equals(o2.fo);
1.74 + }
1.75 +
1.76 + public boolean isSame(ModernizeErrorInfo o1, ModernizeErrorInfo o2) {
1.77 + return o1.getStartOffset() == o2.getStartOffset()
1.78 + && o1.getEndOffset() == o2.getEndOffset()
1.79 + && o1.getDiagnostics().getCheckName().equals(o2.getDiagnostics().getCheckName());
1.80 + }
1.81 + }
1.82 +
1.83 + private final List<ErrorDesc> errors = new ArrayList<>();
1.84 + private final ModernizeResponse delegate;
1.85 +
1.86 + public AnalyzerResponseMerger(ModernizeResponse delegate) {
1.87 + this.delegate = delegate;
1.88 + }
1.89 +
1.90 + public void addError(ModernizeErrorInfo info, FileObject fo) {
1.91 + ErrorDesc errorDesc = new ErrorDesc(info, fo);
1.92 + Optional<ErrorDesc> found = errors.stream()
1.93 + .filter(o1 -> o1.isSame(errorDesc))
1.94 + .findAny();
1.95 +
1.96 + if (found.isPresent()) {
1.97 + found.get().info.addMessageInfixes(errorDesc.info.getMessageInfixes());
1.98 + } else {
1.99 + errors.add(errorDesc);
1.100 + }
1.101 + }
1.102 +
1.103 + public List<ErrorDescription> done() {
1.104 + return errors.stream()
1.105 + .map(error -> delegate.addErrorImpl(error.info, error.fo))
1.106 + .collect(Collectors.toList());
1.107 + }
1.108 +}
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
2.2 +++ b/c.s.tools.ide.analysis.modernize/src/com/sun/tools/ide/analysis/modernize/impl/CsmResponseMerger.java Thu Jun 15 13:26:38 2017 +0300
2.3 @@ -0,0 +1,87 @@
2.4 +/*
2.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
2.6 + *
2.7 + * Copyright (c) 2017 Oracle and/or its affiliates. All rights reserved.
2.8 + *
2.9 + * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
2.10 + * Other names may be trademarks of their respective owners.
2.11 + *
2.12 + * The contents of this file are subject to the terms of either the GNU
2.13 + * General Public License Version 2 only ("GPL") or the Common
2.14 + * Development and Distribution License("CDDL") (collectively, the
2.15 + * "License"). You may not use this file except in compliance with the
2.16 + * License. You can obtain a copy of the License at
2.17 + * http://www.netbeans.org/cddl-gplv2.html
2.18 + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
2.19 + * specific language governing permissions and limitations under the
2.20 + * License. When distributing the software, include this License Header
2.21 + * Notice in each file and include the License file at
2.22 + * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
2.23 + * particular file as subject to the "Classpath" exception as provided
2.24 + * by Oracle in the GPL Version 2 section of the License file that
2.25 + * accompanied this code. If applicable, add the following below the
2.26 + * License Header, with the fields enclosed by brackets [] replaced by
2.27 + * your own identifying information:
2.28 + * "Portions Copyrighted [year] [name of copyright owner]"
2.29 + *
2.30 + * If you wish your version of this file to be governed by only the CDDL
2.31 + * or only the GPL Version 2, indicate your decision by adding
2.32 + * "[Contributor] elects to include this software in this distribution
2.33 + * under the [CDDL or GPL Version 2] license." If you do not indicate a
2.34 + * single choice of license, a recipient has the option to distribute
2.35 + * your version of this file under either the CDDL, the GPL Version 2 or
2.36 + * to extend the choice of license to its licensees as provided above.
2.37 + * However, if you add GPL Version 2 code and therefore, elected the GPL
2.38 + * Version 2 license, then the option applies only if the new code is
2.39 + * made subject to such option by the copyright holder.
2.40 + *
2.41 + * Contributor(s): Ilia Gromov
2.42 + */
2.43 +package com.sun.tools.ide.analysis.modernize.impl;
2.44 +
2.45 +import java.util.ArrayList;
2.46 +import java.util.List;
2.47 +import java.util.Optional;
2.48 +import org.netbeans.modules.cnd.api.model.syntaxerr.CsmErrorInfo;
2.49 +import org.netbeans.modules.cnd.api.model.syntaxerr.CsmErrorProvider;
2.50 +
2.51 +/**
2.52 + *
2.53 + * @author Ilia Gromov
2.54 + */
2.55 +public class CsmResponseMerger implements CsmErrorProvider.Response {
2.56 +
2.57 + private final List<ModernizeErrorInfo> bag = new ArrayList<>();
2.58 + private final CsmErrorProvider.Response delegate;
2.59 +
2.60 + public CsmResponseMerger(CsmErrorProvider.Response delegate) {
2.61 + this.delegate = delegate;
2.62 + }
2.63 +
2.64 + @Override
2.65 + public void addError(CsmErrorInfo errorInfo) {
2.66 + ModernizeErrorInfo o2 = (ModernizeErrorInfo) errorInfo;
2.67 + Optional<ModernizeErrorInfo> found = bag.stream()
2.68 + .filter(o1 -> isSame(o1, o2))
2.69 + .findAny();
2.70 +
2.71 + if (found.isPresent()) {
2.72 + found.get().addMessageInfixes(o2.getMessageInfixes());
2.73 + } else {
2.74 + bag.add(o2);
2.75 + }
2.76 + }
2.77 +
2.78 + @Override
2.79 + public void done() {
2.80 + bag.forEach(delegate::addError);
2.81 + delegate.done();
2.82 + bag.clear();
2.83 + }
2.84 +
2.85 + public boolean isSame(ModernizeErrorInfo o1, ModernizeErrorInfo o2) {
2.86 + return o1.getStartOffset() == o2.getStartOffset()
2.87 + && o1.getEndOffset() == o2.getEndOffset()
2.88 + && o1.getDiagnostics().getCheckName().equals(o2.getDiagnostics().getCheckName());
2.89 + }
2.90 +}
3.1 --- a/c.s.tools.ide.analysis.modernize/src/com/sun/tools/ide/analysis/modernize/impl/ModernizeAnalyzerImpl.java Thu Jun 15 10:19:33 2017 +0300
3.2 +++ b/c.s.tools.ide.analysis.modernize/src/com/sun/tools/ide/analysis/modernize/impl/ModernizeAnalyzerImpl.java Thu Jun 15 13:26:38 2017 +0300
3.3 @@ -73,6 +73,8 @@
3.4
3.5 private static final String PREFIX = "tidy-"; //NOI18N
3.6
3.7 + private ModernizeErrorProvider currentErrorProvider;
3.8 +
3.9 private ModernizeAnalyzerImpl(Context ctx) {
3.10 super(ctx);
3.11 }
3.12 @@ -82,32 +84,43 @@
3.13 return ModernizeErrorProvider.getInstance();
3.14 }
3.15
3.16 - @Override
3.17 + @Override
3.18 protected boolean isCompileUnitBased() {
3.19 return true;
3.20 }
3.21
3.22 @Override
3.23 + protected Collection<ErrorDescription> done() {
3.24 + if (currentErrorProvider != null) {
3.25 + Collection<ErrorDescription> results = currentErrorProvider.done();
3.26 + currentErrorProvider = null;
3.27 + return results;
3.28 + }
3.29 + return Collections.<ErrorDescription>emptyList();
3.30 + }
3.31 +
3.32 + @Override
3.33 protected Collection<? extends ErrorDescription> doRunImpl(final FileObject sr, final Context ctx, final CsmErrorProvider provider, final AtomicBoolean cancel) {
3.34 final CsmFile csmFile = CsmUtilities.getCsmFile(sr, false, false);
3.35 if (csmFile == null) {
3.36 return Collections.<ErrorDescription>emptyList();
3.37 }
3.38 CsmErrorProvider.Request request = new RequestImpl(csmFile, ctx, cancel);
3.39 - final ArrayList<ErrorDescription> res = new ArrayList<ErrorDescription>();
3.40 - CsmErrorProvider.Response response = new ResponseImpl(sr, res, cancel);
3.41 + final ArrayList<ErrorDescription> res = new ArrayList<>();
3.42 + CsmErrorProvider.Response response = new ModernizeResponse(sr, res, cancel);
3.43 + this.currentErrorProvider = (ModernizeErrorProvider) provider;
3.44 provider.getErrors(request, response);
3.45 return res;
3.46 }
3.47
3.48 - protected static class ResponseImpl extends AbstractResponse {
3.49 + protected static class ModernizeResponse extends AbstractResponse {
3.50
3.51 - public ResponseImpl(FileObject sr, ArrayList<ErrorDescription> res, AtomicBoolean cancel) {
3.52 + public ModernizeResponse(FileObject sr, ArrayList<ErrorDescription> res, AtomicBoolean cancel) {
3.53 super(sr, res, cancel);
3.54 }
3.55
3.56 @Override
3.57 - protected ErrorDescription addErrorImpl(CsmErrorInfo errorInfo, FileObject fo) {
3.58 + public ErrorDescription addErrorImpl(CsmErrorInfo errorInfo, FileObject fo) {
3.59 String messages[] = errorInfo.getMessage().split("\n"); //NOI18N
3.60 if (messages.length > 0) {
3.61 StringBuilder sb = new StringBuilder();
3.62 @@ -160,7 +173,7 @@
3.63
3.64 @Override
3.65 public Iterable<? extends WarningDescription> getWarnings() {
3.66 - List<WarningDescription> result = new ArrayList<WarningDescription>();
3.67 + List<WarningDescription> result = new ArrayList<>();
3.68 final ModernizeErrorProvider provider = ModernizeErrorProvider.getInstance();
3.69 for (CodeAudit audit : provider.getAudits()) {
3.70 result.add(WarningDescription.create(PREFIX + audit.getID(), audit.getName(), ModernizeErrorProvider.NAME, provider.getDisplayName()));
4.1 --- a/c.s.tools.ide.analysis.modernize/src/com/sun/tools/ide/analysis/modernize/impl/ModernizeErrorProvider.java Thu Jun 15 10:19:33 2017 +0300
4.2 +++ b/c.s.tools.ide.analysis.modernize/src/com/sun/tools/ide/analysis/modernize/impl/ModernizeErrorProvider.java Thu Jun 15 13:26:38 2017 +0300
4.3 @@ -39,7 +39,6 @@
4.4 */
4.5 package com.sun.tools.ide.analysis.modernize.impl;
4.6
4.7 -import com.sun.tools.ide.analysis.modernize.impl.ModernizeAnalyzerImpl.ResponseImpl;
4.8 import com.sun.tools.ide.analysis.modernize.impl.YamlParser.Replacement;
4.9 import com.sun.tools.ide.analysis.modernize.options.AnalyzerPreferences;
4.10 import com.sun.tools.ide.analysis.modernize.options.ClangAnalyzerOptions;
4.11 @@ -78,6 +77,7 @@
4.12 import org.netbeans.modules.nativeexecution.api.ExecutionEnvironment;
4.13 import org.netbeans.modules.nativeexecution.api.ExecutionEnvironmentFactory;
4.14 import org.netbeans.modules.nativeexecution.api.util.ConnectionManager;
4.15 +import org.netbeans.spi.editor.hints.ErrorDescription;
4.16 import org.netbeans.spi.editor.hints.Fix;
4.17 import org.openide.filesystems.FileObject;
4.18 import org.openide.filesystems.FileUtil;
4.19 @@ -95,8 +95,10 @@
4.20 public final class ModernizeErrorProvider extends CsmErrorProvider implements CodeAuditProvider, AbstractCustomizerProvider {
4.21
4.22 public static final Logger LOG = Logger.getLogger("ide.analysis.tidy"); //NOI18N
4.23 + public static final String NAME = "Modernize"; //NOI18N
4.24 +
4.25 private Collection<CodeAudit> audits;
4.26 - public static final String NAME = "Modernize"; //NOI18N
4.27 + private AnalyzerResponseMerger analyzerResponseMerger;
4.28
4.29 public static ModernizeErrorProvider getInstance() {
4.30 for (CsmErrorProvider provider : Lookup.getDefault().lookupAll(CsmErrorProvider.class)) {
4.31 @@ -187,7 +189,7 @@
4.32
4.33 public void analyze(ExecutionEnvironment execEnv, Item item, Lookup.Provider project, CsmErrorProvider.Request request, CsmErrorProvider.Response response) {
4.34 String binaryPath = ClangAnalyzerOptions.getClangAnalyzerPath();
4.35 - boolean isAnalyzer = response instanceof ModernizeAnalyzerImpl.ResponseImpl;
4.36 + boolean isAnalyzer = response instanceof ModernizeAnalyzerImpl.ModernizeResponse;
4.37 if (binaryPath == null) {
4.38 Level level = isAnalyzer ? Level.INFO : Level.FINE;
4.39 LOG.log(level, "clang-tidy needs to be installed as a plugin"); //NOI18N
4.40 @@ -196,6 +198,7 @@
4.41
4.42 if (isAnalyzer && isNewRun()) {
4.43 AnalyzedFiles.getDefault().clear();
4.44 + analyzerResponseMerger = new AnalyzerResponseMerger((ModernizeAnalyzerImpl.ModernizeResponse) response);
4.45 }
4.46
4.47 DiagnosticsTool diagnosticsTool = new DiagnosticsTool(execEnv, item, (MakeProject) project, binaryPath);
4.48 @@ -215,7 +218,7 @@
4.49 }
4.50
4.51 if (!isAnalyzer) {
4.52 - response = new ResponseMerger(response);
4.53 + response = new CsmResponseMerger(response);
4.54 }
4.55
4.56 for (CsmFile startFile : tu) {
4.57 @@ -234,9 +237,7 @@
4.58 response.done();
4.59 }
4.60
4.61 - } catch (ConnectionManager.CancellationException ex) {
4.62 - Exceptions.printStackTrace(ex);
4.63 - } catch (IOException ex) {
4.64 + } catch (ConnectionManager.CancellationException | IOException ex) {
4.65 Exceptions.printStackTrace(ex);
4.66 }
4.67 }
4.68 @@ -251,9 +252,13 @@
4.69 return false;
4.70 }
4.71
4.72 + public Collection<ErrorDescription> done() {
4.73 + return analyzerResponseMerger.done();
4.74 + }
4.75 +
4.76 public void postProcess(boolean isAnalyzer, CsmFile startFile, Lookup.Provider project, List<YamlParser.Diagnostics> results, CsmErrorProvider.Request request, CsmErrorProvider.Response response) {
4.77 CsmFile file = request.getFile();
4.78 - List<CsmFile> otherCsmFiles = new ArrayList<CsmFile>();
4.79 + List<CsmFile> otherCsmFiles = new ArrayList<>();
4.80
4.81 for (YamlParser.Diagnostics diag : results) {
4.82 // TODO: don't add "Configure Hint" fix multiple times for one line
4.83 @@ -272,7 +277,7 @@
4.84
4.85 if (isAnalyzer) {
4.86 // Add found errors for all files (can be other files from compileUnit)
4.87 - ((ResponseImpl) response).addError(AnalyzerResponse.AnalyzerSeverity.DetectedError, null, fo, info);
4.88 + analyzerResponseMerger.addError(info, fo);
4.89
4.90 if (!csmFile.equals(file)) {
4.91 // May be not header (e.g BBB.cc: AAA.cc -> (includes) BBB.cc -> ... )
5.1 --- a/c.s.tools.ide.analysis.modernize/src/com/sun/tools/ide/analysis/modernize/impl/ResponseMerger.java Thu Jun 15 10:19:33 2017 +0300
5.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
5.3 @@ -1,87 +0,0 @@
5.4 -/*
5.5 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
5.6 - *
5.7 - * Copyright (c) 2017 Oracle and/or its affiliates. All rights reserved.
5.8 - *
5.9 - * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
5.10 - * Other names may be trademarks of their respective owners.
5.11 - *
5.12 - * The contents of this file are subject to the terms of either the GNU
5.13 - * General Public License Version 2 only ("GPL") or the Common
5.14 - * Development and Distribution License("CDDL") (collectively, the
5.15 - * "License"). You may not use this file except in compliance with the
5.16 - * License. You can obtain a copy of the License at
5.17 - * http://www.netbeans.org/cddl-gplv2.html
5.18 - * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
5.19 - * specific language governing permissions and limitations under the
5.20 - * License. When distributing the software, include this License Header
5.21 - * Notice in each file and include the License file at
5.22 - * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
5.23 - * particular file as subject to the "Classpath" exception as provided
5.24 - * by Oracle in the GPL Version 2 section of the License file that
5.25 - * accompanied this code. If applicable, add the following below the
5.26 - * License Header, with the fields enclosed by brackets [] replaced by
5.27 - * your own identifying information:
5.28 - * "Portions Copyrighted [year] [name of copyright owner]"
5.29 - *
5.30 - * If you wish your version of this file to be governed by only the CDDL
5.31 - * or only the GPL Version 2, indicate your decision by adding
5.32 - * "[Contributor] elects to include this software in this distribution
5.33 - * under the [CDDL or GPL Version 2] license." If you do not indicate a
5.34 - * single choice of license, a recipient has the option to distribute
5.35 - * your version of this file under either the CDDL, the GPL Version 2 or
5.36 - * to extend the choice of license to its licensees as provided above.
5.37 - * However, if you add GPL Version 2 code and therefore, elected the GPL
5.38 - * Version 2 license, then the option applies only if the new code is
5.39 - * made subject to such option by the copyright holder.
5.40 - *
5.41 - * Contributor(s): Ilia Gromov
5.42 - */
5.43 -package com.sun.tools.ide.analysis.modernize.impl;
5.44 -
5.45 -import java.util.ArrayList;
5.46 -import java.util.List;
5.47 -import java.util.Optional;
5.48 -import org.netbeans.modules.cnd.api.model.syntaxerr.CsmErrorInfo;
5.49 -import org.netbeans.modules.cnd.api.model.syntaxerr.CsmErrorProvider;
5.50 -
5.51 -/**
5.52 - *
5.53 - * @author Ilia Gromov
5.54 - */
5.55 -public class ResponseMerger implements CsmErrorProvider.Response {
5.56 -
5.57 - private final List<ModernizeErrorInfo> bag = new ArrayList<>();
5.58 - private final CsmErrorProvider.Response delegate;
5.59 -
5.60 - public ResponseMerger(CsmErrorProvider.Response delegate) {
5.61 - this.delegate = delegate;
5.62 - }
5.63 -
5.64 - @Override
5.65 - public void addError(CsmErrorInfo errorInfo) {
5.66 - ModernizeErrorInfo o2 = (ModernizeErrorInfo) errorInfo;
5.67 - Optional<ModernizeErrorInfo> found = bag.stream()
5.68 - .filter(o1 -> isSame(o1, o2))
5.69 - .findAny();
5.70 -
5.71 - if (found.isPresent()) {
5.72 - found.get().addMessageInfixes(o2.getMessageInfixes());
5.73 - } else {
5.74 - bag.add(o2);
5.75 - }
5.76 - }
5.77 -
5.78 - @Override
5.79 - public void done() {
5.80 - bag.forEach(delegate::addError);
5.81 - delegate.done();
5.82 - bag.clear();
5.83 - }
5.84 -
5.85 - public boolean isSame(ModernizeErrorInfo o1, ModernizeErrorInfo o2) {
5.86 - return o1.getStartOffset() == o2.getStartOffset()
5.87 - && o1.getEndOffset() == o2.getEndOffset()
5.88 - && o1.getDiagnostics().getCheckName().equals(o2.getDiagnostics().getCheckName());
5.89 - }
5.90 -}