Movatterモバイル変換


[0]ホーム

URL:


Google Git
Sign in
chromium /chromium /src /main /. /tools /clang /plugins /Util.cpp
blob: 5cbe91cc9cf8e68023a22ee5e534d4c8e9f14909 [file] [log] [blame]
Avi Drissmandfd880852022-09-15 20:11:09[diff] [blame]1// Copyright 2018 The Chromium Authors
Daniel Cheng657bf202018-04-26 21:50:02[diff] [blame]2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include"Util.h"
6
miktdfd2cae2023-05-23 04:10:57[diff] [blame]7#include<algorithm>
8
Daniel Cheng657bf202018-04-26 21:50:02[diff] [blame]9#include"clang/AST/Decl.h"
Xianzhu Wang86b11462022-09-24 22:00:12[diff] [blame]10#include"clang/Basic/SourceManager.h"
danakjc06d5f42024-02-29 17:54:11[diff] [blame]11#include"llvm/ADT/StringRef.h"
Daniel Cheng657bf202018-04-26 21:50:02[diff] [blame]12#include"llvm/Support/Casting.h"
13#include"llvm/Support/raw_ostream.h"
14
15namespace{
16
danakjc06d5f42024-02-29 17:54:11[diff] [blame]17// Directories which are treated as third-party code, which can be used to
18// prevent emitting diagnostics in them.
19//
20// Each one must start and end with a `/` to be used correctly.
21constchar* kTreatAsThirdPartyDirs[]={
22"/breakpad/",//
23"/courgette/",//
24"/frameworks/",//
25"/native_client/",//
26"/ppapi/",//
27"/testing/",//
28"/v8/",//
29};
30
Daniel Cheng657bf202018-04-26 21:50:02[diff] [blame]31std::stringGetNamespaceImpl(const clang::DeclContext* context,
32const std::string& candidate){
33switch(context->getDeclKind()){
34case clang::Decl::TranslationUnit:{
35return candidate;
36}
37case clang::Decl::Namespace:{
38constauto* decl= llvm::dyn_cast<clang::NamespaceDecl>(context);
39 std::string name_str;
40 llvm::raw_string_ostream OS(name_str);
danakjc06d5f42024-02-29 17:54:11[diff] [blame]41if(decl->isAnonymousNamespace()){
Daniel Cheng657bf202018-04-26 21:50:02[diff] [blame]42 OS<<"<anonymous namespace>";
danakjc06d5f42024-02-29 17:54:11[diff] [blame]43}else{
Daniel Cheng657bf202018-04-26 21:50:02[diff] [blame]44 OS<<*decl;
danakjc06d5f42024-02-29 17:54:11[diff] [blame]45}
Daniel Cheng657bf202018-04-26 21:50:02[diff] [blame]46returnGetNamespaceImpl(context->getParent(), OS.str());
47}
danakjc06d5f42024-02-29 17:54:11[diff] [blame]48default:{
49returnGetNamespaceImpl(context->getParent(), candidate);
50}
Daniel Cheng657bf202018-04-26 21:50:02[diff] [blame]51}
52}
53
54}// namespace
55
56std::stringGetNamespace(const clang::Decl* record){
57returnGetNamespaceImpl(record->getDeclContext(), std::string());
58}
Xianzhu Wang86b11462022-09-24 22:00:12[diff] [blame]59
Keishi Hattori615f8fa2022-11-07 23:50:14[diff] [blame]60std::stringGetFilename(const clang::SourceManager& source_manager,
danakj69209bf2024-04-19 14:51:05[diff] [blame]61 clang::SourceLocation loc,
62FilenameLocationType type,
danakje7db1e3f32024-04-16 20:43:24[diff] [blame]63FilenamesFollowPresumed follow_presumed){
danakj69209bf2024-04-19 14:51:05[diff] [blame]64switch(type){
65caseFilenameLocationType::kExactLoc:
66break;
67caseFilenameLocationType::kSpellingLoc:
68 loc= source_manager.getSpellingLoc(loc);
69break;
70caseFilenameLocationType::kExpansionLoc:
71 loc= source_manager.getExpansionLoc(loc);
72break;
73}
danakje7db1e3f32024-04-16 20:43:24[diff] [blame]74 std::string name;
75if(follow_presumed==FilenamesFollowPresumed::kYes){
danakj69209bf2024-04-19 14:51:05[diff] [blame]76 clang::PresumedLoc ploc= source_manager.getPresumedLoc(loc);
danakje7db1e3f32024-04-16 20:43:24[diff] [blame]77if(ploc.isInvalid()){
78// If we're in an invalid location, we're looking at things that aren't
79// actually stated in the source.
80return name;
81}
82 name= ploc.getFilename();
83}else{
danakj69209bf2024-04-19 14:51:05[diff] [blame]84 name= source_manager.getFilename(loc);
danakje7db1e3f32024-04-16 20:43:24[diff] [blame]85}
miktdfd2cae2023-05-23 04:10:57[diff] [blame]86
87// File paths can have separators which differ from ones at this platform.
88// Make them consistent.
89 std::replace(name.begin(), name.end(),'\\','/');
90return name;
Xianzhu Wang86b11462022-09-24 22:00:12[diff] [blame]91}
danakjc06d5f42024-02-29 17:54:11[diff] [blame]92
93namespace chrome_checker{
94
danakje7db1e3f32024-04-16 20:43:24[diff] [blame]95LocationClassificationClassifySourceLocation(
96const clang::HeaderSearchOptions& search,
97const clang::SourceManager& sm,
98 clang::SourceLocation loc){
danakjc06d5f42024-02-29 17:54:11[diff] [blame]99if(sm.isInSystemHeader(loc)){
100returnLocationClassification::kSystem;
101}
102
danakj69209bf2024-04-19 14:51:05[diff] [blame]103 std::string filename=GetFilename(sm, loc,FilenameLocationType::kExactLoc);
danakjc06d5f42024-02-29 17:54:11[diff] [blame]104if(filename.empty()){
105// If the filename cannot be determined, simply treat this as third-party
106// code, where we avoid enforcing rules, instead of going through the full
107// lookup process.
108returnLocationClassification::kThirdParty;
109}
110
danakje7db1e3f32024-04-16 20:43:24[diff] [blame]111// Files in the sysroot do not automatically get categorized as system
112// headers, so we do a path comparison. The sysroot can be set to "/" when it
113// was not specified, which is just the whole filesystem, but every file is
114// not a system header, so this is treated as equivalent to not having a
115// sysroot.
116if(!search.Sysroot.empty()&& search.Sysroot!="/"&&
117 llvm::StringRef(filename).starts_with(search.Sysroot)){
118returnLocationClassification::kSystem;
119}
120
Tom Sepez01e8ba0e2025-01-27 18:51:48[diff] [blame]121// The Win SDK headers don't get categorized as system headers when building
122// with DEPOT_TOOLS_WIN_TOOLCHAIN=0.
123if(filename.find("Program Files (x86)/Windows Kits/")!= std::string::npos){
124returnLocationClassification::kSystem;
125}
126
danakjc06d5f42024-02-29 17:54:11[diff] [blame]127// We need to special case scratch space; which is where clang does its macro
128// expansion. We explicitly want to allow people to do otherwise bad things
129// through macros that were defined due to third party libraries.
130//
131// TODO(danakj): We can further classify this as first/third-party code using
132// a macro defined in first/third-party code. See
133// https://github.com/chromium/subspace/blob/f9c481a241961a7be827d31fadb01badac6ee86a/subdoc/lib/visit.cc#L1566-L1577
134if(filename=="<scratch space>"){
135returnLocationClassification::kMacro;
136}
137
138// Ensure that we can search for patterns of the form "/foo/" even
139// if we have a relative path like "foo/bar.cc". We don't expect
140// this transformed path to exist necessarily.
141if(filename.front()!='/'){
142 filename.insert(0,1,'/');
143}
144
145if(filename.find("/gen/")!= std::string::npos){
146returnLocationClassification::kGenerated;
147}
148
149// While blink is inside third_party, it's not all treated like third-party
150// code.
151if(auto p= filename.find("/third_party/blink/"); p!= std::string::npos){
152// Browser-side code is treated like first party in order to have all
153// diagnostics applied. Over time we want the rest of blink code to
154// converge as well.
155//
156// TODO(danakj): Use starts_with() when Clang is compiled with C++20.
157if(!llvm::StringRef(filename).substr(p).starts_with("browser/")){
158returnLocationClassification::kBlink;
159}
160}
161
162if(filename.find("/third_party/")!= std::string::npos){
163returnLocationClassification::kThirdParty;
164}
165
166for(constchar* dir: kTreatAsThirdPartyDirs){
167if(filename.find(dir)!= std::string::npos){
168returnLocationClassification::kThirdParty;
169}
170}
171
172// TODO(danakj): Designate chromium-owned code in third_party as
173// kChromiumFirstParty.
174returnLocationClassification::kFirstParty;
175}
176
177}// namespace chrome_checker

[8]ページ先頭

©2009-2025 Movatter.jp