Coder Social home page Coder Social logo

sandsmark / unfuckify Goto Github PK

View Code? Open in Web Editor NEW
17.0 3.0 0.0 96 KB

makes c++ code using `auto` readable, i. e. unfucks it. I think this is what the kids call "opinionated"

CMake 1.64% C++ 98.36%
cpp cpp11 cpp17 cpp14 tidy codestyle clean-code readability linter lint

unfuckify's Introduction

unfuckify

Fixes readability of C++ sourcecode by resolving auto and replacing it with the real type.

Why

auto is the worst thing to have happened to C++.

Source code is written once and read many times, so spend the two seconds it takes to write out the type of the variable. Then people that just want to read the code don't have to go look up the return type of everything you call.

Usage

To use just run pass -DCMAKE_EXPORT_COMPILE_COMMANDS=ON to cmake when building the project you want to fix, and then run unfuckify path/to/buildfolder/compile_commands.json and either the name of a file or --all to fix all files in the project.

By default it will write the fixed source to foo.cpp-fixed, pass --replace to replace the existing file.

This uses libclang to parse and resolve the types, so if your project builds with clang this should work as well.

Example

In a random CMake based project:

  • mkdir build && cd build
  • CC="clang" CXX="clang++" cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -GNinja ..
  • ninja (to make sure generated files are available)
  • unfuckify --replace compile_commands.json ../src/main.cpp

Why the offensive name

Like all great projects this was written in a moment of frustration and anger.

Official upstream suggestions for alternative names, if you want to package this for a distro that doesn't allow swearing:

  • Antonyms of autonomy (courtesy of wordhippo.com):
    • Heteronomy
    • Bondage
    • Subservience
    • Subjugation
    • Other BDSM terms, apparently
  • Auto Annihilator
  • Auto-b-Gone
  • auto-was-a-mistake-please-help
  • N1984, The Name Should Have Given It Away
  • no-n1984

Known issues

auto is very magic and hides a lot of complexity, so trying to resolve the actual types is hard.

If libclang is unable to give a proper type for something (like lambdas), we can't replace it.

There are some issues with libclang's handling of tokens vs. cursors, e. g. it will give us the fully qualified type (including const, *, &, etc.), but the extent we get only covers auto itself.

We should handle the cases here properly now, but no guarantees that I have catched all cases. So make sure you review the code it changes.

Cases it fails on:

  • Some extremely magic function pointer stuff. I don't really know myself what the people who wrote it are trying to do, so I don't blame clang.

TODO

  • Handle structured binding (the auto there hides std::pairs, or sometimes QPairs).
  • Chop off the 'const' in std::functions.

unfuckify's People

Contributors

sandsmark avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.