2017-10-09 92 views
6

tldr;>如何在系統標題中隱藏警告?忽略系統標題在叮噹整潔

我有以下的最小的例子源文件,它觸發一個鐺-整齊警告系統中的標頭:

#include <future> 

int main() { 
    std::promise<int> p; 
    p.set_value(3); 
} 

使用鐺-整齊4.0.0用的libstdC++ 7.0.1調用它在Ubuntu 17.04 :

$ clang-tidy main.cpp -extra-arg=-std=c++14 

產生

Running without flags. 
1 warning generated. 
/usr/lib/gcc/x86_64-linux-gnu/7.0.1/../../../../include/c++/7.0.1/mutex:693:5: warning: Address of stack memory associated with local variable '__callable' is still referred to by the global variable '__once_callable' upon returning to the caller. This will be a dangling reference [clang-analyzer-core.StackAddressEscape] 
    } 
    ^
/home/user/main.cpp:5:3: note: Calling 'promise::set_value' 
    p.set_value(3); 
^
/usr/lib/gcc/x86_64-linux-gnu/7.0.1/../../../../include/c++/7.0.1/future:1094:9: note: Calling '_State_baseV2::_M_set_result' 
     { _M_future->_M_set_result(_State::__setter(this, std::move(__r))); } 
     ^
/usr/lib/gcc/x86_64-linux-gnu/7.0.1/../../../../include/c++/7.0.1/future:401:2: note: Calling 'call_once' 
     call_once(_M_once, &_State_baseV2::_M_do_set, this, 
     ^
/usr/lib/gcc/x86_64-linux-gnu/7.0.1/../../../../include/c++/7.0.1/mutex:691:11: note: Assuming '__e' is 0 
     if (__e) 
     ^
/usr/lib/gcc/x86_64-linux-gnu/7.0.1/../../../../include/c++/7.0.1/mutex:691:7: note: Taking false branch 
     if (__e) 
    ^
/usr/lib/gcc/x86_64-linux-gnu/7.0.1/../../../../include/c++/7.0.1/mutex:693:5: note: Address of stack memory associated with local variable '__callable' is still referred to by the global variable '__once_callable' upon returning to the caller. This will be a dangling reference 
    } 

我想隱藏警告系統頭文件中。我試過以下內容:

$ clang-tidy -extra-arg=-std=c++14 main.cpp -header-filter=$(realpath .) -system-headers=0 

但是警告仍然顯示。

回答

2

我也遇到了這個問題,並花了一些時間試圖弄清楚,但我看不到一種方式來禁用這種類型的警告鏗鏘。

從閱讀this discussion on the LLVM issue tracker regarding a similar issue,我得到的印象是,問題是,距離鐺,整潔的角度來看,警告實際上位於main.cpp,因爲調用set_value是從那裏。

我的解決方法是禁用clang-tidy中的靜態分析檢查,並使用scan-build utility運行clang的靜態分析,這似乎可以避免這些問題。例如,使用您的main.cpp

$ scan-build-3.9 clang++ -std=c++14 main.cpp 
scan-build: Using '/usr/lib/llvm-3.9/bin/clang' for static analysis 
In file included from main.cpp:1: 
In file included from /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/c++/6.3.0/future:39: 
/usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/c++/6.3.0/mutex:621:11: warning: Address of stack memory associated with local variable '__callable' is still referred to by the global variable '__once_callable' upon returning to the caller. This will be a dangling reference 
     if (__e) 
      ^~~ 
1 warning generated. 
scan-build: Removing directory '/tmp/scan-build-2017-12-02-112018-13035-1' because it contains no reports. 
scan-build: No bugs found. 

分析儀發現在系統頭相同的錯誤,但它足夠聰明,不包括在最終的報告。 (「沒有發現錯誤」)

如果您對樣式指南類型警告感興趣,例如modernize-*readability-*,您仍然需要單獨運行clang-tidy。