Skip to content

Commit

Permalink
Fix cppcheck not finding cfg files
Browse files Browse the repository at this point in the history
Hack cppcheck library loading logic to supply the cfg path.
We use the runfiles library: https://github.com/bazelbuild/bazel/blob/master/tools/cpp/runfiles/runfiles_src.h

The patch is generated from afq984/cppcheck@d269ded

Also updated cppcheck version to fix a compile error.

BUG=b:320231977
TEST=bazel run @CppCheck --config=local-clang -- $PWD/cras/src/server/cras.c

Change-Id: I77ed6274a39f041971a8a41ade4d711cf00bf895
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/third_party/adhd/+/5261598
Reviewed-by: Baili Deng <bailideng@google.com>
Tested-by: chromeos-cop-builder@chromeos-cop.iam.gserviceaccount.com <chromeos-cop-builder@chromeos-cop.iam.gserviceaccount.com>
Commit-Queue: Li-Yu Yu <aaronyu@google.com>
  • Loading branch information
afq984 authored and Chromeos LUCI committed Feb 2, 2024
1 parent f79d431 commit d3ae083
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 3 deletions.
8 changes: 5 additions & 3 deletions WORKSPACE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,11 @@ http_archive(
http_archive(
name = "cppcheck",
build_file = "//:third_party/BUILD.cppcheck.bazel",
sha256 = "8aae5e116daeaaf5d19f3efa61b91c06f161cb97412a1d1af6e1e20686e48967",
strip_prefix = "cppcheck-2.10.3",
urls = ["https://github.com/danmar/cppcheck/archive/refs/tags/2.10.3.tar.gz"],
integrity = "sha256-gimv4d3cPtiTJIuKcjtCjcIh6gFPvHbmKJhAhXwD1FA=",
patch_args = ["-p1"],
patches = ["@//:third_party/cppcheck.runfiles.patch"],
strip_prefix = "cppcheck-2.13.0",
urls = ["https://github.com/danmar/cppcheck/archive/refs/tags/2.13.0.tar.gz"],
)

http_archive(
Expand Down
4 changes: 4 additions & 0 deletions third_party/BUILD.cppcheck.bazel
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library")

package(
# Mark testonly so we don't accidentally ship this.
default_testonly = True,
Expand Down Expand Up @@ -31,11 +33,13 @@ cc_library(
srcs = glob(["lib/*.cpp"]),
hdrs = glob(["lib/*.h"]),
copts = COPTS,
data = glob(include = ["cfg/*.cfg"]),
includes = ["lib"],
deps = [
":picojson",
":simplecpp",
":tinyxml2",
"@bazel_tools//tools/cpp/runfiles",
],
)

Expand Down
61 changes: 61 additions & 0 deletions third_party/cppcheck.runfiles.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
diff --git a/cli/cmdlineparser.cpp b/cli/cmdlineparser.cpp
index f15f37b8b..7820ad570 100644
--- a/cli/cmdlineparser.cpp
+++ b/cli/cmdlineparser.cpp
@@ -310,7 +310,7 @@ bool CmdLineParser::fillSettingsFromArgs(int argc, const char* const argv[])
// TODO: error out on all missing given files/paths
CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const argv[])
{
- mSettings.exename = Path::getCurrentExecutablePath(argv[0]);
+ mSettings.exename = argv[0];

if (argc <= 1) {
printHelp();
diff --git a/lib/library.cpp b/lib/library.cpp
index 96df48117..068f189c8 100644
--- a/lib/library.cpp
+++ b/lib/library.cpp
@@ -30,6 +30,7 @@
#include "vfvalue.h"

#include <algorithm>
+#include <iostream>
#include <cctype>
#include <climits>
#include <cstring>
@@ -40,8 +41,11 @@
#include <stdexcept>
#include <string>

+#include "tools/cpp/runfiles/runfiles.h"
#include "xml.h"

+using bazel::tools::cpp::runfiles::Runfiles;
+
static std::vector<std::string> getnames(const char *names)
{
std::vector<std::string> ret;
@@ -83,6 +87,15 @@ Library::Error Library::load(const char exename[], const char path[])
return Error();
}

+
+ std::string runfiles_error;
+ std::unique_ptr<Runfiles> runfiles(Runfiles::Create(exename, &runfiles_error));
+ if (runfiles == nullptr) {
+ std::clog << runfiles_error << std::endl;
+ return Error();
+ }
+ std::string bazel_cfg = runfiles->Rlocation("cppcheck/cfg", "cppcheck");
+
std::string absolute_path;
// open file..
tinyxml2::XMLDocument doc;
@@ -101,6 +114,7 @@ Library::Error Library::load(const char exename[], const char path[])
}

std::list<std::string> cfgfolders;
+ cfgfolders.emplace_back(bazel_cfg);
#ifdef FILESDIR
cfgfolders.emplace_back(FILESDIR "/cfg");
#endif

0 comments on commit d3ae083

Please sign in to comment.