Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[clang-tidy] Update llvmlibc-implementation-in-namespace to new rules #66504

Merged
merged 6 commits into from
Sep 21, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,32 +18,32 @@ const static StringRef RequiredNamespaceStart = "__llvm_libc";
const static StringRef RequiredNamespaceMacroName = "LIBC_NAMESPACE";

void ImplementationInNamespaceCheck::registerMatchers(MatchFinder *Finder) {
Finder->addMatcher(
decl(hasParent(translationUnitDecl()), unless(linkageSpecDecl()))
.bind("child_of_translation_unit"),
this);
Finder->addMatcher(decl(isExpansionInMainFile(),
PiotrZSL marked this conversation as resolved.
Show resolved Hide resolved
hasDeclContext(translationUnitDecl()),
unless(linkageSpecDecl()))
.bind("child_of_translation_unit"),
this);
}

void ImplementationInNamespaceCheck::check(
const MatchFinder::MatchResult &Result) {
const auto *MatchedDecl =
Result.Nodes.getNodeAs<Decl>("child_of_translation_unit");
if (!Result.SourceManager->isInMainFile(MatchedDecl->getLocation()))
return;

if (const auto *NS = dyn_cast<NamespaceDecl>(MatchedDecl)) {
if (!Result.SourceManager->isMacroBodyExpansion(NS->getLocation()))
diag(NS->getLocation(),
"the outermost namespace should be the '%0' macro")
<< RequiredNamespaceMacroName;
else if (!NS->getName().starts_with(RequiredNamespaceStart))
else if (NS->isAnonymousNamespace() ||
PiotrZSL marked this conversation as resolved.
Show resolved Hide resolved
NS->getName().starts_with(RequiredNamespaceStart) == false)
diag(NS->getLocation(), "the outermost namespace should start with '%0'")
<< RequiredNamespaceStart;
return;
}
diag(MatchedDecl->getLocation(),
PiotrZSL marked this conversation as resolved.
Show resolved Hide resolved
"declaration must be declared within a namespace starting with '%0'")
<< RequiredNamespaceStart;
"declaration must be declared within the '%0' namespace")
<< RequiredNamespaceMacroName;
}

} // namespace clang::tidy::llvm_libc
10 changes: 5 additions & 5 deletions clang-tools-extra/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,11 @@ Changes in existing checks
<clang-tidy/checks/llvm/namespace-comment>` check to provide fixes for
``inline`` namespaces in the same format as :program:`clang-format`.

- Improved :doc:`llvmlibc-implementation-in-namespace
<clang-tidy/checks/llvmlibc/implementation-in-namespace>` to support
customizable namespace. This further allows for testing the libc when the
system-libc is also LLVM's libc.

- Improved :doc:`misc-include-cleaner
<clang-tidy/checks/misc/include-cleaner>` check by adding option
`DeduplicateFindings` to output one finding per symbol occurrence.
Expand Down Expand Up @@ -300,11 +305,6 @@ Changes in existing checks
<clang-tidy/checks/readability/static-accessed-through-instance>` check to
identify calls to static member functions with out-of-class inline definitions.

- Updated :doc:`llvmlibc-implementation-in-namespace
<clang-tidy/checks/llvmlibc/implementation-in-namespace>` to support
customizable namespace. This further allows for testing the libc when the
system-libc is also LLVM's libc.

Removed checks
^^^^^^^^^^^^^^

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,24 @@
#define MACRO_A "defining macros outside namespace is valid"

class ClassB;
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: declaration must be declared within a namespace starting with '__llvm_libc'
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: declaration must be declared within the 'LIBC_NAMESPACE' namespace
struct StructC {};
// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: declaration must be declared within a namespace starting with '__llvm_libc'
// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: declaration must be declared within the 'LIBC_NAMESPACE' namespace
char *VarD = MACRO_A;
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: declaration must be declared within a namespace starting with '__llvm_libc'
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: declaration must be declared within the 'LIBC_NAMESPACE' namespace
typedef int typeE;
// CHECK-MESSAGES: :[[@LINE-1]]:13: warning: declaration must be declared within a namespace starting with '__llvm_libc'
// CHECK-MESSAGES: :[[@LINE-1]]:13: warning: declaration must be declared within the 'LIBC_NAMESPACE' namespace
void funcF() {}
// CHECK-MESSAGES: :[[@LINE-1]]:6: warning: declaration must be declared within a namespace starting with '__llvm_libc'
// CHECK-MESSAGES: :[[@LINE-1]]:6: warning: declaration must be declared within the 'LIBC_NAMESPACE' namespace

namespace outer_most {
// CHECK-MESSAGES: :[[@LINE-1]]:11: warning: the outermost namespace should be the 'LIBC_NAMESPACE' macro
class A {};
}

PiotrZSL marked this conversation as resolved.
Show resolved Hide resolved
namespace namespaceG {
// CHECK-MESSAGES: :[[@LINE-1]]:11: warning: the outermost namespace should be the 'LIBC_NAMESPACE' macro
namespace __llvm_libc{
namespace __llvm_libc {
namespace namespaceH {
class ClassB;
} // namespace namespaceH
Expand Down