Skip to content

Commit

Permalink
feat: v1 upload
Browse files Browse the repository at this point in the history
  • Loading branch information
liuzhishan committed Nov 24, 2024
1 parent febf5fb commit 843606f
Show file tree
Hide file tree
Showing 223 changed files with 30,948 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# auto-cpp-rewriter
111 changes: 111 additions & 0 deletions convert/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
set(LLVM_LINK_COMPONENTS support)
add_compile_options(-Werror=return-type)

set(CMAKE_CXX_STANDARD 17)

find_package(gflags)
find_package (glog 0.6.0 REQUIRED)
find_package (absl REQUIRED)

include(FetchContent)

FetchContent_Declare(json
GIT_REPOSITORY https://github.com/ArthurSonzogni/nlohmann_json_cmake_fetchcontent
GIT_TAG v3.10.2)

FetchContent_GetProperties(json)
if(NOT json_POPULATED)
FetchContent_Populate(json)
add_subdirectory(${json_SOURCE_DIR} ${json_BINARY_DIR} EXCLUDE_FROM_ALL)
endif()

add_clang_executable(convert
Env.cpp
Tool.cpp
Config.cpp
Convert.cpp
Deleter.cpp
ExprInfo.cpp
ExprParser.cpp
ExprParserDetail.cpp
ExprParserBSField.cpp
ConvertAction.cpp
LogicParser.cpp
info/Info.cpp
info/IfInfo.cpp
info/LoopInfo.cpp
info/InfoBase.cpp
info/NewVarDef.cpp
info/MethodInfo.cpp
info/ActionMethodInfo.cpp
info/NewActionParam.cpp
info/PrefixPair.cpp
info/CommonInfo.cpp
info/CommonInfoCore.cpp
info/CommonInfoLeaf.cpp
info/CommonInfoFixed.cpp
info/CommonInfoFixedList.cpp
info/CommonInfoBodyText.cpp
info/CommonInfoDetail.cpp
info/CommonInfoPrepare.cpp
info/CommonInfoNormal.cpp
info/CommonInfoMultiIntList.cpp
info/CommonInfoMiddleNode.cpp
info/VarDeclInfo.cpp
info/FeatureInfo.cpp
info/BinaryOpInfo.cpp
info/ConstructorInfo.cpp
info/MiddleNodeInfo.cpp
info/SeqListInfo.cpp
info/ActionDetailInfo.cpp
info/ActionDetailFixedInfo.cpp
info/BSFieldInfo.cpp
rule/RuleBase.cpp
rule/PreRule.cpp
rule/GeneralRule.cpp
rule/MiddleNodeRule.cpp
rule/CommonInfoRule.cpp
rule/ActionDetailRule.cpp
rule/DoubleListRule.cpp
rule/ProtoListRule.cpp
rule/SeqListRule.cpp
rule/AddFeatureMethodRule.cpp
rule/HashFnRule.cpp
rule/QueryTokenRule.cpp
rule/StrRule.cpp
rule/BSFieldOrderRule.cpp
rule/proto_list/ProtoListExprInfo.cpp
handler/StrictRewriter.cpp
handler/OverviewHandler.cpp
handler/AdlogFieldHandler.cpp
handler/FieldDeclHandler.cpp
handler/LogicHandler.cpp
handler/BSFieldHandler.cpp
expr_parser/ExprParserQueryToken.cpp
visitor/CtorVisitor.cpp
visitor/BSCtorVisitor.cpp
visitor/FieldDeclVisitor.cpp
visitor/ExtractMethodVisitor.cpp
visitor/BSExtractMethodVisitor.cpp
visitor/MiddleNodeJson.cpp
matcher_callback/FeatureDeclCallback.cpp
matcher_callback/InferFilterCallback.cpp
matcher_callback/TypeAliasCallback.cpp
matcher_callback/BSFeatureDeclCallback.cpp
matcher_callback/BSTypeAliasCallback.cpp
)

target_link_libraries(convert
PUBLIC
clangTooling
clangBasic
clangASTMatchers
clangFrontend
clangSerialization
clangTooling
glog
gflags
nlohmann_json::nlohmann_json
absl::strings
absl::optional
)
20 changes: 20 additions & 0 deletions convert/Config.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include "Config.h"

namespace ks {
namespace ad_algorithm {
namespace convert {

FeatureInfo* GlobalConfig::feature_info_ptr(const std::string& feature_name) {
std::lock_guard<std::mutex> lock(mu);
auto it = feature_info.find(feature_name);
if (it != feature_info.end()) {
return &(it->second);
}

it = feature_info.insert({feature_name, FeatureInfo(feature_name)}).first;
return &(it->second);
}

} // namespace convert
} // namespace ad_algorithm
} // namespace ks
84 changes: 84 additions & 0 deletions convert/Config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#pragma once

#include <glog/logging.h>
#include <absl/types/optional.h>
#include <nlohmann/json.hpp>

#include <algorithm>
#include <iostream>
#include <list>
#include <mutex>
#include <set>
#include <sstream>
#include <map>
#include <string>
#include <unordered_map>
#include <vector>

