我正在处理LLVM-IR代码,该代码由生成,clang -emit-llvm
并希望代码折叠工作。
到目前为止,我正在使用foldmethod=expr
和foldexpr=LLVMFold()
。我想使用foldmethod=syntax
llvm存储库中的语法文件基于语法的折叠(即)。在这里可用。
请注意,第一个正则表达式来自标签的语法文件。
function! LLVMFolds()
let thisline = getline(v:lnum)
if match(thisline, '^[-a-zA-Z$._][-a-zA-Z$._0-9]*:') >= 0
return ">2"
elseif match(thisline, '^\}$') >= 0
return "<1"
elseif match(thisline, '{$') >= 0
return ">1"
else
return "="
endif
endfunction
这将闭合括号吞入了2级折叠中。
还尝试了foldmethod=indent
折叠不充分的问题,对于此示例foldmethod=marker
,foldmark="{,}"
理想情况下,LLVM-IR代码不完整:
define i32 @main() nounwind {
entry:
%retval = alloca i32, align 4
for.cond: ; preds = %entry
%4 = load i32* %i, align 4
%cmp1 = icmp slt i32 %4, 10
br i1 %cmp1, label %for.body, label %for.end
}
我想褶皱是从{
的define
到}
并在每个标记的部分,即,从entry:
向澄清的线。
我已经使用了这个功能
function! LLVMFolds()
let thisline = getline(v:lnum)
let nextline = getline(v:lnum + 1)
" match start of global var block
if match(thisline, '^@') == 0 && foldlevel(v:lnum - 1) <= 0
return ">1"
" match start of global struct block
elseif match(thisline, '^%') == 0 && foldlevel(v:lnum - 1) <= 0
return ">1"
" matches lables
elseif match(thisline, '^[-a-zA-Z$._][-a-zA-Z$._0-9]*:') >= 0
return ">2"
" keep closing brace outside l2 fold
elseif match(nextline, '^\}$') >= 0
return "<2"
" keep closing brace in l1 fold
elseif match(thisline, '^\}$') >= 0
return "<1"
" open new l1 fold for open brace
elseif match(thisline, '{$') >= 0
return ">1"
" for the next line being empty, close the fold for the var and struct blocks
elseif match(nextline, '^$') >= 0
if match(thisline, '^@') == 0 && foldlevel(v:lnum - 1) == 1
return "<1"
elseif match(thisline, '^%') >= 0 && foldlevel(v:lnum - 1) == 1
return "<1"
else
return "="
endif
else
return "="
endif
endfunction
从第2层折叠中不包括右括号,并折叠全局结构和变量的初始列表。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句