当前位置:   article > 正文

vscode+clangd远程服务器看linux内核源码,完美替换source insight_source insight ssh插件

source insight ssh插件

vscode插件需求

插件共有以下几个:

  1. C/C++
  2. clangd
  3. Remote - SSH

插件设置

C/C++ 1插件在这里只需要提供基础的C/C++服务即可,不需要语法解析,自动跳转和补全功能。所以需要关闭C/C++,在vscode设置里搜索C_Cpp: Intelli Sense Engine,选择disabled

Remote - SSH插件用于远程连接服务器或者是虚拟机。配置一下IP及端口即可。ctrl+shift+p:搜索Remote-SSH:Open SSH Configuration File。输入自己的服务器IP及端口,例:

Host xxxx
    HostName 192.168.1.111
    User xxx
    Port xx
  • 1
  • 2
  • 3
  • 4

clangd插件,远程连接到服务器后在内核工程目录创建.vscode文件夹,里面新建settings.json,内容如下:

{
    "clangd.arguments": [
        "--all-scopes-completion",
        "--clang-tidy",
        "--completion-parse=always",
        "--header-insertion=never",
        "--completion-style=detailed",
        "--query-driver=***/***/***gcc",//这里是自己的交叉编译路径
        "--function-arg-placeholders=false",
        "--compile-commands-dir=${workspaceFolder}/",
        "-log=info",
        "-j=32"
    ],
    "clangd.checkUpdates": true,
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

工具需求

  1. bear
  2. clangd

bear编译

bear用于创建compile_commands.json文件,该文件详细描述各个代码文件的编译命令,便于clangd建立代码工程。

下载bear源码:bear下载链接。推荐下载2.4.3的版本。

tar -xvf Bear-2.4.3.tar.gz
cd Bear-2.4.3
camke ./ -DCMAKE_INSTALL_PREFIX=${install_dir}
make all -j32
make install
  • 1
  • 2
  • 3
  • 4
  • 5

${install_dir}/bin添加到$PATH里,将${install_dir/lib64/bear}添加到$LD_LIBRARY_PATH

clangd编译

clangd可以根据compile_commands.json文件建立索引数据库。从而达到索引各种符号的目的。
clangd是在llvm工程里的。所以下载llvm的工程源码llvm源码,下载llvm-project-xx.x.x.src.tar.xz文件,这个才是工程源码。

cmake -G "Unix Makefiles" -DLLVM_ENABLE_PROJECTS="clang;clang-tools-extra" -DCMAKE_INSTALL_PREFIX=~/${install_dir} -DCMAKE_BUILD_TYPE=Release ../llvm -DCMAKE_CXX_COMPILER=${g++_dir}/g++ -DCMAKE_C_COMPILER=${gcc_dir}/gcc
make -j32
make install
  • 1
  • 2
  • 3

${install_dir}/bin添加到$PATH里,将${install_dir/lib}添加到$LD_LIBRARY_PATH

内核索引

bear make ARCH=arm64 CROSS_COMPILE=xxx
  • 1

在编译命令前加上bear就可以生成compile_commands.json文件了。之后就可以愉快的索引了。

设置代码风格及静态检查项

代码工程目录下可以新建.clangd文件,该文件描述了要启用的静态检查项,也可以去除clang无法识别的一些编译指令,这里给个例子,可以参考我这个配置,也可以参考这个网站clang analyzer

Diagnostics:
  ClangTidy:
    Add:#添加的代码检查项
    [
      clang-analyzer-*,
      modernize-*,
      bugprone-*,
      performance-*,
      portability-*,
      readability-*,
      cppcoreguidelines-no-malloc,
      cppcoreguidelines-macro-usage,
      cppcoreguidelines-pro-bounds-pointer-arithmetic,
      bugprone-infinite-loop,
      bugprone-argument-comment,
      bugprone-assert-side-effect,
      bugprone-bad-signal-to-kill-thread,
      bugprone-branch-clone,
      bugprone-copy-constructor-init,
      bugprone-dangling-handle,
      bugprone-dynamic-static-initializers,
      bugprone-fold-init-type,
      bugprone-forward-declaration-namespace,
      bugprone-forwarding-reference-overload,
      bugprone-inaccurate-erase,
      bugprone-incorrect-roundings,
      bugprone-integer-division,
      bugprone-lambda-function-name,
      bugprone-macro-parentheses,
      bugprone-macro-repeated-side-effects,
      bugprone-misplaced-operator-in-strlen-in-alloc,
      bugprone-misplaced-pointer-arithmetic-in-alloc,
      bugprone-misplaced-widening-cast,
      bugprone-move-forwarding-reference,
      bugprone-multiple-statement-macro,
      bugprone-no-escape,
      bugprone-not-null-terminated-result,
      bugprone-parent-virtual-call,
      bugprone-posix-return,
      bugprone-reserved-identifier,
      bugprone-sizeof-container,
      bugprone-sizeof-expression,
      bugprone-spuriously-wake-up-functions,
      bugprone-string-constructor,
      bugprone-string-integer-assignment,
      bugprone-string-literal-with-embedded-nul,
      bugprone-suspicious-enum-usage,
      bugprone-suspicious-include,
      bugprone-suspicious-memory-comparison,
      bugprone-suspicious-memset-usage,
      bugprone-suspicious-missing-comma,
      bugprone-suspicious-semicolon,
      bugprone-suspicious-string-compare,
      bugprone-swapped-arguments,
      bugprone-terminating-continue,
      bugprone-throw-keyword-missing,
      bugprone-too-small-loop-variable,
      bugprone-undefined-memory-manipulation,
      bugprone-undelegated-constructor,
      bugprone-unhandled-self-assignment,
      bugprone-unused-raii,
      bugprone-unused-return-value,
      bugprone-use-after-move,
      bugprone-virtual-near-miss,
            cert-dcl21-cpp,
      cert-dcl58-cpp,
      cert-err34-c,
      cert-err52-cpp,
      cert-err58-cpp,
      cert-err60-cpp,
      cert-flp30-c,
      cert-msc50-cpp,
      cert-msc51-cpp,
      cert-str34-c,
      cppcoreguidelines-interfaces-global-init,
      cppcoreguidelines-narrowing-conversions,
      cppcoreguidelines-pro-type-member-init,
      cppcoreguidelines-pro-type-static-cast-downcast,
      cppcoreguidelines-slicing,
      google-default-arguments,
      google-explicit-constructor,
      google-runtime-operator,
      hicpp-exception-baseclass,
      hicpp-multiway-paths-covered,
      misc-misplaced-const,
      misc-new-delete-overloads,
      misc-no-recursion,
      misc-non-copyable-objects,
      misc-throw-by-value-catch-by-reference,
      misc-unconventional-assign-operator,
      misc-uniqueptr-reset-release,
      modernize-avoid-bind,
      modernize-concat-nested-namespaces,
      modernize-deprecated-headers,
      modernize-deprecated-ios-base-aliases,
      modernize-loop-convert,
      modernize-make-shared,
      modernize-make-unique,
      modernize-pass-by-value,
      modernize-raw-string-literal,
      modernize-redundant-void-arg,
      modernize-replace-auto-ptr,
      modernize-replace-disallow-copy-and-assign-macro,
      modernize-replace-random-shuffle,
      modernize-return-braced-init-list,
      modernize-shrink-to-fit,
      modernize-unary-static-assert,
      modernize-use-auto,
      modernize-use-bool-literals,
      modernize-use-emplace,
      modernize-use-equals-default,
      modernize-use-equals-delete,
      modernize-use-nodiscard,
      modernize-use-noexcept,
      modernize-use-nullptr,
      modernize-use-override,
      modernize-use-transparent-functors,
      modernize-use-uncaught-exceptions,
      openmp-use-default-none,
      performance-faster-string-find,
      performance-for-range-copy,
      performance-implicit-conversion-in-loop,
      performance-inefficient-algorithm,
      performance-inefficient-string-concatenation,
      performance-inefficient-vector-operation,
      performance-move-const-arg,
      performance-move-constructor-init,
      performance-no-automatic-move,
      performance-noexcept-move-constructor,
      performance-trivially-destructible,
      performance-type-promotion-in-math-fn,
      performance-unnecessary-copy-initialization,
      performance-unnecessary-value-param,
      portability-simd-intrinsics,
      readability-avoid-const-params-in-decls,
      readability-const-return-type,
      readability-container-size-empty,
      readability-convert-member-functions-to-static,
      readability-delete-null-pointer,
      readability-inconsistent-declaration-parameter-name,
      readability-make-member-function-const,
      readability-misleading-indentation,
      readability-misplaced-array-index,
      readability-redundant-control-flow,
      readability-redundant-declaration,
      readability-redundant-function-ptr-dereference,
      readability-redundant-smartptr-get,
      readability-redundant-string-cstr,
      readability-redundant-string-init,
      readability-simplify-subscript-expr,
      readability-static-accessed-through-instance,
      readability-static-definition-in-anonymous-namespace,
      readability-string-compare,
      readability-uniqueptr-delete-release,
      readability-use-anyofallof,
      
    ]
    Remove:#忽略的代码检查项
    [
      readability-function-cognitive-complexity,
      readability-identifier-length,
      readability-magic-numbers,
      readability-non-const-parameter,
      bugprone-easily-swappable-parameters,
      readability-misleading-indentation,
      readability-isolate-declaration,
      readability-braces-around-statements,
    ]
CompileFlags:                             
  Add: #添加的编译标志
    [
      -W,
      -Wall,
      -Wshadow,
      -Wtype-limits,
      -Wasm,
      -Wchkp,
      -Warray-parameter,
      -Wthread-safety,
      -Wswitch-default,
      -Wuninitialized,
      -Wunused-label,
      -Wunused-lambda-capture,
      -Wno-error=unused-command-line-argument-hard-error-in-future,
      -Wno-sign-compare,
      -Wno-void-pointer-to-int-cast,
      -Wno-int-to-pointer-cast,
      -Wno-asm_invalid_global_var_reg,
      -Wno-format,
      --target=aarch64-linux-gnu,
    ]
  Remove:#忽略的编译标志
    [
      -mabi=lp64,
      -fno-var-tracking-assignments,
      -fconserve-stack,
    ]
    
      
InlayHints:#嵌入提示
  Enabled: Yes
  ParameterNames: Yes
  DeducedTypes: Yes

Hover:
  ShowAKA: Yes
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206

配置clangd工程代码风格

clangd支持一键格式化代码,索引,空格,换行,变量命名规则等都可以设置,在工程下新建.clang-format文件,具体怎么设置可以参考我的工程配置和这个网站clang format

# Generated from CLion C/C++ Code Style settings

BasedOnStyle: Microsoft

#头文件排序
SortIncludes: Never

#转义换行符对齐
AlignEscapedNewlines: Left

#对齐注释
AlignTrailingComments: true

# 连续赋值时,对齐所有等号
AlignConsecutiveAssignments: 
  Enabled: true
  #跨空行对齐
  AcrossEmptyLines: true
  #跨注释对齐
  AcrossComments: false
  #填充空格
  PadOperators: true
  AlignCompound: true
 
# 连续声明时,对齐所有声明的变量名
AlignConsecutiveDeclarations: 
  Enabled: true
  AcrossEmptyLines: true
  AcrossComments: false
  PadOperators: true
  AlignCompound: true

#对齐宏
AlignConsecutiveMacros:
  Enabled: true
  AcrossEmptyLines: true
  AcrossComments: false
  PadOperators: true

#访问修饰符的额外缩进或升级,例如 .public:
AccessModifierOffset: -4

#将参数水平对齐在左括号之后
AlignAfterOpenBracket: Align

#对结构数组使用初始化时,会将字段对齐到列中
AlignArrayOfStructures: Left

#对齐连续位字段的样式
AlignConsecutiveBitFields: false

#水平对齐二元表达式和三元表达式的操作数
AlignOperands: Align

#如果函数调用或带大括号的初始值设定项列表不适合某一行,则允许将所有参数放在下一行
AllowAllArgumentsOnNextLine: false

#如果函数声明不适合某一行,则允许将函数声明的所有参数放在下一行
AllowAllParametersOfDeclarationOnNextLine: false

#根据值,可以放在一行上
AllowShortBlocksOnASingleLine: Never

#短的case将被收缩到一行
AllowShortCaseLabelsOnASingleLine: false

#允许在一行上使用短枚举
AllowShortEnumsOnASingleLine: false

#短的函数可以放在一行 None:永远不放在一行
AllowShortFunctionsOnASingleLine: None

#根据值,if可以放在一行上
AllowShortIfStatementsOnASingleLine: Never

#根据值,可以放在一行上
AllowShortLambdasOnASingleLine: None

#如果 ,可以放在一行上
AllowShortLoopsOnASingleLine: false

#函数声明返回要使用的类型中断样式
AlwaysBreakAfterReturnType: None

#要使用的模板声明中断样式
AlwaysBreakTemplateDeclarations: Yes

#如果 ,则函数调用的参数要么全部在同一行上,要么每个参数都有一行
BinPackArguments: true


BreakBeforeBraces: Custom
BraceWrapping:
  AfterCaseLabel: true
  AfterClass: true
  AfterControlStatement: Always
  AfterEnum: true
  AfterStruct: true
  AfterFunction: true
  AfterNamespace: true
  AfterUnion: true
  AfterExternBlock: true
  BeforeCatch: true
  BeforeElse: true
  IndentBraces: false
  SplitEmptyFunction: true
  SplitEmptyRecord: true
BreakBeforeBinaryOperators: None
BreakBeforeTernaryOperators: true
BreakConstructorInitializers: BeforeColon
BreakInheritanceList: BeforeColon
ColumnLimit: 80
CompactNamespaces: false
ContinuationIndentWidth: 8
IndentCaseLabels: true
IndentPPDirectives: None
IndentWidth: 4
KeepEmptyLinesAtTheStartOfBlocks: true
MaxEmptyLinesToKeep: 2
NamespaceIndentation: All
ObjCSpaceAfterProperty: false
ObjCSpaceBeforeProtocolList: true
PointerAlignment: Right
ReflowComments: false
SpaceAfterCStyleCast: false
SpaceAfterLogicalNot: false
SpaceAfterTemplateKeyword: false
SpaceBeforeAssignmentOperators: true
SpaceBeforeCpp11BracedList: false
SpaceBeforeCtorInitializerColon: true
SpaceBeforeInheritanceColon: true
SpaceBeforeParens: ControlStatements
SpaceBeforeRangeBasedForLoopColon: false
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 0
SpacesInAngles: false
SpacesInCStyleCastParentheses: false
SpacesInContainerLiterals: false
SpacesInParentheses: false
SpacesInSquareBrackets: false
# TabWidth: 4
UseTab: Never

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/239916
推荐阅读
相关标签
  

闽ICP备14008679号