Skip to content

Instantly share code, notes, and snippets.

@egorpugin
Last active June 16, 2024 13:58
Show Gist options
  • Save egorpugin/78a4131f34126386b2def97f35242acc to your computer and use it in GitHub Desktop.
Save egorpugin/78a4131f34126386b2def97f35242acc to your computer and use it in GitHub Desktop.
Remove spaces from cpp with libclang
#include "x.cpp" /*
aa
*/
# include "x.cpp"
# include "x.cpp"
#ifdef _WIN32 /*
*/asd
#else
asdasd gf fg dfgm df gkmdfg f
#endif
#define ABC (asd asdas)aas ( asd asd ) d\
\
123
10'50;
\uFFFD\uFFFD\uFFFD\uFFFD \uFFFD\uFFFD\uFFFD\uFFFD
"123 sdfsdfsdf" "sdfdsfsd"
void build(Solution &s){
ABC
auto &socks5 = s.addExecutable("socks5");
{
auto &t = socks5;
t.PackageDefinitions = true;
t += cpplatest; // sdfds
/*
sss
*/t += "src/.*"_rr;
R"(
aa
)";
t += "pub.egorpugin.primitives.sw.main"_dep;
t += "org.sw.demo.boost.asio"_dep;
t += "pub.egorpugin.primitives.templates2"_dep;
a+++ ++b;
}
}
// header.hpp
// 123
[[clang::annotate("123")]]
class /*aaaaaaaa*/ MyClass // sdfsdf
/// fsdfsd
{
public:
[[clang::annotate("zzz")]]
int field;
virtual void method() const = 0;
static const int static_field;
[[clang::annotate("vvvvvvvvv")]]
static int static_method();
};
struct A{};
a[ []{}() ]
template <bool> struct X { typedef int type_or_value; };
template <> struct X<false> { static int type_or_value; };
constexpr bool f() {
// any C++ code here
}
int a;
void test() {
X<f()>::type_or_value * a;
};
\uFFFD\uFFFD\uFFFD\uFFFD \uFFFD\uFFFD\uFFFD\uFFFD
#include "x.cpp"
#include "x.cpp"
#include "x.cpp"
#ifdef _WIN32
asd
#else
asdasdgf fg dfgm df gkmdfg f
#endif
#define ABC ( asd asdas ) aas ( asd asd ) d 123
10'50; "123 sdfsdfsdf" "sdfdsfsd" void build ( Solution & s ) { ABC auto & socks5 = s . addExecutable ( "socks5" ) ; { auto & t = socks5 ; t . PackageDefinitions = true ; t += cpplatest ; t += "src/.*"_rr ; R"(
aa
)" ; t += "pub.egorpugin.primitives.sw.main"_dep ; t += "org.sw.demo.boost.asio"_dep ; t += "pub.egorpugin.primitives.templates2"_dep ; a ++ + ++ b ; } } [ [ clang :: annotate ( "123" ) ] ] class MyClass { public : [ [ clang :: annotate ( "zzz" ) ] ] int field ; virtual void method ( ) const = 0 ; static const int static_field ; [ [ clang :: annotate ( "vvvvvvvvv" ) ] ] static int static_method ( ) ; } ; struct A { } ; a [ [ ] { } ( ) ] template < bool > struct X { typedef int type_or_value ; } ; template < > struct X < false > { static int type_or_value ; } ; constexpr bool f ( ) { } int a ; void test ( ) { X < f ( ) > :: type_or_value * a ; } ;
#include <clang/Frontend/CompilerInstance.h>
#include <clang/Tooling/Tooling.h>
#include <fstream>
#include <iostream>
struct dump_tokens : clang::PreprocessorFrontendAction {
enum class prev_line_type {
no_prev_line,
normal,
pp,
};
void ExecuteAction() override {
auto &PP = getCompilerInstance().getPreprocessor();
auto &SM = PP.getSourceManager();
llvm::MemoryBufferRef FromFile = SM.getBufferOrFake(SM.getMainFileID());
clang::Lexer RawLex(SM.getMainFileID(), FromFile, SM, PP.getLangOpts());
RawLex.SetKeepWhitespaceMode(true);
clang::Token t;
prev_line_type prev_line{};
char prev_sym{};
auto print_newline = [&](char c) {
if (!prev_sym || prev_sym == '\n') {
prev_sym = c;
return;
}
prev_sym = c;
std::cout << c;
};
while (1) {
RawLex.LexFromRawLexer(t);
if (t.isAnnotation()) {
continue;
}
switch (t.getKind()) {
case clang::tok::eof:
return;
case clang::tok::comment:
case clang::tok::unknown:
if (PP.getSpelling(t).contains('\n')) {
if (prev_line == prev_line_type::pp) {
print_newline('\n');
}
prev_line = prev_line_type::no_prev_line;
}
continue;
case clang::tok::hash:
if (prev_line == prev_line_type::no_prev_line) {
print_newline('\n');
prev_line = prev_line_type::pp;
}
default:
if (prev_line == prev_line_type::no_prev_line) {
prev_line = prev_line_type::normal;
}
std::cout << PP.getSpelling(t);
print_newline(' ');
continue;
}
}
}
};
int main(int argc, char *argv[]) {
std::ifstream ifile{"sw1.cpp"};
std::stringstream buffer;
buffer << ifile.rdbuf();
clang::tooling::runToolOnCode(std::make_unique<dump_tokens>(), buffer.str().c_str());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment