Solved Clang plugin, CMake and _types.h error

Good afternoon folks!

I am trying to compile this c source file while performing some CLANG frontend action with a CLANG plugin.

My source code (test.c) includes a header file (section_insertion.h). This header file includes stdio.h because I use some size_t variables inside some structs

test.c

C:
// Filename: test.c

#include "section_insertion.h"
void PAYLOAD();

int main(int argc, char *argv[]) {
    return 0;
}

section_insertion.h

C:
//Filename: section_insertion.h

#include <stdio.h>

typedef struct {
    int num_a;
    int num_b;
    char caracter;
    size_t int_size;
} Payload_A;

Part 1: Building with base CLANG/LLVM.

My base CLANG/LLVM version is the following:
Bash:
$ clang -v
FreeBSD clang version 11.0.1 (git@github.com:llvm/llvm-project.git llvmorg-11.0.1-0-g43ff75f2c3fe)
Target: x86_64-unknown-freebsd13.0
Thread model: posix
InstalledDir: /usr/bin

Then, I define the env variables:

Bash:
$ export Clang_DIR=/usr/local/llvm11
$ export Clang_DIR=/usr/bin/clang

needed for the CMake script

Bash:
$ cat CMakeLists.txt
cmake_minimum_required(VERSION 3.4.3)
project(clang-tutor-rename-plugin)

#===============================================================================
# 1. LOAD LLVM CONFIGURATION
#===============================================================================
# Set this to a valid LLVM installation dir
set(CT_Clang_INSTALL_DIR "" CACHE PATH "LLVM installation directory")

# Add the location of ClangConfig.cmake to CMake search paths (so that
# find_package can locate it)
list(APPEND CMAKE_PREFIX_PATH "${CT_Clang_INSTALL_DIR}/lib/cmake/clang/")

find_package(Clang REQUIRED CONFIG)

# HelloWorld includes headers from LLVM and Clang - update the include paths
# accordingly
include_directories(SYSTEM "${LLVM_INCLUDE_DIRS};${CLANG_INCLUDE_DIRS}")

#===============================================================================
# 2. LLVM-TUTOR BUILD CONFIGURATION
#===============================================================================
# Use the same C++ standard as LLVM does
set(CMAKE_CXX_STANDARD 14 CACHE STRING "")

# LLVM is normally built without RTTI. Be consistent with that.
if(NOT LLVM_ENABLE_RTTI)
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-rttic -shared")
endif()

#===============================================================================
# 3. ADD THE TARGET
#===============================================================================
add_library(RenameMacro SHARED RenameMacro.cpp)

# Allow undefined symbols in shared objects on Darwin (this is the default
# behaviour on Linux)
target_link_libraries(RenameMacro
  "$<$<PLATFORM_ID:Darwin>:-undefined dynamic_lookup>")

set(CMAKE_VERBOSE_MAKEFILE on)

Then, building it using the base compiler warns me about not finding the ClangConfig.cmake file

Bash:
$ cmake -DCT_Clang_INSTALL_DIR=$Clang_DIR $CLANG_TUTOR_DIR
-- The C compiler identification is Clang 11.0.1
-- The CXX compiler identification is Clang 11.0.1
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
CMake Error at CMakeLists.txt:14 (find_package):
  Could not find a package configuration file provided by "Clang" with any of
  the following names:
    ClangConfig.cmake
    clang-config.cmake
  Add the installation prefix of "Clang" to CMAKE_PREFIX_PATH or set
  "Clang_DIR" to a directory containing one of the above files.  If "Clang"
  provides a separate development package or SDK, be sure it has been
  installed.

-- Configuring incomplete, errors occurred!

Using the find command doesn't retrieve any file

Bash:
find -name  ClangConfig.cmake

Part 2: Building with pkg llvm11 version

Installing llvm11 via pkg and checking the installed version
Bash:
$ pkg install llvm11

$ clang11 -v
clang version 11.0.1
Target: x86_64-portbld-freebsd13.0
Thread model: posix
InstalledDir: /usr/local/llvm11/bin

Now, I can find the .cmake file, So, I set the path to it and build.

Bash:
$ find -name  ClangConfig.cmake
/usr/local/llvm11/lib/cmake/clang/ClangConfig.cmake

$ export Clang_DIR=/usr/local/llvm11

