From 38a4785b344df10236aedb9c22ef4f9f89a68b44 Mon Sep 17 00:00:00 2001 From: Andi-Bogdan Postelnicu Date: Mon, 27 Mar 2023 16:04:39 +0000 Subject: [PATCH] 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 --- build/clang-plugin/CustomAttributes.cpp | 61 ++++++++++++++----------- 1 file changed, 34 insertions(+), 27 deletions(-) diff --git a/build/clang-plugin/CustomAttributes.cpp b/build/clang-plugin/CustomAttributes.cpp index 886114e38449..d143f5856d7c 100644 --- a/build/clang-plugin/CustomAttributes.cpp +++ b/build/clang-plugin/CustomAttributes.cpp @@ -46,6 +46,7 @@ static CustomAttributesSet CacheAttributes(const Decl *D) { return attrs; } +#ifndef CLANG_TIDY static void Report(const Decl *D, const char *message) { ASTContext &Context = D->getASTContext(); DiagnosticsEngine &Diag = Context.getDiagnostics(); @@ -54,33 +55,6 @@ static void Report(const Decl *D, const char *message) { Diag.Report(D->getBeginLoc(), ID); } -CustomAttributesSet GetAttributes(const Decl *D) { - CustomAttributesSet attrs = {}; - if (D->hasAttr()) { - 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 : public ast_matchers::MatchFinder::MatchCallback { public: @@ -117,3 +91,36 @@ public: static FrontendPluginRegistry::Add X("moz-custom-attributes", "prepare custom attributes for moz-check"); +#endif + +CustomAttributesSet GetAttributes(const Decl *D) { + CustomAttributesSet attrs = {}; + if (D->hasAttr()) { +// 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; +}