CLion fails to index macros

Answered

I am trying to write a Linux kernel module with CLion. This is the cmake file:

cmake_minimum_required(VERSION 3.5)
project(labs)

set(KERNEL_HEADERS
/home/alex/Developer/linux/include
/home/alex/Developer/linux/arch/x86/include
/home/alex/Developer/linux/arch/x86/include/generated
/home/alex/Developer/linux/include/uapi
/home/alex/Developer/linux/include/generated/uapi
/home/alex/Developer/linux/arch/x86/include/uapi
/home/alex/Developer/linux/arch/x86/include/generated/uapi
)

set(MY_MODULE_SOURCES
chapter_03/lab_01/hello.c
)

add_definitions(-imacros /home/alex/Developer/linux/include/linux/kconfig.h)
add_definitions(-D__KERNEL__)
add_definitions(-DMODULE)
add_definitions(-std=gnu89)
include_directories(${KERNEL_HEADERS})


add_custom_target(labs COMMAND $(MAKE) -C ${labs_SOURCE_DIR}
PWD=${labs_SOURCE_DIR})

add_library(dummylib ${MY_MODULE_SOURCES})

The actual building of the kernel module is done with the externally called makefile using "add_custom_target". The "dummylib" is only there so that CLion actually starts to parse the header files and gives me auto completion. With my supplied definitions it does even compile the "dummylib" successfully (look at the screenshot). It is no kernel module though, but that does not matter ;)

My problem is the error you see in the screenshot. Somehow it says that it can't resolve all the macros defined in the kernel headers. Functions, structs and plain defines ( "MODULE_SIG_STRING ") do work (as you see). I do not understand why the editor says it cannot resolve the macro but can still build it. What is more strange is that I can even jump to the declaration using STRG+B of the marked macros. Clearly something is going wrong. The macros are really defined within linux/module.h

Do you have any idea how to resolve this Situation?

 

2
7 comments

clion parses files differently from compiler, so this problems are excepted (compiler understand something clion don't)

The rest of comment is not a fix, but a check of what clion thinks about that macro:

ensure that INCLUDE_DIRECTORIES on cmake adds that headers (for me it fails to do so).

A quick fix to see what's happening underneath is to add:

#include "/usr/lib/modules/4.5.4-1-ARCH/build/include/linux/module.h"
#include "/usr/lib/modules/4.5.4-1-ARCH/build/include/linux/moduleparam.h"

or whatever your path is.

Maybe at that point clion resolves macro correctly. Then you're fixing your file to work with your IDE (bad thing)

If not, but problem change, as happens to me:

"Error after macro substitution, Instantiating an unknown structure without a reference"


Install psiviewer

https://plugins.jetbrains.com/plugin/227

 and check what clion thinks your macro is.

Looking for MODULE_LICENSE it finds it is:

#define MODULE_LICENSE(_license) MODULE_INFO(license, _license)

http://lxr.free-electrons.com/source/include/linux/module.h#L209

then MODULE_INFO

#define MODULE_INFO(tag, info) __MODULE_INFO(tag, tag, info)

http://lxr.free-electrons.com/source/include/linux/module.h#L171

then __MODULE_INFO

#define __MODULE_INFO(tag, name, info) 

http://lxr.free-electrons.com/source/include/linux/moduleparam.h#L20

For me psiviewer says it is a Psicomment.

Seems clion has tons of parse errors like this, we must wait

2
Avatar
Alexander Theißen

Thank you for your response.

The includes did not change anything for me. It already had the correct includes.

What makes the thing work is switching from -std=gnu89 to -std=c89 which makes the build fail but the IDE behave somewhat correct. I also tested with PsiViewer and the result is that even though the IDE behaves differently, the PSIViewer both times says it is a define. I dumped the psi of module.h once with each -std setting. There are some differences but I do not understand all of this. I just leave this here. I also made a bug report about this: https://youtrack.jetbrains.com/issue/CPP-6875#u=1464800829798 

This is where you can find the Psi dumps (and diff them).

1

Since you provide a scce. It was easy to reply.

Without changing -std see clion don't complain if macros are used inside a function

void foo() {
MODULE_LICENSE("GPL");
MODULE_AUTHOR("bla bla");
MODULE_DESCRIPTION("bla bla bla");
}

 

1

I'm having the exact same issue as Alexander. Do you have any progress on this?

Thank you in advance.

1

I would be glad about a solution, too!

1

I will also request a solution to this.  Without _POSIX defined, I have numerous files with falsely reported undefined symbols, for example.  What a hassle.  My IDE is supposed to clarify where my problems are, not obfuscate them.

1

Hi all!

I kindly ask you to comment or upvote the issue created by Alexander: https://youtrack.jetbrains.com/issue/CPP-6875. Please follow it to get updates.

0

Please sign in to leave a comment.