簡體   English   中英

將警告視為錯誤,但忽略googletest中的警告

[英]Treat warnings as errors but ignore warnings in googletest

我有一個C ++項目,我想使用-Wsign-conversion編譯選項。 使用構建工具Bazel 0.28.1。 我自編的代碼不會生成該類型的警告。 該項目使用Googletest。 不幸的是,Googletest會產生這種警告,這會打破我的構建。 這是我的文件:

.bazelrc

build --cxxopt=-Werror           # Every warning is treated as an error.
build --cxxopt=-std=c++14
build --cxxopt=-Wsign-conversion # Warn for implicit conversions that may change the sign of an integer value

gtest.BUILD

cc_library(
    name = "main",
    srcs = glob(
        ["src/*.cc"],
        exclude = ["src/gtest-all.cc"]
    ),
    hdrs = glob([
        "include/**/*.h",
        "src/*.h"
    ]),
    copts = ["-Iexternal/gtest/include"],
    linkopts = ["-pthread"],
    visibility = ["//visibility:public"],
)

工作區

workspace(name = "GTestDemo")

load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository")

git_repository(
    name = "googletest",
    remote = "https://github.com/google/googletest",
    #tag = "release-1.8.1",
    commit = "2fe3bd994b3189899d93f1d5a881e725e046fdc2", 
    shallow_since = "1535728917 -0400",
)

建立

cc_test(
    name = "tests",
    srcs = ["test.cpp"],
    copts = ["-Iexternal/gtest/include"],
    deps = [
            "@googletest//:gtest_main",
        ],
)

TEST.CPP

#include <iostream>

#include "gtest/gtest.h"

TEST(sample_test_case, sample_test)
{
    EXPECT_EQ(1, 1);
}

當我嘗試構建代碼時,顯示以下錯誤:

Starting local Bazel server and connecting to it...
INFO: Analyzed target //:tests (21 packages loaded, 540 targets configured).
INFO: Found 1 target...
INFO: Deleting stale sandbox base /mnt/ramdisk/bazel-sandbox.be60b2910864108c1e29c6fce8ad6ea4
ERROR: /home/admin/.cache/bazel/_bazel_admin/cc9b56275ffa85d1a0fca263d1d708e4/external/googletest/BUILD.bazel:55:1: C++ compilation of rule '@googletest//:gtest' failed (Exit 1) gcc failed: error executing command /usr/bin/gcc -U_FORTIFY_SOURCE -fstack-protector -Wall -Wunused-but-set-parameter -Wno-free-nonheap-object -fno-omit-frame-pointer '-std=c++0x' -MD -MF ... (remaining 36 argument(s) skipped)

Use --sandbox_debug to see verbose messages from the sandbox
external/googletest/googletest/src/gtest-filepath.cc: In member function 'testing::internal::FilePath testing::internal::FilePath::RemoveFileName() const':
external/googletest/googletest/src/gtest-filepath.cc:168:45: error: conversion to 'std::__cxx11::basic_string<char>::size_type {aka long unsigned int}' from 'long int' may change the sign of the result [-Werror=sign-conversion]
     dir = std::string(c_str(), last_sep + 1 - c_str());
                                ~~~~~~~~~~~~~^~~~~~~~~
cc1plus: all warnings being treated as errors
Target //:tests failed to build
Use --verbose_failures to see the command lines of failed build steps.
INFO: Elapsed time: 7.225s, Critical Path: 0.66s
INFO: 0 processes.
FAILED: Build did NOT complete successfully

Bazel是否有可能忽略第三方庫中的警告,例如googletest?

這更像是海灣合作委員會的一個問題。

“系統標題” 不受此類事件的影響

-Wsystem-headers :打印系統頭文件中的構造的警告消息。 系統頭的警告通常被抑制 ,假設它們通常不表示實際問題並且只會使編譯器輸出更難讀。

因此,您可以假設第三方lib是系統頭,使用GCC的-isystem標志 (而不是-I ):

copts = ["-isystem external/gtest/include"],

Bazel的--per_file_copt允許為除了匹配某些正則表達式的文件之外的所有文件設置標志。 在.bazelrc中更像這樣的東西應該做你想要的:

# Warn for implicit conversions that may change the sign of an integer value,
# for C++ files not in googletest
build --per_file_copt=.*\.(cc|cpp),-googletest/.*@-Wsign-conversion

您需要更新它以匹配用於除.cc和.cpp之外的C ++文件的任何擴展。 cc_library.srcs的文檔列出了Bazel使用的所有擴展,以供參考。

我無法弄清楚如何匹配@googletest//在旗幟中,因為我沒有辦法逃避@那里...但是,我很確定它與@googletest匹配而不是external/googletest或某事,因為/googletest與任何東西都不匹配。 可能無關緊要,但如果您有任何其他文件名包含googletest ,請googletest這一點。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM