# HG changeset patch # User Ilia Gromov # Date 1497522398 -10800 # Node ID 4b288c339c5581af9d4269fd58dc8c0552a0ad56 # Parent 54c19122b90f9315083a84ab466e64891f3d2ce6 [clang-tidy] merge analyser errors (only DEV compatible) (transplanted from 853976f2c6166dbb19b482e2247ec824b7183371) diff -r 54c19122b90f -r 4b288c339c55 c.s.tools.ide.analysis.modernize/src/com/sun/tools/ide/analysis/modernize/impl/AnalyzerResponseMerger.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ 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 @@ -0,0 +1,105 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright (c) 2017 Oracle and/or its affiliates. All rights reserved. + * + * Oracle and Java are registered trademarks of Oracle and/or its affiliates. + * Other names may be trademarks of their respective owners. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common + * Development and Distribution License("CDDL") (collectively, the + * "License"). You may not use this file except in compliance with the + * License. You can obtain a copy of the License at + * http://www.netbeans.org/cddl-gplv2.html + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the + * specific language governing permissions and limitations under the + * License. When distributing the software, include this License Header + * Notice in each file and include the License file at + * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the + * License Header, with the fields enclosed by brackets [] replaced by + * your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * If you wish your version of this file to be governed by only the CDDL + * or only the GPL Version 2, indicate your decision by adding + * "[Contributor] elects to include this software in this distribution + * under the [CDDL or GPL Version 2] license." If you do not indicate a + * single choice of license, a recipient has the option to distribute + * your version of this file under either the CDDL, the GPL Version 2 or + * to extend the choice of license to its licensees as provided above. + * However, if you add GPL Version 2 code and therefore, elected the GPL + * Version 2 license, then the option applies only if the new code is + * made subject to such option by the copyright holder. + * + * Contributor(s): + */ +package com.sun.tools.ide.analysis.modernize.impl; + +import com.sun.tools.ide.analysis.modernize.impl.ModernizeAnalyzerImpl.ModernizeResponse; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.stream.Collectors; +import org.netbeans.modules.cnd.analysis.api.AnalyzerResponse; +import org.netbeans.spi.editor.hints.ErrorDescription; +import org.openide.filesystems.FileObject; + +/** + * + * @author Ilia Gromov + */ +public class AnalyzerResponseMerger { + + private static class ErrorDesc { + + final ModernizeErrorInfo info; + final FileObject fo; + + public ErrorDesc(ModernizeErrorInfo info, FileObject fo) { + this.info = info; + this.fo = fo; + } + + public boolean isSame(ErrorDesc o2) { + return isSame(this.info, o2.info) && this.fo.equals(o2.fo); + } + + public boolean isSame(ModernizeErrorInfo o1, ModernizeErrorInfo o2) { + return o1.getStartOffset() == o2.getStartOffset() + && o1.getEndOffset() == o2.getEndOffset() + && o1.getDiagnostics().getCheckName().equals(o2.getDiagnostics().getCheckName()); + } + } + + private final List errors = new ArrayList<>(); + private final ModernizeResponse delegate; + + public AnalyzerResponseMerger(ModernizeResponse delegate) { + this.delegate = delegate; + } + + public void addError(ModernizeErrorInfo info, FileObject fo) { + ErrorDesc errorDesc = new ErrorDesc(info, fo); + Optional found = errors.stream() + .filter(o1 -> o1.isSame(errorDesc)) + .findAny(); + + if (found.isPresent()) { + found.get().info.addMessageInfixes(errorDesc.info.getMessageInfixes()); + } else { + errors.add(errorDesc); + } + } + + public List done() { + return errors.stream() + .map(error -> delegate.addErrorImpl(error.info, error.fo)) + .collect(Collectors.toList()); + } +} diff -r 54c19122b90f -r 4b288c339c55 c.s.tools.ide.analysis.modernize/src/com/sun/tools/ide/analysis/modernize/impl/CsmResponseMerger.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ 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 @@ -0,0 +1,87 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright (c) 2017 Oracle and/or its affiliates. All rights reserved. + * + * Oracle and Java are registered trademarks of Oracle and/or its affiliates. + * Other names may be trademarks of their respective owners. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common + * Development and Distribution License("CDDL") (collectively, the + * "License"). You may not use this file except in compliance with the + * License. You can obtain a copy of the License at + * http://www.netbeans.org/cddl-gplv2.html + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the + * specific language governing permissions and limitations under the + * License. When distributing the software, include this License Header + * Notice in each file and include the License file at + * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the + * License Header, with the fields enclosed by brackets [] replaced by + * your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * If you wish your version of this file to be governed by only the CDDL + * or only the GPL Version 2, indicate your decision by adding + * "[Contributor] elects to include this software in this distribution + * under the [CDDL or GPL Version 2] license." If you do not indicate a + * single choice of license, a recipient has the option to distribute + * your version of this file under either the CDDL, the GPL Version 2 or + * to extend the choice of license to its licensees as provided above. + * However, if you add GPL Version 2 code and therefore, elected the GPL + * Version 2 license, then the option applies only if the new code is + * made subject to such option by the copyright holder. + * + * Contributor(s): Ilia Gromov + */ +package com.sun.tools.ide.analysis.modernize.impl; + +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; +import org.netbeans.modules.cnd.api.model.syntaxerr.CsmErrorInfo; +import org.netbeans.modules.cnd.api.model.syntaxerr.CsmErrorProvider; + +/** + * + * @author Ilia Gromov + */ +public class CsmResponseMerger implements CsmErrorProvider.Response { + + private final List bag = new ArrayList<>(); + private final CsmErrorProvider.Response delegate; + + public CsmResponseMerger(CsmErrorProvider.Response delegate) { + this.delegate = delegate; + } + + @Override + public void addError(CsmErrorInfo errorInfo) { + ModernizeErrorInfo o2 = (ModernizeErrorInfo) errorInfo; + Optional found = bag.stream() + .filter(o1 -> isSame(o1, o2)) + .findAny(); + + if (found.isPresent()) { + found.get().addMessageInfixes(o2.getMessageInfixes()); + } else { + bag.add(o2); + } + } + + @Override + public void done() { + bag.forEach(delegate::addError); + delegate.done(); + bag.clear(); + } + + public boolean isSame(ModernizeErrorInfo o1, ModernizeErrorInfo o2) { + return o1.getStartOffset() == o2.getStartOffset() + && o1.getEndOffset() == o2.getEndOffset() + && o1.getDiagnostics().getCheckName().equals(o2.getDiagnostics().getCheckName()); + } +} diff -r 54c19122b90f -r 4b288c339c55 c.s.tools.ide.analysis.modernize/src/com/sun/tools/ide/analysis/modernize/impl/ModernizeAnalyzerImpl.java --- 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 +++ 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 @@ -73,6 +73,8 @@ private static final String PREFIX = "tidy-"; //NOI18N + private ModernizeErrorProvider currentErrorProvider; + private ModernizeAnalyzerImpl(Context ctx) { super(ctx); } @@ -82,32 +84,43 @@ return ModernizeErrorProvider.getInstance(); } - @Override + @Override protected boolean isCompileUnitBased() { return true; } @Override + protected Collection done() { + if (currentErrorProvider != null) { + Collection results = currentErrorProvider.done(); + currentErrorProvider = null; + return results; + } + return Collections.emptyList(); + } + + @Override protected Collection doRunImpl(final FileObject sr, final Context ctx, final CsmErrorProvider provider, final AtomicBoolean cancel) { final CsmFile csmFile = CsmUtilities.getCsmFile(sr, false, false); if (csmFile == null) { return Collections.emptyList(); } CsmErrorProvider.Request request = new RequestImpl(csmFile, ctx, cancel); - final ArrayList res = new ArrayList(); - CsmErrorProvider.Response response = new ResponseImpl(sr, res, cancel); + final ArrayList res = new ArrayList<>(); + CsmErrorProvider.Response response = new ModernizeResponse(sr, res, cancel); + this.currentErrorProvider = (ModernizeErrorProvider) provider; provider.getErrors(request, response); return res; } - protected static class ResponseImpl extends AbstractResponse { + protected static class ModernizeResponse extends AbstractResponse { - public ResponseImpl(FileObject sr, ArrayList res, AtomicBoolean cancel) { + public ModernizeResponse(FileObject sr, ArrayList res, AtomicBoolean cancel) { super(sr, res, cancel); } @Override - protected ErrorDescription addErrorImpl(CsmErrorInfo errorInfo, FileObject fo) { + public ErrorDescription addErrorImpl(CsmErrorInfo errorInfo, FileObject fo) { String messages[] = errorInfo.getMessage().split("\n"); //NOI18N if (messages.length > 0) { StringBuilder sb = new StringBuilder(); @@ -160,7 +173,7 @@ @Override public Iterable getWarnings() { - List result = new ArrayList(); + List result = new ArrayList<>(); final ModernizeErrorProvider provider = ModernizeErrorProvider.getInstance(); for (CodeAudit audit : provider.getAudits()) { result.add(WarningDescription.create(PREFIX + audit.getID(), audit.getName(), ModernizeErrorProvider.NAME, provider.getDisplayName())); diff -r 54c19122b90f -r 4b288c339c55 c.s.tools.ide.analysis.modernize/src/com/sun/tools/ide/analysis/modernize/impl/ModernizeErrorProvider.java --- 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 +++ 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 @@ -39,7 +39,6 @@ */ package com.sun.tools.ide.analysis.modernize.impl; -import com.sun.tools.ide.analysis.modernize.impl.ModernizeAnalyzerImpl.ResponseImpl; import com.sun.tools.ide.analysis.modernize.impl.YamlParser.Replacement; import com.sun.tools.ide.analysis.modernize.options.AnalyzerPreferences; import com.sun.tools.ide.analysis.modernize.options.ClangAnalyzerOptions; @@ -78,6 +77,7 @@ import org.netbeans.modules.nativeexecution.api.ExecutionEnvironment; import org.netbeans.modules.nativeexecution.api.ExecutionEnvironmentFactory; import org.netbeans.modules.nativeexecution.api.util.ConnectionManager; +import org.netbeans.spi.editor.hints.ErrorDescription; import org.netbeans.spi.editor.hints.Fix; import org.openide.filesystems.FileObject; import org.openide.filesystems.FileUtil; @@ -95,8 +95,10 @@ public final class ModernizeErrorProvider extends CsmErrorProvider implements CodeAuditProvider, AbstractCustomizerProvider { public static final Logger LOG = Logger.getLogger("ide.analysis.tidy"); //NOI18N + public static final String NAME = "Modernize"; //NOI18N + private Collection audits; - public static final String NAME = "Modernize"; //NOI18N + private AnalyzerResponseMerger analyzerResponseMerger; public static ModernizeErrorProvider getInstance() { for (CsmErrorProvider provider : Lookup.getDefault().lookupAll(CsmErrorProvider.class)) { @@ -187,7 +189,7 @@ public void analyze(ExecutionEnvironment execEnv, Item item, Lookup.Provider project, CsmErrorProvider.Request request, CsmErrorProvider.Response response) { String binaryPath = ClangAnalyzerOptions.getClangAnalyzerPath(); - boolean isAnalyzer = response instanceof ModernizeAnalyzerImpl.ResponseImpl; + boolean isAnalyzer = response instanceof ModernizeAnalyzerImpl.ModernizeResponse; if (binaryPath == null) { Level level = isAnalyzer ? Level.INFO : Level.FINE; LOG.log(level, "clang-tidy needs to be installed as a plugin"); //NOI18N @@ -196,6 +198,7 @@ if (isAnalyzer && isNewRun()) { AnalyzedFiles.getDefault().clear(); + analyzerResponseMerger = new AnalyzerResponseMerger((ModernizeAnalyzerImpl.ModernizeResponse) response); } DiagnosticsTool diagnosticsTool = new DiagnosticsTool(execEnv, item, (MakeProject) project, binaryPath); @@ -215,7 +218,7 @@ } if (!isAnalyzer) { - response = new ResponseMerger(response); + response = new CsmResponseMerger(response); } for (CsmFile startFile : tu) { @@ -234,9 +237,7 @@ response.done(); } - } catch (ConnectionManager.CancellationException ex) { - Exceptions.printStackTrace(ex); - } catch (IOException ex) { + } catch (ConnectionManager.CancellationException | IOException ex) { Exceptions.printStackTrace(ex); } } @@ -251,9 +252,13 @@ return false; } + public Collection done() { + return analyzerResponseMerger.done(); + } + public void postProcess(boolean isAnalyzer, CsmFile startFile, Lookup.Provider project, List results, CsmErrorProvider.Request request, CsmErrorProvider.Response response) { CsmFile file = request.getFile(); - List otherCsmFiles = new ArrayList(); + List otherCsmFiles = new ArrayList<>(); for (YamlParser.Diagnostics diag : results) { // TODO: don't add "Configure Hint" fix multiple times for one line @@ -272,7 +277,7 @@ if (isAnalyzer) { // Add found errors for all files (can be other files from compileUnit) - ((ResponseImpl) response).addError(AnalyzerResponse.AnalyzerSeverity.DetectedError, null, fo, info); + analyzerResponseMerger.addError(info, fo); if (!csmFile.equals(file)) { // May be not header (e.g BBB.cc: AAA.cc -> (includes) BBB.cc -> ... ) diff -r 54c19122b90f -r 4b288c339c55 c.s.tools.ide.analysis.modernize/src/com/sun/tools/ide/analysis/modernize/impl/ResponseMerger.java --- 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 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,87 +0,0 @@ -/* - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. - * - * Copyright (c) 2017 Oracle and/or its affiliates. All rights reserved. - * - * Oracle and Java are registered trademarks of Oracle and/or its affiliates. - * Other names may be trademarks of their respective owners. - * - * The contents of this file are subject to the terms of either the GNU - * General Public License Version 2 only ("GPL") or the Common - * Development and Distribution License("CDDL") (collectively, the - * "License"). You may not use this file except in compliance with the - * License. You can obtain a copy of the License at - * http://www.netbeans.org/cddl-gplv2.html - * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the - * specific language governing permissions and limitations under the - * License. When distributing the software, include this License Header - * Notice in each file and include the License file at - * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this - * particular file as subject to the "Classpath" exception as provided - * by Oracle in the GPL Version 2 section of the License file that - * accompanied this code. If applicable, add the following below the - * License Header, with the fields enclosed by brackets [] replaced by - * your own identifying information: - * "Portions Copyrighted [year] [name of copyright owner]" - * - * If you wish your version of this file to be governed by only the CDDL - * or only the GPL Version 2, indicate your decision by adding - * "[Contributor] elects to include this software in this distribution - * under the [CDDL or GPL Version 2] license." If you do not indicate a - * single choice of license, a recipient has the option to distribute - * your version of this file under either the CDDL, the GPL Version 2 or - * to extend the choice of license to its licensees as provided above. - * However, if you add GPL Version 2 code and therefore, elected the GPL - * Version 2 license, then the option applies only if the new code is - * made subject to such option by the copyright holder. - * - * Contributor(s): Ilia Gromov - */ -package com.sun.tools.ide.analysis.modernize.impl; - -import java.util.ArrayList; -import java.util.List; -import java.util.Optional; -import org.netbeans.modules.cnd.api.model.syntaxerr.CsmErrorInfo; -import org.netbeans.modules.cnd.api.model.syntaxerr.CsmErrorProvider; - -/** - * - * @author Ilia Gromov - */ -public class ResponseMerger implements CsmErrorProvider.Response { - - private final List bag = new ArrayList<>(); - private final CsmErrorProvider.Response delegate; - - public ResponseMerger(CsmErrorProvider.Response delegate) { - this.delegate = delegate; - } - - @Override - public void addError(CsmErrorInfo errorInfo) { - ModernizeErrorInfo o2 = (ModernizeErrorInfo) errorInfo; - Optional found = bag.stream() - .filter(o1 -> isSame(o1, o2)) - .findAny(); - - if (found.isPresent()) { - found.get().addMessageInfixes(o2.getMessageInfixes()); - } else { - bag.add(o2); - } - } - - @Override - public void done() { - bag.forEach(delegate::addError); - delegate.done(); - bag.clear(); - } - - public boolean isSame(ModernizeErrorInfo o1, ModernizeErrorInfo o2) { - return o1.getStartOffset() == o2.getStartOffset() - && o1.getEndOffset() == o2.getEndOffset() - && o1.getDiagnostics().getCheckName().equals(o2.getDiagnostics().getCheckName()); - } -}