Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to compile with these clang tools? #33

Open
tylzh97 opened this issue May 29, 2024 · 5 comments
Open

How to compile with these clang tools? #33

tylzh97 opened this issue May 29, 2024 · 5 comments

Comments

@tylzh97
Copy link

tylzh97 commented May 29, 2024

Thank you very much for this anazing project. I've successfully run all the examples. But I still have some confusion. How should I use these tools to compile a real project?

For example, I want to use libCodeStyleChecker.so into a real compilation process of a large project. Normally, I can do this by modifying the system environment variable CC, such as:

CC="clang -cc1 -load /path/to/libCodeStyleChecker.so -plugin CSC " make

But I can't get any output files. Further more, I made the following attempt:

clang -cc1 -fcolor-diagnostics -load ./libCodeStyleChecker.so -plugin CSC ../../test/CodeStyleCheckerFunction.cpp -o test.o
../../test/CodeStyleCheckerFunction.cpp:6:6: warning: Function names should start with lower-case letter
void ClangTutorFuncBad();
     ^~
     clangTutorFuncBad
../../test/CodeStyleCheckerFunction.cpp:12:8: warning: Function names should start with lower-case letter
  void ClangTutorMemberMethodBad();
       ^~
       clangTutorMemberMethodBad
2 warnings generated.

I got the following result:

# xxx @ XXX in ~/Workspace/Testspace/clang-ast/clang-tutor/build/lib on git:main x 15:51:11 
$ ls
CMakeFiles           libCodeRefactor.so      libHelloWorld.so   libObfuscator.so        Makefile
cmake_install.cmake  libCodeStyleChecker.so  libLACommenter.so  libUnusedForLoopVar.so

As we can see that the -o option I entered did not work, and I couldn't compile any cpp files.

Please tell me what should I do to be able to compile a large project while using clang plugins?

@banach-space
Copy link
Owner

Thank you very much for this anazing project.

Thank you for your kind words!

How should I use these tools to compile a real project?

Sorry, but I won't be able to help. I've not worked with Clang for a few years now and I'd need to spend some time trying this myself. Sadly I don't have the spare cycles to try that :(

@tylzh97
Copy link
Author

tylzh97 commented Jun 2, 2024

Thank you very much for this anazing project.

Thank you for your kind words!

How should I use these tools to compile a real project?

Sorry, but I won't be able to help. I've not worked with Clang for a few years now and I'd need to spend some time trying this myself. Sadly I don't have the spare cycles to try that :(

Thank you very much for your reply. I am currently studying llvm/clang, and I hope to keep the normal compilation process and use llvm/clang for code analysis during the compilation process. I've successfully used llvm pass to process its IR code during the compilation process without destroying the compilation process(and ofcourse get output file). However, sometimes my analysis needs to use information in the source code (not information from LLVM-IR). I'm trying to useclang plugins to solve it. However I can only get intermediate analysis results, but I can't get output files normally(I've already tried adding the -o option and consulted a lot of information). This would interrupt the compilation process of some projects, causing me to only get incomplete analysis results from a portion of the sources files.

To meet my needs, a possible solution might be to use tools like rizsotto/Bear to compile the project and obtain the compile_commands.json compilation process file. Then, the compilation information in the file would be manually combined, and clang would be modified to parameters similar to clang -cc1 -load /path/to/libCodeStyleChecker.so -plugin CSC to obtain the entire compilation analysis result. (However, I don't particularly like this workflow)

I am not sure whether the clang plugin provides capabilities similar to llvm pass to analyze or modify code during the compilation process. I will continue to try something new. If I have a corresponding solution, I will reply here and pull the specific examples to this project :)

Finally, I would like to express my gratitude to you once again!

@banach-space
Copy link
Owner

I am not sure whether the clang plugin provides capabilities similar to llvm pass to analyze or modify code during the compilation process

This makes me realise ... Running a plugin in Clang means running a specific "frontend action". Clang allows only one "action" at a time and it sounds like you are trying to run two actions - sadly that's not supported.

Hope this helps :)

@tylzh97
Copy link
Author

tylzh97 commented Jun 4, 2024

Clang allows only one "action" at a time

Sorry I don't quite understand what you mean.

After our last communication, I made some more attempts, and I believe I have achieved my requirements. I can now compile as follows:

$ clang -fplugin=../lib/libCodeStyleChecker.so -o test.o -c ../../test/LACFloat.cpp 
../../test/LACFloat.cpp:7:16: warning: Type and variable names should start with upper-case letter
void foo(float some_arg);
               ^~~~~~~~~
               Some_arg
