如何在整个存储库中使用VSCode替换所有包含保护的#include once?

huangapple go评论52阅读模式
英文:

How to replace all include guards by #pragma once in whole repository in VSCode?

问题

在我们的项目中,我们使用类似以下的包含保护:

#ifndef _SINGLETON_H_
#define _SINGLETON_H_
#endif  // #ifndef _SINGLETON_H_

最后一行可以不带注释等,所以文件可以匹配类似这样的内容:

^.*#ifndef\s+[A-Z_]+\s+#define\s+[A-Z_]+(.*\n)*.*#endif.*$

我想要用以下方式替换它:

#pragma once

如何实现这个目标呢?可能是要匹配包含保护的内容,将内容存储在包含保护内,使用#pragma前缀内容,并替换包含保护的内容,对吗?我在存储(或不修改)包含保护内的内容方面有困难。

英文:

In our project, we use include guards like:

..
#ifndef _SINGLETON_H_
#define _SINGLETON_H_
..
#endif  // #ifndef _SINGLETON_H_
..

The last line can be without comment etc. so the files match something like:
^.*#ifndef\s+[A-Z_]+\s+#define\s+[A-Z_]+(.*\n)*.*#endif.*$.

I want to replace it with:

..
#pragma once
..
..

using Replace All functionality.

How to achieve that?
Probably somehow match the include guard with content, store the content within the include guard, prefix the content with pragma and replace the include guard with content, right?
I am struggling with storing (or not modifying) the content within include guard.

答案1

得分: 0

  1. 搜索:^#ifndef\s+[A-Z_].*\s*#define\s+[A-Z_].*((.*\n)+.*)#endif.*$
    替换:#pragma once$1
  2. 点击“全部替换”。
英文:

So I made it work myself (at least for our project with 4687 headers replaced):

  1. Search: ^#ifndef\s+[A-Z_].*\s*#define\s+[A-Z_].*((.*\n)+.*)#endif.*$
    Replace: #pragma once$1
  2. Hit Replace All.

huangapple
  • 本文由 发表于 2023年4月19日 22:11:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76055541.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定