<commit_before>#include "Console.h"

#include <iostream>
#include <map>
#include <vector>

#include <llvm/ADT/OwningPtr.h>
#include <llvm/Support/MemoryBuffer.h>
#include <llvm/Module.h>
#include <llvm/ModuleProvider.h>
#include <llvm/ExecutionEngine/ExecutionEngine.h>

#include <clang/AST/AST.h>
#include <clang/Basic/LangOptions.h>
#include <clang/Driver/CompileOptions.h>
#include <clang/Driver/TextDiagnosticPrinter.h>
#include <clang/CodeGen/ModuleBuilder.h>
#include <clang/Lex/Preprocessor.h>


using std::string;

namespace ccons {

class StmtFinder : public clang::StmtVisitor<StmtFinder> {
public:

	explicit StmtFinder(unsigned pos) : pos(pos), S(NULL) {}
	~StmtFinder() {}

	void VisitChildren(clang::Stmt *S) {
		for (clang::Stmt::child_iterator I = S->child_begin(), E = S->child_end(); I != E; ++I) {
			if (*I) {
				Visit(*I);
			}
		}
	}

	void VisitStmt(clang::Stmt *S) {
		if (clang::Expr *E = dyn_cast<clang::Expr>(S)) {
			if (E->getLocStart().getRawFilePos() == pos) {
				this->S = S;
			}
		}
	}

	clang::Stmt *getStmt() { return S; }

private:
	unsigned pos;
	clang::Stmt *S;
};

class FunctionBodyConsumer : public clang::ASTConsumer {
public:

	explicit FunctionBodyConsumer(StmtFinder *SF) : SF(SF) {}
	~FunctionBodyConsumer() {}

	void HandleTopLevelDecl(clang::Decl *D) {
		if (clang::FunctionDecl *FD = dyn_cast<clang::FunctionDecl>(D)) {
			if (clang::Stmt *S = FD->getBody()) {
				SF->VisitChildren(S);
			}
		}
	}

private:
	StmtFinder *SF;
};


Console::Console() :
	_parser(_options),
	_prompt(">>> ")
{
	_options.C99 = true;
}

Console::~Console()
{
}

const char * Console::prompt() const
{
	return _prompt.c_str();
}

class ProxyDiagnosticClient : public clang::DiagnosticClient {
public:
	ProxyDiagnosticClient(clang::DiagnosticClient *DC) : DC(DC) {}

