diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c00ba5e --- /dev/null +++ b/.gitignore @@ -0,0 +1,9 @@ +*~ +CMakeCache.txt +CMakeFiles/ +CTestTestfile.cmake +Makefile +Testing/ +bin/ +cmake_install.cmake +lib/ \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..7376b4e --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,52 @@ +cmake_minimum_required(VERSION 2.8) + +# tool name +set(TOOL clang-xform) + +if (NOT DEFINED LLVM_ROOT) + message(FATAL_ERROR "Clang 8.0.0 required. Please provide LLVM root path.\n" + "Usage: cmake -DLLVM_ROOT= \n" + "Clang prebuilt binaries are available at http://releases.llvm.org/download.html") +endif (NOT DEFINED LLVM_ROOT) + +find_package(Clang REQUIRED CONFIG + HINTS "${LLVM_ROOT}/lib/cmake/clang") + +message(STATUS "Found LLVM ${LLVM_PACKAGE_VERSION}") +message(STATUS "Using LLVMConfig.cmake in: ${LLVM_DIR}") + +# source files +file(GLOB_RECURSE SRC_CPP + src/*.cpp +) + +# include/link path +include_directories(${LLVM_INCLUDE_DIRS}) +include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include) +link_directories(${LLVM_LIBRARY_DIRS}) + +# set compile_options +if (NOT ${CMAKE_SYSTEM_NAME} MATCHES "Windows") + add_definitions(${LLVM_DEFINITIONS} -DCXXOPTS_NO_RTTI) + add_compile_options(-std=c++14 -fno-rtti) +else () + add_definitions(${LLVM_DEFINITIONS} /DCXXOPTS_NO_RTTI) + add_compile_options(/std:c++14 /GR-) +endif() + +# clang libs to link +set(CLANG_LIBS clangTooling clangToolingCore clangFrontendTool clangFrontend clangDriver clangBasic) +set(CLANG_LIBS ${CLANG_LIBS} clangSerialization clangParse clangSema clangAnalysis clangEdit) +set(CLANG_LIBS ${CLANG_LIBS} clangRewrite clangRewriteFrontend clangAST clangASTMatchers clangLex) +set(CLANG_LIBS ${CLANG_LIBS} clangToolingRefactor clangFormat clangToolingInclusions) + +# enable testing +enable_testing() +option(BUILD_TESTS "Set to ON to build tests" OFF) + +set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/bin) + +add_executable(${TOOL} ${SRC_CPP}) +target_link_libraries(${TOOL} ${CLANG_LIBS}) + +add_subdirectory(test) diff --git a/include/ApplyReplacements.hpp b/include/ApplyReplacements.hpp new file mode 100644 index 0000000..bbd443f --- /dev/null +++ b/include/ApplyReplacements.hpp @@ -0,0 +1,119 @@ +//===-- ApplyReplacements.hpp - Deduplicate and apply replacements -- C++ -*-===// +// +// 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 +// +//===----------------------------------------------------------------------===// +/// +/// \file +/// \brief This file provides the interface for deduplicating, detecting +/// conflicts in, and applying collections of Replacements. +/// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_CLANG_APPLYREPLACEMENTS_HPP +#define LLVM_CLANG_APPLYREPLACEMENTS_HPP + +#include "clang/Tooling/Core/Diagnostic.h" +#include "clang/Tooling/Refactoring.h" +#include "clang/Tooling/Refactoring/AtomicChange.h" +#include "llvm/ADT/StringMap.h" +#include "llvm/ADT/StringRef.h" +#include +#include +#include + +namespace clang { + +class DiagnosticsEngine; +class Rewriter; + +namespace replace { + +/// \brief Collection of TranslationUnitReplacements. +typedef std::vector TUReplacements; + +/// \brief Collection of TranslationUnitReplacement files. +typedef std::vector TUReplacementFiles; + +/// \brief Collection of TranslationUniDiagnostics. +typedef std::vector TUDiagnostics; + +/// \brief Map mapping file name to a set of AtomicChange targeting that file. +typedef llvm::DenseMap> + FileToChangesMap; + +/// \brief Attempts to deserialize the given yaml file as +/// TranslationUnitReplacements. All docs that successfully deserialize are +/// added to \p TUs. +/// +/// \param[in] FilePath File path to read for serialized +/// TranslationUnitReplacements. +/// \param[out] TUs Collection of all found and deserialized +/// TranslationUnitReplacements or TranslationUnitDiagnostics. +/// \param[in] Diagnostics DiagnosticsEngine used for error output. +/// +/// \returns A boolean indicating success or failure in navigating the +/// directory structure. true for success and false for failure +bool collectReplacementsFromFile( + const llvm::StringRef FilePath, TUReplacements &TUs, + clang::DiagnosticsEngine &Diagnostics); + +bool collectReplacementsFromFile( + const llvm::StringRef FilePath, TUDiagnostics &TUs, + clang::DiagnosticsEngine &Diagnostics); + +/// \brief Deduplicate, check for conflicts, and extract all Replacements stored +/// in \c TUs. Conflicting replacements are skipped. +/// +/// \post For all (key,value) in FileChanges, value[i].getOffset() <= +/// value[i+1].getOffset(). +/// +/// \param[in] TUs Collection of TranslationUnitReplacements or +/// TranslationUnitDiagnostics to merge, deduplicate, and test for conflicts. +/// \param[out] FileChanges Container grouping all changes by the +/// file they target. Only non conflicting replacements are kept into +/// FileChanges. +/// \param[in] SM SourceManager required for conflict reporting. +/// +/// \returns \parblock +/// \li true If all changes were converted successfully. +/// \li false If there were conflicts. +bool mergeAndDeduplicate(const TUReplacements &TUs, const TUDiagnostics &TUDs, + FileToChangesMap &FileChanges, + clang::SourceManager &SM); + +/// \brief Apply \c AtomicChange on File and rewrite it. +/// +/// \param[in] File Path of the file where to apply AtomicChange. +/// \param[in] Changes to apply. +/// \param[in] Spec For code cleanup and formatting. +/// \param[in] Diagnostics DiagnosticsEngine used for error output. +/// +/// \returns The changed code if all changes are applied successfully; +/// otherwise, an llvm::Error carrying llvm::StringError or an error_code. +llvm::Expected +applyChanges(StringRef File, const std::vector &Changes, + const tooling::ApplyChangesSpec &Spec, + DiagnosticsEngine &Diagnostics); + +/// \brief Delete the replacement file. +/// +/// \param[in] File Replacement file to delete. +/// \param[in] Diagnostics DiagnosticsEngine used for error output. +/// +/// \returns \parblock +/// \li true If all files have been deleted successfully. +/// \li false If at least one or more failures occur when deleting +/// files. +bool deleteReplacementFile(const llvm::StringRef File, + clang::DiagnosticsEngine &Diagnostics); + +bool applyReplacements(const llvm::StringRef File, const llvm::StringRef Output = ""); + +} // end namespace replace +} // end namespace clang + +#endif // LLVM_CLANG_APPLYREPLACEMENTS_HPP diff --git a/include/Logger.hpp b/include/Logger.hpp new file mode 100644 index 0000000..1a796da --- /dev/null +++ b/include/Logger.hpp @@ -0,0 +1,207 @@ +#ifndef LOGGER_HPP +#define LOGGER_HPP + +#include +#include +#include +#include +#include +#include +#include +#include + +// a simple logging class + +enum severity {trace, debug, info, warning, error, fatal}; + +enum verbosity {quiet, minimal, normal, verbose}; + +namespace detail{ +const std::string severity_string[6] = {"trace", + "debug", + "info", + "warning", + "error", + "fatal"}; +} // end namespace detail + +template +class Log +{ + public: + Log() = default; + Log(const Log&) = delete; + Log& operator =(const Log&) = delete; + virtual ~Log() { + if (Verbosity() != verbosity::quiet) { + OStream::Output(msg_.str()); + } + } + std::ostringstream& Get(severity level = severity::info) { + switch (Verbosity()) { + case verbosity::quiet: + break; + case verbosity::minimal: + break; + case verbosity::normal: + OutputAttributes(msg_); + break; + case verbosity::verbose: + OutputAttributes(msg_); + msg_ << std::setw(7) << detail::severity_string[level] << " | "; + break; + } + return msg_; + } + + static severity& Severity() { + static severity level = severity::info; + return level; + } + + static verbosity& Verbosity() { + static verbosity level = verbosity::normal; + return level; + } + + protected: + template + std::ostream& OutputAttributes(std::ostream& os) { + Attrib::Output(os); + os << " | "; + return os; + } + + template 0)> > + std::ostream& OutputAttributes(std::ostream& os) { + Attrib1::Output(os); + os << " | "; + return OutputAttributes(os); + } + + template > + std::ostream& OutputAttributes(std::ostream& os) { + return os; + } + + std::ostringstream msg_; +}; + +// attributes +class Counter { + public: + static std::ostream& Output(std::ostream& os) { + return os << "No. " << ++Count(); + } + private: + static int& Count() { + static int n = 0; + return n; + } +}; + +class ThreadID { + public: + static std::ostream& Output(std::ostream& os) { + return os << "T." << std::this_thread::get_id(); + } +}; + +class TimeStamp { + public: + static std::ostream& Output(std::ostream& os) { + std::time_t current_time = std::time(nullptr); + std::string current_time_string = std::ctime(¤t_time); + return os << current_time_string.substr(0, current_time_string.length() - 1); + } +}; + +// ostreams +class FileStream { + public: + static void SetStream(std::ofstream& stream) { + std::lock_guard guard(Mutex()); + GetStream() = &stream; + } + static void Output(const std::string& msg) { + std::lock_guard guard(Mutex()); + std::ofstream* stream = GetStream(); + if (!stream || !stream->is_open()) + return; + + int tmp = msg.length(); + stream->write(msg.c_str(), tmp); + stream->flush(); + } + private: + static std::ofstream*& GetStream() { + static std::ofstream* stream = nullptr; + return stream; + } + static std::mutex& Mutex() { + static std::mutex m; + return m; + } +}; + +class STDCStream { + public: + static void SetStream(std::ostream& stream) { + std::lock_guard guard(Mutex()); + GetStream() = &stream; + } + static void Output(const std::string& msg) { + std::lock_guard guard(Mutex()); + std::ostream* stream = GetStream(); + + stream->write(msg.c_str(), msg.length()); + stream->flush(); + } + private: + static std::ostream*& GetStream() { + static std::ostream* stream = &std::cout; + return stream; + } + static std::mutex& Mutex() { + static std::mutex m; + return m; + } +}; + +// helper class to set output file +class RegisterLogFile { + public: + RegisterLogFile(const std::string& name) + : ofs_(name) + { + FileStream::SetStream(ofs_); + // set file in compilation mode in emacs + ofs_ << "-*- compilation-minor -*-" << "\n\n"; + } + ~RegisterLogFile() { + Close(); + } + void Close() { + if (ofs_.is_open()) { + ofs_.close(); + } + } + private: + std::ofstream ofs_; +}; + + +using FileLog = Log; +using TrivialLog = Log; + +#define FILE_LOG(level) \ + if (level < FileLog::Severity()); \ + else FileLog().Get(level) + +#define TRIVIAL_LOG(level) \ + if (level < TrivialLog::Severity()); \ + else TrivialLog().Get(level) + +#endif diff --git a/include/MatcherFactory.hpp b/include/MatcherFactory.hpp new file mode 100644 index 0000000..6649edc --- /dev/null +++ b/include/MatcherFactory.hpp @@ -0,0 +1,75 @@ +#ifndef MATCHER_FACTORY_HPP +#define MATCHER_FACTORY_HPP + +#include +#include +#include +#include + +#include + +#include "clang/ASTMatchers/ASTMatchFinder.h" +#include "clang/Tooling/Core/Replacement.h" + +class MatcherFactory : private boost::noncopyable { + public: + typedef clang::ast_matchers::MatchFinder::MatchCallback MatchCallback; + typedef clang::ast_matchers::internal::DynTypedMatcher DynTypedMatcher; + typedef std::unique_ptr (*CreateCallbackFunction)(clang::tooling::Replacements&); + + static MatcherFactory& Instance() { + static MatcherFactory factory; + return factory; + } + + void RegisterMatcher(const std::string& id, + const std::vector& matchers, + CreateCallbackFunction fcn) { + matcher_map_.emplace(id, std::make_pair(matchers, fcn)); + } + + std::unique_ptr + CreateCallback(const std::string& id, clang::tooling::Replacements& replacements) const { + auto iter = matcher_map_.find(id); + if (iter == matcher_map_.end()) { + return nullptr; + } else { + return (iter->second.second)(replacements); + } + } + + const std::vector* + CreateMatchers(const std::string& id) const { + auto iter = matcher_map_.find(id); + if (iter == matcher_map_.end()) { + return nullptr; + } else { + return &(iter->second.first); + } + } + + const std::map, CreateCallbackFunction> >& + getMatcherMap() const { + return matcher_map_; + } + + private: + std::map, CreateCallbackFunction> > matcher_map_; +}; + +template +class MatcherHelper { + public: + typedef clang::ast_matchers::MatchFinder::MatchCallback MatchCallback; + typedef clang::ast_matchers::internal::DynTypedMatcher DynTypedMatcher; + + MatcherHelper(const std::string& id, const std::vector& matchers) { + MatcherFactory& factory = MatcherFactory::Instance(); + factory.RegisterMatcher(id, matchers, MatcherHelper::CreateCallback); + } + static std::unique_ptr CreateCallback(clang::tooling::Replacements& replacements) { + return std::make_unique(replacements); + } +}; + +#endif diff --git a/include/MyASTMatchers.hpp b/include/MyASTMatchers.hpp new file mode 100644 index 0000000..eda0419 --- /dev/null +++ b/include/MyASTMatchers.hpp @@ -0,0 +1,182 @@ +#ifndef MY_AST_MATCHERS_HPP +#define MY_AST_MATCHERS_HPP + +#include "clang/ASTMatchers/ASTMatchers.h" + +// self-defined AST Matchers and + +namespace clang { +namespace ast_matchers { + +// match if node is real floating type +AST_MATCHER(QualType, isRealFloating) { + return Node->isRealFloatingType(); +} + +AST_MATCHER(BinaryOperator, isComparisonOperator) { + return Node.isComparisonOp(); +} + +AST_MATCHER(CallExpr, hasSLSizeCmpDecl) { + if (auto decl = Node.getDirectCallee()) { + std::string name = decl->getNameAsString(); + if (name == "DimEqualTo" || + name == "DimNotEqual" || + name == "DimLess" || + name == "DimLessEqual" || + name == "DimGreater" || + name == "DimGreaterEqual") { + return true; + } + } + return false; +} + +AST_MATCHER(CallExpr, hasSLSizeCastDecl) { + if (auto decl = Node.getDirectCallee()) { + std::string name = decl->getNameAsString(); + if (name == "DimValue2Int" || + name == "DimValue2Sizet" || + name == "DimValue2SLSize") { + return true; + } + } + return false; +} + +// match if the value type is SLSize +AST_MATCHER(QualType, isSLSizeType) { + if (Node.isNull()) return false; + + auto typeName = Node.getUnqualifiedType().getAsString(); + + if (typeName == "SLSize" || typeName == "SLIndex") { + return true; + } + + return false; +} + +// match is the expr is SLSize type variable, +// value returned by vector index operator +AST_MATCHER(Expr, isBasicSLSizeExpr) { + BoundNodesTreeBuilder tmpBuilder; + if (qualType(isSLSizeType()).matches(Node.getType(), Finder, &tmpBuilder)) { + return true; + } + // value returned by vector index operator + else if (auto cxxOperatorCallExpr = llvm::dyn_cast(&Node)) { + auto argTypeName = cxxOperatorCallExpr->getArg(0)->getType().getUnqualifiedType().getAsString(); + if (argTypeName == "vector" || argTypeName == "std::vector") return true; + } + return false; +} + +// match if the result of the arithmetic expression is SLSize +AST_MATCHER(Expr, isArithmeticOperatorWithSLSize) { + BoundNodesTreeBuilder tmpBuilder; + if (auto binaryOperator = llvm::dyn_cast(&Node)) + { + // Node is Binaryoperator + if (binaryOperator->isAdditiveOp() || + binaryOperator->isMultiplicativeOp() || + binaryOperator->isShiftOp()) { + // is arithmetic operator + if (expr(isBasicSLSizeExpr()).matches(*(binaryOperator->getLHS()->IgnoreParenImpCasts()), + Finder, &tmpBuilder) || + expr(isBasicSLSizeExpr()).matches(*(binaryOperator->getRHS()->IgnoreParenImpCasts()), + Finder, &tmpBuilder)) { + return true; + } + } + } + if (auto unaryOperator = llvm::dyn_cast(&Node)) + { + // Node is Unaryoperator + if (unaryOperator->isArithmeticOp() || + unaryOperator->isIncrementDecrementOp()) { + // is arithmetic operator or IncrementDecrement operator + if (expr(isBasicSLSizeExpr()).matches(*(unaryOperator->getSubExpr()->IgnoreParenImpCasts()), + Finder, &tmpBuilder)) { + return true; + } + } + } + if (auto callExpr = llvm::dyn_cast(&Node)) + { + // Node is call expr + if (auto fcnDecl = callExpr->getDirectCallee()) { + auto fcnName = fcnDecl->getNameAsString(); + if (fcnName == "max" || + fcnName == "min" || + fcnName == "std::max" || + fcnName == "std::min") { + // is max() or min() + if (callExpr->getNumArgs() == 2 && + expr(isBasicSLSizeExpr()).matches(*(callExpr->getArg(0)->IgnoreParenImpCasts()), + Finder, &tmpBuilder)) { + return true; + } + } + } + } + return false; +} + +// match if the expression is SLSize type +AST_MATCHER(Expr, isSLSizeExpr) { + BoundNodesTreeBuilder tmpBuilder; + if (expr(isBasicSLSizeExpr()).matches(Node, Finder, &tmpBuilder) || + expr(isArithmeticOperatorWithSLSize()).matches(Node, Finder, &tmpBuilder)) { + return true; + } + return false; +} + +// match if the type is int +AST_MATCHER(QualType, isIntType) { + if (Node.isNull()) return false; + + auto typeName = Node.getUnqualifiedType().getAsString(); + auto canonicalTypeName = Node.getCanonicalType().getUnqualifiedType().getAsString(); + if (canonicalTypeName == "int" && typeName != "SLSize" && typeName != "SLIndex") { + return true; + } + return false; +} + +// match if the expr is int type +AST_MATCHER(Expr, isIntExpr) { + BoundNodesTreeBuilder tmpBuilder; + if (qualType(isIntType()).matches(Node.getType(), Finder, &tmpBuilder) && + !expr(isSLSizeExpr()).matches(Node, Finder, &tmpBuilder)) { + return true; + } + return false; +} + +// match if the type is size_t +AST_MATCHER(QualType, isSizetType) { + if (Node.isNull()) return false; + + auto typeName = Node.getUnqualifiedType().getAsString(); + if (typeName == "size_t") { + return true; + } + return false; +} + +// match is the expr is size_t +AST_MATCHER(Expr, isSizetExpr) { + BoundNodesTreeBuilder tmpBuilder; + if (qualType(isSizetType()).matches(Node.getType(), Finder, &tmpBuilder) && + !expr(isSLSizeExpr()).matches(Node, Finder, &tmpBuilder)) { + return true; + } + return false; +} + +} // end clang namespace +} // end ast_matchers namespace + +#endif diff --git a/include/MyFrontendAction.hpp b/include/MyFrontendAction.hpp new file mode 100644 index 0000000..e8902fe --- /dev/null +++ b/include/MyFrontendAction.hpp @@ -0,0 +1,49 @@ +#ifndef MY_FRONTEND_ACTION_HPP +#define MY_FRONTEND_ACTION_HPP + +#include +#include +#include +#include + +#include "clang/Frontend/FrontendActions.h" +#include "clang/ASTMatchers/ASTMatchFinder.h" +#include "clang/Tooling/Core/Replacement.h" +#include "clang/Frontend/CompilerInstance.h" +#include "clang/Tooling/Tooling.h" + +class MyFrontendAction : public clang::ASTFrontendAction +{ + public: + explicit MyFrontendAction(const std::string& outputFile, + const std::vector& matchers); + + protected: + virtual std::unique_ptr + CreateASTConsumer(clang::CompilerInstance &Compiler, llvm::StringRef InFile) override + { + return mFinder.newASTConsumer(); + } + virtual bool BeginSourceFileAction (clang::CompilerInstance &CI) override; + virtual void EndSourceFileAction() override; + private: + std::reference_wrapper mOutputFile; + clang::ast_matchers::MatchFinder mFinder; + clang::tooling::Replacements mReplacements; + std::vector > mCallbacks; + static std::mutex mMutex; +}; + +class MyFrontendActionFactory : public clang::tooling::FrontendActionFactory { + public: + MyFrontendActionFactory(const std::string& outputFile, const std::vector& matchers) + : mOutputFile(outputFile), mMatchers(matchers) {} + + clang::FrontendAction *create() override { return new MyFrontendAction(mOutputFile.get(), mMatchers.get()); } + private: + std::reference_wrapper mOutputFile; + std::reference_wrapper > mMatchers; +}; + + +#endif diff --git a/include/MyMatchCallback.hpp b/include/MyMatchCallback.hpp new file mode 100644 index 0000000..73d34bd --- /dev/null +++ b/include/MyMatchCallback.hpp @@ -0,0 +1,50 @@ +#ifndef MY_MATCH_CALLBACK_HPP +#define MY_MATCH_CALLBACK_HPP + +#include + +#include "clang/ASTMatchers/ASTMatchFinder.h" +#include "clang/Tooling/Core/Replacement.h" + +class MyMatchCallback : public clang::ast_matchers::MatchFinder::MatchCallback { + public : + explicit MyMatchCallback(clang::tooling::Replacements& replacements) + : mReplacements(replacements) + {} + + llvm::Error AddReplacement(const clang::tooling::Replacement& R) { + return mReplacements.get().add(R); + } + // return 0 for success + llvm::Error ReplaceText (const clang::SourceManager &Sources, clang::SourceLocation Start, + unsigned OrigLength, llvm::StringRef NewStr) { + return AddReplacement(clang::tooling::Replacement(Sources, + Start, + OrigLength, + NewStr)); + } + llvm::Error ReplaceText (const clang::SourceManager &Sources, + clang::SourceRange range, + llvm::StringRef NewStr, + const clang::LangOptions &LangOpts=clang::LangOptions()) { + return AddReplacement(clang::tooling::Replacement(Sources, + clang::CharSourceRange::getTokenRange(range), + NewStr, + LangOpts)); + } + + private: + std::reference_wrapper mReplacements; +}; + + +#define MATCH_CALLBACK(NAME) \ +class NAME : public MyMatchCallback { \ + public : \ + explicit NAME (clang::tooling::Replacements& replacements) \ + : MyMatchCallback(replacements) \ + {} \ + virtual void run(const clang::ast_matchers::MatchFinder::MatchResult &Result) override; \ +} + +#endif diff --git a/include/MyReplacementsYaml.hpp b/include/MyReplacementsYaml.hpp new file mode 100644 index 0000000..9c3d580 --- /dev/null +++ b/include/MyReplacementsYaml.hpp @@ -0,0 +1,78 @@ +//===-- MyReplacementsYaml.hpp -- Serialiazation for Replacements ---*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +/// +/// \file +/// This file defines the structure of a YAML document for serializing +/// replacements. This is a modified version based on the original file +/// "clang/Tooling/ReplacementsYaml.h" to support storing absolute file path +/// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_CLANG_TOOLING_REPLACEMENTSYAML_H +#define LLVM_CLANG_TOOLING_REPLACEMENTSYAML_H + +#include "clang/Tooling/Tooling.h" +#include "clang/Tooling/Refactoring.h" +#include "llvm/Support/YAMLTraits.h" +#include + +LLVM_YAML_IS_SEQUENCE_VECTOR(clang::tooling::Replacement) + +namespace llvm { +namespace yaml { + +/// Specialized MappingTraits to describe how a Replacement is +/// (de)serialized. +template <> struct MappingTraits { + /// Helper to (de)serialize a Replacement since we don't have direct + /// access to its data members. + struct NormalizedReplacement { + NormalizedReplacement(const IO &) + : FilePath(""), Offset(0), Length(0), ReplacementText("") {} + + NormalizedReplacement(const IO &, const clang::tooling::Replacement &R) + : FilePath(clang::tooling::getAbsolutePath(R.getFilePath())), + Offset(R.getOffset()), + Length(R.getLength()), + ReplacementText(R.getReplacementText()) {} + + clang::tooling::Replacement denormalize(const IO &) { + return clang::tooling::Replacement(FilePath, Offset, Length, + ReplacementText); + } + + std::string FilePath; + unsigned int Offset; + unsigned int Length; + std::string ReplacementText; + }; + + static void mapping(IO &Io, clang::tooling::Replacement &R) { + MappingNormalization + Keys(Io, R); + Io.mapRequired("FilePath", Keys->FilePath); + Io.mapRequired("Offset", Keys->Offset); + Io.mapRequired("Length", Keys->Length); + Io.mapRequired("ReplacementText", Keys->ReplacementText); + } +}; + +/// Specialized MappingTraits to describe how a +/// TranslationUnitReplacements is (de)serialized. +template <> struct MappingTraits { + static void mapping(IO &Io, + clang::tooling::TranslationUnitReplacements &Doc) { + Io.mapRequired("MainSourceFile", Doc.MainSourceFile); + Io.mapRequired("Replacements", Doc.Replacements); + } +}; +} // end namespace yaml +} // end namespace llvm + +#endif diff --git a/include/ProgramOptions.hpp b/include/ProgramOptions.hpp new file mode 100644 index 0000000..1a6f854 --- /dev/null +++ b/include/ProgramOptions.hpp @@ -0,0 +1,44 @@ +/// +/// @file ProgramOptions.hpp +/// +/// @Copyright 2018 The MathWorks, Inc. + +#include +#include +#include + +#ifndef PROGRAM_OPTIONS_HPP +#define PROGRAM_OPTIONS_HPP + +// Command line arguments. +struct CommandLineArgs +{ + CommandLineArgs() = default; + + // list of files to parse (empty means all files) + std::vector inputFiles; + // number of threads + int numThreads = std::numeric_limits::max(); + // compile commands + std::string compileCommands; + // output file + std::string outputFile; + // replacement file + std::string replaceFile; + // matchers to apply + std::vector matchers; + // config file + std::string configFile; + // display registered matchers + bool display = false; + // silent output + bool quiet = false; + // log file + std::string logFile; +}; + +// Parse the command line arguments. +CommandLineArgs ProcessCommandLine(int argc, char**argv); + + +#endif // PROGRAM_OPTIONS_HPP diff --git a/include/TestingUtil.hpp b/include/TestingUtil.hpp new file mode 100644 index 0000000..a936f95 --- /dev/null +++ b/include/TestingUtil.hpp @@ -0,0 +1,13 @@ +#ifndef TESTING_UTIL_HPP +#define TESTING_UTIL_HPP + +#include + +bool CompareFiles(const std::string& p1, const std::string& p2); + +bool InitTest(const std::string& dirPath, + const std::string& outputFile); + +bool IsEmptyFile(const std::string& file); + +#endif diff --git a/include/ToolingUtil.hpp b/include/ToolingUtil.hpp new file mode 100644 index 0000000..ef6168f --- /dev/null +++ b/include/ToolingUtil.hpp @@ -0,0 +1,62 @@ +#ifndef TOOLING_UTIL_HPP +#define TOOLING_UTIL_HPP + +#include + +#include "clang/Frontend/TextDiagnosticPrinter.h" +#include "clang/Basic/SourceLocation.h" + +// execute the given command line +int ExecCmd(const std::string& cmd, std::string& result); +inline int ExecCmd(const std::string& cmd) { + std::string tmp; + return ExecCmd(cmd, tmp); +} + +// parse config file and return string values for a given key +// return true if succeed +bool ParseConfigFile(const std::string& fileName, const std::string& key, std::vector& args); + +/** + * Ignoring all diagnostic messages generated by libTooling unless + * libTooling actually fails (returns non-zero). + */ +class DiagnosticLogger final : public clang::TextDiagnosticPrinter { + public: + using clang::TextDiagnosticPrinter::TextDiagnosticPrinter; + + void HandleDiagnostic(clang::DiagnosticsEngine::Level level, + const clang::Diagnostic& info) override { + clang::TextDiagnosticPrinter::HandleDiagnostic(std::move(level), info); + NumErrors = 0u; // ignore any/all errors encountered. + } + + bool IncludeInDiagnosticCounts() const override { + return false; // ignore any/all errors encountered. + } +}; + +// forward declaration +namespace clang { +class SourceManager; +class LangOptions; +} + +// self-defined functions to get SourceLocation for expression in MACRO +clang::SourceLocation getExpansionLocStart(clang::SourceLocation loc, const clang::SourceManager& sm); +clang::SourceLocation getExpansionLocEnd(clang::SourceLocation loc, const clang::SourceManager& sm); + +// self-define functions to retrieve source code context from Lexer module +std::string getSourceText(clang::SourceLocation start, + clang::SourceLocation end, + const clang::SourceManager& sm, + const clang::LangOptions &langOpts); + +// convinent function for logging +void LogReplacement(clang::SourceLocation loc, const clang::SourceManager& sm, + const std::string& oldExpr, const std::string& newExpr); + +void LogASTNode(clang::SourceLocation loc, const clang::SourceManager& sm, + const std::string& expr); + +#endif diff --git a/include/cxxopts.hpp b/include/cxxopts.hpp new file mode 100644 index 0000000..41712f0 --- /dev/null +++ b/include/cxxopts.hpp @@ -0,0 +1,2077 @@ +/* +Copyright (c) 2014, 2015, 2016, 2017 Jarryd Beck +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +*/ + +#ifndef CXXOPTS_HPP_INCLUDED +#define CXXOPTS_HPP_INCLUDED + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#ifdef __cpp_lib_optional +#include +#define CXXOPTS_HAS_OPTIONAL +#endif + +#define CXXOPTS__VERSION_MAJOR 2 +#define CXXOPTS__VERSION_MINOR 2 +#define CXXOPTS__VERSION_PATCH 0 + +namespace cxxopts +{ + static constexpr struct { + uint8_t major, minor, patch; + } version = { + CXXOPTS__VERSION_MAJOR, + CXXOPTS__VERSION_MINOR, + CXXOPTS__VERSION_PATCH + }; +} + +//when we ask cxxopts to use Unicode, help strings are processed using ICU, +//which results in the correct lengths being computed for strings when they +//are formatted for the help output +//it is necessary to make sure that can be found by the +//compiler, and that icu-uc is linked in to the binary. + +#ifdef CXXOPTS_USE_UNICODE +#include + +namespace cxxopts +{ + typedef icu::UnicodeString String; + + inline + String + toLocalString(std::string s) + { + return icu::UnicodeString::fromUTF8(std::move(s)); + } + + class UnicodeStringIterator : public + std::iterator + { + public: + + UnicodeStringIterator(const icu::UnicodeString* string, int32_t pos) + : s(string) + , i(pos) + { + } + + value_type + operator*() const + { + return s->char32At(i); + } + + bool + operator==(const UnicodeStringIterator& rhs) const + { + return s == rhs.s && i == rhs.i; + } + + bool + operator!=(const UnicodeStringIterator& rhs) const + { + return !(*this == rhs); + } + + UnicodeStringIterator& + operator++() + { + ++i; + return *this; + } + + UnicodeStringIterator + operator+(int32_t v) + { + return UnicodeStringIterator(s, i + v); + } + + private: + const icu::UnicodeString* s; + int32_t i; + }; + + inline + String& + stringAppend(String&s, String a) + { + return s.append(std::move(a)); + } + + inline + String& + stringAppend(String& s, int n, UChar32 c) + { + for (int i = 0; i != n; ++i) + { + s.append(c); + } + + return s; + } + + template + String& + stringAppend(String& s, Iterator begin, Iterator end) + { + while (begin != end) + { + s.append(*begin); + ++begin; + } + + return s; + } + + inline + size_t + stringLength(const String& s) + { + return s.length(); + } + + inline + std::string + toUTF8String(const String& s) + { + std::string result; + s.toUTF8String(result); + + return result; + } + + inline + bool + empty(const String& s) + { + return s.isEmpty(); + } +} + +namespace std +{ + inline + cxxopts::UnicodeStringIterator + begin(const icu::UnicodeString& s) + { + return cxxopts::UnicodeStringIterator(&s, 0); + } + + inline + cxxopts::UnicodeStringIterator + end(const icu::UnicodeString& s) + { + return cxxopts::UnicodeStringIterator(&s, s.length()); + } +} + +//ifdef CXXOPTS_USE_UNICODE +#else + +namespace cxxopts +{ + typedef std::string String; + + template + T + toLocalString(T&& t) + { + return std::forward(t); + } + + inline + size_t + stringLength(const String& s) + { + return s.length(); + } + + inline + String& + stringAppend(String&s, String a) + { + return s.append(std::move(a)); + } + + inline + String& + stringAppend(String& s, size_t n, char c) + { + return s.append(n, c); + } + + template + String& + stringAppend(String& s, Iterator begin, Iterator end) + { + return s.append(begin, end); + } + + template + std::string + toUTF8String(T&& t) + { + return std::forward(t); + } + + inline + bool + empty(const std::string& s) + { + return s.empty(); + } +} + +//ifdef CXXOPTS_USE_UNICODE +#endif + +namespace cxxopts +{ + namespace + { +#ifdef _WIN32 + const std::string LQUOTE("\'"); + const std::string RQUOTE("\'"); +#else + const std::string LQUOTE(" "); + const std::string RQUOTE(" "); +#endif + } + + class Value : public std::enable_shared_from_this + { + public: + + virtual ~Value() = default; + + virtual + std::shared_ptr + clone() const = 0; + + virtual void + parse(const std::string& text) const = 0; + + virtual void + parse() const = 0; + + virtual bool + has_default() const = 0; + + virtual bool + is_container() const = 0; + + virtual bool + has_implicit() const = 0; + + virtual std::string + get_default_value() const = 0; + + virtual std::string + get_implicit_value() const = 0; + + virtual std::shared_ptr + default_value(const std::string& value) = 0; + + virtual std::shared_ptr + implicit_value(const std::string& value) = 0; + + virtual bool + is_boolean() const = 0; + }; + + class OptionException : public std::exception + { + public: + OptionException(const std::string& message) + : m_message(message) + { + } + + virtual const char* + what() const noexcept + { + return m_message.c_str(); + } + + private: + std::string m_message; + }; + + class OptionSpecException : public OptionException + { + public: + + OptionSpecException(const std::string& message) + : OptionException(message) + { + } + }; + + class OptionParseException : public OptionException + { + public: + OptionParseException(const std::string& message) + : OptionException(message) + { + } + }; + + class option_exists_error : public OptionSpecException + { + public: + option_exists_error(const std::string& option) + : OptionSpecException("Option " + LQUOTE + option + RQUOTE + " already exists") + { + } + }; + + class invalid_option_format_error : public OptionSpecException + { + public: + invalid_option_format_error(const std::string& format) + : OptionSpecException("Invalid option format " + LQUOTE + format + RQUOTE) + { + } + }; + + class option_syntax_exception : public OptionParseException { + public: + option_syntax_exception(const std::string& text) + : OptionParseException("Argument " + LQUOTE + text + RQUOTE + + " starts with a - but has incorrect syntax") + { + } + }; + + class option_not_exists_exception : public OptionParseException + { + public: + option_not_exists_exception(const std::string& option) + : OptionParseException("Option " + LQUOTE + option + RQUOTE + " does not exist") + { + } + }; + + class missing_argument_exception : public OptionParseException + { + public: + missing_argument_exception(const std::string& option) + : OptionParseException( + "Option " + LQUOTE + option + RQUOTE + " is missing an argument" + ) + { + } + }; + + class option_requires_argument_exception : public OptionParseException + { + public: + option_requires_argument_exception(const std::string& option) + : OptionParseException( + "Option " + LQUOTE + option + RQUOTE + " requires an argument" + ) + { + } + }; + + class option_not_has_argument_exception : public OptionParseException + { + public: + option_not_has_argument_exception + ( + const std::string& option, + const std::string& arg + ) + : OptionParseException( + "Option " + LQUOTE + option + RQUOTE + + " does not take an argument, but argument " + + LQUOTE + arg + RQUOTE + " given" + ) + { + } + }; + + class option_not_present_exception : public OptionParseException + { + public: + option_not_present_exception(const std::string& option) + : OptionParseException("Option " + LQUOTE + option + RQUOTE + " not present") + { + } + }; + + class argument_incorrect_type : public OptionParseException + { + public: + argument_incorrect_type + ( + const std::string& arg + ) + : OptionParseException( + "Argument " + LQUOTE + arg + RQUOTE + " failed to parse" + ) + { + } + }; + + class option_required_exception : public OptionParseException + { + public: + option_required_exception(const std::string& option) + : OptionParseException( + "Option " + LQUOTE + option + RQUOTE + " is required but not present" + ) + { + } + }; + + namespace values + { + namespace + { + std::basic_regex integer_pattern + ("(-)?(0x)?([0-9a-zA-Z]+)|((0x)?0)"); + std::basic_regex truthy_pattern + ("(t|T)(rue)?"); + std::basic_regex falsy_pattern + ("((f|F)(alse)?)?"); + } + + namespace detail + { + template + struct SignedCheck; + + template + struct SignedCheck + { + template + void + operator()(bool negative, U u, const std::string& text) + { + if (negative) + { + if (u > static_cast(-(std::numeric_limits::min)())) + { + throw argument_incorrect_type(text); + } + } + else + { + if (u > static_cast((std::numeric_limits::max)())) + { + throw argument_incorrect_type(text); + } + } + } + }; + + template + struct SignedCheck + { + template + void + operator()(bool, U, const std::string&) {} + }; + + template + void + check_signed_range(bool negative, U value, const std::string& text) + { + SignedCheck::is_signed>()(negative, value, text); + } + } + + template + R + checked_negate(T&& t, const std::string&, std::true_type) + { + // if we got to here, then `t` is a positive number that fits into + // `R`. So to avoid MSVC C4146, we first cast it to `R`. + // See https://github.com/jarro2783/cxxopts/issues/62 for more details. + return -static_cast(t); + } + + template + T + checked_negate(T&&, const std::string& text, std::false_type) + { + throw argument_incorrect_type(text); + } + + template + void + integer_parser(const std::string& text, T& value) + { + std::smatch match; + std::regex_match(text, match, integer_pattern); + + if (match.length() == 0) + { + throw argument_incorrect_type(text); + } + + if (match.length(4) > 0) + { + value = 0; + return; + } + + using US = typename std::make_unsigned::type; + + constexpr auto umax = (std::numeric_limits::max)(); + constexpr bool is_signed = std::numeric_limits::is_signed; + const bool negative = match.length(1) > 0; + const uint8_t base = match.length(2) > 0 ? 16 : 10; + + auto value_match = match[3]; + + US result = 0; + + for (auto iter = value_match.first; iter != value_match.second; ++iter) + { + US digit = 0; + + if (*iter >= '0' && *iter <= '9') + { + digit = *iter - '0'; + } + else if (base == 16 && *iter >= 'a' && *iter <= 'f') + { + digit = *iter - 'a' + 10; + } + else if (base == 16 && *iter >= 'A' && *iter <= 'F') + { + digit = *iter - 'A' + 10; + } + else + { + throw argument_incorrect_type(text); + } + + if (umax - digit < result * base) + { + throw argument_incorrect_type(text); + } + + result = result * base + digit; + } + + detail::check_signed_range(negative, result, text); + + if (negative) + { + value = checked_negate(result, + text, + std::integral_constant()); + } + else + { + value = result; + } + } + + template + void stringstream_parser(const std::string& text, T& value) + { + std::stringstream in(text); + in >> value; + if (!in) { + throw argument_incorrect_type(text); + } + } + + inline + void + parse_value(const std::string& text, uint8_t& value) + { + integer_parser(text, value); + } + + inline + void + parse_value(const std::string& text, int8_t& value) + { + integer_parser(text, value); + } + + inline + void + parse_value(const std::string& text, uint16_t& value) + { + integer_parser(text, value); + } + + inline + void + parse_value(const std::string& text, int16_t& value) + { + integer_parser(text, value); + } + + inline + void + parse_value(const std::string& text, uint32_t& value) + { + integer_parser(text, value); + } + + inline + void + parse_value(const std::string& text, int32_t& value) + { + integer_parser(text, value); + } + + inline + void + parse_value(const std::string& text, uint64_t& value) + { + integer_parser(text, value); + } + + inline + void + parse_value(const std::string& text, int64_t& value) + { + integer_parser(text, value); + } + + inline + void + parse_value(const std::string& text, bool& value) + { + std::smatch result; + std::regex_match(text, result, truthy_pattern); + + if (!result.empty()) + { + value = true; + return; + } + + std::regex_match(text, result, falsy_pattern); + if (!result.empty()) + { + value = false; + return; + } + + throw argument_incorrect_type(text); + } + + inline + void + parse_value(const std::string& text, std::string& value) + { + value = text; + } + + // The fallback parser. It uses the stringstream parser to parse all types + // that have not been overloaded explicitly. It has to be placed in the + // source code before all other more specialized templates. + template + void + parse_value(const std::string& text, T& value) { + stringstream_parser(text, value); + } + + template + void + parse_value(const std::string& text, std::vector& value) + { + T v; + parse_value(text, v); + value.push_back(v); + } + +#ifdef CXXOPTS_HAS_OPTIONAL + template + void + parse_value(const std::string& text, std::optional& value) + { + T result; + parse_value(text, result); + value = std::move(result); + } +#endif + + template + struct type_is_container + { + static constexpr bool value = false; + }; + + template + struct type_is_container> + { + static constexpr bool value = true; + }; + + template + class abstract_value : public Value + { + using Self = abstract_value; + + public: + abstract_value() + : m_result(std::make_shared()) + , m_store(m_result.get()) + { + } + + abstract_value(T* t) + : m_store(t) + { + } + + virtual ~abstract_value() = default; + + abstract_value(const abstract_value& rhs) + { + if (rhs.m_result) + { + m_result = std::make_shared(); + m_store = m_result.get(); + } + else + { + m_store = rhs.m_store; + } + + m_default = rhs.m_default; + m_implicit = rhs.m_implicit; + m_default_value = rhs.m_default_value; + m_implicit_value = rhs.m_implicit_value; + } + + void + parse(const std::string& text) const + { + parse_value(text, *m_store); + } + + bool + is_container() const + { + return type_is_container::value; + } + + void + parse() const + { + parse_value(m_default_value, *m_store); + } + + bool + has_default() const + { + return m_default; + } + + bool + has_implicit() const + { + return m_implicit; + } + + std::shared_ptr + default_value(const std::string& value) + { + m_default = true; + m_default_value = value; + return shared_from_this(); + } + + std::shared_ptr + implicit_value(const std::string& value) + { + m_implicit = true; + m_implicit_value = value; + return shared_from_this(); + } + + std::string + get_default_value() const + { + return m_default_value; + } + + std::string + get_implicit_value() const + { + return m_implicit_value; + } + + bool + is_boolean() const + { + return std::is_same::value; + } + + const T& + get() const + { + if (m_store == nullptr) + { + return *m_result; + } + else + { + return *m_store; + } + } + + protected: + std::shared_ptr m_result; + T* m_store; + + bool m_default = false; + bool m_implicit = false; + + std::string m_default_value; + std::string m_implicit_value; + }; + + template + class standard_value : public abstract_value + { + public: + using abstract_value::abstract_value; + + std::shared_ptr + clone() const + { + return std::make_shared>(*this); + } + }; + + template <> + class standard_value : public abstract_value + { + public: + ~standard_value() = default; + + standard_value() + { + set_default_and_implicit(); + } + + standard_value(bool* b) + : abstract_value(b) + { + set_default_and_implicit(); + } + + std::shared_ptr + clone() const + { + return std::make_shared>(*this); + } + + private: + + void + set_default_and_implicit() + { + m_default = true; + m_default_value = "false"; + m_implicit = true; + m_implicit_value = "true"; + } + }; + } + + template + std::shared_ptr + value() + { + return std::make_shared>(); + } + + template + std::shared_ptr + value(T& t) + { + return std::make_shared>(&t); + } + + class OptionAdder; + + class OptionDetails + { + public: + OptionDetails + ( + const std::string& short_, + const std::string& long_, + const String& desc, + std::shared_ptr val + ) + : m_short(short_) + , m_long(long_) + , m_desc(desc) + , m_value(val) + , m_count(0) + { + } + + OptionDetails(const OptionDetails& rhs) + : m_desc(rhs.m_desc) + , m_count(rhs.m_count) + { + m_value = rhs.m_value->clone(); + } + + OptionDetails(OptionDetails&& rhs) = default; + + const String& + description() const + { + return m_desc; + } + + const Value& value() const { + return *m_value; + } + + std::shared_ptr + make_storage() const + { + return m_value->clone(); + } + + const std::string& + short_name() const + { + return m_short; + } + + const std::string& + long_name() const + { + return m_long; + } + + private: + std::string m_short; + std::string m_long; + String m_desc; + std::shared_ptr m_value; + int m_count; + }; + + struct HelpOptionDetails + { + std::string s; + std::string l; + String desc; + bool has_default; + std::string default_value; + bool has_implicit; + std::string implicit_value; + std::string arg_help; + bool is_container; + bool is_boolean; + }; + + struct HelpGroupDetails + { + std::string name; + std::string description; + std::vector options; + }; + + class OptionValue + { + public: + void + parse + ( + std::shared_ptr details, + const std::string& text + ) + { + ensure_value(details); + ++m_count; + m_value->parse(text); + } + + void + parse_default(std::shared_ptr details) + { + ensure_value(details); + m_value->parse(); + } + + size_t + count() const + { + return m_count; + } + + template + const T& + as() const + { + if (m_value == nullptr) { + throw std::domain_error("No value"); + } + +#ifdef CXXOPTS_NO_RTTI + return static_cast&>(*m_value).get(); +#else + return dynamic_cast&>(*m_value).get(); +#endif + } + + private: + void + ensure_value(std::shared_ptr details) + { + if (m_value == nullptr) + { + m_value = details->make_storage(); + } + } + + std::shared_ptr m_value; + size_t m_count = 0; + }; + + class KeyValue + { + public: + KeyValue(std::string key_, std::string value_) + : m_key(std::move(key_)) + , m_value(std::move(value_)) + { + } + + const + std::string& + key() const + { + return m_key; + } + + const + std::string& + value() const + { + return m_value; + } + + template + T + as() const + { + T result; + values::parse_value(m_value, result); + return result; + } + + private: + std::string m_key; + std::string m_value; + }; + + class ParseResult + { + public: + + ParseResult( + const std::shared_ptr< + std::unordered_map> + >, + std::vector, + bool allow_unrecognised, + int&, char**&); + + size_t + count(const std::string& o) const + { + auto iter = m_options->find(o); + if (iter == m_options->end()) + { + return 0; + } + + auto riter = m_results.find(iter->second); + + return riter->second.count(); + } + + const OptionValue& + operator[](const std::string& option) const + { + auto iter = m_options->find(option); + + if (iter == m_options->end()) + { + throw option_not_present_exception(option); + } + + auto riter = m_results.find(iter->second); + + return riter->second; + } + + const std::vector& + arguments() const + { + return m_sequential; + } + + private: + + void + parse(int& argc, char**& argv); + + void + add_to_option(const std::string& option, const std::string& arg); + + bool + consume_positional(std::string a); + + void + parse_option + ( + std::shared_ptr value, + const std::string& name, + const std::string& arg = "" + ); + + void + parse_default(std::shared_ptr details); + + void + checked_parse_arg + ( + int argc, + char* argv[], + int& current, + std::shared_ptr value, + const std::string& name + ); + + const std::shared_ptr< + std::unordered_map> + > m_options; + std::vector m_positional; + std::vector::iterator m_next_positional; + std::unordered_set m_positional_set; + std::unordered_map, OptionValue> m_results; + + bool m_allow_unrecognised; + + std::vector m_sequential; + }; + + class Options + { + typedef std::unordered_map> + OptionMap; + public: + + Options(std::string program, std::string help_string = "") + : m_program(std::move(program)) + , m_help_string(toLocalString(std::move(help_string))) + , m_custom_help("[OPTION...]") + , m_positional_help("positional parameters") + , m_show_positional(false) + , m_allow_unrecognised(false) + , m_options(std::make_shared()) + , m_next_positional(m_positional.end()) + { + } + + Options& + positional_help(std::string help_text) + { + m_positional_help = std::move(help_text); + return *this; + } + + Options& + custom_help(std::string help_text) + { + m_custom_help = std::move(help_text); + return *this; + } + + Options& + show_positional_help() + { + m_show_positional = true; + return *this; + } + + Options& + allow_unrecognised_options() + { + m_allow_unrecognised = true; + return *this; + } + + ParseResult + parse(int& argc, char**& argv); + + OptionAdder + add_options(std::string group = ""); + + void + add_option + ( + const std::string& group, + const std::string& s, + const std::string& l, + std::string desc, + std::shared_ptr value, + std::string arg_help + ); + + //parse positional arguments into the given option + void + parse_positional(std::string option); + + void + parse_positional(std::vector options); + + void + parse_positional(std::initializer_list options); + + template + void + parse_positional(Iterator begin, Iterator end) { + parse_positional(std::vector{begin, end}); + } + + std::string + help(const std::vector& groups = {}) const; + + const std::vector + groups() const; + + const HelpGroupDetails& + group_help(const std::string& group) const; + + private: + + void + add_one_option + ( + const std::string& option, + std::shared_ptr details + ); + + String + help_one_group(const std::string& group) const; + + void + generate_group_help + ( + String& result, + const std::vector& groups + ) const; + + void + generate_all_groups_help(String& result) const; + + std::string m_program; + String m_help_string; + std::string m_custom_help; + std::string m_positional_help; + bool m_show_positional; + bool m_allow_unrecognised; + + std::shared_ptr m_options; + std::vector m_positional; + std::vector::iterator m_next_positional; + std::unordered_set m_positional_set; + + //mapping from groups to help options + std::map m_help; + }; + + class OptionAdder + { + public: + + OptionAdder(Options& options, std::string group) + : m_options(options), m_group(std::move(group)) + { + } + + OptionAdder& + operator() + ( + const std::string& opts, + const std::string& desc, + std::shared_ptr value + = ::cxxopts::value(), + std::string arg_help = "" + ); + + private: + Options& m_options; + std::string m_group; + }; + + namespace + { + constexpr int OPTION_LONGEST = 30; + constexpr int OPTION_DESC_GAP = 2; + + std::basic_regex option_matcher + ("--([[:alnum:]][-_[:alnum:]]+)(=(.*))?|-([[:alnum:]]+)"); + + std::basic_regex option_specifier + ("(([[:alnum:]]),)?[ ]*([[:alnum:]][-_[:alnum:]]*)?"); + + String + format_option + ( + const HelpOptionDetails& o + ) + { + auto& s = o.s; + auto& l = o.l; + + String result = " "; + + if (s.size() > 0) + { + result += "-" + toLocalString(s) + ","; + } + else + { + result += " "; + } + + if (l.size() > 0) + { + result += " --" + toLocalString(l); + } + + auto arg = o.arg_help.size() > 0 ? toLocalString(o.arg_help) : "arg"; + + if (!o.is_boolean) + { + if (o.has_implicit) + { + result += " [=" + arg + "(=" + toLocalString(o.implicit_value) + ")]"; + } + else + { + result += " " + arg; + } + } + + return result; + } + + String + format_description + ( + const HelpOptionDetails& o, + size_t start, + size_t width + ) + { + auto desc = o.desc; + + if (o.has_default && (!o.is_boolean || o.default_value != "false")) + { + desc += toLocalString(" (default: " + o.default_value + ")"); + } + + String result; + + auto current = std::begin(desc); + auto startLine = current; + auto lastSpace = current; + + auto size = size_t{}; + + while (current != std::end(desc)) + { + if (*current == ' ') + { + lastSpace = current; + } + + if (*current == '\n') + { + startLine = current + 1; + lastSpace = startLine; + } + else if (size > width) + { + if (lastSpace == startLine) + { + stringAppend(result, startLine, current + 1); + stringAppend(result, "\n"); + stringAppend(result, start, ' '); + startLine = current + 1; + lastSpace = startLine; + } + else + { + stringAppend(result, startLine, lastSpace); + stringAppend(result, "\n"); + stringAppend(result, start, ' '); + startLine = lastSpace + 1; + } + size = 0; + } + else + { + ++size; + } + + ++current; + } + + //append whatever is left + stringAppend(result, startLine, current); + + return result; + } + } + +inline +ParseResult::ParseResult +( + const std::shared_ptr< + std::unordered_map> + > options, + std::vector positional, + bool allow_unrecognised, + int& argc, char**& argv +) +: m_options(options) +, m_positional(std::move(positional)) +, m_next_positional(m_positional.begin()) +, m_allow_unrecognised(allow_unrecognised) +{ + parse(argc, argv); +} + +inline +OptionAdder +Options::add_options(std::string group) +{ + return OptionAdder(*this, std::move(group)); +} + +inline +OptionAdder& +OptionAdder::operator() +( + const std::string& opts, + const std::string& desc, + std::shared_ptr value, + std::string arg_help +) +{ + std::match_results result; + std::regex_match(opts.c_str(), result, option_specifier); + + if (result.empty()) + { + throw invalid_option_format_error(opts); + } + + const auto& short_match = result[2]; + const auto& long_match = result[3]; + + if (!short_match.length() && !long_match.length()) + { + throw invalid_option_format_error(opts); + } else if (long_match.length() == 1 && short_match.length()) + { + throw invalid_option_format_error(opts); + } + + auto option_names = [] + ( + const std::sub_match& short_, + const std::sub_match& long_ + ) + { + if (long_.length() == 1) + { + return std::make_tuple(long_.str(), short_.str()); + } + else + { + return std::make_tuple(short_.str(), long_.str()); + } + }(short_match, long_match); + + m_options.add_option + ( + m_group, + std::get<0>(option_names), + std::get<1>(option_names), + desc, + value, + std::move(arg_help) + ); + + return *this; +} + +inline +void +ParseResult::parse_default(std::shared_ptr details) +{ + m_results[details].parse_default(details); +} + +inline +void +ParseResult::parse_option +( + std::shared_ptr value, + const std::string& /*name*/, + const std::string& arg +) +{ + auto& result = m_results[value]; + result.parse(value, arg); + + m_sequential.emplace_back(value->long_name(), arg); +} + +inline +void +ParseResult::checked_parse_arg +( + int argc, + char* argv[], + int& current, + std::shared_ptr value, + const std::string& name +) +{ + if (current + 1 >= argc) + { + if (value->value().has_implicit()) + { + parse_option(value, name, value->value().get_implicit_value()); + } + else + { + throw missing_argument_exception(name); + } + } + else + { + if (value->value().has_implicit()) + { + parse_option(value, name, value->value().get_implicit_value()); + } + else + { + parse_option(value, name, argv[current + 1]); + ++current; + } + } +} + +inline +void +ParseResult::add_to_option(const std::string& option, const std::string& arg) +{ + auto iter = m_options->find(option); + + if (iter == m_options->end()) + { + throw option_not_exists_exception(option); + } + + parse_option(iter->second, option, arg); +} + +inline +bool +ParseResult::consume_positional(std::string a) +{ + while (m_next_positional != m_positional.end()) + { + auto iter = m_options->find(*m_next_positional); + if (iter != m_options->end()) + { + auto& result = m_results[iter->second]; + if (!iter->second->value().is_container()) + { + if (result.count() == 0) + { + add_to_option(*m_next_positional, a); + ++m_next_positional; + return true; + } + else + { + ++m_next_positional; + continue; + } + } + else + { + add_to_option(*m_next_positional, a); + return true; + } + } + ++m_next_positional; + } + + return false; +} + +inline +void +Options::parse_positional(std::string option) +{ + parse_positional(std::vector{std::move(option)}); +} + +inline +void +Options::parse_positional(std::vector options) +{ + m_positional = std::move(options); + m_next_positional = m_positional.begin(); + + m_positional_set.insert(m_positional.begin(), m_positional.end()); +} + +inline +void +Options::parse_positional(std::initializer_list options) +{ + parse_positional(std::vector(std::move(options))); +} + +inline +ParseResult +Options::parse(int& argc, char**& argv) +{ + ParseResult result(m_options, m_positional, m_allow_unrecognised, argc, argv); + return result; +} + +inline +void +ParseResult::parse(int& argc, char**& argv) +{ + int current = 1; + + int nextKeep = 1; + + bool consume_remaining = false; + + while (current != argc) + { + if (strcmp(argv[current], "--") == 0) + { + consume_remaining = true; + ++current; + break; + } + + std::match_results result; + std::regex_match(argv[current], result, option_matcher); + + if (result.empty()) + { + //not a flag + + // but if it starts with a `-`, then it's an error + if (argv[current][0] == '-' && argv[current][1] != '\0') { + throw option_syntax_exception(argv[current]); + } + + //if true is returned here then it was consumed, otherwise it is + //ignored + if (consume_positional(argv[current])) + { + } + else + { + argv[nextKeep] = argv[current]; + ++nextKeep; + } + //if we return from here then it was parsed successfully, so continue + } + else + { + //short or long option? + if (result[4].length() != 0) + { + const std::string& s = result[4]; + + for (std::size_t i = 0; i != s.size(); ++i) + { + std::string name(1, s[i]); + auto iter = m_options->find(name); + + if (iter == m_options->end()) + { + if (m_allow_unrecognised) + { + continue; + } + else + { + //error + throw option_not_exists_exception(name); + } + } + + auto value = iter->second; + + if (i + 1 == s.size()) + { + //it must be the last argument + checked_parse_arg(argc, argv, current, value, name); + } + else if (value->value().has_implicit()) + { + parse_option(value, name, value->value().get_implicit_value()); + } + else + { + //error + throw option_requires_argument_exception(name); + } + } + } + else if (result[1].length() != 0) + { + const std::string& name = result[1]; + + auto iter = m_options->find(name); + + if (iter == m_options->end()) + { + if (m_allow_unrecognised) + { + // keep unrecognised options in argument list, skip to next argument + argv[nextKeep] = argv[current]; + ++nextKeep; + ++current; + continue; + } + else + { + //error + throw option_not_exists_exception(name); + } + } + + auto opt = iter->second; + + //equals provided for long option? + if (result[2].length() != 0) + { + //parse the option given + + parse_option(opt, name, result[3]); + } + else + { + //parse the next argument + checked_parse_arg(argc, argv, current, opt, name); + } + } + + } + + ++current; + } + + for (auto& opt : *m_options) + { + auto& detail = opt.second; + auto& value = detail->value(); + + auto& store = m_results[detail]; + + if(!store.count() && value.has_default()){ + parse_default(detail); + } + } + + if (consume_remaining) + { + while (current < argc) + { + if (!consume_positional(argv[current])) { + break; + } + ++current; + } + + //adjust argv for any that couldn't be swallowed + while (current != argc) { + argv[nextKeep] = argv[current]; + ++nextKeep; + ++current; + } + } + + argc = nextKeep; + +} + +inline +void +Options::add_option +( + const std::string& group, + const std::string& s, + const std::string& l, + std::string desc, + std::shared_ptr value, + std::string arg_help +) +{ + auto stringDesc = toLocalString(std::move(desc)); + auto option = std::make_shared(s, l, stringDesc, value); + + if (s.size() > 0) + { + add_one_option(s, option); + } + + if (l.size() > 0) + { + add_one_option(l, option); + } + + //add the help details + auto& options = m_help[group]; + + options.options.emplace_back(HelpOptionDetails{s, l, stringDesc, + value->has_default(), value->get_default_value(), + value->has_implicit(), value->get_implicit_value(), + std::move(arg_help), + value->is_container(), + value->is_boolean()}); +} + +inline +void +Options::add_one_option +( + const std::string& option, + std::shared_ptr details +) +{ + auto in = m_options->emplace(option, details); + + if (!in.second) + { + throw option_exists_error(option); + } +} + +inline +String +Options::help_one_group(const std::string& g) const +{ + typedef std::vector> OptionHelp; + + auto group = m_help.find(g); + if (group == m_help.end()) + { + return ""; + } + + OptionHelp format; + + size_t longest = 0; + + String result; + + if (!g.empty()) + { + result += toLocalString(" " + g + " options:\n"); + } + + for (const auto& o : group->second.options) + { + if (o.is_container && + m_positional_set.find(o.l) != m_positional_set.end() && + !m_show_positional) + { + continue; + } + + auto s = format_option(o); + longest = (std::max)(longest, stringLength(s)); + format.push_back(std::make_pair(s, String())); + } + + longest = (std::min)(longest, static_cast(OPTION_LONGEST)); + + //widest allowed description + auto allowed = size_t{76} - longest - OPTION_DESC_GAP; + + auto fiter = format.begin(); + for (const auto& o : group->second.options) + { + if (o.is_container && + m_positional_set.find(o.l) != m_positional_set.end() && + !m_show_positional) + { + continue; + } + + auto d = format_description(o, longest + OPTION_DESC_GAP, allowed); + + result += fiter->first; + if (stringLength(fiter->first) > longest) + { + result += '\n'; + result += toLocalString(std::string(longest + OPTION_DESC_GAP, ' ')); + } + else + { + result += toLocalString(std::string(longest + OPTION_DESC_GAP - + stringLength(fiter->first), + ' ')); + } + result += d; + result += '\n'; + + ++fiter; + } + + return result; +} + +inline +void +Options::generate_group_help +( + String& result, + const std::vector& print_groups +) const +{ + for (size_t i = 0; i != print_groups.size(); ++i) + { + const String& group_help_text = help_one_group(print_groups[i]); + if (empty(group_help_text)) + { + continue; + } + result += group_help_text; + if (i < print_groups.size() - 1) + { + result += '\n'; + } + } +} + +inline +void +Options::generate_all_groups_help(String& result) const +{ + std::vector all_groups; + all_groups.reserve(m_help.size()); + + for (auto& group : m_help) + { + all_groups.push_back(group.first); + } + + generate_group_help(result, all_groups); +} + +inline +std::string +Options::help(const std::vector& help_groups) const +{ + String result = m_help_string + "\nUsage:\n " + + toLocalString(m_program) + " " + toLocalString(m_custom_help); + + if (m_positional.size() > 0 && m_positional_help.size() > 0) { + result += " " + toLocalString(m_positional_help); + } + + result += "\n\n"; + + if (help_groups.size() == 0) + { + generate_all_groups_help(result); + } + else + { + generate_group_help(result, help_groups); + } + + return toUTF8String(result); +} + +inline +const std::vector +Options::groups() const +{ + std::vector g; + + std::transform( + m_help.begin(), + m_help.end(), + std::back_inserter(g), + [] (const std::map::value_type& pair) + { + return pair.first; + } + ); + + return g; +} + +inline +const HelpGroupDetails& +Options::group_help(const std::string& group) const +{ + return m_help.at(group); +} + +} + +#endif //CXXOPTS_HPP_INCLUDED diff --git a/scripts/gen-matcher.py b/scripts/gen-matcher.py new file mode 100755 index 0000000..53e464f --- /dev/null +++ b/scripts/gen-matcher.py @@ -0,0 +1,38 @@ +#!/usr/bin/env python + +""" +Generate a template cpp file for registering new clang AST matcher and callback +""" + +import argparse +import os +import sys + +def main(argv): + parser = argparse.ArgumentParser() + parser.add_argument( + 'matcher_name', + type=str, + nargs=1, + help='Matcher name') + args = parser.parse_args() + matcher_name = args.matcher_name[0] + script_dir = os.path.dirname(__file__); + file_path = os.path.join(script_dir, 'template/MatcherTemplate.cpp') + + # read template file + file_data = None + with open(file_path, "r") as file: + file_data = file.read() + # replace with the matcher name provided by the user + file_data = file_data.replace('__NAME__', matcher_name) + + # write the data into matcher_name.cpp + new_file = matcher_name + '.cpp' + with open(new_file, "w") as file: + file.write(file_data) + + print(new_file + ' is generated') + +if __name__ == '__main__': + sys.exit(main(sys.argv[1])) diff --git a/scripts/gen-test.py b/scripts/gen-test.py new file mode 100755 index 0000000..55bfa79 --- /dev/null +++ b/scripts/gen-test.py @@ -0,0 +1,124 @@ +#!/usr/bin/env python3 + +""" +Generate a unit test file and its baseline for a given matcher +""" + +import argparse +import os +import sys +import re +import subprocess +import shutil + +def main(argv): + parser = argparse.ArgumentParser() + + parser.add_argument( + '-m', + '--matcher', + required=True, + type=str, + help='matcher string ID') + + parser.add_argument( + '-l', + '--log', + nargs='?', + type=str, + default='sbcodexform.log', + help='log file name') + + parser.add_argument( + 'input', + type=str, + nargs=1, + help='input file for baseline generation') + + args = parser.parse_args() + logname = args.log + matcher = args.matcher + + file = args.input[0] + file = os.path.abspath(file) + dirname, filename = os.path.split(file) + root_name = 'clang-xform' + root = os.path.join(dirname[:dirname.find(root_name)-1], root_name) + dir_rel = dirname[dirname.find(root_name):] + dir_rel = os.path.relpath(dir_rel, root_name) + + script_dir = os.path.dirname(__file__); + template = os.path.join(script_dir, 'template/TestTemplate.cpp') + + # generate gtest file + file_data = None + with open(template, "r") as f: + file_data = f.read() + # replace with the matcher name provided by the user + file_data = file_data.replace('__NAME__', matcher) + file_data = file_data.replace('__PATH__', dir_rel) + file_data = file_data.replace('__FILE__', filename) + file_data = file_data.replace('__LOG__', logname) + + # write the data into tmatcher.cpp + new_file = os.path.join(dirname, 't' + matcher + '.cpp') + print('generating ' + new_file) + with open(new_file, "w") as f: + f.write(file_data) + + # generate baseline + # make a copy of the original file + file_copy = os.path.join(dirname, filename + '.tmp') + shutil.copyfile(file, file_copy) + + # generate compilation database for the file + json_file = os.path.join(dirname, 'compile_commands.json') + print('generating ' + json_file) + # read template file + template = os.path.join(root, 'scripts', 'template', 'compile_commands.json') + template_data = None + with open(template, "r") as f: + template_data = f.read() + # replace the directory name and file name + template_data = template_data.replace('__DIRECTORY__', dirname) + template_data = template_data.replace('__FILE__', filename) + + # write the data into dirname path + with open(json_file, "w") as f: + f.write(template_data) + + # generating new baselines + log = os.path.join(dirname, logname) + file_gold = os.path.join(dirname, filename + '.gold') + log_gold = os.path.join(dirname, logname + '.gold') + + print('generating ' + log_gold) + print('generating ' + file_gold) + cmd = root + '/bin/clang-xform -m ' + matcher + ' -l ' + log + ' -q ' + file + process = subprocess.run(cmd, shell=True, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE) + + # remove old baseline if already exists + if os.path.exists(file_gold): + os.remove(file_gold) + if os.path.exists(log_gold): + os.remove(log_gold) + + # rename generated files into baselines + os.rename(file, file_gold) + os.rename(log, log_gold) + os.rename(file_copy, file) + + # adjust log baseline by removing attributes info + lines = None + with open(log_gold, "r") as f: + lines = f.readlines() + with open(log_gold, "w") as f: + for line in lines: + if line.startswith('No.'): + line = line[line.rfind('|') + 2:] + f.write(line) + +if __name__ == '__main__': + sys.exit(main(sys.argv[1:])) diff --git a/scripts/generate_compdb.py b/scripts/generate_compdb.py new file mode 100755 index 0000000..610fca9 --- /dev/null +++ b/scripts/generate_compdb.py @@ -0,0 +1,70 @@ +#!/usr/bin/env python + +""" +Helper for generating compile DBs for clang tooling. On non-Windows platforms, +this is pretty straightforward. On Windows, the tool does a bit of extra work to +integrate the content of response files, force clang tooling to run in clang-cl +mode, etc. +""" +import argparse +import json +import re +import subprocess +import os +import sys + +def main(argv): + parser = argparse.ArgumentParser() + parser.add_argument( + 'json_files', + nargs='*', + help='Compile commands json files to update') + args = parser.parse_args() + + for json_file in args.json_files: + # read compile commands from json file + with open(json_file, "r+") as f: + build_path = os.path.dirname(os.path.abspath(json_file)) + compdbs = json.load(f) + header_set = set() + # deal with header files first + for compdb in reversed(compdbs): + command = compdb['command'] + file = compdb['file'] + compdb['directory'] = build_path + name, extension = os.path.splitext(file) + if ((extension == '.h') or (extension == '.hpp')): + header_set.add(file) + continue + # find -I and -isystem arguments + paths = re.findall(r'-(?:I|isystem)\s*.+?\s', command) + # set up and run cmd to get a list of included files + cmd = ''.join(paths) + cmd = 'cpp -M ' + cmd + file + process = subprocess.run(cmd, shell=True, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE) + headers = re.findall(r'/.+?(?:\.hpp|\.h)', process.stdout.decode()) + # create compile commands for header files under build path + for header in headers: + if (build_path in header) and (header not in header_set): + header_set.add(header) + header_compdb = dict() + header_compdb['directory'] = build_path + header_command = re.sub(r'-o\s.+/.+?\.o(?=\s)', + '-o /tmp/CMakeFiles/'+os.path.relpath(header, build_path+'/..')+'.o', + command) + header_compdb['command'] = re.sub(r'-c\s+.+?\.(?:cpp|cc|c)', + '-c '+header, + header_command) + header_compdb['file'] = header + compdbs.append(header_compdb) + + # rewrite new compdbs into the json file + f.seek(0) + f.write(json.dumps(compdbs, indent=4)) + f.truncate() + + +if __name__ == '__main__': + sys.exit(main(sys.argv[1:])) diff --git a/scripts/template/MatcherTemplate.cpp b/scripts/template/MatcherTemplate.cpp new file mode 100644 index 0000000..0e02dab --- /dev/null +++ b/scripts/template/MatcherTemplate.cpp @@ -0,0 +1,68 @@ +#include "MyMatchCallback.hpp" // provides macro MATCH_CALLBACK to define a new match callback +#include "MatcherFactory.hpp" // a factory class used to register new matcher and callback +#include "ToolingUtil.hpp" // APIs to extract locations and tokens for a given AST node +#include "Logger.hpp" // basic logging functionality + +#include + +using namespace clang; +using namespace clang::ast_matchers; + +// This a generated template file to help write your own clang AST matcher and callback. +// Please address all comments in /**/ form below! +// [1] : use StatementMatcher or DeclarationMatcher +// [2] : replace it with a suitable node matcher +// [3] : add narrowing or traversal matchers here +// [4] : rename the string ID for a better description of the matched node +// [5] : replace it with the corresponding node class type binded with the string ID +// [6] : variable name for the matched node +// [7] : same as [5] +// [8] : string ID used in matcher, same as [4] +// [9] : name of the node to be replaced +// [10]: string used to replace the matched node +// [11]: use "LogASTNode(locStart, srcMgr, oldExprString)" to log matched AST Node infomation +// [12]: can register more than one matcher of different types + +namespace { +// Define your own matcher here. +// Use StatementMatcher to match statements and DeclarationMatcher to match declarations. +// It is recommended that isExpansionInMainFile() is used to avoid matches in +// system headers or third-party libraries. +// For AST matcher reference, see: https://clang.llvm.org/docs/LibASTMatchersReference.html +// For AST matcher examples, check files under "matchers/" directory +StatementMatcher/*[1]*/ __NAME__Matcher = + expr/*[2]*/(/*[3]*/, + isExpansionInMainFile() + ).bind("__NAME__Expr"/*[4]*/); + +// Match callback class __NAME__Callback is defined here +MATCH_CALLBACK(__NAME__Callback); + +// Defination of __NAME__Callback::run +void __NAME__Callback::run(const clang::ast_matchers::MatchFinder::MatchResult& Result) { + std::string oldExprString; + std::string newExprString; + const auto& srcMgr = Result.Context->getSourceManager(); + const auto& langOpts = Result.Context->getLangOpts(); + + // Check any AST node matched for the given string ID. + // The node class name is usually the capitalized node matcher name. + if (const Expr*/*[5]*/ __NAME__Expr/*[6]*/ = + Result.Nodes.getNodeAs("__NAME__Expr"/*[8]*/)) { + // find begin and end file locations of a given node + auto locStart = srcMgr.getFileLoc(__NAME__Expr/*[9]*/->getBeginLoc()); + auto locEnd = srcMgr.getFileLoc(__NAME__Expr/*[9]*/->getEndLoc()); + newExprString = ""/*[10]*/; + // find source text for a given location + oldExprString = getSourceText(locStart, locEnd, srcMgr, langOpts); + // replace source text with a given string + ReplaceText(srcMgr, SourceRange(std::move(locStart), std::move(locEnd)), newExprString); + // log the replacement or AST node if no replacement is made + LogReplacement(locStart, srcMgr, oldExprString, newExprString)/*[11]*/; + } +} + +// register your own matcher and callback +MatcherHelper<__NAME__Callback> Register__NAME__Matcher("__NAME__", {__NAME__Matcher}/*12*/); + +} diff --git a/scripts/template/TestTemplate.cpp b/scripts/template/TestTemplate.cpp new file mode 100644 index 0000000..f398707 --- /dev/null +++ b/scripts/template/TestTemplate.cpp @@ -0,0 +1,58 @@ +#include "MyFrontendAction.hpp" +#include "ApplyReplacements.hpp" +#include "TestingUtil.hpp" +#include "Logger.hpp" + +#include +#include + +#include "clang/Tooling/Tooling.h" +#include "clang/Tooling/CompilationDatabase.h" + +#include "gtest/gtest.h" + +using namespace clang::tooling; +// This unit test compares the log file and refactored src file with corresponding baseline. +// The test is self-explained. The user does not need to make any changes here unless +// for other customizations. It is recommended to check the following few places. +// [1] dirPath: test directory path, must start with "test/" +// [2] logFile: log file name (not path), default is "sbcodexform.log" +// [3] inputFile, src file name (not path) to refactor, default is "example.cpp" + +TEST(MatcherTest, __NAME__) { + // must start with test/ + std::string dirPath/*[1]*/ = "__PATH__"; + std::string logFile/*[2]*/ = "__LOG__"; + std::string inputFile/*[3]*/ = "__FILE__"; + std::string outputFile = "tmp_output_file.yaml"; + // chdir dirPath, create outputFile, set logging properties + int status = InitTest(dirPath, outputFile); + ASSERT_TRUE(status); + // setup log file + RegisterLogFile log_file(logFile); + + std::string refactoredFile = inputFile + ".refactored"; + std::string baselineFile = inputFile + ".gold"; + std::string baselineLog = logFile + ".gold"; + std::vector matchers = {"__NAME__"}; + + // retrieve compliation database + std::string errMsg; + std::unique_ptr compilations = + CompilationDatabase::autoDetectFromSource(inputFile, + errMsg); + ASSERT_TRUE(compilations != nullptr); + clang::tooling::ClangTool tool(*compilations, inputFile); + status = tool.run(std::make_unique(outputFile, matchers).get()); + ASSERT_EQ(status, 0); + // test if matched locations are correct + ASSERT_TRUE(CompareFiles(logFile, baselineLog)); + + status = clang::replace::applyReplacements(outputFile, refactoredFile); + ASSERT_TRUE(status); + + if (!IsEmptyFile(outputFile)) { + // test if replacements are correct when outputFile is not empty + ASSERT_TRUE(CompareFiles(refactoredFile, baselineFile)); + } +} diff --git a/scripts/template/compile_commands.json b/scripts/template/compile_commands.json new file mode 100644 index 0000000..d10f7ce --- /dev/null +++ b/scripts/template/compile_commands.json @@ -0,0 +1,7 @@ +[ + { + "directory": "__DIRECTORY__", + "file": "__FILE__", + "command": "g++ -c __FILE__ -I/usr/include/c++/6 -I/usr/include/x86_64-linux-gnu/c++/6 -I/usr/include/c++/6/backward -I/usr/lib/gcc/x86_64-linux-gnu/6/include -I/usr/local/include -I/usr/lib/gcc/x86_64-linux-gnu/6/include-fixed -I/usr/include/x86_64-linux-gnu -I/usr/include" + } +] diff --git a/scripts/update-compile-commands.py b/scripts/update-compile-commands.py new file mode 100755 index 0000000..d4b1d11 --- /dev/null +++ b/scripts/update-compile-commands.py @@ -0,0 +1,45 @@ +#!/usr/bin/env python + +""" +update compile_commands.json file used in unit test framework +""" + +import argparse +import os +import sys +import re +import json + +def main(argv): + parser = argparse.ArgumentParser() + parser.add_argument( + 'json_file', + type=str, + nargs='?', + default='compile_commands.json', + help='compile_commands.json file to udpate') + + args = parser.parse_args() + json_file = args.json_file + + json_file = os.path.abspath(json_file) + if not os.path.exists(json_file): + sys.exit('compile_commands.json file does not exist!') + + compdbs = None + with open(json_file, "r") as file: + compdbs = json.load(file) + + cwd = os.getcwd() + + for compdb in compdbs: + # update directory + compdb['directory'] = cwd; + + # write compdbs back to compile_commands.json + with open(json_file, "w") as file: + file.write(json.dumps(compdbs, indent=4)) + + +if __name__ == '__main__': + sys.exit(main(sys.argv[1:])) diff --git a/src/ApplyReplacements.cpp b/src/ApplyReplacements.cpp new file mode 100644 index 0000000..6e34925 --- /dev/null +++ b/src/ApplyReplacements.cpp @@ -0,0 +1,302 @@ +//===-- ApplyReplacements.cpp - Apply and deduplicate replacements --------===// +// +// 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 +// +//===----------------------------------------------------------------------===// +/// +/// \file +/// \brief This file provides the implementation for deduplicating, detecting +/// conflicts in, and applying collections of Replacements. +/// +/// FIXME: Use Diagnostics for output instead of llvm::errs(). +/// +//===----------------------------------------------------------------------===// +#include "ApplyReplacements.hpp" +#include "MyReplacementsYaml.hpp" +#include "ToolingUtil.hpp" +#include "Logger.hpp" + +#include "clang/Basic/LangOptions.h" +#include "clang/Basic/SourceManager.h" +#include "clang/Format/Format.h" +#include "clang/Lex/Lexer.h" +#include "clang/Rewrite/Core/Rewriter.h" +#include "clang/Tooling/DiagnosticsYaml.h" +#include "llvm/ADT/ArrayRef.h" +#include "llvm/Support/FileSystem.h" +#include "llvm/Support/MemoryBuffer.h" +#include "llvm/Support/Path.h" +#include "llvm/Support/raw_ostream.h" + +using namespace llvm; +using namespace clang; + +static void eatDiagnostics(const SMDiagnostic &, void *) {} + +namespace clang { +namespace replace { + +bool collectReplacementsFromFile( + const llvm::StringRef FilePath, TUReplacements &TUs, + clang::DiagnosticsEngine &Diagnostics) { + using namespace llvm::sys::fs; + using namespace llvm::sys::path; + + if (extension(FilePath) != ".yaml") { + errs() << "Error reading " << FilePath << ": file extension is not yaml" << '\n'; + return false; + } + + ErrorOr> Out = + MemoryBuffer::getFile(FilePath); + if (std::error_code BufferError = Out.getError()) { + errs() << "Error reading " << FilePath << ": " << BufferError.message() + << "\n"; + return false; + } + + // Only keep files that properly parse. + auto buffer = Out.get()->getBuffer(); + if (buffer.empty()) { + // ignore empty file + return true; + } + tooling::TranslationUnitReplacements TU; + yaml::Input YIn(buffer, nullptr, &eatDiagnostics); + if (!YIn.error()) { + // File doesn't appear to be a header change description. Ignore it. + YIn >> TU; + TUs.push_back(TU); + } + while (YIn.nextDocument()) { + if (!YIn.error()) { + YIn >> TU; + TUs.push_back(TU); + } + } + return true; +} + +bool collectReplacementsFromFile( + const llvm::StringRef FilePath, TUDiagnostics &TUs, + clang::DiagnosticsEngine &Diagnostics) { + using namespace llvm::sys::fs; + using namespace llvm::sys::path; + + if (extension(FilePath) != ".yaml") { + errs() << "Error reading " << FilePath << ": file extension is not yaml" << '\n'; + return false; + } + + ErrorOr> Out = + MemoryBuffer::getFile(FilePath); + if (std::error_code BufferError = Out.getError()) { + errs() << "Error reading " << FilePath << ": " << BufferError.message() + << "\n"; + return false; + } + auto buffer = Out.get()->getBuffer(); + if (buffer.empty()) { + // ignore empty file + return true; + } + yaml::Input YIn(buffer, nullptr, &eatDiagnostics); + // Only keep files that properly parse. + tooling::TranslationUnitDiagnostics TU; + if (!YIn.error()) { + // File doesn't appear to be a header change description. Ignore it. + YIn >> TU; + TUs.push_back(TU); + } + while (YIn.nextDocument()) { + if (!YIn.error()) { + YIn >> TU; + TUs.push_back(TU); + } + } + return true; +} + +/// \brief Extract replacements from collected TranslationUnitReplacements and +/// TranslationUnitDiagnostics and group them per file. Identical replacements +/// from diagnostics are deduplicated. +/// +/// \param[in] TUs Collection of all found and deserialized +/// TranslationUnitReplacements. +/// \param[in] TUDs Collection of all found and deserialized +/// TranslationUnitDiagnostics. +/// \param[in] SM Used to deduplicate paths. +/// +/// \returns A map mapping FileEntry to a set of Replacement targeting that +/// file. +static llvm::DenseMap> +groupReplacements(const TUReplacements &TUs, const TUDiagnostics &TUDs, + const clang::SourceManager &SM) { + std::set Warned; + llvm::DenseMap> + GroupedReplacements; + // Deduplicate identical replacements in diagnostics. + // FIXME: Find an efficient way to deduplicate on diagnostics level. + llvm::DenseMap> + DiagReplacements; + auto AddToGroup = [&](const tooling::Replacement &R, bool FromDiag) { + // Use the file manager to deduplicate paths. FileEntries are + // automatically canonicalized. + if (const FileEntry *Entry = SM.getFileManager().getFile(R.getFilePath())) { + if (FromDiag) { + auto &Replaces = DiagReplacements[Entry]; + if (!Replaces.insert(R).second) + return; + } + GroupedReplacements[Entry].push_back(R); + } else if (Warned.insert(R.getFilePath()).second) { + errs() << "Described file '" << R.getFilePath() + << "' doesn't exist. Ignoring...\n"; + } + }; + for (const auto &TU : TUs) + for (const tooling::Replacement &R : TU.Replacements) + AddToGroup(R, false); + + for (const auto &TU : TUDs) + for (const auto &D : TU.Diagnostics) + for (const auto &Fix : D.Fix) + for (const tooling::Replacement &R : Fix.second) + AddToGroup(R, true); + + // Sort replacements per file to keep consistent behavior when + // clang-apply-replacements run on differents machine. + for (auto &FileAndReplacements : GroupedReplacements) { + llvm::sort(FileAndReplacements.second.begin(), + FileAndReplacements.second.end()); + } + return GroupedReplacements; +} + +bool mergeAndDeduplicate(const TUReplacements &TUs, const TUDiagnostics &TUDs, + FileToChangesMap &FileChanges, + clang::SourceManager &SM) { + auto GroupedReplacements = groupReplacements(TUs, TUDs, SM); + bool ConflictDetected = false; + // To report conflicting replacements on corresponding file, all replacements + // are stored into 1 big AtomicChange. + for (const auto &FileAndReplacements : GroupedReplacements) { + const FileEntry *Entry = FileAndReplacements.first; + const SourceLocation BeginLoc = + SM.getLocForStartOfFile(SM.getOrCreateFileID(Entry, SrcMgr::C_User)); + tooling::AtomicChange FileChange(Entry->getName(), Entry->getName()); + for (const auto &R : FileAndReplacements.second) { + llvm::Error Err = + FileChange.replace(SM, BeginLoc.getLocWithOffset(R.getOffset()), + R.getLength(), R.getReplacementText()); + if (Err) { + // FIXME: This will report conflicts by pair using a file+offset format + // which is not so much human readable. + // A first improvement could be to translate offset to line+col. For + // this and without loosing error message some modifications arround + // `tooling::ReplacementError` are need (access to + // `getReplacementErrString`). + // A better strategy could be to add a pretty printer methods for + // conflict reporting. Methods that could be parameterized to report a + // conflict in different format, file+offset, file+line+col, or even + // more human readable using VCS conflict markers. + // For now, printing directly the error reported by `AtomicChange` is + // the easiest solution. + errs() << llvm::toString(std::move(Err)) << "\n"; + ConflictDetected = true; + } + } + FileChanges.try_emplace(Entry, + std::vector{FileChange}); + } + return !ConflictDetected; +} +llvm::Expected +applyChanges(StringRef File, const std::vector &Changes, + const tooling::ApplyChangesSpec &Spec, + DiagnosticsEngine &Diagnostics) { + FileManager Files((FileSystemOptions())); + SourceManager SM(Diagnostics, Files); + llvm::ErrorOr> Buffer = + SM.getFileManager().getBufferForFile(File); + if (!Buffer) + return errorCodeToError(Buffer.getError()); + return tooling::applyAtomicChanges(File, Buffer.get()->getBuffer(), Changes, + Spec); +} + +bool deleteReplacementFile(const llvm::StringRef FilePath, + clang::DiagnosticsEngine &Diagnostics) { + bool Success = true; + std::error_code Error = llvm::sys::fs::remove(FilePath); + if (Error) { + Success = false; + // FIXME: Use Diagnostics for outputting errors. + errs() << "Error deleting file: " << FilePath << "\n"; + errs() << Error.message() << "\n"; + errs() << "Please delete the file manually\n"; + } + return Success; +} + +bool applyReplacements(const llvm::StringRef FilePath, const llvm::StringRef Output) { + IntrusiveRefCntPtr DiagOpts(new DiagnosticOptions()); + DiagnosticsEngine Diagnostics( + IntrusiveRefCntPtr(new DiagnosticIDs()), DiagOpts.get()); + + TUReplacements TURs; + + bool Success = + collectReplacementsFromFile(FilePath, TURs, Diagnostics); + + TUDiagnostics TUDs; + Success = + collectReplacementsFromFile(FilePath, TUDs, Diagnostics); + + if (!Success) { + errs() << "Cannot read the file '" << FilePath + << "'\n"; + return false; + } + + FileManager Files((FileSystemOptions())); + SourceManager SM(Diagnostics, Files); + + FileToChangesMap Changes; + if (!mergeAndDeduplicate(TURs, TUDs, Changes, SM)) + return false; + + tooling::ApplyChangesSpec Spec; + + for (const auto &FileChange : Changes) { + const FileEntry *Entry = FileChange.first; + StringRef FileName = Entry->getName(); + llvm::Expected NewFileData = + applyChanges(FileName, FileChange.second, Spec, Diagnostics); + if (!NewFileData) { + errs() << llvm::toString(NewFileData.takeError()) << "\n"; + continue; + } + + // Write new file to disk + std::error_code EC; + if (!Output.empty()) { + FileName = Output; + } + llvm::raw_fd_ostream FileStream(FileName, EC, llvm::sys::fs::F_None); + if (EC) { + llvm::errs() << "Could not open " << FileName << " for writing\n"; + continue; + } + FileStream << *NewFileData; + } + + // Remove yaml file + Success = deleteReplacementFile(FilePath, Diagnostics); + return Success; +} + +} // end namespace replace +} // end namespace clang diff --git a/src/MyFrontendAction.cpp b/src/MyFrontendAction.cpp new file mode 100644 index 0000000..d8e9123 --- /dev/null +++ b/src/MyFrontendAction.cpp @@ -0,0 +1,77 @@ +#include "MyFrontendAction.hpp" +#include "Logger.hpp" +#include "ToolingUtil.hpp" +#include "MatcherFactory.hpp" +#include "MyReplacementsYaml.hpp" + +#include "clang/Tooling/Tooling.h" +#include "clang/Lex/Lexer.h" +#include "llvm/Support/raw_ostream.h" +#include "llvm/Support/FileSystem.h" +#include "llvm/Support/YAMLTraits.h" + +#include +#include + +#include + +using namespace clang; +using namespace llvm; +using namespace llvm::sys; +using namespace clang::ast_matchers; +using namespace clang::tooling; + +std::mutex MyFrontendAction::mMutex; + +MyFrontendAction::MyFrontendAction(const std::string& outputFile, + const std::vector& ids) + : mOutputFile(outputFile) +{ + // register AST Matchers with callback functions + MatcherFactory& factory = MatcherFactory::Instance(); + for (const auto& id : ids) { + mCallbacks.push_back(factory.CreateCallback(id, mReplacements)); + const auto matchers_ptr = factory.CreateMatchers(id); + BOOST_ASSERT(matchers_ptr); + const auto& matchers = *matchers_ptr; + for (const auto& matcher : matchers) { + mFinder.addDynamicMatcher(matcher, + mCallbacks.back().get()); + } + } +} + +bool MyFrontendAction::BeginSourceFileAction (CompilerInstance &CI) { + SourceManager& srcMgr = CI.getSourceManager(); + FileID fileID = srcMgr.getMainFileID(); + const FileEntry* fileEntry = srcMgr.getFileEntryForID(fileID); + SmallString<256> tmp_path(fileEntry->getName()); + fs::make_absolute(tmp_path); + const std::string fileName = tmp_path.str().str(); + TRIVIAL_LOG(info) << "Processing file: " << fileName << '\n'; + return true; +} + +void MyFrontendAction::EndSourceFileAction() { + // see https://github.com/llvm-mirror/clang/blob/master/tools/clang-rename/ClangRename.cpp + // return if no replacements + if (mReplacements.empty()) return; + std::error_code EC; + + std::lock_guard guard(mMutex); + llvm::raw_fd_ostream OS(mOutputFile.get(), EC, llvm::sys::fs::F_Append); + if (EC) { + llvm::errs() << "Error opening output file: " << EC.message() << '\n'; + return; + } + + tooling::TranslationUnitReplacements TUR; + TUR.Replacements.insert(TUR.Replacements.end(), + mReplacements.begin(), + mReplacements.end()); + + yaml::Output YAML(OS); + YAML << TUR; + OS.close(); + mReplacements.clear(); +} diff --git a/src/ProgramOptions.cpp b/src/ProgramOptions.cpp new file mode 100644 index 0000000..66f44b7 --- /dev/null +++ b/src/ProgramOptions.cpp @@ -0,0 +1,80 @@ +/// +/// @file programOptions.cpp +/// +/// @Copyright 2019 The MathWorks, Inc. + +#include "ProgramOptions.hpp" +#include "cxxopts.hpp" + + +CommandLineArgs ProcessCommandLine(int argc, char**argv) +{ + CommandLineArgs args; + + cxxopts::Options options("clang-xform", "Clang tool for large-scale C++ code refactoring"); + + options.add_options("Group") + ("h, help", "produce help message") + ("j, num-threads", "number of threads", cxxopts::value()) + ("p, compile-commands", "compile commands", cxxopts::value()) + ("o, output", "output file", cxxopts::value()) + ("a, apply", "apply replacements", cxxopts::value()) + ("f, input-files", "input files", cxxopts::value >()) + ("m, matchers", "matchers to apply", cxxopts::value >()) + ("c, config", "config file", cxxopts::value()) + ("d, display", "display registered matchers", cxxopts::value()) + ("q, quiet", "silent output", cxxopts::value()) + ("l, log", "log file", cxxopts::value()); + + options.parse_positional({"input-files"}); + + auto result = options.parse(argc, argv); + + if (result.count("num-threads")) { + args.numThreads = result["num-threads"].as(); + } + + if (result.count("compile-commands")) { + args.compileCommands = result["compile-commands"].as(); + } + + if (result.count("output")) { + args.outputFile = result["output"].as(); + } + + if (result.count("apply")) { + args.replaceFile = result["apply"].as(); + } + + if (result.count("input-files")) { + args.inputFiles = result["input-files"].as >(); + } + + if (result.count("matchers")) { + args.matchers = result["matchers"].as >(); + } + + if (result.count("config")) { + args.configFile = result["config"].as(); + } + + if (result.count("display")) { + args.display = result["display"].as(); + } + + if (result.count("quiet")) { + args.quiet = result["quiet"].as(); + } + + if (result.count("log")) { + args.logFile = result["log"].as(); + } + + if (result.count("help")) + { + std::cout << options.help({"Group"}) << std::endl; + exit(0); + } + + return args; +} diff --git a/src/TestingUtil.cpp b/src/TestingUtil.cpp new file mode 100644 index 0000000..8d215e0 --- /dev/null +++ b/src/TestingUtil.cpp @@ -0,0 +1,64 @@ +#include "TestingUtil.hpp" +#include "Logger.hpp" +#include "ToolingUtil.hpp" + +#include + +#include "llvm/Support/FileSystem.h" +#include "llvm/Support/Path.h" + +using namespace llvm; +using namespace llvm::sys; + +bool CompareFiles(const std::string& p1, const std::string& p2) { + std::ifstream f1(p1, std::ifstream::binary|std::ifstream::ate); + std::ifstream f2(p2, std::ifstream::binary|std::ifstream::ate); + + if (f1.fail() || f2.fail()) { + return false; //file problem + } + + if (f1.tellg() != f2.tellg()) { + return false; //size mismatch + } + + //seek back to beginning and use std::equal to compare contents + f1.seekg(0, std::ifstream::beg); + f2.seekg(0, std::ifstream::beg); + return std::equal(std::istreambuf_iterator(f1.rdbuf()), + std::istreambuf_iterator(), + std::istreambuf_iterator(f2.rdbuf())); +} + +bool InitTest(const std::string& dirPath, + const std::string& outputFile) { + SmallString<256> tmp_path; + fs::current_path(tmp_path); + std::string root = tmp_path.str(); + std::string appname = "clang-xform"; + auto pos = root.find(appname); + if (pos == std::string::npos) return false; + + root = root.substr(0, pos + appname.length()); + // cd to clang-xform root directory + fs::set_current_path(root + '/' + dirPath); + // create a new file for output + if (fs::exists(outputFile)) { + fs::remove(outputFile); + } + + fs::createUniqueFile(outputFile, tmp_path); + // setup logging + FileLog::Verbosity() = verbosity::minimal; + TrivialLog::Verbosity() = verbosity::quiet; + // update compile_commands.json + if (ExecCmd(root + "/scripts/update-compile-commands.py")) + return false; + + return true; +} + +bool IsEmptyFile(const std::string& file) { + std::ifstream ifs("file"); + return ifs.peek() == std::ifstream::traits_type::eof(); +} diff --git a/src/ToolingUtil.cpp b/src/ToolingUtil.cpp new file mode 100644 index 0000000..57539e9 --- /dev/null +++ b/src/ToolingUtil.cpp @@ -0,0 +1,147 @@ +#include "ToolingUtil.hpp" +#include "Logger.hpp" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#include "clang/Tooling/Tooling.h" +#include "clang/Basic/SourceManager.h" +#include "clang/Basic/LangOptions.h" +#include "clang/Lex/Lexer.h" +#include "llvm/Support/FileSystem.h" +#include "llvm/Support/Path.h" + +using namespace clang; +using namespace llvm; +using namespace llvm::sys; + +namespace { + +// remove whitespace for the given string +inline std::string RemoveWhitespace(const std::string& s) +{ + auto begin = s.find_first_not_of(' '); + auto end = s.find_last_not_of(' '); + assert((begin != std::string::npos) && (end != std::string::npos)); + return s.substr(begin, end + 1 - begin); +} + +} // end anonymous namespace + +int ExecCmd(const std::string& cmd, std::string& result) { + std::array buffer; + FILE* pipe = popen(cmd.c_str(), "r"); + if (!pipe) { + throw std::runtime_error("popen() failed!"); + } + while (fgets(buffer.data(), buffer.size(), pipe) != nullptr) { + result += buffer.data(); + } + return WEXITSTATUS(pclose(pipe)); +} + +bool ParseConfigFile(const std::string& fileName, const std::string& key, std::vector& args) +{ + BOOST_ASSERT(args.empty()); + // open file for reading + std::ifstream ifs(fileName); + std::string line; + + if (ifs.good()) { + while (std::getline(ifs, line)) { + std::istringstream is_line(line); + std::string next_key; + if (std::getline(is_line, next_key, '=') || std::getline(is_line, next_key, ' ')) { + // check if the next_key string is the same as input key string after removing extra white spaces + next_key = RemoveWhitespace(next_key); + if (next_key != key) continue; + std::string value; + while (std::getline(is_line, value, ' ')) { + if (value.empty()) { + continue; + } + value = RemoveWhitespace(value); + args.emplace_back(std::move(value)); + } + } + } + } else { + std::cerr << "Cannot open file: " << fileName << '\n'; + return false; + } + return true; +} + +clang::SourceLocation getExpansionLocStart(clang::SourceLocation Loc, const clang::SourceManager& sm) +{ + if (Loc.isFileID()) return Loc; + else { + do { + Loc = sm.getImmediateExpansionRange(Loc).getBegin(); + // Loc = sm.getImmediateExpansionRange(Loc).first; + } while (!Loc.isFileID()); + return Loc; + } +} + +SourceLocation getExpansionLocEnd(SourceLocation Loc, const SourceManager& sm) +{ + if (Loc.isFileID()) return Loc; + else { + do { + if (sm.isMacroArgExpansion(Loc)) + Loc = sm.getImmediateExpansionRange(Loc).getBegin(); + // Loc = sm.getImmediateExpansionRange(Loc).first; + else + Loc = sm.getImmediateExpansionRange(Loc).getEnd(); + // Loc = sm.getImmediateExpansionRange(Loc).second; + } while (!Loc.isFileID()); + return Loc; + } +} + +std::string getSourceText(SourceLocation Start, + SourceLocation End, + const SourceManager& SM, + const LangOptions &LangOpts) +{ + End = Lexer::getLocForEndOfToken(End, 0, SM, LangOpts); + //CharSourceRange Range = Lexer::getAsCharRange(SourceRange(std::move(Start), std::move(End)), SM, LangOpts); + //return Lexer::getSourceText(Range ,SM, LangOpts); + return std::string(SM.getCharacterData(Start), + SM.getCharacterData(End) - SM.getCharacterData(Start)); +} + +void LogReplacement(clang::SourceLocation loc, const clang::SourceManager& sm, + const std::string& oldExpr, const std::string& newExpr) +{ + std::string screenLogString = "Editting file: " + loc.printToString(sm) + ": \"" + oldExpr + "\"" + + " --> " + "\"" + newExpr + "\""; + std::string fileLogString = "Editting file:\n" + loc.printToString(sm) + ":\n\"" + oldExpr + "\"" + + " --> " + "\"" + newExpr + "\""; + TRIVIAL_LOG(severity::info) << screenLogString << '\n'; + FILE_LOG(severity::info) << fileLogString << "\n\n"; +} + +void LogASTNode(clang::SourceLocation loc, const clang::SourceManager& sm, + const std::string& expr) { + std::string screenLogString = "Finding AST Node: " + + loc.printToString(sm) + ": \"" + expr + "\""; + std::string fileLogString = "Finding AST Node:\n" + + loc.printToString(sm) + ":\n\"" + expr + "\""; + + TRIVIAL_LOG(info) << screenLogString << '\n'; + FILE_LOG(info) << fileLogString << "\n\n"; +} diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..3d38b0d --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,367 @@ +#include "ProgramOptions.hpp" +#include "Logger.hpp" +#include "MyFrontendAction.hpp" +#include "ToolingUtil.hpp" +#include "ApplyReplacements.hpp" +#include "MatcherFactory.hpp" +#include "cxxopts.hpp" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#include "clang/Tooling/Tooling.h" +#include "clang/Tooling/CompilationDatabase.h" +#include "clang/Tooling/JSONCompilationDatabase.h" +#include "clang/Basic/Diagnostic.h" +#include "clang/Basic/DiagnosticOptions.h" +#include "llvm/Support/raw_os_ostream.h" +#include "llvm/Support/FileSystem.h" +#include "llvm/Support/Path.h" + +using namespace clang::tooling; +using namespace clang; +using namespace llvm::sys; +using namespace llvm; + +int ProcessFiles(const CompilationDatabase& compilationDatabase, + const std::vector& inputFiles, + const std::string& outputFile, + const std::vector& matchers, + unsigned int numThreads) +{ + // We are trying to achieve a balance between two competing efficiency sources: + // - Efficiency is gained by having the files processed by individual threads. This is an + // "embarrasing parallel problem." + // - Efficiency is gained by having each clang::tooling::ClangTool do multiple files because + // each tool only needs to + // create the AST of each header file once. + // + // To obtain a balance, we have each thread have one clang::toolingClangTool instance and each + // instance do several files. + auto const hwConcurrency = std::max( + 1u, std::min(numThreads, std::max(4u, std::thread::hardware_concurrency()))); + auto const numFiles = inputFiles.size(); + + auto const filesPerCore = std::max( + size_t(3), static_cast(std::round(double(numFiles) / double(hwConcurrency)))); + + numThreads = ceil(double(numFiles) / double(filesPerCore)); + + // std::cout << "numFiles: " << numFiles << std::endl; + // std::cout << "filesPerCore: " << filesPerCore << std::endl; + // std::cout << "numThreads: " << numThreads << std::endl; + + std::vector threads; + std::vector > > futures; + + // loop through the list of files and process filesPerCore during each loop iteration. + for (size_t beginRange = 0, endRange = std::min(numFiles, filesPerCore); beginRange < numFiles; + beginRange = endRange, endRange = std::min(numFiles, endRange + filesPerCore)) { + + std::vector fileSubset(inputFiles.begin() + beginRange, + inputFiles.begin() + endRange); + + std::packaged_task()> task( + [&compilationDatabase, &outputFile, &matchers, files = std::move(fileSubset)]() + { + clang::tooling::ClangTool tool(compilationDatabase, files); + + std::stringstream diagnostics; + llvm::raw_os_ostream raw_ostream(diagnostics); + clang::DiagnosticOptions diagOpts; + // replace DiagnosticLogger with TextDiagnosticPrinter to debug + auto printDiagnostics = + std::make_unique(raw_ostream, &diagOpts); + + tool.setDiagnosticConsumer(printDiagnostics.release()); + + //tool.setDiagnosticConsumer(new clang::IgnoringDiagConsumer()); + + const int toolStatus = tool.run(std::make_unique(outputFile, matchers).get()); + + return std::make_tuple(toolStatus, diagnostics.str()); + }); + futures.push_back(task.get_future()); + // let the current thread perform the last batch of files. + if (endRange < numFiles) { + // start new threads + threads.emplace_back(std::move(task)); + } + else { + task(); + } + } + + // wait for all the futures to finish and return the number of failed tasks. + auto ret = std::accumulate(futures.begin(), futures.end(), 0, + [](int sum, std::future >& f) + { + auto next = f.get(); + const int nextStatus = std::get<0>(next); + const std::string nextErrorMessage = std::move(std::get<1>(next)); + + // when the tool fails, log any diagnostics accumulated from clang. + if ((0 != nextStatus) && !nextErrorMessage.empty()) { + TRIVIAL_LOG(info) << nextErrorMessage << '\n'; + } + + return sum + nextStatus; + }); + + // join threads + std::for_each(threads.begin(), threads.end(), + [](std::thread& t) + { + if (t.joinable()) + { + t.join(); + } + }); + return ret; +} + +int main(int argc, char **argv) { + std::string errMsg; + // first read and remove flags after seperator -- + std::unique_ptr compilations = FixedCompilationDatabase::loadFromCommandLine(argc, argv, errMsg); + + // parse command line options + CommandLineArgs args; + try { + args = ProcessCommandLine(argc, argv); + } + catch (cxxopts::OptionParseException& e) { + std::cerr << e.what() << '\n'; + std::cerr << "See sbcodexform --help" << '\n'; + exit(1); + } + + // read flags + std::string compileCommands = std::move(args.compileCommands); + std::string configFile = std::move(args.configFile); + std::vector inputFiles = std::move(args.inputFiles); + std::vector matchers = std::move(args.matchers); + unsigned int numThreads = args.numThreads; + std::string outputFile = std::move(args.outputFile); + std::string replaceFile = std::move(args.replaceFile); + std::string logFile = std::move(args.logFile); + bool display = args.display; + bool quiet = args.quiet; + + // setup log file + if (logFile.empty()) { + logFile = "clang-xform.log"; + } + RegisterLogFile log_file(logFile); + if (quiet) { + TrivialLog::Verbosity() = verbosity::quiet; + } + + // if cfg file is specified, read the flags + if (!configFile.empty()) { + std::vector extraMatchers; + std::vector extraInputFiles; + if (!ParseConfigFile(configFile, "matchers", extraMatchers)) { + std::cerr << "Failed in parsing config file: " << configFile << '\n'; + return 1; + } + if (!ParseConfigFile(configFile, "input-files", extraInputFiles)) { + std::cerr << "Failed in parsing config file: " << configFile << '\n'; + return 1; + } + matchers.insert(matchers.end(), + std::make_move_iterator(extraMatchers.begin()), + std::make_move_iterator(extraMatchers.end())); + inputFiles.insert(inputFiles.end(), + std::make_move_iterator(extraInputFiles.begin()), + std::make_move_iterator(extraInputFiles.end())); + } + + // validate flags + // Flags --compile-commands and -- [CLANG_FLAGS] are mutually exclusive + if ((!compileCommands.empty() + (compilations != nullptr)) > 1) { + std::cerr << "Options --compile-commands and -- [CLANG_FLAGS] are mutually exclusive!" << '\n'; + std::cerr << "See clang-xform --help" << '\n'; + return 1; + } + int flagsum = !compileCommands.empty() + !configFile.empty() + + !inputFiles.empty() + !matchers.empty() + + !outputFile.empty() + !replaceFile.empty() + + display; + // Flags --apply should be mutually exclusive with the rest options + if (!replaceFile.empty() && flagsum > 1) { + std::cerr << "Options --apply should be mutually exclusive with the rest options" << '\n'; + std::cerr << "See clang-xform --help" << '\n'; + return 1; + } + // Flags --display should be mutually exclusive with the rest options + if (display && flagsum > 1) { + std::cerr << "Options --display should be mutually exclusive with the rest options" << '\n'; + std::cerr << "See clang-xform --help" << '\n'; + return 1; + } + // option --output should be a file with yaml extension + if (!outputFile.empty() && path::extension(outputFile) != ".yaml") { + std::cerr << "Output file extension is not yaml" << '\n'; + std::cerr << "See clang-xform --help" << '\n'; + return 1; + } + // arguments for option --matchers should be registered + MatcherFactory& factory = MatcherFactory::Instance(); + for (const auto& matcher : matchers) { + if (!factory.CreateMatchers(matcher)) { + // matcher id does not exist + std::cerr << "Matcher ID: " << matcher << " is not registered!\n"; + return 1; + } + } + + // is --output is not set, by default the replacements will be applied at the end of the program. + SmallString<256> tmp_path; + std::string outputFileName = "tmp_output_file.yaml"; + if (outputFile.empty() && replaceFile.empty()) { + outputFile = outputFileName; + } + + // create a new file for output + if (fs::exists(outputFile)) { + fs::remove(outputFile); + } + fs::current_path(tmp_path); + fs::createUniqueFile(outputFile, tmp_path); + + // convert dirs and files to absolute path + if (!compileCommands.empty()) { + fs::real_path(compileCommands, tmp_path, true); + fs::make_absolute(tmp_path); + compileCommands = tmp_path.str().str(); + } + if (!outputFile.empty()) { + fs::real_path(outputFile, tmp_path, true); + fs::make_absolute(tmp_path); + outputFile = tmp_path.str().str(); + } + if (!replaceFile.empty()) { + fs::real_path(replaceFile, tmp_path, true); + fs::make_absolute(tmp_path); + replaceFile = tmp_path.str().str(); + } + if (!logFile.empty()) { + fs::real_path(logFile, tmp_path, true); + fs::make_absolute(tmp_path); + logFile = tmp_path.str().str(); + } + for(auto& file : inputFiles) { + fs::real_path(file, tmp_path, true); + fs::make_absolute(tmp_path); + file = tmp_path.str().str(); + } + + // display registered matchers + if (display) { + const auto& matcherMap = factory.getMatcherMap(); + for (const auto& pair : matcherMap) { + std::cout << pair.first << '\n'; + } + return 0; + } + // when -a is given + if (!replaceFile.empty()) { + // apply replacements + TRIVIAL_LOG(info) << "Apply replacements: " << replaceFile << '\n'; + if (!replace::applyReplacements(replaceFile)) { + std::cerr << "Failed in applying replacements in " + replaceFile << '\n'; + return 1; + } + return 0; + } + + + // store cwd + std::string cwd; + fs::current_path(tmp_path); + cwd = tmp_path.str(); + + int status = 0; + // components option is not specified + // if -p is given + if (!compileCommands.empty()) + { + TRIVIAL_LOG(info) << "Loading file: " << compileCommands << '\n'; + auto pos = compileCommands.find_last_of('/'); + fs::set_current_path(compileCommands.substr(0, pos)); + // use jsonCompilationDatabase provided in json file + compilations = + JSONCompilationDatabase::loadFromFile(compileCommands.substr(pos + 1, + compileCommands.length() -pos - 1), + errMsg, + JSONCommandLineSyntax::Gnu); + if (!compilations) { + std::cerr << "Error while trying to load a json compilation database:\n" + << errMsg << '\n' + << "Json file does not exist.\n"; + return 1; + } + if (inputFiles.empty()) + { + inputFiles = compilations->getAllFiles(); + } + status = ProcessFiles(*compilations, inputFiles, outputFile, matchers, numThreads); + } + else + { + if (!inputFiles.empty()) { + if (compilations) { + // use fixedCompilationDatabase provided in command line + status = ProcessFiles(*compilations, inputFiles, outputFile, matchers, numThreads); + } + else { + // auto detect compile_commands.json file if only on input file + compilations = CompilationDatabase::autoDetectFromSource(inputFiles[0], + errMsg); + if (!compilations) { + std::cerr << "Error while trying to load a compilation database:\n" + << errMsg << "Running without flags.\n"; + return 1; + } + status = ProcessFiles(*compilations, inputFiles, outputFile, matchers, numThreads); + } + } + else { + std::cerr << "No given input file!" << '\n'; + return 1; + } + } + + // restore cwd + fs::set_current_path(cwd); + + // apply replacement automatically if the outputFile is default + if (outputFile.rfind(outputFileName) != std::string::npos) { + // apply replacements + TRIVIAL_LOG(info) << "Apply replacements: " << outputFile << '\n'; + if (!replace::applyReplacements(outputFile)) { + std::cerr << "Failed in applying replacements in " + outputFile << '\n'; + return 1; + } + } else { + std::cout << '\n' << "Replacements are stored in " << outputFile << "\n\n"; + std::cout << "To apply replacements, run:" << "\n\n"; + std::cout << "clang-xform -a " + outputFile << '\n'; + } + + std::cout << '\n' << "Check " << logFile << " to see log information" << "\n\n"; + + return status; +} diff --git a/src/matchers/rename/RenameFcn.cpp b/src/matchers/rename/RenameFcn.cpp new file mode 100644 index 0000000..bdb1913 --- /dev/null +++ b/src/matchers/rename/RenameFcn.cpp @@ -0,0 +1,69 @@ +#include "MyMatchCallback.hpp" // provides macro MATCH_CALLBACK to define a new match callback +#include "MatcherFactory.hpp" // a factory class used to register new matcher and callback +#include "ToolingUtil.hpp" // APIs to extract locations and tokens for a given AST node +#include "Logger.hpp" // basic logging functionality + +#include + +using namespace clang; +using namespace clang::ast_matchers; + +// This a generated template file to help write your own clang AST matcher and callback. +// Please address all comments in /**/ form below! +// [1] : use StatementMatcher or DeclarationMatcher +// [2] : replace it with a suitable node matcher +// [3] : add narrowing or traversal matchers here +// [4] : rename the string ID for a better description of the matched node +// [5] : replace it with the corresponding node class type binded with the string ID +// [6] : variable name for the matched node +// [7] : same as [5] +// [8] : string ID used in matcher, same as [4] +// [9] : name of the node to be replaced +// [10]: string used to replace the matched node +// [11]: use "LogASTNode(locStart, srcMgr, oldExprString)" to log matched AST Node infomation +// [12]: can register more than one matcher of different types + +namespace { +// oldName: qualified function name to match +// newName: new function name to use +const std::string oldName = "Foo"; +const std::string newName = "Bar"; + +StatementMatcher RenameFcnMatcher = + callExpr(callee(functionDecl(hasName(oldName))), + isExpansionInMainFile() + ).bind("RenameFcnExpr"); + +// Match callback class RenameFcnCallback is defined here +MATCH_CALLBACK(RenameFcnCallback); + +// Definiation of RenameFcnCallback::run +void RenameFcnCallback::run(const clang::ast_matchers::MatchFinder::MatchResult& Result) { + std::string oldExprString; + std::string newExprString; + const auto& srcMgr = Result.Context->getSourceManager(); + const auto& langOpts = Result.Context->getLangOpts(); + + // Check any AST node matched for the given string ID. + // The node class name is usually the capitalized node matcher name. + if (const CallExpr* RenameFcnExpr = + Result.Nodes.getNodeAs("RenameFcnExpr")) { + // find begin and end file locations of a given node + // use getExprLoc() for the begin loc which returns MemberLoc if it is a member function. + // i.e. X->F return F + auto locStart = srcMgr.getFileLoc(RenameFcnExpr->getCallee()->getExprLoc()); + auto locEnd = srcMgr.getFileLoc(RenameFcnExpr->getCallee()->getEndLoc()); + newExprString = newName; + // find source text for a given location + oldExprString = getSourceText(locStart, locEnd, srcMgr, langOpts); + // replace source text with a given string + ReplaceText(srcMgr, SourceRange(std::move(locStart), std::move(locEnd)), newExprString); + // log the replacement or AST node if no replacement is made + LogReplacement(locStart, srcMgr, oldExprString, newExprString); + } +} + +// register your own matcher and callback +MatcherHelper RegisterRenameFcnMatcher("RenameFcn", {RenameFcnMatcher}); + +} diff --git a/test/.gitignore b/test/.gitignore new file mode 100644 index 0000000..4293968 --- /dev/null +++ b/test/.gitignore @@ -0,0 +1,9 @@ +*~ +bin/ +CMakeFiles/ +cmake_install.cmake +CTestTestfile.cmake +googletest-build/ +googletest-download/ +googletest-src +Makefile \ No newline at end of file diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt new file mode 100644 index 0000000..872c40f --- /dev/null +++ b/test/CMakeLists.txt @@ -0,0 +1,52 @@ +if (BUILD_TESTS) + + # Download and unpack googletest at configure time + configure_file(CMakeLists.txt.in googletest-download/CMakeLists.txt) + execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" . + RESULT_VARIABLE result + WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/googletest-download ) + if(result) + message(FATAL_ERROR "CMake step for googletest failed: ${result}") + endif() + execute_process(COMMAND ${CMAKE_COMMAND} --build . + RESULT_VARIABLE result + WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/googletest-download ) + if(result) + message(FATAL_ERROR "Build step for googletest failed: ${result}") + endif() + + # Prevent overriding the parent project's compiler/linker + # settings on Windows + set(gtest_force_shared_crt ON CACHE BOOL "" FORCE) + + # Add googletest directly to our build. This defines + # the gtest and gtest_main targets. + add_subdirectory(${CMAKE_CURRENT_BINARY_DIR}/googletest-src + ${CMAKE_CURRENT_BINARY_DIR}/googletest-build + EXCLUDE_FROM_ALL) + + # The gtest/gtest_main targets carry header search path + # dependencies automatically when using CMake 2.8.11 or + # later. Otherwise we have to add them here ourselves. + if (CMAKE_VERSION VERSION_LESS 2.8.11) + include_directories("${gtest_SOURCE_DIR}/include") + endif() + + include_directories("${CMAKE_CURRENT_SOURCE_DIR}/../include") + + # source files + file(GLOB_RECURSE TEST_CPP + ${CMAKE_CURRENT_SOURCE_DIR}/t*.cpp + ) + + # remove main.cpp + list(FILTER SRC_CPP EXCLUDE REGEX ".*main.cpp$") + + set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/bin) + # Link GTest library + add_executable(unittest ${TEST_CPP} ${SRC_CPP}) + target_link_libraries(unittest gtest_main ${CLANG_LIBS}) + + add_test(NAME unittest COMMAND unittest) + +endif(BUILD_TESTS) diff --git a/test/CMakeLists.txt.in b/test/CMakeLists.txt.in new file mode 100644 index 0000000..c6247af --- /dev/null +++ b/test/CMakeLists.txt.in @@ -0,0 +1,15 @@ +cmake_minimum_required(VERSION 2.8.2) + +project(googletest-download NONE) + +include(ExternalProject) +ExternalProject_Add(googletest + GIT_REPOSITORY https://github.com/google/googletest.git + GIT_TAG master + SOURCE_DIR "${CMAKE_CURRENT_BINARY_DIR}/googletest-src" + BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/googletest-build" + CONFIGURE_COMMAND "" + BUILD_COMMAND "" + INSTALL_COMMAND "" + TEST_COMMAND "" +) diff --git a/test/rename/RenameFcn/compile_commands.json b/test/rename/RenameFcn/compile_commands.json new file mode 100644 index 0000000..58835db --- /dev/null +++ b/test/rename/RenameFcn/compile_commands.json @@ -0,0 +1,7 @@ +[ + { + "directory": "/home/sean/Desktop/clang-xform/test/rename/RenameFcn", + "file": "example.cpp", + "command": "g++ -c example.cpp -I/usr/include/c++/6 -I/usr/include/x86_64-linux-gnu/c++/6 -I/usr/include/c++/6/backward -I/usr/lib/gcc/x86_64-linux-gnu/6/include -I/usr/local/include -I/usr/lib/gcc/x86_64-linux-gnu/6/include-fixed -I/usr/include/x86_64-linux-gnu -I/usr/include" + } +] \ No newline at end of file diff --git a/test/rename/RenameFcn/example.cpp b/test/rename/RenameFcn/example.cpp new file mode 100644 index 0000000..ed2a7ce --- /dev/null +++ b/test/rename/RenameFcn/example.cpp @@ -0,0 +1,6 @@ +void Foo() {} + +int main() { + Foo(); + return 0; +} diff --git a/test/rename/RenameFcn/example.cpp.gold b/test/rename/RenameFcn/example.cpp.gold new file mode 100644 index 0000000..dd5f45c --- /dev/null +++ b/test/rename/RenameFcn/example.cpp.gold @@ -0,0 +1,6 @@ +void Foo() {} + +int main() { + Bar(); + return 0; +} diff --git a/test/rename/RenameFcn/example.cpp.refactored b/test/rename/RenameFcn/example.cpp.refactored new file mode 100644 index 0000000..dd5f45c --- /dev/null +++ b/test/rename/RenameFcn/example.cpp.refactored @@ -0,0 +1,6 @@ +void Foo() {} + +int main() { + Bar(); + return 0; +} diff --git a/test/rename/RenameFcn/sbcodexform.log b/test/rename/RenameFcn/sbcodexform.log new file mode 100644 index 0000000..957bc0e --- /dev/null +++ b/test/rename/RenameFcn/sbcodexform.log @@ -0,0 +1,6 @@ +-*- compilation-minor -*- + +Editting file: +example.cpp:4:5: +"Foo" --> "Bar" + diff --git a/test/rename/RenameFcn/sbcodexform.log.gold b/test/rename/RenameFcn/sbcodexform.log.gold new file mode 100644 index 0000000..957bc0e --- /dev/null +++ b/test/rename/RenameFcn/sbcodexform.log.gold @@ -0,0 +1,6 @@ +-*- compilation-minor -*- + +Editting file: +example.cpp:4:5: +"Foo" --> "Bar" + diff --git a/test/rename/RenameFcn/tRenameFcn.cpp b/test/rename/RenameFcn/tRenameFcn.cpp new file mode 100644 index 0000000..ea83ba3 --- /dev/null +++ b/test/rename/RenameFcn/tRenameFcn.cpp @@ -0,0 +1,58 @@ +#include "MyFrontendAction.hpp" +#include "ApplyReplacements.hpp" +#include "TestingUtil.hpp" +#include "Logger.hpp" + +#include +#include + +#include "clang/Tooling/Tooling.h" +#include "clang/Tooling/CompilationDatabase.h" + +#include "gtest/gtest.h" + +using namespace clang::tooling; +// This unit test compares the log file and refactored src file with corresponding baseline. +// The test is self-explained. The user does not need to make any changes here unless +// for other customizations. It is recommended to check the following few places. +// [1] dirPath: test directory path, must start with "test/" +// [2] logFile: log file name (not path), default is "sbcodexform.log" +// [3] inputFile, src file name (not path) to refactor, default is "example.cpp" + +TEST(MatcherTest, RenameFcn) { + // must start with test/ + std::string dirPath/*[1]*/ = "test/rename/RenameFcn"; + std::string logFile/*[2]*/ = "sbcodexform.log"; + std::string inputFile/*[3]*/ = "example.cpp"; + std::string outputFile = "tmp_output_file.yaml"; + // chdir dirPath, create outputFile, set logging properties + int status = InitTest(dirPath, outputFile); + ASSERT_TRUE(status); + // setup log file + RegisterLogFile log_file(logFile); + + std::string refactoredFile = inputFile + ".refactored"; + std::string baselineFile = inputFile + ".gold"; + std::string baselineLog = logFile + ".gold"; + std::vector matchers = {"RenameFcn"}; + + // retrieve compliation database + std::string errMsg; + std::unique_ptr compilations = + CompilationDatabase::autoDetectFromSource(inputFile, + errMsg); + ASSERT_TRUE(compilations != nullptr); + clang::tooling::ClangTool tool(*compilations, inputFile); + status = tool.run(std::make_unique(outputFile, matchers).get()); + ASSERT_EQ(status, 0); + // test if matched locations are correct + ASSERT_TRUE(CompareFiles(logFile, baselineLog)); + + status = clang::replace::applyReplacements(outputFile, refactoredFile); + ASSERT_TRUE(status); + + if (!IsEmptyFile(outputFile)) { + // test if replacements are correct when outputFile is not empty + ASSERT_TRUE(CompareFiles(refactoredFile, baselineFile)); + } +}