#include "clang/AST/AST.h"
#include "clang/AST/ASTConsumer.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include "clang/ASTMatchers/ASTMatchers.h"
#include "clang/Basic/SourceLocation.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Frontend/FrontendActions.h"
#include "clang/Lex/Preprocessor.h"
#include "clang/Rewrite/Core/Rewriter.h"
#include "clang/Tooling/CommonOptionsParser.h"
#include "clang/Tooling/Tooling.h"
#include "info/FeatureInfo.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/raw_ostream.h"

namespace ks {
namespace ad_algorithm {
namespace convert {

using nlohmann::json;

class FeatureInfo;

class GlobalConfig final {
public:
static GlobalConfig* Instance() {
static GlobalConfig instance;
return &instance;
}

FeatureInfo* feature_info_ptr(const std::string& feature_name);

std::mutex mu;
clang::FileID file_id;
clang::SourceManager* source_manager = nullptr;

std::string cmd = "";
bool remove_comment = false;
bool dump_ast = false;
bool overwrite = false;
std::string filename;
std::string message_def_filename;
std::string field_detail_filename;
bool use_reco_user_info = false;
bool rewrite_reco_user_info = false;

std::string middle_node_json_file = "data/middle_node.json";

json all_adlog_fields = json::object();
json feature_def = json::object();
json fast_feature_def = json::object();
std::map<std::string, int> enum_map;

std::vector<std::string> filenames;
std::string feature_list_filename =
"teams/ad/ad_algorithm/feature/fast/impl/feature_list_complete_adlog.cc";
std::map<std::string, std::string> feature_filename_map;
std::map<std::string, std::string> feature_content_map;
std::unordered_map<std::string, FeatureInfo> feature_info;

/// infer filter 函数实现,暂时先用 map 简单处理,后面再重构。
std::unordered_map<std::string, std::string> infer_filter_funcs;
};

} // namespace convert
} // namespace ad_algorithm
} // namespace ks
108 changes: 108 additions & 0 deletions convert/Convert.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
#include <glog/logging.h>
#include <gflags/gflags.h>
#include "clang/Frontend/FrontendActions.h"
#include "clang/Tooling/CommonOptionsParser.h"
#include "clang/Tooling/Tooling.h"
#include "llvm/Support/CommandLine.h"
#include "clang/ASTMatchers/ASTMatchers.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"

#include "clang/Rewrite/Core/Rewriter.h"
#include "clang/Basic/SourceLocation.h"
#include "clang/AST/AST.h"
#include "clang/AST/ASTConsumer.h"
#include "llvm/Support/raw_ostream.h"
#include "clang/Frontend/CompilerInstance.h"
#include "llvm/ADT/StringRef.h"
#include <iostream>
#include <string>
#include <sstream>
#include <list>
#include <algorithm>

#include "ConvertAction.h"
#include "LogicParser.h"

using namespace llvm;
using namespace clang;
using namespace clang::tooling;

static llvm::cl::OptionCategory MatcherCategory("Matcher");

cl::opt<std::string> Cmd("cmd",
cl::desc("cmd (default = 'hello') "),
cl::value_desc("cmd"),
cl::init("hello"));

cl::opt<bool> RemoveComment("remove-comment",
cl::desc("remove comment"),
cl::init(false));

cl::opt<bool> DumpAst("dump-ast",
cl::desc("dump ast"),
cl::init(false));

cl::opt<bool> Overwrite("overwrite",
cl::desc("overwrite exists bs file, default false"),
cl::init(false));

cl::opt<std::string> Filename("filename",
cl::desc("filename"),
cl::init(""));

cl::opt<std::string> FieldDetailFilename("field-detail-filename",
cl::desc("field detail filename for parse result"),
cl::init(""));

cl::opt<std::string> MessageDefFilename("message-def-filename",
cl::desc("json filename for message def"),
cl::init(""));

cl::opt<bool> UseRecoUserInfo("use_reco_user_info",
cl::desc("use reco user info"),
cl::init(false));

DECLARE_bool(logtostderr);

using ks::ad_algorithm::convert::GlobalConfig;
using ks::ad_algorithm::convert::ConvertAction;
using ks::ad_algorithm::convert::LogicParser;

int main(int argc, const char **argv) {
google::InitGoogleLogging(argv[0]);
FLAGS_logtostderr = 1;

auto ExpectedParser = CommonOptionsParser::create(argc, argv, MatcherCategory);
if (!ExpectedParser) {
LOG(ERROR) << "ExpectedParser error, return";
return 1;
}

auto config = GlobalConfig::Instance();
config->cmd = Cmd;
config->remove_comment = RemoveComment;
config->dump_ast = DumpAst;
config->overwrite = Overwrite;
config->filename = Filename;
config->field_detail_filename = FieldDetailFilename;
config->message_def_filename = MessageDefFilename;
config->use_reco_user_info = UseRecoUserInfo;

LOG(INFO) << "Cmd: " << config->cmd;

CommonOptionsParser& op = ExpectedParser.get();
ClangTool Tool(op.getCompilations(), op.getSourcePathList());

if (config->cmd == "hello") {
LOG(INFO) << "hello";
} else if (config->cmd == "convert") {
return Tool.run(newFrontendActionFactory<ConvertAction>().get());
} else if (config->cmd == "parse_logic") {
return Tool.run(newFrontendActionFactory<LogicParser>().get());
} else {
LOG(ERROR) << "unsupported cmd: " << config->cmd;
}

return 0;
}

Loading

0 comments on commit 843606f

Please sign in to comment.