|
| 1 | +//===- unittests/AST/cheri/AttrTests.cpp --- CHERI Attribute tests --------===// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | + |
| 9 | +#include "clang/AST/Attr.h" |
| 10 | +#include "clang/ASTMatchers/ASTMatchFinder.h" |
| 11 | +#include "clang/ASTMatchers/ASTMatchers.h" |
| 12 | +#include "clang/Basic/AttrKinds.h" |
| 13 | +#include "clang/Tooling/Tooling.h" |
| 14 | +#include "gmock/gmock.h" |
| 15 | +#include "gtest/gtest.h" |
| 16 | + |
| 17 | +using namespace clang; |
| 18 | + |
| 19 | +namespace { |
| 20 | + |
| 21 | +using clang::ast_matchers::constantExpr; |
| 22 | +using clang::ast_matchers::equals; |
| 23 | +using clang::ast_matchers::functionDecl; |
| 24 | +using clang::ast_matchers::has; |
| 25 | +using clang::ast_matchers::hasDescendant; |
| 26 | +using clang::ast_matchers::hasName; |
| 27 | +using clang::ast_matchers::integerLiteral; |
| 28 | +using clang::ast_matchers::match; |
| 29 | +using clang::ast_matchers::selectFirst; |
| 30 | +using clang::ast_matchers::stringLiteral; |
| 31 | +using clang::ast_matchers::varDecl; |
| 32 | +using clang::tooling::buildASTFromCode; |
| 33 | +using clang::tooling::buildASTFromCodeWithArgs; |
| 34 | + |
| 35 | +template <typename Attr> |
| 36 | +testing::AssertionResult HasAttribute(const FunctionDecl *FD, |
| 37 | + const std::string attrName) { |
| 38 | + if (FD->hasAttr<Attr>()) |
| 39 | + return testing::AssertionSuccess(); |
| 40 | + |
| 41 | + return testing::AssertionFailure() |
| 42 | + << *FD << " doesn't have a " << attrName << " attribute"; |
| 43 | +} |
| 44 | + |
| 45 | +TEST(Attr, CHERICompartmentName) { |
| 46 | + // cheri_compartment name is a strange attribute which is both: |
| 47 | + // |
| 48 | + // * a function type attribute: affects the calling convention |
| 49 | + // * a function declaration attribute: marks the function as an compartment |
| 50 | + // entrypoint |
| 51 | + // |
| 52 | + // Thus this attribute is both handled by SemaType and SemaDeclAttr and in |
| 53 | + // order to not output twice a diagnosis we have to handle it specially. The |
| 54 | + // following test ensure we don't skip silently the attribute. |
| 55 | + |
| 56 | + auto AST = buildASTFromCode(R"cpp( |
| 57 | + #define cheri_compartment(name) __attribute__((cheri_compartment(name))) |
| 58 | + #define default_compartment cheri_compartment("default_compartment") |
| 59 | +
|
| 60 | + void default_compartment f_00(); |
| 61 | + default_compartment void f_01(); |
| 62 | + default_compartment void *f_03(); |
| 63 | + void default_compartment *f_04(); |
| 64 | + void * default_compartment f_05(); |
| 65 | + )cpp"); |
| 66 | + |
| 67 | + { |
| 68 | + for (auto Node : match(functionDecl().bind("fn"), AST->getASTContext())) { |
| 69 | + const FunctionDecl *FD = Node.getNodeAs<FunctionDecl>("fn"); |
| 70 | + |
| 71 | + EXPECT_TRUE( |
| 72 | + HasAttribute<CHERICompartmentNameAttr>(FD, "cheri_compartment")); |
| 73 | + }; |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +} // namespace |
0 commit comments