tbourvon created this revision.
tbourvon added a reviewer: lebedev.ri.
tbourvon added a project: clang-tools-extra.
Herald added subscribers: hintonda, xazax.hun, mgorny.

This is a simple Lexer util to get the source text of a statement given as 
parameter. This is needed for https://reviews.llvm.org/D37014.


https://reviews.llvm.org/D42623

Files:
  clang-tidy/utils/LexerUtils.cpp
  clang-tidy/utils/LexerUtils.h
  unittests/clang-tidy/CMakeLists.txt
  unittests/clang-tidy/LexerUtilsTest.cpp

Index: unittests/clang-tidy/LexerUtilsTest.cpp
===================================================================
--- /dev/null
+++ unittests/clang-tidy/LexerUtilsTest.cpp
@@ -0,0 +1,51 @@
+#include "ClangTidyTest.h"
+#include "clang/Tooling/Tooling.h"
+#include "utils/LexerUtils.h"
+#include "gtest/gtest.h"
+
+namespace clang {
+namespace tidy {
+namespace test {
+
+class GetStmtTextTest : public ::testing::Test {
+protected:
+  GetStmtTextTest()
+    : Unit(CheckASTUnitNotNull(tooling::buildASTFromCode("void foo() { int bar = 0; }"))),
+      SM(Unit->getSourceManager())
+  {}
+
+  std::unique_ptr<ASTUnit> CheckASTUnitNotNull(std::unique_ptr<ASTUnit> Unit) {
+    EXPECT_TRUE(Unit);
+    return Unit;
+  }
+
+  std::unique_ptr<ASTUnit> Unit;
+  SourceManager& SM;
+};
+
+TEST_F(GetStmtTextTest, NullStatement) {
+  EXPECT_EQ(llvm::NoneType(), utils::lexer::getStmtText(nullptr, SM));
+}
+
+TEST_F(GetStmtTextTest, SimpleStatement) {
+  auto FooIdent = &Unit->getASTContext().Idents.getOwn("foo");
+  auto FooDeclName = Unit->getASTContext().DeclarationNames.getIdentifier(FooIdent);
+  auto FooLookup = Unit->getASTContext().getTranslationUnitDecl()->lookup(FooDeclName);
+  auto FooFunction = dyn_cast_or_null<FunctionDecl>(FooLookup.front());
+  EXPECT_TRUE(FooFunction);
+
+  auto FooBody = dyn_cast_or_null<CompoundStmt>(FooFunction->getBody());
+  EXPECT_TRUE(FooBody);
+  EXPECT_TRUE(FooBody->body_begin());
+
+  auto DeclStmt = *FooBody->body_begin();
+
+
+  auto Res = utils::lexer::getStmtText(DeclStmt, SM);
+  EXPECT_TRUE(Res.hasValue());
+  EXPECT_EQ("int bar = 0;", *Res);
+}
+
+} // namespace test
+} // namespace tidy
+} // namespace clang
Index: unittests/clang-tidy/CMakeLists.txt
===================================================================
--- unittests/clang-tidy/CMakeLists.txt
+++ unittests/clang-tidy/CMakeLists.txt
@@ -11,6 +11,7 @@
   ClangTidyOptionsTest.cpp
   IncludeInserterTest.cpp
   GoogleModuleTest.cpp
+  LexerUtilsTest.cpp
   LLVMModuleTest.cpp
   NamespaceAliaserTest.cpp
   ObjCModuleTest.cpp
Index: clang-tidy/utils/LexerUtils.h
===================================================================
--- clang-tidy/utils/LexerUtils.h
+++ clang-tidy/utils/LexerUtils.h
@@ -22,6 +22,9 @@
 Token getPreviousToken(const ASTContext &Context, SourceLocation Location,
                        bool SkipComments = true);
 
+/// Get source code text for statement.
+Optional<StringRef> getStmtText(const Stmt* Statement, const SourceManager& SM);
+
 } // namespace lexer
 } // namespace utils
 } // namespace tidy
Index: clang-tidy/utils/LexerUtils.cpp
===================================================================
--- clang-tidy/utils/LexerUtils.cpp
+++ clang-tidy/utils/LexerUtils.cpp
@@ -35,6 +35,20 @@
   return Token;
 }
 
+Optional<StringRef> getStmtText(const Stmt* Statement, const SourceManager& SM) {
+  if (!Statement) {
+    return llvm::NoneType();
+  }
+
+  bool Error = false;
+  auto Ret = Lexer::getSourceText(
+    CharSourceRange::getTokenRange(Statement->getSourceRange()),
+    SM, LangOptions(),
+    &Error);
+
+  return Error ? llvm::NoneType() : Optional<StringRef>(Ret);
+}
+
 } // namespace lexer
 } // namespace utils
 } // namespace tidy
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to