Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
9 changes: 1 addition & 8 deletions clang-tools-extra/clang-tidy/hicpp/NoAssemblerCheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,10 @@ using namespace clang::ast_matchers;

namespace clang::tidy::hicpp {

namespace {
AST_MATCHER(VarDecl, isAsm) { return Node.hasAttr<clang::AsmLabelAttr>(); }
const ast_matchers::internal::VariadicDynCastAllOfMatcher<Decl,
FileScopeAsmDecl>
fileScopeAsmDecl; // NOLINT(readability-identifier-*) preserve clang style
} // namespace

void NoAssemblerCheck::registerMatchers(MatchFinder *Finder) {
Finder->addMatcher(asmStmt().bind("asm-stmt"), this);
Finder->addMatcher(fileScopeAsmDecl().bind("asm-file-scope"), this);
Finder->addMatcher(varDecl(isAsm()).bind("asm-var"), this);
Finder->addMatcher(varDecl(hasAttr(attr::AsmLabel)).bind("asm-var"), this);
}

void NoAssemblerCheck::check(const MatchFinder::MatchResult &Result) {
Expand Down
14 changes: 14 additions & 0 deletions clang/docs/LibASTMatchersReference.html
Original file line number Diff line number Diff line change
Expand Up @@ -825,6 +825,20 @@ <h2 id="decl-matchers">Node Matchers</h2>
</pre></td></tr>


<tr><td>Matcher&lt;<a href="https://clang.llvm.org/doxygen/classclang_1_1Decl.html">Decl</a>&gt;</td><td class="name" onclick="toggle('fileScopeAsmDecl0')"><a name="fileScopeAsmDecl0Anchor">fileScopeAsmDecl</a></td><td>Matcher&lt;<a href="https://clang.llvm.org/doxygen/classclang_1_1FileScopeAsmDecl.html">FileScopeAsmDecl</a>&gt;...</td></tr>
<tr><td colspan="4" class="doc" id="fileScopeAsmDecl0"><pre>Matches top level asm declarations.

Given
__asm("nop");
void f() {
__asm("mov al, 2");
}
fileScopeAsmDecl()
matches '__asm("nop")',
but not '__asm("mov al, 2")'.
</pre></td></tr>


<tr><td>Matcher&lt;<a href="https://clang.llvm.org/doxygen/classclang_1_1Decl.html">Decl</a>&gt;</td><td class="name" onclick="toggle('friendDecl0')"><a name="friendDecl0Anchor">friendDecl</a></td><td>Matcher&lt;<a href="https://clang.llvm.org/doxygen/classclang_1_1FriendDecl.html">FriendDecl</a>&gt;...</td></tr>
<tr><td colspan="4" class="doc" id="friendDecl0"><pre>Matches friend declarations.

Expand Down
15 changes: 15 additions & 0 deletions clang/include/clang/ASTMatchers/ASTMatchers.h
Original file line number Diff line number Diff line change
Expand Up @@ -2478,6 +2478,21 @@ extern const internal::VariadicDynCastAllOfMatcher<Stmt, NullStmt> nullStmt;
/// matches '__asm("mov al, 2")'
extern const internal::VariadicDynCastAllOfMatcher<Stmt, AsmStmt> asmStmt;

/// Matches top level asm declarations.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the difference between top-level and not top-level asm declarations?
The __asm isn't top level?

Could we provide here example of non-top level declaration which wouldn't match

Copy link
Contributor Author

@capitan-davide capitan-davide Oct 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the difference between top-level and not top-level asm declarations?

You can find an example here of top-level vs non top-level (i.e., inline) assembly. This is what the ast looks like, with AsmStmt vs FileScopeAsmDecl.

The __asm isn't top level?

Yes, but I had the following issues:

  • asm C++ keyword is not supported by MSVC, also is not standard in C
  • __asm {} MSVC extension, does not work on Clang/GCC
  • __asm () Failed on AArch64 build (not supported?)
  • __asm__ GCC/Clang extension, but the most widely supported

Could we provide here example of non-top level declaration which wouldn't match

Yes, sure

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we provide here example of non-top level declaration which wouldn't match

Added a negative test

///
/// Given
/// \code
/// __asm("nop");
/// void f() {
/// __asm("mov al, 2");
/// }
/// \endcode
/// fileScopeAsmDecl()
/// matches '__asm("nop")',
/// but not '__asm("mov al, 2")'.
extern const internal::VariadicDynCastAllOfMatcher<Decl, FileScopeAsmDecl>
fileScopeAsmDecl;

/// Matches bool literals.
///
/// Example matches true
Expand Down
2 changes: 2 additions & 0 deletions clang/lib/ASTMatchers/ASTMatchersInternal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -954,6 +954,8 @@ const internal::VariadicDynCastAllOfMatcher<Stmt, CXXTryStmt> cxxTryStmt;
const internal::VariadicDynCastAllOfMatcher<Stmt, CXXThrowExpr> cxxThrowExpr;
const internal::VariadicDynCastAllOfMatcher<Stmt, NullStmt> nullStmt;
const internal::VariadicDynCastAllOfMatcher<Stmt, AsmStmt> asmStmt;
const internal::VariadicDynCastAllOfMatcher<Decl, FileScopeAsmDecl>
fileScopeAsmDecl;
const internal::VariadicDynCastAllOfMatcher<Stmt, CXXBoolLiteralExpr>
cxxBoolLiteral;
const internal::VariadicDynCastAllOfMatcher<Stmt, StringLiteral> stringLiteral;
Expand Down
1 change: 1 addition & 0 deletions clang/lib/ASTMatchers/Dynamic/Registry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ RegistryMaps::RegistryMaps() {
REGISTER_MATCHER(expr);
REGISTER_MATCHER(exprWithCleanups);
REGISTER_MATCHER(fieldDecl);
REGISTER_MATCHER(fileScopeAsmDecl);
REGISTER_MATCHER(fixedPointLiteral);
REGISTER_MATCHER(floatLiteral);
REGISTER_MATCHER(forCallable);
Expand Down
14 changes: 11 additions & 3 deletions clang/unittests/ASTMatchers/ASTMatchersNodeTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1179,6 +1179,12 @@ TEST_P(ASTMatchersTest, PredefinedExpr) {
has(stringLiteral()))));
}

