Skip to content

Instantly share code, notes, and snippets.

@andrewseidl
Last active August 29, 2015 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrewseidl/c3eefe4e529c802a2deb to your computer and use it in GitHub Desktop.
Save andrewseidl/c3eefe4e529c802a2deb to your computer and use it in GitHub Desktop.

% ClangFormat All The Things % Andrew Seidl % 27 February 2015

ClangFormat All The Things

ClangFormat All The Things

ClangFormat

  • C/C++/ObjC source code formatter
    • like astyle, autopep8, go-fmt
  • Part of the Clang/LLVM project
    • note: Apple doesn't include it with Xcode

Why?

  • we have all seen some pretty horrible looking code
  • w/o shared editor settings, each dev is responsible for config'ing editor
  • mitigate flamewars
    • BDFL sets repo standard, dissenters use git hooks

Example: from this...

struct  ChThreadConstructionInfo
  {
    ChThreadConstructionInfo(char* uniqueName,
                  int numThreads=1,
                  int threadStackSize=65535
                  )
                  :m_uniqueName(uniqueName),
                  m_numThreads(numThreads),
                  m_threadStackSize(threadStackSize)
    {

    }

    char*         m_uniqueName;
    int           m_numThreads;
    int           m_threadStackSize;

  };

Example: ...to this (Chromium style)

struct ChThreadConstructionInfo {
  ChThreadConstructionInfo(char *uniqueName,
                           int numThreads = 1,
                           int threadStackSize = 65535)
      : m_uniqueName(uniqueName),
        m_numThreads(numThreads),
        m_threadStackSize(threadStackSize) {}

  char *m_uniqueName;
  int m_numThreads;
  int m_threadStackSize;
};

How?

  • style specified in .clang-format file in top source dir
    • builtins: LLVM, Google, Chromium, Mozilla, WebKit
    • can BasedOnStyle, then override specifics
  • can be run from command line or inside editor
BasedOnStyle: Chromium

# Longer lines requested by @hmazhar
ColumnLimit: 120

Editor Support

Links

ALL THE C (segue to Olympics)

find . \( -regex '.*\.\(cpp\|cu\|h\)' ! -ipath 'thirdparty' \) -exec clang-format -i '{}' \;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment