llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
29.33k stars 12.12k forks source link

Are the tutorials up-to-date? (Clang version is 20.0.0git) #114711

Open bobhairgrove opened 3 weeks ago

bobhairgrove commented 3 weeks ago

Nothing written here seems to work for me: https://clang.llvm.org/docs/LibASTMatchersTutorial.html

In my previous post, I describe my frustrating experience building LLVM and Clang from latest sources. After having finally successfully built and installed everything after following a different tutorial found on the LLVM homepage, I went back to the LibASTMatcher tutorial to see if I could continue with the steps starting after the ninja install command (which should be sudo ninja install).

I didn't have ccmake installed (had to run sudo apt install cmake-curses-gui), so I did that. But I could not figure out where I needed to run this from since the tutorial installed everything immediately under $HOME and assumes that whoever is trying to use this tutorial would do the same. However, it seems that the step mentioned is to use clang to (re)compile LLVM using Clang, which isn't really necessary.

Then I went to this tutorial and tried to compile the very first example (using TEST() and EXPECT_TRUE(). It doesn't compile, unfortunately, but nothing written there gives me a hint about what is going on. Compiling with g++ should work, but here is the output:

// contents of "main.cpp":
#include "clang/Tooling/Tooling.h"

TEST(runToolOnCode, CanSyntaxCheckCode) {
  // runToolOnCode returns whether the action was correctly run over the
  // given code.
  EXPECT_TRUE(runToolOnCode(std::make_unique<clang::SyntaxOnlyAction>(), "class X {};"));
}

Output:

bob@bobs-laptop:~/code/LibToolingProject$ g++ -c main.cpp
main.cpp:3:5: error: expected constructor, destructor, or type conversion before ‘(’ token
    3 | TEST(runToolOnCode, CanSyntaxCheckCode) {
      |     ^

I know that TEST and EXPECT_TRUE are macros defined by Google test framework, but assume that they are somehow pulled in by the Tooling.h header. But perhaps not?

Why do the tutorial writers assume that certain tools are already installed without even mentioning the dependencies? Since all of these tutorials are located on the official LLVM and Clang websites, one would assume that they would be better organized and tested...

efriedma-quic commented 3 weeks ago

There are some in-tree users of Tooling.h in clang-tools-extra you can use as reference (which are known to build because they're part of normal builds).

I agree it probably doesn't make sense to bootstrap clang in the directions in LibASTMatchersTutorial.rst . You could, anyway, but the gap between a random system compiler and a newly built clang is much smaller than it used to be.

It's very easy for the tutorial files to fall out of date, especially when the tutorial doesn't correspond to some in-tree code. The files in question are clang/docs/LibASTMatchersTutorial.rst and clang/docs/LibTooling.rst in the LLVM tree; patches to clarify would be welcome.

llvmbot commented 3 weeks ago

Hi!

This issue may be a good introductory issue for people new to working on LLVM. If you would like to work on this issue, your first steps are:

  1. Check that no other contributor has already been assigned to this issue. If you believe that no one is actually working on it despite an assignment, ping the person. After one week without a response, the assignee may be changed.
  2. In the comments of this issue, request for it to be assigned to you, or just create a pull request after following the steps below. Mention this issue in the description of the pull request.
  3. Fix the issue locally.
  4. Run the test suite locally. Remember that the subdirectories under test/ create fine-grained testing targets, so you can e.g. use make check-clang-ast to only run Clang's AST tests.
  5. Create a Git commit.
  6. Run git clang-format HEAD~1 to format your changes.
  7. Open a pull request to the upstream repository on GitHub. Detailed instructions can be found in GitHub's documentation. Mention this issue in the description of the pull request.

If you have any further questions about this issue, don't hesitate to ask via a comment in the thread below.

llvmbot commented 3 weeks ago

@llvm/issue-subscribers-good-first-issue

Author: None (bobhairgrove)

Nothing written here seems to work for me: https://clang.llvm.org/docs/LibASTMatchersTutorial.html In my [previous post](https://github.com/llvm/llvm-project/issues/114498#issue-2628174550), I describe my frustrating experience building LLVM and Clang from latest sources. After having finally successfully built and installed everything after following a [different tutorial](https://llvm.org/docs/GettingStarted.html#getting-the-source-code-and-building-llvm) found on the LLVM homepage, I went back to the LibASTMatcher tutorial to see if I could continue with the steps starting after the `ninja install` command (which should be `sudo ninja install`). I didn't have `ccmake` installed (had to run `sudo apt install cmake-curses-gui`), so I did that. But I could not figure out where I needed to run this from since the tutorial installed everything immediately under `$HOME` and assumes that whoever is trying to use this tutorial would do the same. However, it seems that the step mentioned is to use `clang` to (re)compile LLVM using Clang, which isn't really necessary. Then I went to [this tutorial](https://clang.llvm.org/docs/LibTooling.html) and tried to compile the very first example (using `TEST()` and `EXPECT_TRUE()`. It doesn't compile, unfortunately, but nothing written there gives me a hint about what is going on. Compiling with `g++` should work, but here is the output: ``` // contents of "main.cpp": #include "clang/Tooling/Tooling.h" TEST(runToolOnCode, CanSyntaxCheckCode) { // runToolOnCode returns whether the action was correctly run over the // given code. EXPECT_TRUE(runToolOnCode(std::make_unique<clang::SyntaxOnlyAction>(), "class X {};")); } ``` Output: ``` bob@bobs-laptop:~/code/LibToolingProject$ g++ -c main.cpp main.cpp:3:5: error: expected constructor, destructor, or type conversion before ‘(’ token 3 | TEST(runToolOnCode, CanSyntaxCheckCode) { | ^ ``` I know that `TEST` and `EXPECT_TRUE` are macros defined by Google test framework, but assume that they are somehow pulled in by the `Tooling.h` header. But perhaps not? Why do the tutorial writers assume that certain tools are already installed without even mentioning the dependencies? Since all of these tutorials are located on the official LLVM and Clang websites, one would assume that they would be better organized and tested...
shrey1605 commented 2 weeks ago

Hi, I would like to work on this. Can someone please assign this to me? Thank you.