赞
踩
本文介绍了clangd, clang-format, clang-tidy的相关内容,主要是给出了包括VSCode在内的若干配置文件。下载VSCode、安装相关插件等过程略。
clangd是一个…(clangd官网),简单来说就是一个提供高亮补全等功能的lsp,不能直接用,一般需要通过插件进行访问。
要想在VSCode中使用clangd进行高亮、补全,而且指定使用的c++标准,大致需要以下操作:
"C_Cpp.intelliSenseEngine": "disabled",
更精细的配置是使用配置文件,如下:
.clangd
或config.yaml
在当前项目目录,或者全局目录(LINUX: ~/.config/clangd/config.yaml, WINDOWS: %LocalAppData%\clangd\config.yaml)下创建配置文件。可以指定clangd使用的c++版本,以及clangd-tidy进行诊断的条目
Index: Background: Build CompileFlags: Add: [-xc++, -Wall, -std=c++20] Compiler: clang++ Diagnostics: ClangTidy: Add: ["*"] Remove: [ abseil*, fuchsia*, llvmlib*, zircon*, altera*, google-readability-todo, readability-braces-around-statements, hicpp-braces-around-statements, modernize-use-trailing-return-type, # 不要每个都加上尾返回值类型 readability-identifier-length, # 不检查变量名长度 cppcoreguidelines-avoid-magic-numbers, # 不检查魔法数字 readability-magic-numbers, #同上 ]
.clang-format
#基于那个配置文件 BasedOnStyle: Google Language: Cpp # 标准: Cpp03, Cpp11, Auto Standard: c++20 ColumnLimit: 120 BraceWrapping: # case标签后面 AfterCaseLabel: false # class定义后面 AfterClass: false # 控制语句后面 AfterControlStatement: Never # enum定义后面 AfterEnum: false # 函数定义后面 AfterFunction: false # 命名空间定义后面 AfterNamespace: false # ObjC定义后面 AfterObjCDeclaration: false # struct定义后面 AfterStruct: false # union定义后面 AfterUnion: false #ExternBlock定义后面 AfterExternBlock: false # catch之前 BeforeCatch: false # else之前 BeforeElse: false # lambda块之前 BeforeLambdaBody: false # while之前 BeforeWhile: false # 缩进大括号 IndentBraces: false # 分割空函数 SplitEmptyFunction: true # 分割空记录 SplitEmptyRecord: true # 分割空命名空间 SplitEmptyNamespace: true # 在 @property 后面添加空格, \@property (readonly) 而不是 \@property(readonly). ObjCSpaceAfterProperty: true # 访问说明符(public、private等)的偏移 AccessModifierOffset: -4 # 开括号(开圆括号、开尖括号、开方括号)后的对齐: Align, DontAlign, AlwaysBreak(总是在开括号后换行) AlignAfterOpenBracket: Align # 连续赋值时,对齐所有等号 AlignConsecutiveAssignments: Consecutive # 连续声明时,对齐所有声明的变量名 AlignConsecutiveDeclarations: Consecutive # 连续宏声明时,对齐空格 #clang-format-10 可用 AlignConsecutiveMacros: Consecutive # 对齐连接符: DontAlign(不对齐), Left(左对齐), Right(右对齐) AlignEscapedNewlines: Left # 水平对齐二元和三元表达式的操作数 AlignOperands: Align # 对齐连续的尾随的注释 AlignTrailingComments: true # 允许函数调用的所有参数在放在下一行 AllowAllArgumentsOnNextLine: true # 允许函数声明的所有参数在放在下一行 AllowAllParametersOfDeclarationOnNextLine: true # 允许短的块放在同一行 AllowShortBlocksOnASingleLine: Never # 允许短的case标签放在同一行 AllowShortCaseLabelsOnASingleLine: false # 允许短的枚举放在同一行 AllowShortEnumsOnASingleLine: false # 允许短的函数放在同一行: None, InlineOnly(定义在类中), Empty(空函数), Inline(定义在类中,空函数), All AllowShortFunctionsOnASingleLine: None # 允许短的if语句保持在同一行 AllowShortIfStatementsOnASingleLine: Never # 允许短的Lambdas语句保持在同一行 AllowShortLambdasOnASingleLine: None # 允许短的循环保持在同一行 AllowShortLoopsOnASingleLine: false # 总是在定义返回类型后换行(deprecated) AlwaysBreakAfterDefinitionReturnType: None # 总是在返回类型后换行: None, All, TopLevel(顶级函数,不包括在类中的函数), # AllDefinitions(所有的定义,不包括声明), TopLevelDefinitions(所有的顶级函数的定义) AlwaysBreakAfterReturnType: None # 总是在多行string字面量前换行 AlwaysBreakBeforeMultilineStrings: false # 总是在template声明后换行 AlwaysBreakTemplateDeclarations: Yes # false表示函数实参要么都在同一行,要么都各自一行 BinPackArguments: true # false表示所有形参要么都在同一行,要么都各自一行 BinPackParameters: true # 大括号换行,只有当BreakBeforeBraces设置为Custom时才有效 # 在二元运算符前换行: None(在操作符后换行), NonAssignment(在非赋值的操作符前换行), All(在操作符前换行) BreakBeforeBinaryOperators: NonAssignment # 在大括号前换行: Attach(始终将大括号附加到周围的上下文), Linux(除函数、命名空间和类定义,与Attach类似), # Mozilla(除枚举、函数、记录定义,与Attach类似), Stroustrup(除函数定义、catch、else,与Attach类似), # Allman(总是在大括号前换行), GNU(总是在大括号前换行,并对于控制语句的大括号增加额外的缩进), WebKit(在函数前换行), Custom # 注:这里认为语句块也属于函数 BreakBeforeBraces: Custom # 在三元运算符前换行 BreakBeforeTernaryOperators: true # 在构造函数的初始化列表的逗号前换行 BreakConstructorInitializersBeforeComma: false BreakConstructorInitializers: BeforeColon # 在类声明继承列表的逗号前换行 BreakInheritanceList: BeforeColon BreakAfterJavaFieldAnnotations: false # 允许中断长字符串 BreakStringLiterals: true # 描述具有特殊意义的注释的正则表达式,它不应该被分割为多行或以其它方式改变 CommentPragmas: '^ IWYU pragma:' # 允许连续的名称空间声明将在同一行 CompactNamespaces: false # 构造函数的初始化列表的缩进宽度 ConstructorInitializerIndentWidth: 4 # 延续的行的缩进宽度 ContinuationIndentWidth: 4 # 去除C++11的列表初始化的大括号{后和}前的空格 Cpp11BracedListStyle: true # 继承最常用的指针和引用的对齐方式 DerivePointerAlignment: true # 关闭格式化 DisableFormat: false # 修饰符后放置空行 EmptyLineAfterAccessModifier: Never # 修饰符前放置空行 EmptyLineBeforeAccessModifier: LogicalBlock # 自动检测函数的调用和定义是否被格式为每行一个参数(Experimental) ExperimentalAutoDetectBinPacking: false # 修正命名空间注释 FixNamespaceComments: true # 需要被解读为foreach循环而不是函数调用的宏 ForEachMacros: - foreach - Q_FOREACH - BOOST_FOREACH # 需要解读为if的函数 IfMacros: - KJ_IF_MAYBE IncludeBlocks: Regroup # 对#include进行排序,匹配了某正则表达式的#include拥有对应的优先级,匹配不到的则默认优先级为INT_MAX(优先级越小排序越靠前), # 可以定义负数优先级从而保证某些#include永远在最前面 IncludeCategories: - Regex: '^<ext/.*\.h>' Priority: 2 SortPriority: 0 CaseSensitive: false - Regex: '^<.*\.h>' Priority: 1 SortPriority: 0 CaseSensitive: false - Regex: '^<.*' Priority: 2 SortPriority: 0 CaseSensitive: false - Regex: '.*' Priority: 3 SortPriority: 0 CaseSensitive: false # include块排序 IncludeIsMainRegex: '([-_](test|unittest))?$' # 缩进修饰符 IndentAccessModifiers: false # 缩进case块 IndentCaseBlocks: false # 缩进case标签 IndentCaseLabels: true # 缩进goto标签 IndentGotoLabels: true # 预处理缩进 IndentPPDirectives: None # 缩进extern块 IndentExternBlock: AfterExternBlock # 缩进宽度 IndentWidth: 4 # 函数返回类型换行时,缩进函数声明或函数定义的函数名 IndentWrappedFunctionNames: false # 添加尾部注释 InsertTrailingCommas: None # 保留在块开始处的空行 KeepEmptyLinesAtTheStartOfBlocks: true # Lambda块缩进 LambdaBodyIndentation: Signature # 开始一个块的宏的正则表达式 MacroBlockBegin: '' # 结束一个块的宏的正则表达式 MacroBlockEnd: '' # 连续空行的最大数量 MaxEmptyLinesToKeep: 1 # 命名空间的缩进: None, Inner(缩进嵌套的命名空间中的内容), All NamespaceIndentation: None # 使用ObjC块时缩进宽度 ObjCBlockIndentWidth: 4 # 在ObjC的protocol列表前添加一个空格 ObjCSpaceBeforeProtocolList: false # 使用的包构造函数初始化式样式 PackConstructorInitializers: NextLine # 在call(后对函数调用换行的penalty PenaltyBreakBeforeFirstCallParameter: 19 # 在一个注释中引入换行的penalty PenaltyBreakComment: 300 # 第一次在<<前换行的penalty PenaltyBreakFirstLessLess: 120 # 在一个字符串字面量中引入换行的penalty PenaltyBreakString: 1000 # 对于每个在行字符数限制之外的字符的penalty PenaltyExcessCharacter: 1000000 # 将函数的返回类型放到它自己的行的penalty PenaltyReturnTypeOnItsOwnLine: 200 # 指针和引用的对齐: Left, Right, Middle PointerAlignment: Left PPIndentWidth: -1 RawStringFormats: - Language: Cpp Delimiters: - cc - CC - cpp - Cpp - CPP - 'c++' - 'C++' CanonicalDelimiter: '' BasedOnStyle: google - Language: TextProto Delimiters: - pb - PB - proto - PROTO EnclosingFunctions: - EqualsProto - EquivToProto - PARSE_PARTIAL_TEXT_PROTO - PARSE_TEST_PROTO - PARSE_TEXT_PROTO - ParseTextOrDie - ParseTextProtoOrDie - ParseTestProto - ParsePartialTestProto CanonicalDelimiter: pb BasedOnStyle: google # 引用对齐 ReferenceAlignment: Pointer # 允许重新排版注释 ReflowComments: true # 允许排序#include SortIncludes: CaseSensitive # 允许排序声明 SortUsingDeclarations: true # 单独的定义块 SeparateDefinitionBlocks: Always # 在C风格类型转换后添加空格 SpaceAfterCStyleCast: false # 在赋值运算符之前添加空格 SpaceBeforeAssignmentOperators: true # 在逻辑非操作符之后插入一个空格 SpaceAfterLogicalNot: false # 在' template '关键字之后会插入一个空格 SpaceAfterTemplateKeyword: true # 在用于初始化对象的c++ 11带括号的列表之前(在前面的标识符或类型之后)将插入一个空格 SpaceBeforeCpp11BracedList: false # 构造函数初始化式冒号前的空格将被删除 SpaceBeforeCtorInitializerColon: true # 在继承冒号前添加空格 SpaceBeforeInheritanceColon: true # 控制括号前的单独空格。 SpaceBeforeParens: ControlStatements # 开圆括号之前添加一个空格 SpaceBeforeParensOptions: AfterControlStatements: true AfterForeachMacros: true AfterFunctionDefinitionName: false AfterFunctionDeclarationName: false AfterIfMacros: true AfterOverloadedOperator: false BeforeNonEmptyParentheses: false # 在空的圆括号中添加空格 SpaceInEmptyParentheses: false # 在基于冒号的范围循环之前 添加空格 SpaceBeforeRangeBasedForLoopColon: true # 在尾随的评论前添加的空格数(只适用于//) SpacesBeforeTrailingComments: 2 # 在尖括号的<后和>前添加空格 SpacesInAngles: Never # 在容器(ObjC和JavaScript的数组和字典等)字面量中添加空格 SpacesInContainerLiterals: true # 在C风格类型转换的括号中添加空格 SpacesInCStyleCastParentheses: false # 在圆括号的(后和)前添加空格 SpacesInParentheses: false # 在方括号的[后和]前添加空格,lamda表达式和未指明大小的数组的声明不受影响 SpacesInSquareBrackets: false # tab宽度 TabWidth: 4 # 使用tab字符: Never, ForIndentation, ForContinuationAndIndentation, Always UseTab: Never DeriveLineEnding: false UseCRLF: false WhitespaceSensitiveMacros: - STRINGIZE - PP_STRINGIZE - BOOST_PP_STRINGIZE - NS_SWIFT_NAME - CF_SWIFT_NAME
.cmake-format.json
如果需要对cmake文件进行格式化,需要安装 cmake-format插件或者工具,配置文件
{ "_help_parse": "Options affecting listfile parsing", "parse": { "_help_additional_commands": [ "Specify structure for custom cmake functions" ], "additional_commands": { "foo": { "flags": ["BAR", "BAZ"], "kwargs": { "HEADERS": "*", "SOURCES": "*", "DEPENDS": "*" } } }, "_help_override_spec": [ "Override configurations per-command where available" ], "override_spec": {}, "_help_vartags": ["Specify variable tags."], "vartags": [], "_help_proptags": ["Specify property tags."], "proptags": [] }, "_help_format": "Options affecting formatting.", "format": { "_help_disable": [ "Disable formatting entirely, making cmake-format a no-op" ], "disable": false, "_help_line_width": ["How wide to allow formatted cmake files"], "line_width": 120, "_help_tab_size": ["How many spaces to tab for indent"], "tab_size": 4, "_help_use_tabchars": [ "If true, lines are indented using tab characters (utf-8", "0x09) instead of <tab_size> space characters (utf-8 0x20).", "In cases where the layout would require a fractional tab", "character, the behavior of the fractional indentation is", "governed by <fractional_tab_policy>" ], "use_tabchars": false, "_help_fractional_tab_policy": [ "If <use_tabchars> is True, then the value of this variable", "indicates how fractional indentions are handled during", "whitespace replacement. If set to 'use-space', fractional", "indentation is left as spaces (utf-8 0x20). If set to", "`round-up` fractional indentation is replaced with a single", "tab character (utf-8 0x09) effectively shifting the column", "to the next tabstop" ], "fractional_tab_policy": "use-space", "_help_max_subgroups_hwrap": [ "If an argument group contains more than this many sub-groups", "(parg or kwarg groups) then force it to a vertical layout." ], "max_subgroups_hwrap": 3, "_help_max_pargs_hwrap": [ "If a positional argument group contains more than this many", "arguments, then force it to a vertical layout." ], "max_pargs_hwrap": 6, "_help_max_rows_cmdline": [ "If a cmdline positional group consumes more than this many", "lines without nesting, then invalidate the layout (and nest)" ], "max_rows_cmdline": 2, "_help_separate_ctrl_name_with_space": [ "If true, separate flow control names from their parentheses", "with a space" ], "separate_ctrl_name_with_space": false, "_help_separate_fn_name_with_space": [ "If true, separate function names from parentheses with a", "space" ], "separate_fn_name_with_space": false, "_help_dangle_parens": [ "If a statement is wrapped to more than one line, than dangle", "the closing parenthesis on its own line." ], "dangle_parens": false, "_help_dangle_align": [ "If the trailing parenthesis must be 'dangled' on its on", "line, then align it to this reference: `prefix`: the start", "of the statement, `prefix-indent`: the start of the", "statement, plus one indentation level, `child`: align to", "the column of the arguments" ], "dangle_align": "prefix", "_help_min_prefix_chars": [ "If the statement spelling length (including space and", "parenthesis) is smaller than this amount, then force reject", "nested layouts." ], "min_prefix_chars": 4, "_help_max_prefix_chars": [ "If the statement spelling length (including space and", "parenthesis) is larger than the tab width by more than this", "amount, then force reject un-nested layouts." ], "max_prefix_chars": 10, "_help_max_lines_hwrap": [ "If a candidate layout is wrapped horizontally but it exceeds", "this many lines, then reject the layout." ], "max_lines_hwrap": 2, "_help_line_ending": ["What style line endings to use in the output."], "line_ending": "unix", "_help_command_case": [ "Format command names consistently as 'lower' or 'upper' case" ], "command_case": "canonical", "_help_keyword_case": [ "Format keywords consistently as 'lower' or 'upper' case" ], "keyword_case": "unchanged", "_help_always_wrap": [ "A list of command names which should always be wrapped" ], "always_wrap": [], "_help_enable_sort": [ "If true, the argument lists which are known to be sortable", "will be sorted lexicographicall" ], "enable_sort": true, "_help_autosort": [ "If true, the parsers may infer whether or not an argument", "list is sortable (without annotation)." ], "autosort": false, "_help_require_valid_layout": [ "By default, if cmake-format cannot successfully fit", "everything into the desired linewidth it will apply the", "last, most agressive attempt that it made. If this flag is", "True, however, cmake-format will print error, exit with non-", "zero status code, and write-out nothing" ], "require_valid_layout": false, "_help_layout_passes": [ "A dictionary mapping layout nodes to a list of wrap", "decisions. See the documentation for more information." ], "layout_passes": {} }, "_help_markup": "Options affecting comment reflow and formatting.", "markup": { "_help_bullet_char": ["What character to use for bulleted lists"], "bullet_char": "*", "_help_enum_char": [ "What character to use as punctuation after numerals in an", "enumerated list" ], "enum_char": ".", "_help_first_comment_is_literal": [ "If comment markup is enabled, don't reflow the first comment", "block in each listfile. Use this to preserve formatting of", "your copyright/license statements." ], "first_comment_is_literal": false, "_help_literal_comment_pattern": [ "If comment markup is enabled, don't reflow any comment block", "which matches this (regex) pattern. Default is `None`", "(disabled)." ], "literal_comment_pattern": null, "_help_fence_pattern": [ "Regular expression to match preformat fences in comments", "default= ``r'^\\s*([`~]{3}[`~]*)(.*)$'``" ], "fence_pattern": "^\\s*([`~]{3}[`~]*)(.*)$", "_help_ruler_pattern": [ "Regular expression to match rulers in comments default=", "``r'^\\s*[^\\w\\s]{3}.*[^\\w\\s]{3}$'``" ], "ruler_pattern": "^\\s*[^\\w\\s]{3}.*[^\\w\\s]{3}$", "_help_explicit_trailing_pattern": [ "If a comment line matches starts with this pattern then it", "is explicitly a trailing comment for the preceeding", "argument. Default is '#<'" ], "explicit_trailing_pattern": "#<", "_help_hashruler_min_length": [ "If a comment line starts with at least this many consecutive", "hash characters, then don't lstrip() them off. This allows", "for lazy hash rulers where the first hash char is not", "separated by space" ], "hashruler_min_length": 10, "_help_canonicalize_hashrulers": [ "If true, then insert a space between the first hash char and", "remaining hash chars in a hash ruler, and normalize its", "length to fill the column" ], "canonicalize_hashrulers": true, "_help_enable_markup": ["enable comment markup parsing and reflow"], "enable_markup": true }, "_help_lint": "Options affecting the linter", "lint": { "_help_disabled_codes": ["a list of lint codes to disable"], "disabled_codes": [], "_help_function_pattern": [ "regular expression pattern describing valid function names" ], "function_pattern": "[0-9a-z_]+", "_help_macro_pattern": [ "regular expression pattern describing valid macro names" ], "macro_pattern": "[0-9A-Z_]+", "_help_global_var_pattern": [ "regular expression pattern describing valid names for", "variables with global (cache) scope" ], "global_var_pattern": "[A-Z][0-9A-Z_]+", "_help_internal_var_pattern": [ "regular expression pattern describing valid names for", "variables with global scope (but internal semantic)" ], "internal_var_pattern": "_[A-Z][0-9A-Z_]+", "_help_local_var_pattern": [ "regular expression pattern describing valid names for", "variables with local scope" ], "local_var_pattern": "[a-z][a-z0-9_]+", "_help_private_var_pattern": [ "regular expression pattern describing valid names for", "privatedirectory variables" ], "private_var_pattern": "_[0-9a-z_]+", "_help_public_var_pattern": [ "regular expression pattern describing valid names for public", "directory variables" ], "public_var_pattern": "[A-Z][0-9A-Z_]+", "_help_argument_var_pattern": [ "regular expression pattern describing valid names for", "function/macro arguments and loop variables." ], "argument_var_pattern": "[a-z][a-z0-9_]+", "_help_keyword_pattern": [ "regular expression pattern describing valid names for", "keywords used in functions or macros" ], "keyword_pattern": "[A-Z][0-9A-Z_]+", "_help_max_conditionals_custom_parser": [ "In the heuristic for C0201, how many conditionals to match", "within a loop in before considering the loop a parser." ], "max_conditionals_custom_parser": 2, "_help_min_statement_spacing": [ "Require at least this many newlines between statements" ], "min_statement_spacing": 1, "_help_max_statement_spacing": [ "Require no more than this many newlines between statements" ], "max_statement_spacing": 2, "max_returns": 6, "max_branches": 12, "max_arguments": 5, "max_localvars": 15, "max_statements": 50 }, "_help_encode": "Options affecting file encoding", "encode": { "_help_emit_byteorder_mark": [ "If true, emit the unicode byte-order mark (BOM) at the start", "of the file" ], "emit_byteorder_mark": false, "_help_input_encoding": [ "Specify the encoding of the input file. Defaults to utf-8" ], "input_encoding": "utf-8", "_help_output_encoding": [ "Specify the encoding of the output file. Defaults to utf-8.", "Note that cmake only claims to support utf-8 so be careful", "when using anything else" ], "output_encoding": "utf-8" }, "_help_misc": "Miscellaneous configurations options.", "misc": { "_help_per_command": [ "A dictionary containing any per-command configuration", "overrides. Currently only `command_case` is supported." ], "per_command": {} } }
launch.json
{ // Use IntelliSense to learn about possible attributes. // Hover to view descriptions of existing attributes. // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 "version": "0.2.0", "configurations": [ { "name": "DebugActiveCppFile", "type": "cppdbg", "request": "launch", "program": "${workspaceFolder}/build/bin/${fileBasenameNoExtension}.out", "args": [], "stopAtEntry": false, "cwd": "${workspaceFolder}", "environment": [], "externalConsole": false, "MIMode": "gdb", "setupCommands": [ { "description": "Enable pretty-printing for gdb", "text": "-enable-pretty-printing", "ignoreFailures": true } ], "preLaunchTask": "BuildActiveCppFile", "miDebuggerPath": "/usr/bin/gdb" } ] }
task.json
{ "version": "2.0.0", "tasks": [ { "type": "shell", "label": "BuildActiveCppFile", "command": "/usr/bin/g++", "args": [ "-g", "${file}", "-I${workspaceFolder}/include", "-o", "${workspaceFolder}/build/bin/${fileBasenameNoExtension}.out", "-std=c++20" ], "options": { "cwd": "/usr/bin" }, "problemMatcher": [ "$gcc" ], "group": "build" } ] }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。