TEST_P(ASTMatchersTest, FileScopeAsmDecl) {
EXPECT_TRUE(matches("__asm(\"nop\");", fileScopeAsmDecl()));
EXPECT_TRUE(
notMatches("void f() { __asm(\"mov al, 2\"); }", fileScopeAsmDecl()));
}

TEST_P(ASTMatchersTest, AsmStatement) {
EXPECT_TRUE(matches("void foo() { __asm(\"mov al, 2\"); }", asmStmt()));
}
Expand Down Expand Up @@ -2442,15 +2448,17 @@ TEST_P(ASTMatchersTest, LambdaCaptureTest_BindsToCaptureOfReferenceType) {
"int main() {"
" int a;"
" f(a);"
"}", matcher));
"}",
matcher));
EXPECT_FALSE(matches("template <class ...T> void f(T &...args) {"
" [...args = args] () mutable {"
" }();"
"}"
"int main() {"
" int a;"
" f(a);"
"}", matcher));
"}",
matcher));
}

TEST_P(ASTMatchersTest, IsDerivedFromRecursion) {
Expand Down Expand Up @@ -2628,7 +2636,7 @@ TEST(ASTMatchersTestObjC, ObjCStringLiteral) {
" [Test someFunction:@\"Ola!\"]; "
"}\n"
"@end ";
EXPECT_TRUE(matchesObjC(Objc1String, objcStringLiteral()));
EXPECT_TRUE(matchesObjC(Objc1String, objcStringLiteral()));
}

TEST(ASTMatchersTestObjC, ObjCDecls) {
Expand Down