$ cmake -DCT_Clang_INSTALL_DIR=$Clang_DIR $CLANG_TUTOR_DIR
-- Configuring done
-- Generating done
-- Build files have been written to: /home/freebsd/pluginClang/build

Part 3: Compile the clang plugin

Bash:
$ make
make
/usr/local/bin/cmake -S/home/freebsd/pluginClang -B/home/freebsd/pluginClang/build --check-build-system CMakeFiles/Makefile.cmake 0
/usr/local/bin/cmake -E cmake_progress_start /home/freebsd/pluginClang/build/CMakeFiles /home/freebsd/pluginClang/build//CMakeFiles/progress.marks
make  -f CMakeFiles/Makefile2 all
make  -f CMakeFiles/RenameMacro.dir/build.make CMakeFiles/RenameMacro.dir/depend
cd /home/freebsd/pluginClang/build && /usr/local/bin/cmake -E cmake_depends "Unix Makefiles" /home/freebsd/pluginClang /home/freebsd/pluginClang /home/freebsd/pluginClang/build /home/freebsd/pluginClang/build /home/freebsd/pluginClang/build/CMakeFiles/RenameMacro.dir/DependInfo.cmake --color=
make  -f CMakeFiles/RenameMacro.dir/build.make CMakeFiles/RenameMacro.dir/build
[ 50%] Building CXX object CMakeFiles/RenameMacro.dir/RenameMacro.cpp.o
/usr/bin/c++ -DRenameMacro_EXPORTS -isystem /usr/local/llvm11/include -fPIC -MD -MT CMakeFiles/RenameMacro.dir/RenameMacro.cpp.o -MF CMakeFiles/RenameMacro.dir/RenameMacro.cpp.o.d -o CMakeFiles/RenameMacro.dir/RenameMacro.cpp.o -c /home/freebsd/pluginClang/RenameMacro.cpp
[100%] Linking CXX shared library libRenameMacro.so
/usr/local/bin/cmake -E cmake_link_script CMakeFiles/RenameMacro.dir/link.txt --verbose=1
/usr/bin/c++ -fPIC -shared -Wl,-soname,libRenameMacro.so -o libRenameMacro.so CMakeFiles/RenameMacro.dir/RenameMacro.cpp.o
[100%] Built target RenameMacro
/usr/local/bin/cmake -E cmake_progress_start /home/freebsd/pluginClang/build/CMakeFiles 0

Part 4: Compile the source code using the clang plugin

Bash:
$ ~/pluginClang/build $ ls
CMakeCache.txt          Makefile                libRenameMacro.so       test.c
CMakeFiles              cmake_install.cmake     section_insertion.h
Compiling the source code using Clang/LLVM with the plugin, throws me four errors. HOWEVER, it generates the output file into /tmp/test.c

Bash:
$ clang11 cc1 -load ./libRenameMacro.so -plugin -rename-plugin test.c -v
clang -cc1 version 11.0.1 based upon LLVM 11.0.1 default target x86_64-portbld-freebsd13.0
#include "..." search starts here:
#include <...> search starts here:
 /usr/local/llvm11/lib/clang/11.0.1/include
 /usr/include
End of search list.
In file included from test.c:1:
In file included from ./section_insertion.h:4:
In file included from /usr/include/stdio.h:43:
/usr/include/sys/_types.h:107:24: error: expected ';' at end of declaration list
        long long __max_align1 __aligned(_Alignof(long long));
                              ^
/usr/include/sys/_types.h:109:26: error: expected ';' at end of declaration list
        long double __max_align2 __aligned(_Alignof(long double));
                                ^
/usr/include/sys/_types.h:135:2: error: "No support for your compiler for stdargs"
#error "No support for your compiler for stdargs"
 ^
In file included from test.c:1:
In file included from ./section_insertion.h:4:
/usr/include/stdio.h:77:9: error: unknown type name '__va_list'
typedef __va_list       va_list;
        ^
encontro func
Output file created - /tmp/test.c
4 errors generated.

Using the -D __GNUCLIKE_BUILTIN_VARARGS flag, allows me to supress two errors but there are two left.

Bash:
$ clang11 -cc1 -load ./libRenameMacro.so -plugin -rename-plugin test.c -D __GNUCLIKE_BUILTIN_VARARGS -v
clang -cc1 version 11.0.1 based upon LLVM 11.0.1 default target x86_64-portbld-freebsd13.0
#include "..." search starts here:
#include <...> search starts here:
 /usr/local/llvm11/lib/clang/11.0.1/include
 /usr/include