	void HandleDiagnostic(clang::Diagnostic::Level DiagLevel, const clang::DiagnosticInfo &Info) {
		if (Info.getID() != clang::diag::warn_unused_expr)
			DC->HandleDiagnostic(DiagLevel, Info);
	}

private:
	clang::DiagnosticClient *DC;
};

string transform(string line, string type)
{
	std::map<string, string> type_to_format_string;
	type_to_format_string[string("char")] = string("'%c'"); // or %hii if its not printable?
	type_to_format_string[string("short")] = string("%hi");
	type_to_format_string[string("int")] = string("%i");
	type_to_format_string[string("long")] = string("%li");
	type_to_format_string[string("long long")] = string("%lli");
	type_to_format_string[string("float")] = string("%f");
	type_to_format_string[string("double")] = string("%lf");
	type_to_format_string[string("long double")] = string("%Lf");
	type_to_format_string[string("size_t")] = string("%z");

	string format_string;
	// we also need a regexp library to parse input before giving it to clang...
	// for example, #include statements
	if (!strncmp(type.c_str(), "char [", 5) && type[type.length() - 1] == ']') {
		// string constants - this is a hack! we should support arrays better with
		// a special case for arrays of characters... (which we should call our own
		// smart function to print... that decides if its a string or not)
		format_string = "\\\"%s\\\"";
	} else if (type.length() > 0 && type[type.length() - 1] == '*') {
		format_string = "%p";
	} else if (type_to_format_string.find(type) != type_to_format_string.end()) {
		format_string = type_to_format_string[type];
	}<commit_msg>format strings for unsigned types<commit_after>#include "Console.h"

#include <iostream>
#include <map>
#include <vector>

#include <llvm/ADT/OwningPtr.h>
#include <llvm/Support/MemoryBuffer.h>
#include <llvm/Module.h>
#include <llvm/ModuleProvider.h>
#include <llvm/ExecutionEngine/ExecutionEngine.h>

#include <clang/AST/AST.h>
#include <clang/Basic/LangOptions.h>
#include <clang/Driver/CompileOptions.h>
#include <clang/Driver/TextDiagnosticPrinter.h>
#include <clang/CodeGen/ModuleBuilder.h>
#include <clang/Lex/Preprocessor.h>


using std::string;

namespace ccons {

class StmtFinder : public clang::StmtVisitor<StmtFinder> {
public:

	explicit StmtFinder(unsigned pos) : pos(pos), S(NULL) {}
	~StmtFinder() {}

	void VisitChildren(clang::Stmt *S) {
		for (clang::Stmt::child_iterator I = S->child_begin(), E = S->child_end(); I != E; ++I) {
			if (*I) {
				Visit(*I);
			}
		}
	}

	void VisitStmt(clang::Stmt *S) {
		if (clang::Expr *E = dyn_cast<clang::Expr>(S)) {
			if (E->getLocStart().getRawFilePos() == pos) {
				this->S = S;
			}
		}
	}

	clang::Stmt *getStmt() { return S; }

private:
	unsigned pos;
	clang::Stmt *S;
};

class FunctionBodyConsumer : public clang::ASTConsumer {
public:

	explicit FunctionBodyConsumer(StmtFinder *SF) : SF(SF) {}
	~FunctionBodyConsumer() {}

	void HandleTopLevelDecl(clang::Decl *D) {
		if (clang::FunctionDecl *FD = dyn_cast<clang::FunctionDecl>(D)) {
			if (clang::Stmt *S = FD->getBody()) {
				SF->VisitChildren(S);
			}
		}
	}

private:
	StmtFinder *SF;
};


Console::Console() :
	_parser(_options),
	_prompt(">>> ")
{
	_options.C99 = true;
}

Console::~Console()
{
}

const char * Console::prompt() const
{
	return _prompt.c_str();
}

class ProxyDiagnosticClient : public clang::DiagnosticClient {
public:
	ProxyDiagnosticClient(clang::DiagnosticClient *DC) : DC(DC) {}

	void HandleDiagnostic(clang::Diagnostic::Level DiagLevel, const clang::DiagnosticInfo &Info) {
		if (Info.getID() != clang::diag::warn_unused_expr)
			DC->HandleDiagnostic(DiagLevel, Info);
	}

private:
	clang::DiagnosticClient *DC;
};

string transform(string line, string type)
{
	std::map<string, string> type_to_format_string;
	type_to_format_string[string("char")] = string("'%c'"); // or %hii if its not printable?
	type_to_format_string[string("short")] = string("%hi");
	type_to_format_string[string("unsigned short")] = string("%hu");
	type_to_format_string[string("int")] = string("%i");
	type_to_format_string[string("unsigned int")] = string("%u");
	type_to_format_string[string("long")] = string("%li");
	type_to_format_string[string("unsigned long")] = string("%lu");
	type_to_format_string[string("long long")] = string("%lli");
	type_to_format_string[string("unsigned long long")] = string("%llu");
	type_to_format_string[string("float")] = string("%f");
	type_to_format_string[string("double")] = string("%lf");
	type_to_format_string[string("long double")] = string("%Lf");
	type_to_format_string[string("size_t")] = string("%z");

	string format_string;
	// we also need a regexp library to parse input before giving it to clang...
	// for example, #include statements
	if (!strncmp(type.c_str(), "char [", 5) && type[type.length() - 1] == ']') {
		// string constants - this is a hack! we should support arrays better with
		// a special case for arrays of characters... (which we should call our own
		// smart function to print... that decides if its a string or not)
		format_string = "\\\"%s\\\"";
	} else if (type.length() > 0 && type[type.length() - 1] == '*') {
		format_string = "%p";
	} else if (type_to_format_string.find(type) != type_to_format_string.end()) {
		format_string = type_to_format_string[type];
	}
{'fraction_non_alphanumeric': 0.17547838412473424, 'fraction_numerical': 0.001700921332388377, 'mean_word_length': 5.266429840142096, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 61, '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': '5119987', 'n_tokens_mistral': 2607, 'n_tokens_neox': 2525, 'n_words': 523}