<commit_before>//===- PrintFunctionNames.cpp ---------------------------------------------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// Example clang plugin which simply prints the names of all the top-level decls
// in the input file.
//
//===----------------------------------------------------------------------===//

#include "clang/Frontend/FrontendPluginRegistry.h"
#include "clang/AST/AST.h"
#include "clang/AST/ASTConsumer.h"
#include "clang/Frontend/CompilerInstance.h"
#include "llvm/Support/raw_ostream.h"
using namespace clang;

namespace {
	const std::vector<std::string> endsWith = {
		"iterator",
		"iplist",
		"analysis",
		"analysisusage",
		"ref",
		"Impl",
		"Traits",
		"Pass",
		"Set",
		"iterator_range",
		"Builder",
		"Pair",
		"Map",
		"Info",
		"Entry",
		"resolver",
		"tracking",
	};
	const std::vector<std::string> startswith = {
		"__", 
		"std", 
		"llvm::PointerUnion",
		"llvm::sys",
		"knowledge::",
		"llvm::raw",
		"llvm::pass",
		"llvm::pm",
		"llvm::prettystack",
		"llvm::dominatortree",
	};
	const std::vector<std::string> findCalls = {
		"Allocator",
		"vector", 
		"Vector",
		"Iterator",
		"iterator",
		"ilist",
		"Node",
		"Base",
		"PassManager",
		"llvm::is_integral_or_enum",
		"llvm::Optional",
		"Flags",
		"FlagIt",
	};
	const std::vector<std::string> specialValueTypes = {
		"class llvm::StringRef",
		"class std::basic_string<char>",
		"const class std::basic_string<char>",
		"const class std::basic_string<char>&",
		"class std::basic_string<char>&",
		"class std::basic_string<char>&",
		"class llvm::APInt",
		"const unsigned char*",
		"const char*",
		"char*",
		"unsigned char*",
	};
	class PrintFunctionsConsumer : public ASTConsumer {
		public:
			virtual void HandleTagDeclDefinition(TagDecl *D) {
				clang::LangOptions lo;
				lo.CPlusPlus = true;
				clang::PrintingPolicy p(lo);
				if (D->hasNameForLinkage() && D->isClass()) {
					//llvm::errs() << "Class " << D->getNameAsString() << "\n";
					if (const clang::CXXRecordDecl* x = dyn_cast<CXXRecordDecl>(D)) {
						if (!x->isInStdNamespace()) {
							// eliminate all of types taht I will _NEVER_ need
							llvm::StringRef z = x->getQualifiedNameAsString();
							for (std::vector<std::string>::const_iterator it = endsWith.cbegin(); it != endsWith.cend(); ++it) {
								if (z.endswith_lower(*it)) {
									return;
								}
							}

							for (std::vector<std::string>::const_iterator it = startswith.cbegin(); it != startswith.cend(); ++it) {
								if (z.startswith_lower(*it)) {
									return;
								}
							}

							for (std::vector<std::string>::const_iterator it = findCalls.cbegin(); it != findCalls.cend(); ++it) {
								if (z.find(*it) != llvm::StringRef::npos) {
									return;
								}
							}
							llvm::errs() << "Begin(" << x->getQualifiedNameAsString() << ")\n";
							for (clang::CXXRecordDecl::base_class_const_iterator it = x->bases_begin(); it != x->bases_end(); ++it) {
								//if (!it->getType()->isInstantiationDependentType()) {
									llvm::errs() << "\tsuper(" << it->getType().getCanonicalType().getAsString(lo) << ")\n";
								//}
							}
							std::vector<std::string> multifields;
							for (clang::CXXRecordDecl::method_iterator it = x->method_begin(); it != x->method_end(); ++it) {
								if (it->getAccess() == AS_public && 
										it->getNumParams() == 0 && 
										it->isInstance() && 
										!it->getReturnType()->isVoidType() && 
										!it->isTemplateInstantiation() &&
										!it->isCopyAssignmentOperator() &&
										//!it->isTemplateDecl() &&
										!it->isInAnonymousNamespace() &&
										!it->isOverloadedOperator() && it->isConst()) {
									std::string target,
										slotName,
										ret = it->getReturnType().getCanonicalType().getAsString(p), 
										name = it->getNameInfo().getAsString();
									StringRef sr = name;
									if (sr.startswith_lower("getContext") || sr.startswith_lower("convert") || sr.startswith_lower("clone") || sr.startswith_lower("Parse") || sr.startswith_lower("strip")) {
										continue;
									} else if (sr == "rbegin" || sr == "rend" || sr.endswith("rbegin") || sr.endswith("rend") || sr == "operands") {
										continue;
#define ignore(str) \
									} else if (sr == str) { \
										continue
#define X(begin, end, size, tbeg, tend, tsize, field) \
									} else if (sr == begin) { \
										std::string tmp; \
										llvm::raw_string_ostream q(tmp); \
										q << "X(" << ret << ", \"" << field << "\", 0, Multifield, " << tbeg << ", " << tend << ", " << tsize << ", 0) // " << it->getParent()->getQualifiedNameAsString() << "\n"; \
										multifields.push_back(q.str()); \
										continue; \
										ignore(end); \
										ignore(size); \
										ignore(field)
#define Y(begin, end, size, tbeg, tend, field) X(begin, end, size, tbeg, tend, "std::distance(" << tbeg << ", " << tend << ")", field)
									X("begin", "end", "size", "t->begin()", "t->end()", "t->size()", "children");
									X("op_begin", "op_end", "getNumOperands", "t->op_begin()", "t->op_end()", "t->getNumOperands()", "operands");
									X("alias_begin", "alias_end", "alias_size", "t->alias_begin()", "t->alias_end()", "t->alias_size()", "aliases");
									X("named_metadata_begin", "named_metadata_end", "named_metadata_size", "t->named_metadata_begin()", "t->named_metadata_end()", "t->named_metadata_size()", "named_metadata");
									Y("global_begin", "global_end", "getNumGlobals", "t->global_begin()", "t->global_end()", "globals");
									X("subtype_begin", "subtype_end", "getNumContainedTypes", "t->subtype_begin()", "t->subtype_end()", "t->getNumContainedTypes()", "subtypes");
									X("param_begin", "param_end", "getNumParams", "t->param_begin()", "t->param_end()", "t->getNumParams()", "params");<commit_msg>More mods<commit_after>//===- PrintFunctionNames.cpp ---------------------------------------------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// Example clang plugin which simply prints the names of all the top-level decls
// in the input file.
//
//===----------------------------------------------------------------------===//

#include "clang/Frontend/FrontendPluginRegistry.h"
#include "clang/AST/AST.h"
#include "clang/AST/ASTConsumer.h"
#include "clang/Frontend/CompilerInstance.h"
#include "llvm/Support/raw_ostream.h"
using namespace clang;

namespace {
	const std::vector<std::string> endsWith = {
		"iterator",
		"iplist",
		"analysis",
		"analysisusage",
		"ref",
		"Impl",
		"Traits",
		"Pass",
		"Set",
		"iterator_range",
		"Builder",
		"Pair",
		"Map",
		"Info",
		"Entry",
		"resolver",
		"tracking",
	};
	const std::vector<std::string> startswith = {
		"__", 
		"std", 
		"llvm::PointerUnion",
		"llvm::sys",
		"knowledge::",
		"llvm::raw",
		"llvm::pass",
		"llvm::pm",
		"llvm::prettystack",
		"llvm::dominatortree",
	};
	const std::vector<std::string> findCalls = {
		"Allocator",
		"vector", 
		"Vector",
		"Iterator",
		"iterator",
		"ilist",
		"Node",
		"Base",
		"PassManager",
		"llvm::is_integral_or_enum",
		"llvm::Optional",
		"Flags",
		"FlagIt",
	};
	const std::vector<std::string> specialValueTypes = {
		"class llvm::StringRef",
		"class std::basic_string<char>",
		"const class std::basic_string<char>",
		"const class std::basic_string<char>&",
		"class std::basic_string<char>&",
		"class llvm::APInt",
		"const unsigned char*",
		"const char*",
		"char*",
		"unsigned char*",
		"llvm::StringRef",
		"llvm::APInt",
		"std::basic_string<char>",
		"const std::basic_string<char>",
		"const std::basic_string<char>&",
		"std::basic_string<char>&",
		"const std::string &",
		"std::string &",
		"const std::string",
		"std::string",
	};
	class PrintFunctionsConsumer : public ASTConsumer {
		public:
			virtual void HandleTagDeclDefinition(TagDecl *D) {
				clang::LangOptions lo;
				lo.CPlusPlus = true;
				clang::PrintingPolicy p(lo);
				if (D->hasNameForLinkage() && D->isClass()) {
					//llvm::errs() << "Class " << D->getNameAsString() << "\n";
					if (const clang::CXXRecordDecl* x = dyn_cast<CXXRecordDecl>(D)) {
						if (!x->isInStdNamespace()) {
							// eliminate all of types taht I will _NEVER_ need
							llvm::StringRef z = x->getQualifiedNameAsString();
							for (std::vector<std::string>::const_iterator it = endsWith.cbegin(); it != endsWith.cend(); ++it) {
								if (z.endswith_lower(*it)) {
									return;
								}
							}

							for (std::vector<std::string>::const_iterator it = startswith.cbegin(); it != startswith.cend(); ++it) {
								if (z.startswith_lower(*it)) {
									return;
								}
							}

							for (std::vector<std::string>::const_iterator it = findCalls.cbegin(); it != findCalls.cend(); ++it) {
								if (z.find(*it) != llvm::StringRef::npos) {
									return;
								}
							}
							llvm::errs() << "Begin(" << x->getQualifiedNameAsString() << ")\n";
							for (clang::CXXRecordDecl::base_class_const_iterator it = x->bases_begin(); it != x->bases_end(); ++it) {
								//if (!it->getType()->isInstantiationDependentType()) {
								llvm::errs() << "\tsuper(" << it->getType().getAsString(lo) << ")\n";
								//}
							}
							std::vector<std::string> multifields;
							for (clang::CXXRecordDecl::method_iterator it = x->method_begin(); it != x->method_end(); ++it) {
								if (it->getAccess() == AS_public && 
										it->getNumParams() == 0 && 
										it->isInstance() && 
										!it->getReturnType()->isVoidType() && 
										!it->isTemplateInstantiation() &&
										!it->isCopyAssignmentOperator() &&
										//!it->isTemplateDecl() &&
										!it->isInAnonymousNamespace() &&
										!it->isOverloadedOperator() && it->isConst()) {
									//llvm::errs() << " // hasQualifiers = " << it->getReturnType().hasQualifiers() << "\n";
									//llvm::errs() << " // hasLocalQualifiers = " << it->getReturnType().hasLocalQualifiers() << "\n";
									//llvm::errs() << " // isCanonical = " << it->getReturnType().isCanonical() << "\n";
									std::string target,
										slotName,
										ret = it->getReturnType().getAsString(p), 
										name = it->getNameInfo().getAsString();
									StringRef sr = name;
									if (sr.startswith_lower("getContext") || sr.startswith_lower("convert") || sr.startswith_lower("clone") || sr.startswith_lower("Parse") || sr.startswith_lower("strip")) {
										continue;
									} else if (sr == "rbegin" || sr == "rend" || sr.endswith("rbegin") || sr.endswith("rend") || sr == "operands") {
										continue;
#define ignore(str) \
									} else if (sr == str) { \
										continue
#define X(begin, end, size, tbeg, tend, tsize, field) \
									} else if (sr == begin) { \
										std::string tmp; \
										llvm::raw_string_ostream q(tmp); \
										q << "X("; \
										if (!it->getReturnType().isCanonical()) { \
											q << x->getQualifiedNameAsString() << "::"; \
										} \
										q << ret << ", \"" << field << "\", 0, Multifield, " << tbeg << ", " << tend << ", " << tsize << ", 0) // " << it->getParent()->getQualifiedNameAsString() << "\n"; \
										multifields.push_back(q.str()); \
										continue; \
										ignore(end); \
										ignore(size); \
										ignore(field)
#define Y(begin, end, size, tbeg, tend, field) X(begin, end, size, tbeg, tend, "std::distance(" << tbeg << ", " << tend << ")", field)
									X("begin", "end", "size", "t->begin()", "t->end()", "t->size()", "children");
									X("op_begin", "op_end", "getNumOperands", "t->op_begin()", "t->op_end()", "t->getNumOperands()", "operands");
									X("alias_begin", "alias_end", "alias_size", "t->alias_begin()", "t->alias_end()", "t->alias_size()", "aliases");
									X("named_metadata_begin", "named_metadata_end", "named_metadata_size", "t->named_metadata_begin()", "t->named_metadata_end()", "t->named_metadata_size()", "named_metadata");
									Y("global_begin", "global_end", "getNumGlobals", "t->global_begin()", "t->global_end()", "globals");
									X("subtype_begin", "subtype_end", "getNumContainedTypes", "t->subtype_begin()", "t->subtype_end()", "t->getNumContainedTypes()", "subtypes");
									X("param_begin", "param_end", "getNumParams", "t->param_begin()", "t->param_end()", "t->getNumParams()", "params");
{'fraction_non_alphanumeric': 0.23391352287942033, 'fraction_numerical': 0.0004725525714735764, 'mean_word_length': 2.8086382723455308, 'pattern_counts': {'":': 1, '<': 160, '<?xml version=': 0, '>': 135, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5154341', 'n_tokens_mistral': 5419, 'n_tokens_neox': 4190, 'n_words': 771}