-
Notifications
You must be signed in to change notification settings - Fork 14.9k
[AST matchers] matchers for absl Status / StatusOr #160953
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
Closed
fmayer
wants to merge
2
commits into
main
from
users/fmayer/spr/ast-matchers-matchers-for-absl-status-statusor
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.