Avi Drissman | dfd88085 | 2022-09-15 20:11:09 | [diff] [blame] | 1 | // Copyright 2018 The Chromium Authors |
Daniel Cheng | 657bf20 | 2018-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 | |
mikt | dfd2cae | 2023-05-23 04:10:57 | [diff] [blame] | 7 | #include<algorithm> |
| 8 | |
Daniel Cheng | 657bf20 | 2018-04-26 21:50:02 | [diff] [blame] | 9 | #include"clang/AST/Decl.h" |
Xianzhu Wang | 86b1146 | 2022-09-24 22:00:12 | [diff] [blame] | 10 | #include"clang/Basic/SourceManager.h" |
danakj | c06d5f4 | 2024-02-29 17:54:11 | [diff] [blame] | 11 | #include"llvm/ADT/StringRef.h" |
Daniel Cheng | 657bf20 | 2018-04-26 21:50:02 | [diff] [blame] | 12 | #include"llvm/Support/Casting.h" |
| 13 | #include"llvm/Support/raw_ostream.h" |
| 14 | |
| 15 | namespace{ |
| 16 | |
danakj | c06d5f4 | 2024-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. |
| 21 | constchar* kTreatAsThirdPartyDirs[]={ |
| 22 | "/breakpad/",// |
| 23 | "/courgette/",// |
| 24 | "/frameworks/",// |
| 25 | "/native_client/",// |
| 26 | "/ppapi/",// |
| 27 | "/testing/",// |
| 28 | "/v8/",// |
| 29 | }; |
| 30 | |
Daniel Cheng | 657bf20 | 2018-04-26 21:50:02 | [diff] [blame] | 31 | std::stringGetNamespaceImpl(const clang::DeclContext* context, |
| 32 | const std::string& candidate){ |
| 33 | switch(context->getDeclKind()){ |
| 34 | case clang::Decl::TranslationUnit:{ |
| 35 | return candidate; |
| 36 | } |
| 37 | case clang::Decl::Namespace:{ |
| 38 | constauto* decl= llvm::dyn_cast<clang::NamespaceDecl>(context); |
| 39 | std::string name_str; |
| 40 | llvm::raw_string_ostream OS(name_str); |
danakj | c06d5f4 | 2024-02-29 17:54:11 | [diff] [blame] | 41 | if(decl->isAnonymousNamespace()){ |
Daniel Cheng | 657bf20 | 2018-04-26 21:50:02 | [diff] [blame] | 42 | OS<<"<anonymous namespace>"; |
danakj | c06d5f4 | 2024-02-29 17:54:11 | [diff] [blame] | 43 | }else{ |
Daniel Cheng | 657bf20 | 2018-04-26 21:50:02 | [diff] [blame] | 44 | OS<<*decl; |
danakj | c06d5f4 | 2024-02-29 17:54:11 | [diff] [blame] | 45 | } |
Daniel Cheng | 657bf20 | 2018-04-26 21:50:02 | [diff] [blame] | 46 | returnGetNamespaceImpl(context->getParent(), OS.str()); |
| 47 | } |
danakj | c06d5f4 | 2024-02-29 17:54:11 | [diff] [blame] | 48 | default:{ |
| 49 | returnGetNamespaceImpl(context->getParent(), candidate); |
| 50 | } |
Daniel Cheng | 657bf20 | 2018-04-26 21:50:02 | [diff] [blame] | 51 | } |
| 52 | } |
| 53 | |
| 54 | }// namespace |
| 55 | |
| 56 | std::stringGetNamespace(const clang::Decl* record){ |
| 57 | returnGetNamespaceImpl(record->getDeclContext(), std::string()); |
| 58 | } |
Xianzhu Wang | 86b1146 | 2022-09-24 22:00:12 | [diff] [blame] | 59 | |
Keishi Hattori | 615f8fa | 2022-11-07 23:50:14 | [diff] [blame] | 60 | std::stringGetFilename(const clang::SourceManager& source_manager, |
danakj | 69209bf | 2024-04-19 14:51:05 | [diff] [blame] | 61 | clang::SourceLocation loc, |
| 62 | FilenameLocationType type, |
danakj | e7db1e3f3 | 2024-04-16 20:43:24 | [diff] [blame] | 63 | FilenamesFollowPresumed follow_presumed){ |
danakj | 69209bf | 2024-04-19 14:51:05 | [diff] [blame] | 64 | switch(type){ |
| 65 | caseFilenameLocationType::kExactLoc: |
| 66 | break; |
| 67 | caseFilenameLocationType::kSpellingLoc: |
| 68 | loc= source_manager.getSpellingLoc(loc); |
| 69 | break; |
| 70 | caseFilenameLocationType::kExpansionLoc: |
| 71 | loc= source_manager.getExpansionLoc(loc); |
| 72 | break; |
| 73 | } |
danakj | e7db1e3f3 | 2024-04-16 20:43:24 | [diff] [blame] | 74 | std::string name; |
| 75 | if(follow_presumed==FilenamesFollowPresumed::kYes){ |
danakj | 69209bf | 2024-04-19 14:51:05 | [diff] [blame] | 76 | clang::PresumedLoc ploc= source_manager.getPresumedLoc(loc); |
danakj | e7db1e3f3 | 2024-04-16 20:43:24 | [diff] [blame] | 77 | if(ploc.isInvalid()){ |
| 78 | // If we're in an invalid location, we're looking at things that aren't |
| 79 | // actually stated in the source. |
| 80 | return name; |
| 81 | } |
| 82 | name= ploc.getFilename(); |
| 83 | }else{ |
danakj | 69209bf | 2024-04-19 14:51:05 | [diff] [blame] | 84 | name= source_manager.getFilename(loc); |
danakj | e7db1e3f3 | 2024-04-16 20:43:24 | [diff] [blame] | 85 | } |
mikt | dfd2cae | 2023-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(),'\\','/'); |
| 90 | return name; |
Xianzhu Wang | 86b1146 | 2022-09-24 22:00:12 | [diff] [blame] | 91 | } |
danakj | c06d5f4 | 2024-02-29 17:54:11 | [diff] [blame] | 92 | |
| 93 | namespace chrome_checker{ |
| 94 | |
danakj | e7db1e3f3 | 2024-04-16 20:43:24 | [diff] [blame] | 95 | LocationClassificationClassifySourceLocation( |
| 96 | const clang::HeaderSearchOptions& search, |
| 97 | const clang::SourceManager& sm, |
| 98 | clang::SourceLocation loc){ |
danakj | c06d5f4 | 2024-02-29 17:54:11 | [diff] [blame] | 99 | if(sm.isInSystemHeader(loc)){ |
| 100 | returnLocationClassification::kSystem; |
| 101 | } |
| 102 | |
danakj | 69209bf | 2024-04-19 14:51:05 | [diff] [blame] | 103 | std::string filename=GetFilename(sm, loc,FilenameLocationType::kExactLoc); |
danakj | c06d5f4 | 2024-02-29 17:54:11 | [diff] [blame] | 104 | if(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. |
| 108 | returnLocationClassification::kThirdParty; |
| 109 | } |
| 110 | |
danakj | e7db1e3f3 | 2024-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. |
| 116 | if(!search.Sysroot.empty()&& search.Sysroot!="/"&& |
| 117 | llvm::StringRef(filename).starts_with(search.Sysroot)){ |
| 118 | returnLocationClassification::kSystem; |
| 119 | } |
| 120 | |
Tom Sepez | 01e8ba0e | 2025-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. |
| 123 | if(filename.find("Program Files (x86)/Windows Kits/")!= std::string::npos){ |
| 124 | returnLocationClassification::kSystem; |
| 125 | } |
| 126 | |
danakj | c06d5f4 | 2024-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 |
| 134 | if(filename=="<scratch space>"){ |
| 135 | returnLocationClassification::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. |
| 141 | if(filename.front()!='/'){ |
| 142 | filename.insert(0,1,'/'); |
| 143 | } |
| 144 | |
| 145 | if(filename.find("/gen/")!= std::string::npos){ |
| 146 | returnLocationClassification::kGenerated; |
| 147 | } |
| 148 | |
| 149 | // While blink is inside third_party, it's not all treated like third-party |
| 150 | // code. |
| 151 | if(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. |
| 157 | if(!llvm::StringRef(filename).substr(p).starts_with("browser/")){ |
| 158 | returnLocationClassification::kBlink; |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | if(filename.find("/third_party/")!= std::string::npos){ |
| 163 | returnLocationClassification::kThirdParty; |
| 164 | } |
| 165 | |
| 166 | for(constchar* dir: kTreatAsThirdPartyDirs){ |
| 167 | if(filename.find(dir)!= std::string::npos){ |
| 168 | returnLocationClassification::kThirdParty; |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | // TODO(danakj): Designate chromium-owned code in third_party as |
| 173 | // kChromiumFirstParty. |
| 174 | returnLocationClassification::kFirstParty; |
| 175 | } |
| 176 | |
| 177 | }// namespace chrome_checker |