../../test/LACFloat.cpp:7:20: warning: `_` in names is not allowed
void foo(float some_arg);
               ~~~~^~~~~
               somearg
2 warnings generated.

$ nm test.o                                                                                 
0000000000000000 r .LCPI0_0
0000000000000000 T _Z3barv
                 U _Z3foof

It seems that I have successfully compiled this file and successfully used the CodeStyleChecker tool to give me hints during the compilation process.

Furthermore, I tried to compile a real project, ffmpeg, in this way, and successfully got an executable file.

$ ./configure --prefix=../build --cc="clang -fplugin=/home/waji/Workspace/Testspace/clang-ast/clang-tutor/build/lib/libCodeStyleChecker.so" --cxx="clang -fplugin=/home/waji/Workspace/Testspace/clang-ast/clang-tutor/build/lib/libCodeStyleChecker.so" --disable-asm
$ make

$ ./ffmpeg -version
...
libswscale/output.c:2175:52: warning: Type and variable names should start with upper-case letter
                                      yuv2anyX_fn *yuv2anyX)
                                                   ^~~~~~~~~
                                                   Yuv2anyX
libswscale/output.c:2177:24: warning: Type and variable names should start with upper-case letter
    enum AVPixelFormat dstFormat = c->dstFormat;
                       ^~~~~~~~~
                       DstFormat
libswscale/output.c:2178:31: warning: Type and variable names should start with upper-case letter
    const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(dstFormat);
                              ^~~~
                              Desc
3414 warnings generated.
AR      libswscale/libswscale.a
LD      ffmpeg_g
LD      ffprobe_g
LD      ffserver_g
CP      ffserver
STRIP   ffserver
CP      ffprobe
CP      ffmpeg
STRIP   ffprobe
STRIP   ffmpeg

$ ./ffmpeg -version
ffmpeg version n3.2.1 Copyright (c) 2000-2016 the FFmpeg developers
built with clang version 12.0.1 (https://gitee.com/mirrors/LLVM.git fed41342a82f5a3a9201819a82bf7a48313e296b)
configuration: --prefix=../build --cc='clang -fplugin=/home/waji/Workspace/Testspace/clang-ast/clang-tutor/build/lib/libCodeStyleChecker.so' --cxx='clang -fplugin=/home/waji/Workspace/Testspace/clang-ast/clang-tutor/build/lib/libCodeStyleChecker.so' --disable-asm
libavutil      55. 34.100 / 55. 34.100
libavcodec     57. 64.101 / 57. 64.101
libavformat    57. 56.100 / 57. 56.100
libavdevice    57.  1.100 / 57.  1.100
libavfilter     6. 65.100 /  6. 65.100
libswscale      4.  2.100 /  4.  2.100
libswresample   2.  3.100 /  2.  3.100

It can be seen that I have successfully compiled ffmpeg (although the compilation process has significantly slowed down), and obtained a binary program that can be executed normally. This meets my needs:

I hope to keep the normal compilation process and use llvm/clang for code analysis during the compilation process.

My modification is very simple. On the official website of the Clang Plugin, I found the answer. You can automatically add a plugin in the public PluginASTAction by overriding the getActionType function:

// Automatically run the plugin after the main AST action
PluginASTAction::ActionType getActionType() override {
  return AddAfterMainAction;
}

PR in #34

@tylzh97
Copy link
Author

tylzh97 commented Jun 5, 2024

I found something new:

$ clang -cc1 -help | grep "plugin"
  -add-plugin <name>      Use the named plugin action in addition to the default action
  -fpass-plugin=<dsopath> Load pass plugin from a dynamic shared object file (only with new pass manager).
  -load <dsopath>         Load the named plugin (dynamic shared object)
  -plugin-arg-<name> <arg>
                          Pass <arg> to plugin <name>
  -plugin <name>          Use the named plugin action instead of the default action (use "help" to list available options)

When we passing plugin with -plugin, clang will replace the default compile action to our specify action. But we can use plugin with -add-plugin, which would keep the default action and add the specify action following the getActionType() return.

So, I can make CodeStyleChecker during my compile process, like following (without change any codes in CodeStyleChecker.cpp):

clang -Xclang -load -Xclang lib/libCodeStyleChecker.so -Xclang -add-plugin -Xclang CSC -o 1.o -c ../test/CodeStyleCheckerFunction.cpp

I'm now study how to write test suits with lit (and add some docs)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants