================
@@ -0,0 +1,78 @@
+//===--- UseInternalLinkageCheck.cpp - 
clang-tidy--------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "UseInternalLinkageCheck.h"
+#include "../utils/FileExtensionsUtils.h"
+#include "clang/AST/Decl.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+#include "clang/ASTMatchers/ASTMatchers.h"
+#include "clang/ASTMatchers/ASTMatchersMacros.h"
+#include "clang/Basic/SourceLocation.h"
+#include "clang/Basic/Specifiers.h"
+#include "llvm/ADT/STLExtras.h"
+
+using namespace clang::ast_matchers;
+
+namespace clang::tidy::misc {
+
+namespace {
+
+AST_MATCHER(Decl, isFirstDecl) { return Node.isFirstDecl(); }
+
+AST_MATCHER_P(Decl, isInMainFile, 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);
+  });
----------------
HerrCai0907 wrote:

I got the point. Our main disagreement is about

> when declaration and definition is in header file, in such case adding 
> static/anonymous namespace is also expected

In my opinion, we should not emit any wrong for the definition in header file. 
Add static in header file is dangerous and cause potential bug.

e.g.
```c++
/// a.hpp
PREFIX int f() {
  static int a = 1;
  return a++;
}
void b();

/// a.cpp
#include "a.hpp"
#include <iostream>

int main() {
  std::cout << __func__ << " " << f() << "\n";
  b();
  std::cout << __func__ << " " << f() << "\n";
  b();
}

/// b.cpp
#include "a.hpp"
#include <iostream>

void b() { std::cout << __func__ << " " << f() << "\n"; }
```

if `PREFIX` is `inline`, result is `1 2 3 4`.
if `PREFIX` is `static`, result is `1 2 1 2`.
if `PREFIX` is `inline static`, result is `1 2 1 2`.

if `PREFIX` is `template <int>`, result is `1 2 3 4`.
if `PREFIX` is `template <int> static`, result is `1 2 1 2`.

https://github.com/llvm/llvm-project/pull/90830
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to