A C++ formatter using clang-format
This C++ formatter runs clang-format, the de facto standard formatter for C++, compiled to WebAssembly so it runs in your browser. It understands templates, namespaces, lambdas, access specifiers, attributes and modern C++ syntax up to recent standards. Choose a style preset and get output that matches what clang-format would produce on the command line with the same options.
Example
#include <map>
#include <string>
#include <algorithm>
namespace app{template<typename T>class Registry{public:void add(const std::string&key,T value){items_[key]=std::move(value);}std::optional<T> get(const std::string&key)const{auto it=items_.find(key);if(it==items_.end())return std::nullopt;return it->second;}private:std::map<std::string,T> items_;};}
With the Google preset:
#include <algorithm>
#include <map>
#include <string>
namespace app {
template <typename T>
class Registry {
public:
void add(const std::string& key, T value) { items_[key] = std::move(value); }
std::optional<T> get(const std::string& key) const {
auto it = items_.find(key);
if (it == items_.end()) return std::nullopt;
return it->second;
}
private:
std::map<std::string, T> items_;
};
} // namespace app
Several Google C++ Style rules are visible: the includes are sorted, public: and private: are indented by one space, & binds to the type (const std::string& key), a short method body stays on one line, and clang-format added a // namespace app comment to the closing brace. The code inside the namespace is not indented.
Presets at a glance
| Preset | Indent | Pointer/reference | Notes |
|---|---|---|---|
| 2 | int* p | Access modifiers at +1, short ifs on one line | |
| Chromium | 2 | int* p | Google-based; one parameter per line when they do not fit |
| LLVM | 2 | int *p | Access modifiers flush left |
| Mozilla | 2 | int* p | Return type on its own line for definitions |
| WebKit | 4 | int* p | Function braces on a new line |
| Microsoft | 4 | int* p | Allman braces |
| GNU | 2 | int *p | GNU brace indentation, space before parentheses |
Mozilla style comes from the Firefox code base and WebKit style from the WebKit browser engine, so they are the natural choice when contributing to those projects. The difference between Google and Chromium shows up with long parameter lists: Google fills lines as far as the column limit allows, while Chromium puts each parameter on its own line once they do not all fit.
Options
- Style – the
BasedOnStylepreset. - Indent – keep the style's width or override it; tabs are 4 columns wide.
- Column limit – 100 by default. Google and LLVM styles officially use 80; set 80 to match them exactly, or 0 to disable wrapping.
Modern C++ support
clang-format handles auto and structured bindings (auto [key, value] = *it;), range-based for, lambdas with captures, constexpr and consteval, concepts and requires clauses, attributes such as [[nodiscard]], and raw string literals, which are left untouched. Template angle brackets are closed as >> where C++11 allows it.
Limits and tips
- It is not a compiler. Syntax errors do not produce messages; they produce strange indentation. Check the first line where the layout looks wrong.
- Macros that expand to statements or declarations can confuse the layout. clang-format has options for this in a config file, which this tool does not expose.
- Protect a region with
// clang-format offand// clang-format oncomments, for example around a hand-aligned lookup table. The markers work here just as they do on the command line.
For plain C use the C Formatter (LLVM preset by default). To generate C++ classes from JSON, see JSON to C++. Your code stays in your browser.