You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@heron.apache.org by GitBox <gi...@apache.org> on 2019/02/15 01:14:51 UTC

[GitHub] nwangtw commented on a change in pull request #3194: Add cppchecker static check

nwangtw commented on a change in pull request #3194: Add cppchecker static check
URL: https://github.com/apache/incubator-heron/pull/3194#discussion_r257074916
 
 

 ##########
 File path: tools/java/src/org/apache/bazel/cppcheck/CppCheck.java
 ##########
 @@ -0,0 +1,143 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.bazel.cppcheck;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+import java.util.logging.Logger;
+
+import com.google.common.base.Predicates;
+import com.google.common.collect.Collections2;
+import com.google.devtools.build.lib.actions.extra.CppCompileInfo;
+import com.google.devtools.build.lib.actions.extra.ExtraActionInfo;
+
+import org.apache.bazel.checkstyle.ExtraActionUtils;
+import org.apache.commons.cli.CommandLine;
+import org.apache.commons.cli.CommandLineParser;
+import org.apache.commons.cli.DefaultParser;
+import org.apache.commons.cli.HelpFormatter;
+import org.apache.commons.cli.Option;
+import org.apache.commons.cli.Options;
+import org.apache.commons.cli.ParseException;
+
+/**
+ * Runs cppcheck static analysis on cpp files
+ */
+public final class CppCheck {
+  public static final Logger LOG = Logger.getLogger(CppCheck.class.getName());
+  private static final String CLASSNAME = CppCheck.class.getCanonicalName();
+
+  private CppCheck() {
+  }
+
+  public static void main(String[] args) throws IOException {
+    CommandLineParser parser = new DefaultParser();
+
+    // create the Options
+    Options options = new Options();
+    options.addOption(Option.builder("f")
+            .required(true).hasArg()
+            .longOpt("extra_action_file")
+            .desc("bazel extra action protobuf file")
+            .build());
+    options.addOption(Option.builder("c")
+            .required(true).hasArg()
+            .longOpt("cppcheck_file")
+            .desc("Executable cppcheck file to invoke")
+            .build());
+
+    try {
+      // parse the command line arguments
+      CommandLine line = parser.parse(options, args);
+
+      String extraActionFile = line.getOptionValue("f");
+      String cppcheckFile = line.getOptionValue("c");
+
+      Collection<String> sourceFiles = getSourceFiles(extraActionFile);
+      if (sourceFiles.size() == 0) {
+        LOG.fine("No cpp files found by checkstyle");
+        return;
+      }
+
+      LOG.fine(sourceFiles.size() + " cpp files found by checkstyle");
+
+      // Create and run the command
+      List<String> commandBuilder = new ArrayList<>();
+      commandBuilder.add(cppcheckFile);
+      commandBuilder.add("--std=c++11");
+      commandBuilder.add("--language=c++");
+      commandBuilder.add("--error-exitcode=1"); // exit with 1 on error
+      commandBuilder.add("--library=googletest"); // use googletest cfg so that TEST_F is not considered syntax error
+      commandBuilder.addAll(sourceFiles);
+      runChecker(commandBuilder);
+
+    } catch (ParseException exp) {
+      LOG.severe(String.format("Invalid input to %s: %s", CLASSNAME, exp.getMessage()));
+      HelpFormatter formatter = new HelpFormatter();
+      formatter.printHelp("java " + CLASSNAME, options);
+    }
+  }
+
+  private static void runChecker(List<String> command) throws IOException {
+    LOG.fine("checkstyle command: " + command);
+
+    ProcessBuilder processBuilder = new ProcessBuilder(command);
+    processBuilder.redirectOutput(ProcessBuilder.Redirect.INHERIT);
+    processBuilder.redirectError(ProcessBuilder.Redirect.INHERIT);
+    Process cppcheck = processBuilder.start();
+
+    try {
+      cppcheck.waitFor();
+    } catch (InterruptedException e) {
+      throw new RuntimeException("cppcheck command was interrupted: " + command, e);
+    }
+
+    if (cppcheck.exitValue() == 1) {
+      LOG.warning("cppcheck detected bad cpp files.");
 
 Review comment:
   Maybe error()?

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services