Bug 1798305 - for clang-tidy do not cache attributes using a matcher since incomplete definitions may occur. r=sergesanspaille

Differential Revision: https://phabricator.services.mozilla.com/D169821
This commit is contained in:
Andi-Bogdan Postelnicu 2023-03-27 16:04:39 +00:00
parent 263c62f558
commit 38a4785b34

View File

@ -46,6 +46,7 @@ static CustomAttributesSet CacheAttributes(const Decl *D) {
return attrs; return attrs;
} }
#ifndef CLANG_TIDY
static void Report(const Decl *D, const char *message) { static void Report(const Decl *D, const char *message) {
ASTContext &Context = D->getASTContext(); ASTContext &Context = D->getASTContext();
DiagnosticsEngine &Diag = Context.getDiagnostics(); DiagnosticsEngine &Diag = Context.getDiagnostics();
@ -54,33 +55,6 @@ static void Report(const Decl *D, const char *message) {
Diag.Report(D->getBeginLoc(), ID); Diag.Report(D->getBeginLoc(), ID);
} }
CustomAttributesSet GetAttributes(const Decl *D) {
CustomAttributesSet attrs = {};
if (D->hasAttr<AnnotateAttr>()) {
Report(D, "Declaration has unhandled annotations.");
attrs = CacheAttributes(D);
} else {
auto attributes = AttributesCache.find(D);
if (attributes != AttributesCache.end()) {
attrs = attributes->second;
}
}
return attrs;
}
bool hasCustomAttribute(const clang::Decl *D, CustomAttributes A) {
CustomAttributesSet attrs = GetAttributes(D);
switch (A) {
#define ATTR(a) \
case a: \
return attrs.has_##a;
#include "CustomAttributes.inc"
#include "external/CustomAttributes.inc"
#undef ATTR
}
return false;
}
class CustomAttributesMatcher class CustomAttributesMatcher
: public ast_matchers::MatchFinder::MatchCallback { : public ast_matchers::MatchFinder::MatchCallback {
public: public:
@ -117,3 +91,36 @@ public:
static FrontendPluginRegistry::Add<CustomAttributesAction> static FrontendPluginRegistry::Add<CustomAttributesAction>
X("moz-custom-attributes", "prepare custom attributes for moz-check"); X("moz-custom-attributes", "prepare custom attributes for moz-check");
#endif
CustomAttributesSet GetAttributes(const Decl *D) {
CustomAttributesSet attrs = {};
if (D->hasAttr<AnnotateAttr>()) {
// If we are not in clang-tidy env push warnings, most likely we are in the
// build environment and this should have been done in AstMatcher -
// CustomAttributesMatcher
#ifndef CLANG_TIDY
Report(D, "Declaration has unhandled annotations.");
#endif
attrs = CacheAttributes(D);
} else {
auto attributes = AttributesCache.find(D);
if (attributes != AttributesCache.end()) {
attrs = attributes->second;
}
}
return attrs;
}
bool hasCustomAttribute(const clang::Decl *D, CustomAttributes A) {
CustomAttributesSet attrs = GetAttributes(D);
switch (A) {
#define ATTR(a) \
case a: \
return attrs.has_##a;
#include "CustomAttributes.inc"
#include "external/CustomAttributes.inc"
#undef ATTR
}
return false;
}