[clang-tools-extra] [clang-tidy] add fixhint for misc-use-internal-linkage (PR #96203)

Julian Schmidt via cfe-commits cfe-commits at lists.llvm.org
Sun Jun 23 07:39:57 PDT 2024


================
@@ -82,11 +82,19 @@ static constexpr StringRef Message =
 
 void UseInternalLinkageCheck::check(const MatchFinder::MatchResult &Result) {
   if (const auto *FD = Result.Nodes.getNodeAs<FunctionDecl>("fn")) {
-    diag(FD->getLocation(), Message) << "function" << FD;
+    DiagnosticBuilder DB = diag(FD->getLocation(), Message) << "function" << FD;
+    SourceLocation FixLoc = FD->getTypeSpecStartLoc();
+    if (FixLoc.isInvalid() || FixLoc.isMacroID())
+      return;
+    DB << FixItHint::CreateInsertion(FixLoc, "static ");
     return;
   }
   if (const auto *VD = Result.Nodes.getNodeAs<VarDecl>("var")) {
-    diag(VD->getLocation(), Message) << "variable" << VD;
+    DiagnosticBuilder DB = diag(VD->getLocation(), Message) << "variable" << VD;
+    SourceLocation FixLoc = VD->getTypeSpecStartLoc();
+    if (FixLoc.isInvalid() || FixLoc.isMacroID())
+      return;
+    DB << FixItHint::CreateInsertion(FixLoc, "static ");
----------------
5chmidti wrote:

I think that this fix should be behind an option, so that projects that want to use anonymous namespaces have the choice to leave the option off or disable it (depending on the default).

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


More information about the cfe-commits mailing list