Cedet/Semantic 未考虑 C 预处理器定义

发布于 2024-12-03 01:15:26 字数 4329 浏览 0 评论 0原文

我正在尝试在我的 Linux 内核开发项目中使用 Emacs 作为 IDE。所以我安装了 CEDET 和 GNU Global (gtags),没有任何问题。

当我打开属于我的 Linux 内核源文件夹的 C 源代码时,会自动检测到 linux 项目。函数、变量、标题和其他关键字都正确突出显示。

通过 ~/.emacs 文件我配置了语义的代码完成和智能感知。因此,当我按 C- 时,会出现代码完成菜单,我可以选择需要的内容。

问题是当我使用预处理器定义时,智能感知系统显示当前位置的菜单,但在调用时没有检测到正确的内容(C-)。即使我关闭并重新启动 emacs,也会重现相同的行为。

测试代码为:

//-------------------------------------------------------- --------------------------------------------------------

typedef struct
{
     int p1;

        #ifdef __KERNEL__

            int p2;

        #endif

        #ifdef USE1

             char p3;

          #endif

#ifdef __LINUX_ARM_ARCH__

   int p4;

#endif

}OBJ;

OBJ g_obj;

//--- -------------------------------------------------- -----------------------------------

KERNEL 、 USE1 和 LINUX_ARM_ARCH 已定义如下:

__KERNEL__ : (add-to-list 'semantic-lex-c-preprocessor-symbol-map '("__KERNEL__" . "")) in .emacs
USE1 : defined in a header file included by the current C source file.
__LINUX_ARM_ARCH__ : (add-to-list 'semantic-lex-c-preprocessor-symbol-file "/home/abdellatif/kernel/include/generated/autoconf.h") in .emacs

在 .emacs 文件中,我还添加了 GCC 交叉编译器(GCC for ARM)构建内核项目所需的所有包含和定义。

我还尝试强制语义使用 GCC 交叉编译器命令行代替系统 GCC 来正确预处理,但还没有找到如何做到这一点。

“Mx语义-c-描述-环境”命令显示正确的包含路径并定义 (在 .emacs 中设置)。但 GCC 定义的系统也列出了。

.emacs 文件和软件版本如下所示。

欢迎任何帮助或指示:) 此致

############# Software versions #############