End of search list.
In file included from test.c:1:
In file included from ./section_insertion.h:4:
In file included from /usr/include/stdio.h:43:
/usr/include/sys/_types.h:107:24: error: expected ';' at end of declaration list
        long long __max_align1 __aligned(_Alignof(long long));
                              ^
/usr/include/sys/_types.h:109:26: error: expected ';' at end of declaration list
        long double __max_align2 __aligned(_Alignof(long double));
                                ^
encontro func
Output file created - /tmp/test.c
2 errors generated.

The error is in /usr/include/sys/_types.h. The relevant lines on that header are:

C:
$ cat /usr/include/sys/_types.h

typedef struct {
        long long __max_align1 __aligned(_Alignof(long long));
#ifndef _STANDALONE
        long double __max_align2 __aligned(_Alignof(long double));
#endif
} __max_align_t;

Although the output file is generated at /tmp/test.c, I am looking to solve those errors.
Does anybody know what I am doing wrong?

I also built the same plugin and test in Ubuntu and no errors were generated.
Thanks for reading and for the help in advance.


EDIT 1: Added test.c code
EDIT 2: Added section_insertion.h code
 
For starters you made a very detailed question and then given a brief, heavily edited test.c from which I can make no possible conclusion.
One can only guess. But you have issues in your cmake (code around -fno_rtti which is more for embedded use) but it's not activated anyway.
I think you need to show the code generating the error?
 
For starters you made a very detailed question and then given a brief, heavily edited test.c from which I can make no possible conclusion.
One can only guess. But you have issues in your cmake (code around -fno_rtti which is more for embedded use) but it's not activated anyway.
I think you need to show the code generating the error?

mark_j Hey! Sorry! I forgot to add test.c code

Here it goes my test.c code

C:
// test.c
#include "section_insertion.h"
void PAYLOAD();

int main(int argc, char *argv[]) {
    return 0;
}

I just added it to the first message too.

Thanks Mark!
 
Your error stems from "In file included from ./section_insertion.h:4:" but you don't show section_insertion.h or is your code commented with "//section_header.h" actually section_insertion.h?

Anyway, perhaps your issue is utilising -cc1? The documentation is explicit here:
Directly call the parsing process by using the -cc1 option; this has the downside of not configuring the default header search paths, so you’ll need to specify the full system path configuration on the command line.

Perhaps you could try --nostdlibinc?

Disclaimer: I've not done plugins since, oh, Clang 6?
Personal View: Their API was unstable then, I see no evidence that's changed but this is irrelevant to your question.
 
Your error stems from "In file included from ./section_insertion.h:4:" but you don't show section_insertion.h or is your code commented with "//section_header.h" actually section_insertion.h?

Anyway, perhaps your issue is utilising -cc1? The documentation is explicit here:


Perhaps you could try --nostdlibinc?

Disclaimer: I've not done plugins since, oh, Clang 6?
Personal View: Their API was unstable then, I see no evidence that's changed but this is irrelevant to your question.
mark_j Again, sorry for not including the full source and header files. I updated them on top.

As you guided and correctly mentioned, I switched from using -cc1 to -Xclang as you cited from the Docs: Running the plugin - Using the cc1 command line
Then, I added -c flag as it explains in Docs: I run clang -cc1 ... and get weird errors about missing headers to only run the preprocess, compile, and assemble steps.

The correct compilation command is:

Code:
$ clang11 -Xclang -load  -Xclang ./libRenameMacro.so -Xclang -plugin -Xclang PPSplugin -c ../src/test.c -v

st.o -x c ../src/test.c

clang -cc1 version 11.0.1 based upon LLVM 11.0.1 default target x86_64-portbld-freebsd13.0
#include "..." search starts here:
#include <...> search starts here:
     /usr/local/llvm11/lib/clang/11.0.1/include
     /usr/include

End of search list.
encontro func
Output file created - /tmp/test.c

No errors generated. I could get rid of the 4 errors now so I didn't need to add the D __GNUCLIKE_BUILTIN_VARARGS flag.

Thank you very much Mark! I appreciate your help! Have a great week!
 
Back
Top