summaryrefslogtreecommitdiff
path: root/clang/unittests/AST/AttrTest.cpp
blob: 500e9cfa528b79b3713694642b98727362f41d36 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
//===- unittests/AST/AttrTests.cpp --- Attribute 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/AST/Attr.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include "clang/ASTMatchers/ASTMatchers.h"
#include "clang/Basic/AttrKinds.h"
#include "clang/Tooling/Tooling.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"

using namespace clang;

namespace {

using clang::ast_matchers::constantExpr;
using clang::ast_matchers::equals;
using clang::ast_matchers::functionDecl;
using clang::ast_matchers::has;
using clang::ast_matchers::hasDescendant;
using clang::ast_matchers::hasName;
using clang::ast_matchers::integerLiteral;
using clang::ast_matchers::match;
using clang::ast_matchers::selectFirst;
using clang::ast_matchers::stringLiteral;
using clang::ast_matchers::varDecl;
using clang::tooling::buildASTFromCode;
using clang::tooling::buildASTFromCodeWithArgs;

TEST(Attr, Doc) {
  EXPECT_THAT(Attr::getDocumentation(attr::Used).str(),
              testing::HasSubstr("The compiler must emit the definition even "
                                 "if it appears to be unused"));
}

const FunctionDecl *getFunctionNode(ASTUnit *AST, const std::string &Name) {
  auto Result =
      match(functionDecl(hasName(Name)).bind("fn"), AST->getASTContext());
  EXPECT_EQ(Result.size(), 1u);
  return Result[0].getNodeAs<FunctionDecl>("fn");
}

const VarDecl *getVariableNode(ASTUnit *AST, const std::string &Name) {
  auto Result = match(varDecl(hasName(Name)).bind("var"), AST->getASTContext());
  EXPECT_EQ(Result.size(), 1u);
  return Result[0].getNodeAs<VarDecl>("var");
}

template <class ModifiedTypeLoc>
void AssertAnnotatedAs(TypeLoc TL, llvm::StringRef annotation,
                       ModifiedTypeLoc &ModifiedTL,
                       const AnnotateTypeAttr **AnnotateOut = nullptr) {
  const auto AttributedTL = TL.getAs<AttributedTypeLoc>();
  ASSERT_FALSE(AttributedTL.isNull());
  ModifiedTL = AttributedTL.getModifiedLoc().getAs<ModifiedTypeLoc>();
  ASSERT_TRUE(ModifiedTL);

  ASSERT_NE(AttributedTL.getAttr(), nullptr);
  const auto *Annotate = dyn_cast<AnnotateTypeAttr>(AttributedTL.getAttr());
  ASSERT_NE(Annotate, nullptr);
  EXPECT_EQ(Annotate->getAnnotation(), annotation);
  if (AnnotateOut) {
    *AnnotateOut = Annotate;
  }
}

TEST(Attr, AnnotateType) {

  // Test that the AnnotateType attribute shows up in the right places and that
  // it stores its arguments correctly.

  auto AST = buildASTFromCode(R"cpp(
    void f(int* [[clang::annotate_type("foo", "arg1", 2)]] *,
           int [[clang::annotate_type("bar")]]);

    int [[clang::annotate_type("int")]] * [[clang::annotate_type("ptr")]]
      array[10] [[clang::annotate_type("arr")]];

    void (* [[clang::annotate_type("funcptr")]] fp)(void);

    struct S { int mem; };
    int [[clang::annotate_type("int")]]
    S::* [[clang::annotate_type("ptr_to_mem")]] ptr_to_member = &S::mem;
  )cpp");

  {
    const FunctionDecl *Func = getFunctionNode(AST.get(), "f");

    // First parameter.
    const auto PointerTL = Func->getParamDecl(0)
                               ->getTypeSourceInfo()
                               ->getTypeLoc()
                               .getAs<PointerTypeLoc>();
    ASSERT_FALSE(PointerTL.isNull());
    PointerTypeLoc PointerPointerTL;
    const AnnotateTypeAttr *Annotate;
    AssertAnnotatedAs(PointerTL.getPointeeLoc(), "foo", PointerPointerTL,
                      &Annotate);

    EXPECT_EQ(Annotate->args_size(), 2u);
    const auto *StringLit = selectFirst<StringLiteral>(
        "str", match(constantExpr(hasDescendant(stringLiteral().bind("str"))),
                     *Annotate->args_begin()[0], AST->getASTContext()));
    ASSERT_NE(StringLit, nullptr);
    EXPECT_EQ(StringLit->getString(), "arg1");
    EXPECT_EQ(match(constantExpr(has(integerLiteral(equals(2u)).bind("int"))),
                    *Annotate->args_begin()[1], AST->getASTContext())
                  .size(),
              1u);

    // Second parameter.
    BuiltinTypeLoc IntTL;
    AssertAnnotatedAs(Func->getParamDecl(1)->getTypeSourceInfo()->getTypeLoc(),
                      "bar", IntTL);
    EXPECT_EQ(IntTL.getType(), AST->getASTContext().IntTy);
  }

  {
    const VarDecl *Var = getVariableNode(AST.get(), "array");

    ArrayTypeLoc ArrayTL;
    AssertAnnotatedAs(Var->getTypeSourceInfo()->getTypeLoc(), "arr", ArrayTL);
    PointerTypeLoc PointerTL;
    AssertAnnotatedAs(ArrayTL.getElementLoc(), "ptr", PointerTL);
    BuiltinTypeLoc IntTL;
    AssertAnnotatedAs(PointerTL.getPointeeLoc(), "int", IntTL);
    EXPECT_EQ(IntTL.getType(), AST->getASTContext().IntTy);
  }

  {
    const VarDecl *Var = getVariableNode(AST.get(), "fp");

    PointerTypeLoc PointerTL;
    AssertAnnotatedAs(Var->getTypeSourceInfo()->getTypeLoc(), "funcptr",
                      PointerTL);
    ASSERT_TRUE(
        PointerTL.getPointeeLoc().IgnoreParens().getAs<FunctionTypeLoc>());
  }

  {
    const VarDecl *Var = getVariableNode(AST.get(), "ptr_to_member");

    MemberPointerTypeLoc MemberPointerTL;
    AssertAnnotatedAs(Var->getTypeSourceInfo()->getTypeLoc(), "ptr_to_mem",
                      MemberPointerTL);
    BuiltinTypeLoc IntTL;
    AssertAnnotatedAs(MemberPointerTL.getPointeeLoc(), "int", IntTL);
    EXPECT_EQ(IntTL.getType(), AST->getASTContext().IntTy);
  }

  // Test type annotation on an `__auto_type` type in C mode.
  AST = buildASTFromCodeWithArgs(R"c(
    __auto_type [[clang::annotate_type("auto")]] auto_var = 1;
  )c",
                                 {"-fdouble-square-bracket-attributes"},
                                 "input.c");

  {
    const VarDecl *Var = getVariableNode(AST.get(), "auto_var");

    AutoTypeLoc AutoTL;
    AssertAnnotatedAs(Var->getTypeSourceInfo()->getTypeLoc(), "auto", AutoTL);
  }
}

} // namespace