[clang-tools-extra] [clang-tidy] new check misc-use-internal-linkage (PR #90830)

Piotr Zegar via cfe-commits cfe-commits at lists.llvm.org
Thu Jun 6 04:52:27 PDT 2024


================
@@ -24,12 +24,28 @@ namespace {
 
 AST_MATCHER(Decl, isFirstDecl) { return Node.isFirstDecl(); }
 
-AST_MATCHER_P(Decl, isInMainFile, FileExtensionsSet, HeaderFileExtensions) {
+static bool isInMainFile(SourceLocation L, SourceManager &SM,
+                         const FileExtensionsSet &HeaderFileExtensions) {
+  for (;;) {
+    if (utils::isSpellingLocInHeaderFile(L, SM, HeaderFileExtensions))
+      return false;
+    if (SM.isInMainFile(L))
+      return true;
+    // not in header file but not in main file
+    L = SM.getIncludeLoc(SM.getFileID(L));
+    if (L.isValid())
+      continue;
+    // Conservative about the unknown
+    return false;
+  }
+}
+
+AST_MATCHER_P(Decl, isAllRedeclsInMainFile, FileExtensionsSet,
+              HeaderFileExtensions) {
   return llvm::all_of(Node.redecls(), [&](const Decl *D) {
-    SourceManager &SM = Finder->getASTContext().getSourceManager();
-    const SourceLocation L = D->getLocation();
-    return SM.isInMainFile(L) &&
-           !utils::isSpellingLocInHeaderFile(L, SM, HeaderFileExtensions);
+    return isInMainFile(D->getLocation(),
+                        Finder->getASTContext().getSourceManager(),
+                        HeaderFileExtensions);
----------------
PiotrZSL wrote:

this isn't needed.
All you need to do is just get location and:
```
return SM.isInMainFile(L) ||  !utils::isSpellingLocInHeaderFile(L, SM, HeaderFileExtensions);
```

Or better add: isSpellingLocInSourceFile

https://github.com/llvm/llvm-project/pull/90830


More information about the cfe-commits mailing list