clang-format in the browser
clang-format is the formatter from the LLVM project, the same tool behind format-on-save in CLion, VS Code’s C/C++ extension and most C++ code review bots. Normally it reads a .clang-format file from your repository. Here you pick the pieces that matter from the options panel instead, and the formatter runs inside the page, so firmware, game engine code or anything under NDA is not sent to a server.
Input that has #include lines is detected automatically, as are Objective-C @interface and @implementation blocks. Press Ctrl/Cmd+Enter to format, or let it update as you type.
Language and Style
Language tells clang-format how to read the code: C++, C or Objective-C. In practice C and C++ come out nearly the same, because clang-format parses both with its C++ rules. Objective-C matters more, since method declarations like - (void)reload:(BOOL)animated need their own handling. With C++ selected, input containing @interface, @implementation, @protocol or #import is switched to Objective-C for you.
Style picks a predefined base style, matching BasedOnStyle in a .clang-format file:
- LLVM (default): attached braces,
char **argvandconst Item &itemwith the symbol next to the name, access specifiers flush with the class. - Google and Chromium: attached braces,
char** argvwith the symbol on the type, andpublic:indented by one space. Google also regroups includes into C headers, C++ standard headers and project headers. - Mozilla: class and function braces on their own lines, control-statement braces attached. WebKit is similar but keeps the class brace attached.
- Microsoft: every brace on its own line, namespaces included (Allman style).
- GNU: return types on a separate line, a space before every argument list as in
main (void), and braces ofifandforindented half a level.
LLVM, Google, Chromium and Microsoft also append a // namespace shop comment to the brace that closes a namespace.
Two values always come from the toolbar rather than the style: indentation width (or tabs, via Indent) and an 80-column limit. That differs from some styles’ defaults, such as Microsoft’s 120 columns or WebKit’s unlimited line length.
Include sorting and protected regions
clang-format sorts each block of #include lines alphabetically, where a block is a run of lines without a blank line in between. That is usually harmless, but some headers depend on being included first: <windows.h> before <shellapi.h> is the classic case. Separate order-sensitive includes with a blank line, or protect them with // clang-format off and // clang-format on; the lines between those comments are left exactly as written. The same comments help with hand-aligned lookup tables and register maps.
A formatter, not a compiler
clang-format works from tokens and does not need the code to compile. Missing headers, undefined types and even unbalanced braces do not produce an error message. That is useful for fragments, but it means a clean format is not proof the code is valid. A missing closing brace shows up indirectly: everything after it is indented one level deeper than you expect, and the file ends without returning to column 1.
Examples
C++ class in Google style with mixed includes
Includes are regrouped into C, C++ standard and project headers, and references are written as const Item& with the symbol on the type.
#include <vector>
#include "inventory/item.h"
#include <stdio.h>
#include <string>
namespace shop{class Cart{public:void add(const Item&item){items_.push_back(item);}
double total()const{double t=0;for(const auto&i:items_)t+=i.price*i.qty;return t;}
private:std::vector<Item> items_;};}#include <stdio.h>
#include <string>
#include <vector>
#include "inventory/item.h"
namespace shop {
class Cart {
public:
void add(const Item& item) { items_.push_back(item); }
double total() const {
double t = 0;
for (const auto& i : items_) t += i.price * i.qty;
return t;
}
private:
std::vector<Item> items_;
};
} // namespace shop
Embedded C in GNU style
GNU style puts return types on their own line, braces on separate lines and a space before each argument list.
#include <stdint.h>
typedef struct{uint16_t raw;float celsius;}reading_t;
static float to_celsius(uint16_t raw){return (raw*0.0625f)-40.0f;}
int read_sensor(reading_t*out){if(!out)return -1;out->raw=adc_read(3);out->celsius=to_celsius(out->raw);return 0;}#include <stdint.h>
typedef struct
{
uint16_t raw;
float celsius;
} reading_t;
static float
to_celsius (uint16_t raw)
{
return (raw * 0.0625f) - 40.0f;
}
int
read_sensor (reading_t *out)
{
if (!out)
return -1;
out->raw = adc_read (3);
out->celsius = to_celsius (out->raw);
return 0;
}
Objective-C interface
Property attributes, block types and the method signature get the spacing Xcode users expect.
#import <UIKit/UIKit.h>
@interface OrderView:UIView
@property(nonatomic,strong) NSString *title;
@property(nonatomic,copy) void(^onSelect)(NSString *orderId);
-(void)reloadWithOrders:(NSArray<Order*>*)orders animated:(BOOL)animated;
@end#import <UIKit/UIKit.h>
@interface OrderView : UIView
@property(nonatomic, strong) NSString *title;
@property(nonatomic, copy) void (^onSelect)(NSString *orderId);
- (void)reloadWithOrders:(NSArray<Order *> *)orders animated:(BOOL)animated;
@end
Common errors and how to fix them
| Error | Cause | Fix |
|---|---|---|
No error, but everything below one function is indented a level too deep | A closing brace is missing. clang-format does not reject unbalanced braces; it formats the rest of the file as if it were still inside the open block. | Find the first line whose indentation looks wrong and add the missing } just above it. |
Build fails after formatting with errors such as unknown type name 'HWND' | Include sorting moved a header above another one it depends on, for example <shellapi.h> ahead of <windows.h>. | Put a blank line between the two includes so they are sorted as separate blocks, or wrap them in // clang-format off and // clang-format on. |
Lines longer than 80 characters are split even with the Microsoft or WebKit style | The column limit is fixed at 80 on this page, overriding the larger or unlimited width those styles normally use. | Run clang-format locally with your own .clang-format file if you need a different ColumnLimit. |
A hand-aligned table was reflowed into one long initializer | clang-format treats braced lists like any other code and repacks them to fit the line. | Wrap the table in // clang-format off and // clang-format on comments, or add a trailing comma after the last element so each entry stays on its own line. |
Frequently asked questions
Can I use my own .clang-format file?
Not directly. The page exposes the base style, the language and the toolbar indentation; the column limit is fixed at 80. For other settings, run clang-format locally with your configuration.
Which style is the default?
LLVM, which is also what clang-format uses when no configuration file is found.
Does it check that my C++ compiles?
No. clang-format only looks at tokens and layout. Use a compiler or clang-tidy to find real errors.
Why did the order of my #include lines change?
clang-format sorts includes within each block separated by blank lines, and the Google style also regroups them by kind. Use blank lines or clang-format off comments to keep a specific order.
Does it support Objective-C++ (.mm) files?
Choose Objective-C as the language. clang-format handles C++ constructs inside Objective-C input, so most .mm files format correctly.