diff options
author | fridtjof <fridtjof@das-labor.org> | 2023-08-11 10:02:06 -0400 |
---|---|---|
committer | Aaron Ballman <aaron@aaronballman.com> | 2023-08-11 10:03:39 -0400 |
commit | 177ec17944af5fb87445017c3ac9028bbc1d710f (patch) | |
tree | b436490efc50f5942e9bf6bbd4a8d7fa90b5d7d3 /clang/unittests/libclang/LibclangTest.cpp | |
parent | e6f18b75d7ffeb6d5a5cb740a61f38f8f122fee1 (diff) | |
download | llvm-177ec17944af5fb87445017c3ac9028bbc1d710f.zip llvm-177ec17944af5fb87445017c3ac9028bbc1d710f.tar.gz llvm-177ec17944af5fb87445017c3ac9028bbc1d710f.tar.bz2 |
[libclang] Expose arguments of clang::annotate
This enables easy consumption of arbitrary data added
to this annotation in addition to the annotation category,
which was already exposed.
This is a re-application of the changes in
5aa06b18940c9b96cbf1c31da6aee3fbb92183ed which were reverted in
332a34c71e7675ab4e0ebd28b0d2c15302a81a51.
Differential Revision: https://reviews.llvm.org/D151373
Diffstat (limited to 'clang/unittests/libclang/LibclangTest.cpp')
-rw-r--r-- | clang/unittests/libclang/LibclangTest.cpp | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/clang/unittests/libclang/LibclangTest.cpp b/clang/unittests/libclang/LibclangTest.cpp index 295706c..60904bc 100644 --- a/clang/unittests/libclang/LibclangTest.cpp +++ b/clang/unittests/libclang/LibclangTest.cpp @@ -1246,6 +1246,52 @@ static_assert(true, message); EXPECT_EQ(fromCXString(clang_getCursorSpelling(*staticAssertCsr)), ""); } +TEST_F(LibclangParseTest, ExposesAnnotateArgs) { + const char testSource[] = R"cpp( +[[clang::annotate("category", 42)]] +void func() {} +)cpp"; + std::string fileName = "main.cpp"; + WriteFile(fileName, testSource); + + const char *Args[] = {"-xc++"}; + ClangTU = clang_parseTranslationUnit(Index, fileName.c_str(), Args, 1, + nullptr, 0, TUFlags); + + int attrCount = 0; + + Traverse( + [&attrCount](CXCursor cursor, CXCursor parent) -> CXChildVisitResult { + if (cursor.kind == CXCursor_AnnotateAttr) { + int childCount = 0; + clang_visitChildren( + cursor, + [](CXCursor child, CXCursor, + CXClientData data) -> CXChildVisitResult { + int *pcount = static_cast<int *>(data); + + // we only expect one argument here, so bail otherwise + EXPECT_EQ(*pcount, 0); + + auto *result = clang_Cursor_Evaluate(child); + EXPECT_NE(result, nullptr); + EXPECT_EQ(clang_EvalResult_getAsInt(result), 42); + clang_EvalResult_dispose(result); + + ++*pcount; + + return CXChildVisit_Recurse; + }, + &childCount); + attrCount++; + return CXChildVisit_Continue; + } + return CXChildVisit_Recurse; + }); + + EXPECT_EQ(attrCount, 1); +} + class LibclangRewriteTest : public LibclangParseTest { public: CXRewriter Rew = nullptr; |