Clang-format-qt-creator-setup » History » Version 1
Redmine Admin, 03/25/2024 06:18 AM
| 1 | 1 | Redmine Admin | # Настройка clang-format для Qt |
|---|---|---|---|
| 2 | |||
| 3 | ## Подключение clang-format |
||
| 4 | |||
| 5 | 1. Установить LLVM через установщик |
||
| 6 | 2. Открыть Qt, Справка -> О Модулях... -> C++, поставить галочку на "Beautifier" |
||
| 7 | 3. Если хотите, чтобы автоформатирование применялось при сохранении, Инструменты -> Параметры -> Стилизатор -> Основное, поставить галочку "Включить автоформатирование", и в списке выбрать "ClangFormat" |
||
| 8 | 4. Для форматирования по своему стилю на вкладке Инструменты -> Параметры -> Стилизатор -> Clang Format, выбрать "Использовать особый стиль", затем нажать "Добавить". В поле "Значение" вы можете описать свой стиль. |
||
| 9 | 5. На вкладке Инструменты -> Параметры -> Стилизатор -> Clang Format в поле `Команда Clang Format` указать полный путь к утилите `clang-format` (поставляется в составе LLVM `[\\bugmel\distrib\LLVM\]`, по умолчанию находится по пути `C:\Program Files (x86)\LLVM\bin\clang-format.exe`) |
||
| 10 | |||
| 11 | ## Пример с настройками нашего codestyle |
||
| 12 | |||
| 13 | ``` |
||
| 14 | # Format Style Options - Created with Clang Power Tools |
||
| 15 | --- |
||
| 16 | AccessModifierOffset: -4 |
||
| 17 | AllowShortFunctionsOnASingleLine: All |
||
| 18 | AllowShortIfStatementsOnASingleLine: Never |
||
| 19 | AllowShortLoopsOnASingleLine: false |
||
| 20 | BasedOnStyle: Google |
||
| 21 | BinPackArguments: true |
||
| 22 | BinPackParameters: true |
||
| 23 | BraceWrapping: |
||
| 24 | AfterCaseLabel: false |
||
| 25 | AfterClass: true |
||
| 26 | AfterControlStatement: false |
||
| 27 | AfterEnum: false |
||
| 28 | AfterFunction: false |
||
| 29 | AfterNamespace: false |
||
| 30 | AfterObjCDeclaration: false |
||
| 31 | AfterStruct: false |
||
| 32 | AfterUnion: false |
||
| 33 | AfterExternBlock: false |
||
| 34 | BeforeCatch: false |
||
| 35 | BeforeElse: true |
||
| 36 | IndentBraces: false |
||
| 37 | SplitEmptyFunction: false |
||
| 38 | SplitEmptyRecord: false |
||
| 39 | SplitEmptyNamespace: false |
||
| 40 | BeforeLambdaBody: false |
||
| 41 | BeforeWhile: false |
||
| 42 | BreakBeforeBraces: Custom |
||
| 43 | ColumnLimit: 80 |
||
| 44 | IncludeCategories: |
||
| 45 | - Regex: '^<ext/.*\.(h|hpp)' |
||
| 46 | Priority: 2 |
||
| 47 | SortPriority: 0 |
||
| 48 | CaseSensitive: false |
||
| 49 | - Regex: '^<.*\.(h|hpp)>' |
||
| 50 | Priority: 1 |
||
| 51 | SortPriority: 0 |
||
| 52 | CaseSensitive: false |
||
| 53 | - Regex: '^<.*' |
||
| 54 | Priority: 2 |
||
| 55 | SortPriority: 0 |
||
| 56 | CaseSensitive: false |
||
| 57 | - Regex: '.*' |
||
| 58 | Priority: 3 |
||
| 59 | SortPriority: 0 |
||
| 60 | CaseSensitive: false |
||
| 61 | |||
| 62 | IndentWidth: 4 |
||
| 63 | Language: Cpp |
||
| 64 | ObjCBlockIndentWidth: 4 |
||
| 65 | ObjCSpaceBeforeProtocolList: false |
||
| 66 | SortIncludes: true |
||
| 67 | SpaceBeforeCaseColon: false |
||
| 68 | SpaceBeforeCpp11BracedList: true |
||
| 69 | SpaceBeforeInheritanceColon: true |
||
| 70 | SpaceBeforeParens: Never |
||
| 71 | SpaceBeforeRangeBasedForLoopColon: false |
||
| 72 | TabWidth: 4 |
||
| 73 | UseTab: Never |
||
| 74 | ``` |
||
| 75 | |||
| 76 | Также файл можно скачать: attachment:.clang-format |
||
| 77 | |||
| 78 | ## Пример со всеми опциями |
||
| 79 | |||
| 80 | ``` |
||
| 81 | # Based on that profile |
||
| 82 | BasedOnStyle: Google |
||
| 83 | Language: Cpp |
||
| 84 | ColumnLimit: 120 |
||
| 85 | |||
| 86 | BraceWrapping: |
||
| 87 | # Class definitions behind |
||
| 88 | AfterClass: false |
||
| 89 | # Control statements later |
||
| 90 | AfterControlStatement: false |
||
| 91 | # Enum defined below |
||
| 92 | AfterEnum: true |
||
| 93 | Behind # function definition |
||
| 94 | AfterFunction: false |
||
| 95 | # Define the space behind the name |
||
| 96 | AfterNamespace: false |
||
| 97 | # ObjC defined later |
||
| 98 | AfterObjCDeclaration: false |
||
| 99 | # Struct defined below |
||
| 100 | AfterStruct: false |
||
| 101 | # Union defined below |
||
| 102 | AfterUnion: false |
||
| 103 | #ExternBlock defined below |
||
| 104 | AfterExternBlock: false |
||
| 105 | Before catch # |
||
| 106 | BeforeCatch: false |
||
| 107 | Before else # |
||
| 108 | BeforeElse: false |
||
| 109 | # Indent braces |
||
| 110 | IndentBraces: false |
||
| 111 | # Segmentation empty function |
||
| 112 | SplitEmptyFunction: true |
||
| 113 | # Segmentation empty record |
||
| 114 | SplitEmptyRecord: true |
||
| 115 | # Split empty namespace |
||
| 116 | SplitEmptyNamespace: true |
||
| 117 | |||
| 118 | # Add space after @property, \ @property (readonly) instead of \ @property (readonly). |
||
| 119 | ObjCSpaceAfterProperty: true |
||
| 120 | # Access specifier (public, private, etc.) offset |
||
| 121 | AccessModifierOffset: -4 |
||
| 122 | # The open bracket (open parenthesis, open angle brackets, open bracket) alignment: Align, DontAlign, AlwaysBreak (always open parenthesis after the line feed) |
||
| 123 | AlignAfterOpenBracket: Align |
||
| 124 | # When continuous assignment, align all equal sign |
||
| 125 | AlignConsecutiveAssignments: false |
||
| 126 | # When continuous statement, all statements of alignment variable name |
||
| 127 | AlignConsecutiveDeclarations: false |
||
| 128 | # Left escaped newline (backslash newline) backslash |
||
| 129 | AlignEscapedNewlinesLeft: true |
||
| 130 | # Horizontally aligned operand binary and ternary expressions |
||
| 131 | AlignOperands: true |
||
| 132 | # Align consecutive trailing comments |
||
| 133 | AlignTrailingComments: true |
||
| 134 | # Allow all the parameters declared in the function on the next line |
||
| 135 | AllowAllParametersOfDeclarationOnNextLine: true |
||
| 136 | # Allows a short block on the same line |
||
| 137 | AllowShortBlocksOnASingleLine: false |
||
| 138 | # Allows a short case the label on the same line |
||
| 139 | AllowShortCaseLabelsOnASingleLine: false |
||
| 140 | # Function allows short on the same line: None, InlineOnly (defined in the class), Empty (empty function), Inline (defined in the class, the empty function), All |
||
| 141 | AllowShortFunctionsOnASingleLine: None |
||
| 142 | # Allows a short statement if kept in the same row |
||
| 143 | AllowShortIfStatementsOnASingleLine: false |
||
| 144 | # Allows short cycle remains in the same row |
||
| 145 | AllowShortLoopsOnASingleLine: false |
||
| 146 | # After defining the return type is always wrap (deprecated) |
||
| 147 | AlwaysBreakAfterDefinitionReturnType: None |
||
| 148 | # Line after the return type is always changing: None, All, TopLevel (top-level function, the function is not included in the class), |
||
| 149 | # AllDefinitions (all definitions, not including the declaration), TopLevelDefinitions (defined all the top functions) |
||
| 150 | AlwaysBreakAfterReturnType: None |
||
| 151 | # Always before multi-line string literal newline |
||
| 152 | AlwaysBreakBeforeMultilineStrings: false |
||
| 153 | # Always wrap in a statement after the template |
||
| 154 | AlwaysBreakTemplateDeclarations: false |
||
| 155 | # False representation function arguments are either on the same line, or have each row |
||
| 156 | BinPackArguments: true |
||
| 157 | # False indicates that all formal parameters are either on the same line, or have each row |
||
| 158 | BinPackParameters: true |
||
| 159 | # Braces wrap only when BreakBeforeBraces set to Custom effective |
||
| 160 | # Wrap before binary operators: None (after a newline operator), NonAssignment (non-wrap before the assignment operator), All (operator before newline) |
||
| 161 | BreakBeforeBinaryOperators: NonAssignment |
||
| 162 | # Braces line before change: Attach (the braces are always attached to the surrounding context), the Linux (in addition to the function, and namespace class definition, and the like Attach), |
||
| 163 | # Mozilla (except the enumeration, the function, definition of recording, and the like Attach), Stroustrup (in addition to the function definition, catch, else, and is similar to Attach), |
||
| 164 | # Allman (always before braces wrap), GNU (always before braces wrap and indent for additional braces control statements), WebKit (before the function wrap), Custom |
||
| 165 | # Note: This statement is considered to belong to function block |
||
| 166 | BreakBeforeBraces: Custom |
||
| 167 | # Ternary operator before linefeed |
||
| 168 | BreakBeforeTernaryOperators: true |
||
| 169 | # Comma before the constructor initialization list of wrap |
||
| 170 | BreakConstructorInitializersBeforeComma: false |
||
| 171 | BreakConstructorInitializers: BeforeColon |
||
| 172 | BreakAfterJavaFieldAnnotations: false |
||
| 173 | BreakStringLiterals: true |
||
| 174 | Comment # Description of special significance regular expression, it should not be split into multiple lines or otherwise change |
||
| 175 | CommentPragmas: '^ IWYU pragma:' |
||
| 176 | # Constructor initializer list are either on the same line, or have each row |
||
| 177 | ConstructorInitializerAllOnOneLineOrOnePerLine: true |
||
| 178 | # Constructor comfort of a list of all line breaks |
||
| 179 | AllowAllConstructorInitializersOnNextLine: false |
||
| 180 | Indent width initialization list of the constructor # |
||
| 181 | ConstructorInitializerIndentWidth: 4 |
||
| 182 | Indent a continuation of the line width # |
||
| 183 | ContinuationIndentWidth: 4 |
||
| 184 | # C ++ removal list initialization 11 and braces {} after the front space |
||
| 185 | Cpp11BracedListStyle: true |
||
| 186 | # The most common inherited pointers and references alignment |
||
| 187 | DerivePointerAlignment: false |
||
| 188 | # Closed format |
||
| 189 | DisableFormat: false |
||
| 190 | # Calls and automatic detection function define the format for each row whether a parameter (Experimental) |
||
| 191 | ExperimentalAutoDetectBinPacking: false |
||
| 192 | # Amended namespace Annotations |
||
| 193 | FixNamespaceComments: true |
||
| 194 | # Needs to be interpreted as foreach loop instead of a macro function calls |
||
| 195 | ForEachMacros: |
||
| 196 | - foreach |
||
| 197 | - Q_FOREACH |
||
| 198 | - BOOST_FOREACH |
||
| 199 | #include #include # sort of match the regular expression has a corresponding priority, then not match the default priority is INT_MAX (the smaller the priority ordering more front), |
||
| 200 | # You can define negative priorities in order to ensure that certain #include always on top |
||
| 201 | IncludeCategories: |
||
| 202 | - Regex: '^<ext/.*\.h>' |
||
| 203 | Priority: 2 |
||
| 204 | - Regex: '^<.*\.h>' |
||
| 205 | Priority: 1 |
||
| 206 | - Regex: '^<.*' |
||
| 207 | Priority: 2 |
||
| 208 | - Regex: '.*' |
||
| 209 | Priority: 3 |
||
| 210 | |||
| 211 | IncludeIsMainRegex: '([-_](test|unittest))?$' |
||
| 212 | # Indent label case |
||
| 213 | IndentCaseLabels: true |
||
| 214 | # Indentation width |
||
| 215 | IndentWidth: 4 |
||
| 216 | # Function return type change lines, indentation function name function declaration or function definition |
||
| 217 | IndentWrappedFunctionNames: false |
||
| 218 | JavaScriptQuotes: Leave |
||
| 219 | JavaScriptWrapImports: true |
||
| 220 | # Retain the blank line at the beginning of the block |
||
| 221 | KeepEmptyLinesAtTheStartOfBlocks: true |
||
| 222 | # Macro being the beginning of a block of expression |
||
| 223 | MacroBlockBegin: '' |
||
| 224 | # End of a macro block of a regular expression |
||
| 225 | MacroBlockEnd: '' |
||
| 226 | The maximum number of consecutive blank lines # |
||
| 227 | MaxEmptyLinesToKeep: 1 |
||
| 228 | # Namespace indent: (Content indent nested namespace) None, Inner, All |
||
| 229 | NamespaceIndentation: None |
||
| 230 | Indent width used ObjC block # |
||
| 231 | ObjCBlockIndentWidth: 4 |
||
| 232 | # Add a space before the protocol list of ObjC |
||
| 233 | ObjCSpaceBeforeProtocolList: false |
||
| 234 | # In the call (after calling the penalty wrap the function |
||
| 235 | PenaltyBreakBeforeFirstCallParameter: 19 |
||
| 236 | # Introduction of penalty wrap in a comment |
||
| 237 | PenaltyBreakComment: 300 |
||
| 238 | penalty # for the first time before << wrap |
||
| 239 | PenaltyBreakFirstLessLess: 120 |
||
| 240 | # Wrap penalty introduced in a string literals |
||
| 241 | PenaltyBreakString: 1000 |
||
| 242 | # For each line outside the limit on the number of characters in the character of penalty |
||
| 243 | PenaltyExcessCharacter: 1000000 |
||
| 244 | # The function returns the type of penalty into its own row |
||
| 245 | PenaltyReturnTypeOnItsOwnLine: 60 |
||
| 246 | # Pointers and references alignment: Left, Right, Middle |
||
| 247 | PointerAlignment: Left |
||
| 248 | RawStringFormats: |
||
| 249 | - Language: Cpp |
||
| 250 | Delimiters: |
||
| 251 | - cc |
||
| 252 | - CC |
||
| 253 | - cpp |
||
| 254 | - Cpp |
||
| 255 | - CPP |
||
| 256 | - 'c++' |
||
| 257 | - 'C++' |
||
| 258 | CanonicalDelimiter: '' |
||
| 259 | BasedOnStyle: google |
||
| 260 | - Language: TextProto |
||
| 261 | Delimiters: |
||
| 262 | - pb |
||
| 263 | - PB |
||
| 264 | - proto |
||
| 265 | - PROTO |
||
| 266 | EnclosingFunctions: |
||
| 267 | - EqualsProto |
||
| 268 | - EquivToProto |
||
| 269 | - PARSE_PARTIAL_TEXT_PROTO |
||
| 270 | - PARSE_TEST_PROTO |
||
| 271 | - PARSE_TEXT_PROTO |
||
| 272 | - ParseTextOrDie |
||
| 273 | - ParseTextProtoOrDie |
||
| 274 | CanonicalDelimiter: '' |
||
| 275 | BasedOnStyle: google |
||
| 276 | # Allowed to re-format comments |
||
| 277 | ReflowComments: true |
||
| 278 | # Allow sorting #include |
||
| 279 | SortIncludes: true |
||
| 280 | SortUsingDeclarations: true |
||
| 281 | # Add a space after the C-style casts |
||
| 282 | SpaceAfterCStyleCast: false |
||
| 283 | # Add a space before the assignment operator |
||
| 284 | SpaceAfterTemplateKeyword: true |
||
| 285 | SpaceBeforeAssignmentOperators: true |
||
| 286 | SpaceBeforeCpp11BracedList: false |
||
| 287 | SpaceBeforeCtorInitializerColon: true |
||
| 288 | SpaceBeforeInheritanceColon: true |
||
| 289 | SpaceBeforeParens: ControlStatements |
||
| 290 | # Add a space before the opening parenthesis: Never, ControlStatements, Always |
||
| 291 | SpaceBeforeParens: ControlStatements |
||
| 292 | # Add a space empty parentheses |
||
| 293 | SpaceInEmptyParentheses: false |
||
| 294 | # Before the number of spaces trailing comments added (only applicable to //) |
||
| 295 | SpacesBeforeTrailingComments: 2 |
||
| 296 | # In angle brackets <and after> before adding spaces |
||
| 297 | SpacesInAngles: false |
||
| 298 | # Add a space literal container (ObjC and JavaScript arrays and dictionaries, etc.) |
||
| 299 | SpacesInContainerLiterals: true |
||
| 300 | # Add a space in C-style casts in parentheses |
||
| 301 | SpacesInCStyleCastParentheses: false |
||
| 302 | # Add a space in parentheses (and after) ago |
||
| 303 | SpacesInParentheses: false |
||
| 304 | # Declaration in square brackets [and after] before adding a space, lamda expressions, and does not indicate the size of the array is not affected |
||
| 305 | SpacesInSquareBrackets: false |
||
| 306 | # Standard: Cpp03, Cpp11, Auto |
||
| 307 | Standard: Cpp11 |
||
| 308 | # Tab width |
||
| 309 | TabWidth: 4 |
||
| 310 | # Use the tab characters: Never, ForIndentation, ForContinuationAndIndentation, Always |
||
| 311 | UseTab: Never |
||
| 312 | ``` |