C and C++ Code Formatter (clang-format Styles)
Choose a clang-format style, tune IndentWidth, braces and pointer alignment in a .clang-format editor, and format many C or C++ files with a diff view.
Berjalan dalam pelayar anda. Fail anda tidak pernah meninggalkan peranti. Lihat cara menyemaknya
…
Cara melakukannya
- 1Open or paste files
Add .c, .cpp, .h or .hpp files, or use the samples.
- 2Choose a base style
Pick LLVM, Google, Chromium, Mozilla, WebKit or Microsoft.
- 3Tune the keys
Change IndentWidth, BreakBeforeBraces, PointerAlignment and the other fields, or paste a .clang-format file.
- 4Review the diff
Read the side-by-side diff and the issues list for each file.
- 5Try the official build
Optionally load clang-format 23.1.1 in WebAssembly for wrapping and full key support.
- 6Export
Download the formatted files as a ZIP, a .patch, or your settings as .clang-format.
Tentang alat ini
clang-format is the formatter most C and C++ projects agree on, and its behaviour is driven by a .clang-format file with keys such as BasedOnStyle, IndentWidth, BreakBeforeBraces and PointerAlignment. This tool gives you those keys as form fields with the six common base styles as presets. LLVM uses two-space indentation, attached braces, pointer stars next to the name and an access-modifier offset that puts public: back at the class column. Google and Chromium attach the star to the type, indent case labels and place access modifiers one space in. Mozilla and WebKit break before function braces, and Microsoft puts every brace on its own line with four-space indentation. The built-in engine tokenises preprocessor lines, including backslash continuations, raw string literals such as R"x(...)x", encoding prefixes, char literals and digit separators like 1'000'000, so none of them are altered. It then re-indents by braces, handles namespaces without extra indentation as NamespaceIndentation: None does, aligns pointers and references, spaces operators, tells templates apart from comparisons, and sorts #include lines using clang's categories, with the file's own header first. The editor can read a .clang-format file you paste or open, apply the keys it understands, and export your choices as a new file.
Includes, preprocessor and header checks
Include sorting follows the base style. LLVM keeps your blank-line blocks and sorts inside each block, ranking the main header first, then quoted project headers, then system headers in angle brackets. Google regroups everything into blocks of C headers, C++ standard headers and project headers. The checker counts #if, #ifdef and #ifndef against #endif and reports any that are never closed. A header without an include guard or #pragma once is flagged, as is using namespace in a header, and NULL in C++ files is counted with a hint to use nullptr.
Worked example
The sample shape.cpp has 30 lines. With the LLVM preset it becomes 28 lines; the diff shows 12 changed lines, 2 added and 4 removed. The includes are reordered to "shape.h" first, because it is the main header of shape.cpp, then "util/log.h", then algorithm, cstdio and vector. The class brace on its own line joins "class Circle : public Shape {", public: and private: move to column 0, and "std::vector<Shape*>& shapes" becomes "std::vector<Shape *> &shapes". The loop header "for(const auto* s:shapes)" becomes "for (const auto *s : shapes)", case labels stay level with the switch, and the raw string keeps its quotes and braces. Choosing Microsoft instead gives 33 lines with Allman braces and four-space indentation.
Limits
The built-in engine does not reflow code: it will not break a long line at ColumnLimit or pack arguments, and it reads only the main .clang-format keys. For exact clang-format output, load the official WebAssembly build of clang-format 23.1.1 (about 2.6 MB) from jsDelivr; it runs in your browser and understands every key. Macros that expand into braces can confuse the indentation, as they can with any token-based tool.
Contoh berselesaian
shape.cpp with LLVM: 30 lines become 28, "shape.h" sorted first as the main header, std::vector<Shape *> &shapes with right pointer alignment.
Ringkasnya
- Kategori: Pembangun & data
- Berjalan dalam pelayar, tanpa pendaftaran, tanpa muat naik.
- Maklumat yang anda masukkan kekal pada peranti anda.
- Tersedia dalam bahasa Inggeris.
- Kandungan disemak kali terakhir:
- Sumber rasmi: LLVM ClangFormatStyleOptions
Soalan lazim
Which clang-format styles are included?
LLVM, Google, Chromium, Mozilla, WebKit and Microsoft. Each preset sets indent width, brace breaking, pointer alignment, case label indentation, access modifier offset and include grouping.
Can I use my existing .clang-format file?
Yes. Paste or open it in the .clang-format section and press Apply. The keys the built-in engine supports change the form; any others are listed so you know they need the official build.
Does it run the real clang-format?
It can. The default engine is a small TypeScript formatter that works offline. The official button loads the LLVM clang-format WebAssembly build from jsDelivr and formats with it inside your browser.
How are #include lines sorted?
By clang's categories for the chosen style, then alphabetically. The header with the same name as the source file goes first. LLVM keeps blank-line blocks; Google regroups them.
Does it work for plain C files?
Yes. .c and .h files use the same engine, and the header checks for include guards apply to .h files. C++-only hints, such as nullptr, appear only for C++ file names.
Will preprocessor macros be reformatted?
No. Every line that starts with # is copied as written, including multi-line macros joined with backslashes. Only trailing spaces are removed.