- Emacs (23.2.1)
- Ubuntu machine (natty, 11.04)
- Cedet 1.0 (from http://cedet.sourceforge.net/)
############# .emacs #############

;load CEDET

(load-file "~/Documents/my_emacs/cedet-1.0/common/cedet.el")

(require 'ede)

(global-ede-mode t)
; turn on which-func support (plus all other code helpers)
(semantic-load-enable-gaudy-code-helpers)
(semantic-load-enable-excessive-code-helpers)

; turn on all "useful" features
(setq semantic-load-turn-useful-things-on t)

(setq-mode-local c-mode
         semanticdb-find-default-throttle
         '(project unloaded system recursive))

;init names completion, and displaying of information for tags & classes
(require 'semantic-ia)

;preprocessing of source code

(require 'semantic-c)

(semantic-reset-system-include 'c-mode)
(semantic-reset-system-include 'c++-mode)
(semantic-add-system-include "/home/abdellatif/toolchain/arm-eabi-4.4.3/lib/gcc/arm-eabi/4.4.3/include" 'c-mode)
(semantic-add-system-include "/home/abdellatif/kernel/arch/arm/include" 'c-mode)
(semantic-add-system-include "/home/abdellatif/kernel/include" 'c-mode)
(semantic-add-system-include "/home/abdellatif/kernel/arch/arm/mach-omap2/include" 'c-mode)
(semantic-add-system-include "/home/abdellatif/kernel/arch/arm/plat-omap/include" 'c-mode)
(add-to-list 'semantic-lex-c-preprocessor-symbol-file "/home/abdellatif/kernel/include/generated/autoconf.h")
(add-to-list 'semantic-lex-c-preprocessor-symbol-map '("__KERNEL__" . ""))
(add-to-list 'semantic-lex-c-preprocessor-symbol-map '("__LINUX_ARM_ARCH__" . "7"))
(add-to-list 'semantic-lex-c-preprocessor-symbol-map '("KBUILD_STR(s)" . "#s"))
(add-to-list 'semantic-lex-c-preprocessor-symbol-map '("KBUILD_BASENAME" . "KBUILD_STR(main)"))
(add-to-list 'semantic-lex-c-preprocessor-symbol-map '("KBUILD_MODNAME" . "KBUILD_STR(main)"))

;semantic integration with imenu (display of a menu with a list of functions, variables, and other tags)
(defun my-semantic-hook ()
  (imenu-add-to-menubar "TAGS"))
(add-hook 'semantic-init-hooks 'my-semantic-hook)

;names completion (semantic commands)
(defun my-cedet-hook ()
 (local-set-key [(control return)] 'semantic-ia-complete-symbol)
  (local-set-key "\C-x " 'semantic-ia-complete-symbol-menu)
   (local-set-key "\C-x\r" 'gtags-find-tag-from-here))
(add-hook 'c-mode-common-hook 'my-cedet-hook)

;navigating in source code
(semantic-mru-bookmark-mode 1)

;tell semantic to store its tags database between sessions here
(require 'semanticdb)
(setq-default semanticdb-default-save-directory "~/.semantic.cache")
(setq-default semanticdb-default-system-save-directory "~/.semantic.cache")

;allow Semanticdb use databases generated by global(gtags)
(require 'semanticdb-global)
(semanticdb-enable-gnu-global-databases 'c-mode)

I am trying to use Emacs as an IDE in my Linux kernel development projects. So I installed CEDET and GNU Global (gtags) with no problems.

When I open a C source code belonging to my Linux kernel source folder, the linux project is detected automatically. Functions, variables, headers and other keywords are highlighted correctly.

Through the ~/.emacs file I configured semantic's code completion and intellisense. So when I press C- the code completion menu appears and I can choose what's needed.

The issue is when I use preprocessor defines, the intellisense system shows the menu on the current location but doesn't detect the right things when invoked (C-). Even I close and restart emacs the same behavior is reproduced.

The test code is:

//----------------------------------------------------------------------------------------

typedef struct
{
     int p1;

        #ifdef __KERNEL__

            int p2;

        #endif

        #ifdef USE1

             char p3;

          #endif

#ifdef __LINUX_ARM_ARCH__

   int p4;

#endif

}OBJ;

OBJ g_obj;

//----------------------------------------------------------------------------------------

KERNEL , USE1 and LINUX_ARM_ARCH are already defined as follows:

__KERNEL__ : (add-to-list 'semantic-lex-c-preprocessor-symbol-map '("__KERNEL__" . "")) in .emacs
USE1 : defined in a header file included by the current C source file.
__LINUX_ARM_ARCH__ : (add-to-list 'semantic-lex-c-preprocessor-symbol-file "/home/abdellatif/kernel/include/generated/autoconf.h") in .emacs

In the .emacs file I also added all the needed includes and defines required by GCC cross compiler (GCC for ARM) to build the kernel project.

I also tried to force semantic to use GCC cross compiler command line in place of the system GCC to preprocess correctly but haven't found how to do that.

The "M-x semantic-c-describe-environment" command shows the right include paths and defines
(set in .emacs). But the system GCC defines are also listed.

The .emacs file and the software versions are shown below.

Any help or indication is welcomed :)
Best regards

############# Software versions #############

- Emacs (23.2.1)
- Ubuntu machine (natty, 11.04)
- Cedet 1.0 (from http://cedet.sourceforge.net/)
############# .emacs #############

;load CEDET

(load-file "~/Documents/my_emacs/cedet-1.0/common/cedet.el")

(require 'ede)

(global-ede-mode t)
; turn on which-func support (plus all other code helpers)
(semantic-load-enable-gaudy-code-helpers)
(semantic-load-enable-excessive-code-helpers)

; turn on all "useful" features
(setq semantic-load-turn-useful-things-on t)

(setq-mode-local c-mode
         semanticdb-find-default-throttle
         '(project unloaded system recursive))

;init names completion, and displaying of information for tags & classes
(require 'semantic-ia)

;preprocessing of source code

(require 'semantic-c)

(semantic-reset-system-include 'c-mode)
(semantic-reset-system-include 'c++-mode)
(semantic-add-system-include "/home/abdellatif/toolchain/arm-eabi-4.4.3/lib/gcc/arm-eabi/4.4.3/include" 'c-mode)
(semantic-add-system-include "/home/abdellatif/kernel/arch/arm/include" 'c-mode)
(semantic-add-system-include "/home/abdellatif/kernel/include" 'c-mode)
(semantic-add-system-include "/home/abdellatif/kernel/arch/arm/mach-omap2/include" 'c-mode)
(semantic-add-system-include "/home/abdellatif/kernel/arch/arm/plat-omap/include" 'c-mode)
(add-to-list 'semantic-lex-c-preprocessor-symbol-file "/home/abdellatif/kernel/include/generated/autoconf.h")
(add-to-list 'semantic-lex-c-preprocessor-symbol-map '("__KERNEL__" . ""))
(add-to-list 'semantic-lex-c-preprocessor-symbol-map '("__LINUX_ARM_ARCH__" . "7"))
(add-to-list 'semantic-lex-c-preprocessor-symbol-map '("KBUILD_STR(s)" . "#s"))
(add-to-list 'semantic-lex-c-preprocessor-symbol-map '("KBUILD_BASENAME" . "KBUILD_STR(main)"))
(add-to-list 'semantic-lex-c-preprocessor-symbol-map '("KBUILD_MODNAME" . "KBUILD_STR(main)"))

;semantic integration with imenu (display of a menu with a list of functions, variables, and other tags)
(defun my-semantic-hook ()
  (imenu-add-to-menubar "TAGS"))
(add-hook 'semantic-init-hooks 'my-semantic-hook)

;names completion (semantic commands)
(defun my-cedet-hook ()
 (local-set-key [(control return)] 'semantic-ia-complete-symbol)
  (local-set-key "\C-x " 'semantic-ia-complete-symbol-menu)
   (local-set-key "\C-x\r" 'gtags-find-tag-from-here))
(add-hook 'c-mode-common-hook 'my-cedet-hook)

;navigating in source code
(semantic-mru-bookmark-mode 1)

;tell semantic to store its tags database between sessions here
(require 'semanticdb)
(setq-default semanticdb-default-save-directory "~/.semantic.cache")
(setq-default semanticdb-default-system-save-directory "~/.semantic.cache")

;allow Semanticdb use databases generated by global(gtags)
(require 'semanticdb-global)
(semanticdb-enable-gnu-global-databases 'c-mode)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

再浓的妆也掩不了殇 2024-12-10 01:15:26
  1. 你应该从主线获得 cedet:
    bzr://cedet.bzr.sourceforge.net/bzrroot/cedet/code/trunk/
    最近对预处理器处理进行了一些修复。

  2. 更改semantic-lex-c-preprocessor-symbol-file 和类似文件的列表后
    的东西,你应该删除语义缓存并允许它从头开始重新创建它。

  1. You should get cedet from mainline:
    bzr://cedet.bzr.sourceforge.net/bzrroot/cedet/code/trunk/
    there were several fixes for preprocessor handling recently.

  2. After changing the list of semantic-lex-c-preprocessor-symbol-file and similar
    stuff, you should remove semantic cache and allow it recreate it from scratch.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文