Skip to content
Closed
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
29 changes: 29 additions & 0 deletions clang/include/clang/ASTMatchers/AbslMatchers.h
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'd be interested in other clang member opinions as to whether this is the right location for these matchers. As much as they are general, their intended use/need is still the StatusOr model, so co-locating them with the model code might be more appropriate. I suspect AST matchers maintainers are not looking to add matcher support for a wide variety of (common?) library types.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

FWIW there is also a file for Gtest (next to this one). But I have no real opinion about the namespace, and am happy to just colocate it with the model.

Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//===- AbslMatchers.h - AST Matchers for Abseil -----------------*- C++ -*-===//
//
// 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
//
//===----------------------------------------------------------------------===//
//
// This file implements matchers specific to structures in Abseil
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_CLANG_ASTMATCHERS_ABSLMATCHERS_H
#define LLVM_CLANG_ASTMATCHERS_ABSLMATCHERS_H

#include "clang/ASTMatchers/ASTMatchers.h"

namespace clang {
namespace ast_matchers {
namespace absl_matchers {

DeclarationMatcher statusOrClass();
DeclarationMatcher statusClass();

} // end namespace absl_matchers
} // end namespace ast_matchers
} // end namespace clang

#endif // LLVM_CLANG_ASTMATCHERS_ABSLMATCHERS_H
27 changes: 27 additions & 0 deletions clang/lib/ASTMatchers/AbslMatchers.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//===- AbslMatchers.cpp - AST Matchers for Abseil --------------*- C++ -*-===//
//
// 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 "clang/ASTMatchers/AbslMatchers.h"

namespace clang {
namespace ast_matchers {
namespace absl_matchers {

DeclarationMatcher statusOrClass() {
return classTemplateSpecializationDecl(
hasName("::absl::StatusOr"),
hasTemplateArgument(0, refersToType(type().bind("StatusOrValueType"))));
}

DeclarationMatcher statusClass() {
return cxxRecordDecl(hasName("::absl::Status"));
}

} // end namespace absl_matchers
} // end namespace ast_matchers
} // end namespace clang
1 change: 1 addition & 0 deletions clang/lib/ASTMatchers/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ set(LLVM_LINK_COMPONENTS
add_clang_library(clangASTMatchers
ASTMatchFinder.cpp
ASTMatchersInternal.cpp
AbslMatchers.cpp
GtestMatchers.cpp
LowLevelHelpers.cpp

Expand Down
59 changes: 59 additions & 0 deletions clang/unittests/ASTMatchers/AbslMatchersTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
//===- unittests/ASTMatchers/GTestMatchersTest.cpp - GTest matcher unit tests //
//
// 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 "clang/ASTMatchers/AbslMatchers.h"
#include "ASTMatchersTest.h"
#include "clang/ASTMatchers/ASTMatchers.h"

namespace clang {
namespace ast_matchers {
namespace absl_matchers {

constexpr llvm::StringLiteral abslMockDecls = R"cc(
namespace absl {
template <typename T> class StatusOr {};
class Status {};
}

)cc";

static auto wrapAbsl(llvm::StringRef Input) { return abslMockDecls + Input; }

TEST(AbslMatchersTest, StatusOrDecl) {
DeclarationMatcher StatusOrDecl =
varDecl(hasType(qualType(hasDeclaration(statusOrClass()))));
EXPECT_TRUE(matchAndVerifyResultTrue(
wrapAbsl("void Test() { absl::StatusOr<int> X; }"), StatusOrDecl,
std::make_unique<VerifyIdIsBoundTo<Type>>("StatusOrValueType")));
EXPECT_TRUE(notMatches(wrapAbsl("void Test() { int X; }"), StatusOrDecl));
EXPECT_TRUE(notMatches(wrapAbsl(R"cc(
namespace foo { namespace absl {
template <typename T> class StatusOr {};
}}
void Test() { foo::absl::StatusOr<int> X; }
)cc"),
StatusOrDecl));
}

TEST(AbslMatchersTest, StatusDecl) {
DeclarationMatcher StatusDecl =
varDecl(hasType(recordType(hasDeclaration(statusClass()))));
EXPECT_TRUE(matches(wrapAbsl("void Test() { absl::Status X; }"), StatusDecl));
EXPECT_TRUE(notMatches(wrapAbsl("void Test() { int X; }"), StatusDecl));
EXPECT_TRUE(notMatches(wrapAbsl(R"cc(
namespace foo { namespace absl {
class Status {};
}}
void Test() { foo::absl::Status X; }
)cc"),
StatusDecl));
}

} // end namespace absl_matchers
} // end namespace ast_matchers
} // end namespace clang
1 change: 1 addition & 0 deletions clang/unittests/ASTMatchers/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ add_clang_unittest(ASTMatchersTests
ASTMatchersNodeTest.cpp
ASTMatchersNarrowingTest.cpp
ASTMatchersTraversalTest.cpp
AbslMatchersTest.cpp
GtestMatchersTest.cpp
CLANG_LIBS
clangAST
Expand Down