Coder Social home page Coder Social logo

clang-tutorial's Introduction

About

This is a collection of tutorials showing off how to use core Clang types. It is based directly on two older tutorials which no longer built due to code rot.

  1. tutorial 1 by Nico Weber - 9/28/2008
  2. tutorial 2 by Justin LaPre at Rensselaer Polytechnic Institute - 10/20/2009
  3. tutorial 3 by Larry Olson - 4/14/2012

This particular set of tutorials tracks the llvm / clang mainline and is updated semi-regularly to account for llvm / clang API changes.

See contents of the links above for a walkthrough of what these tutorials are doing.

Last Update

This was last built on 5/25/2015 against
URL: http://llvm.org/svn/llvm-project/llvm/trunk
Revision:237487

Other Options

The Clang team has been hard at work making it easier to write tools using Clang. There are 4 options for developing tools using clang and llvm infrastructure.

Latest Stable LLVM / Clang (v3.4)

The master branch tracks recent commits to the clang and llvm svn. The tutorial assumes you have grabbed a copy of both llvm and clang by following these instructions and that you have modified the makefile of this project to point to the build's resulting llvm-config. If you want the latest public release, then checkout the 3.4 branch.

git clone [email protected]:loarabia/Clang-tutorial.git
git checkout 3.4

CI tutorials

The tutorials prefixed with CI are the same as the original tutorials but use the CompilerInstance object and its helper methods to perform the same tasks as the original tutorials. For the most part, this makes the code much more compact.

Tooling tutorials

These tutorials (and the clang tooling infrastrucutre) depend on having a file called compile_commands.json which defines the commands used to compile the input file. You will need to modify this file's directory field to point to the absolute path of the Clang-tutorial on your storage.

Update compile_commands.json to the directory containing input

./ToolingTutorial input04.c
./CommentHandling input04.c

Windows Build

Note on the Windows build: Currently the paths are hardcoded. Please see the SharedBuild.targets file inside of the SharedBuild project to update the path for your specific LLVM and CLANG install.

In particular, the LLVMLibsDirs property and the LLVMIncludes property should be updated.

Contact Me

For any questions, please ping me via my github account. Changes and additions are always welcome.

clang-tutorial's People

Contributors

aseeda avatar bvdberg avatar cedlemo avatar chisophugis avatar e3 avatar eonil avatar loarabia avatar lucamot avatar rpavlik avatar scriptdevil avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

clang-tutorial's Issues

Compatibility with 3.0

Commit f4fb4ef claims to have updated to the 3.0 API, but using this version I get the following compilation error:

tutorial1.cpp: In function ‘int main()’:
tutorial1.cpp:46:67: error: no matching function for call to ‘clang::HeaderSearch::HeaderSearch(clang::FileManager&, clang::DiagnosticsEngine&)’ 
tutorial1.cpp:46:67: note: candidates are:
/usr/include/clang/Lex/HeaderSearch.h:183:3: note: clang::HeaderSearch::HeaderSearch(clang::FileManager&)
/usr/include/clang/Lex/HeaderSearch.h:183:3: note:   candidate expects 1 argument, 2 provided
/usr/include/clang/Lex/HeaderSearch.h:180:12: note: clang::HeaderSearch::HeaderSearch(const clang::HeaderSearch&)
/usr/include/clang/Lex/HeaderSearch.h:180:12: note:   candidate expects 1 argument, 2 provided
/usr/include/clang/Lex/HeaderSearch.h:179:12: note: clang::HeaderSearch::HeaderSearch()
/usr/include/clang/Lex/HeaderSearch.h:179:12: note:   candidate expects 0 arguments, 2 provided 
tutorial1.cpp:49:25: error: ‘getDefaultTargetTriple’ is not a member of ‘llvm::sys’ 

Could this be fixed? Also, the README file says you can be contacted through your Github account, but it is not possible to send you messages unless you specify an email address.

Why do tutorial 3 and 4 have extra tokens?

See the notes from commit: ed0e679.

Output of tutorial 3 has the below extra tokens and the statistics from tutorial 4 seem to reflect these extra tokens as well.

typedef 'typedef'
struct 'struct'
identifier '__va_list_tag'
l_brace '{'
unsigned 'unsigned'
identifier 'gp_offset'
semi ';'
unsigned 'unsigned'
identifier 'fp_offset'
semi ';'
void 'void'
star '*'
identifier 'overflow_arg_area'
semi ';'
void 'void'
star '*'
identifier 'reg_save_area'
semi ';'
r_brace '}'
identifier '__va_list_tag'
semi ';'
typedef 'typedef'
identifier '__va_list_tag'
identifier '__builtin_va_list'
l_square '['
numeric_constant '1'
r_square ']'
semi ';'

unable to debug with gdb

I wanted to debug the tutorial with GDB, so I inserted -O0 -g options in the makefile, but it's unable to locate the source file.

CItutorial1.cpp build error

I'm building the code from CItutorial1.cpp with trunk Clang/LLVM (r157925).

I get the error:

error: ‘getDefaultTargetTriple’ is not a member of ‘llvm::sys’
make: *** [basic_clang_client] Error 1

Compile errors with clang/llvm 3.2

both llvm and clang are version 3.2 and its the same fail if I use the makefile

g++ -I/usr/include/clang llvm-config --cxxflags llvm-config --libs -fno-rtti -lLLVMSupport -lLLVMSystem -lLLVMBitReader -lLLVMBitWriter tutorial1.cpp -o build/tutorial1
tutorial1.cpp: In function ‘int main()’:
tutorial1.cpp:55:27: error: no matching function for call to ‘clang::TargetInfo::CreateTargetInfo(clang::DiagnosticsEngine&, clang::TargetOptions_)’
tutorial1.cpp:55:27: note: candidate is:
/usr/include/clang/Basic/TargetInfo.h:111:22: note: static clang::TargetInfo_ clang::TargetInfo::CreateTargetInfo(clang::DiagnosticsEngine&, clang::TargetOptions&)
/usr/include/clang/Basic/TargetInfo.h:111:22: note: no known conversion for argument 2 from ‘clang::TargetOptions*’ to ‘clang::TargetOptions&’

Unable to build stable branch of tutorial

Cloned the repo, switched to stable branch and trying to build vs. Clang+LLVM 3.0 binary download
Pointed llvm-config to the one in the download.

Get link errors:

/home/ebenders_test/clang+llvm-3.0-x86_64-linux-debian/lib/libclangFrontend.a(CompilerInstance.o): In function clang::CompilerInstance::addOutputFile(clang::CompilerInstance::OutputFile const&)':
/home/duncan/llvm-3.0/64/final/cfe.src/lib/Frontend/CompilerInstance.cpp:(.text+0x173e): undefined reference to std::__detail::_List_node_base::_M_hook(std::__detail::_List_node_base*)' /home/ebenders_test/clang+llvm-3.0-x86_64-linux-debian/lib/libclangFrontend.a(CompilerInstance.o): In function clang::CompilerInstance::createOutputFile(llvm::StringRef, bool, bool, llvm::StringRef, llvm::StringRef, bool)':
/home/duncan/llvm-3.0/64/final/cfe.src/lib/Frontend/CompilerInstance.cpp:(.text+0x1d1b): undefined reference to std::__detail::_List_node_base::_M_hook(std::__detail::_List_node_base*)' /home/ebenders_test/clang+llvm-3.0-x86_64-linux-debian/lib/libclangSema.a(SemaCodeComplete.o): In function clang::Sema::CodeCompleteOrdinaryName(clang::Scope*, clang::Sema::ParserCompletionContext)':
/home/duncan/llvm-3.0/64/final/cfe.src/lib/Sema/SemaCodeComplete.cpp:(.text+0x7b1f): undefined reference to std::__detail::_List_node_base::_M_hook(std::__detail::_List_node_base*)' /home/ebenders_test/clang+llvm-3.0-x86_64-linux-debian/lib/libclangSema.a(SemaCodeComplete.o): In function (anonymous namespace)::ResultBuilder::ExitScope()':
/home/duncan/llvm-3.0/64/final/cfe.src/lib/Sema/SemaCodeComplete.cpp:(.text+0x18292): undefined reference to std::__detail::_List_node_base::_M_unhook()'

And many others

Tutorial7 VC++ project missing?

Tutorial7 seems to be missing from the repo. It's referenced in the SLN file, but none of the files (source or proj) are in the repo.

Issue with tutorial3.cpp

I had a little issue while trying the third tutorial. After typing make and running the executable, the output is

testInclude.c:1:10: fatal error: 'stdio.h' file not found

include <stdio.h>

     ^

I did not change the files in any way. I'm running Ubuntu Linux.

Any help would be highly appreciated!
Thanks,
Diego

Malformed compile_commands.json

Hi!
I've been browsing through examples and found maybe-a-bug in "compile_commands.json" in the root of the repo: there is no ending "," at the end of the line 4 ("command": ...).
I don't know if it is crucial or anything, but I doubt it is by design also.

CItutorial3.cpp not finding #includes

Hello,

I am having trouble running the CItutorial3.cpp. First, the code doesn't compile since the InitializePreprocessor() function (screenshot attached). Hence I added the missing argument and made few changes to adapt the code to my directories. However, when I run the output CItutorial3, it gives me another error (screenshot attached) although I checked and made sure that stdio.h is in /usr/include/. I also made few changes to the Makefile to adapt it to my directory paths.
I'm working on Ubuntu 16.04 on VirtualBox.

error1
error2
source code.zip

Make the tutorials take command line files

Don't hardcode the sample file in the tutorial -- take it in as a command line argument. This just makes the tutorials easier to play with.

Basically, if no file is specified, use test.c otherwise use the file specified. This is nice to have and will make it easy to pick up.

linked libraries order

I had trouble compiling this on linux (fedora 16) with current svn. I fixed this by emulating the clang binary links, no doubt some of these are extraneous.
CLANGLIBS =
-lclangFrontendTool
-lclangFrontend
-lclangDriver
-lclangSerialization
-lclangCodeGen
-lclangParse
-lclangSema
-lclangStaticAnalyzerFrontend
-lclangStaticAnalyzerCheckers
-lclangStaticAnalyzerCore
-lclangAnalysis
-lclangARCMigrate
-lclangRewrite
-lclangEdit
-lclangAST
-lclangLex
-lclangBasic

Cannot build tutorial 3.

Hello,

After following your tutorial I cannot build tutorial 3. It keeps giving me an error on line 85:
invalid initialization of reference of type ‘const clang::PCHContainerReader&’ from expression of type ‘clang::FrontendOptions’
frontendOptions);

I am compiling it with the following command:
g++ llvm-config --cxxflags --ldflags tutorial3.cpp $CLANG_LIBS llvm-config --libs --system-libs -o tutorial3

where $CLANG_LIBS = "-lclangTooling -lclangFrontendTool -lclangFrontend -lclangDriver -lclangSerialization -lclangCodeGen -lclangParse -lclangSema -lclangStaticAnalyzerFrontend -lclangStaticAnalyzerCheckers -lclangStaticAnalyzerCore -lclangAnalysis -lclangARCMigrate -lclangRewrite -lclangRewriteFrontend -lclangEdit -lclangAST -lclangLex -lclangBasic -lclang"

Update tutorial code for latest Clang revision

The latest Clang needs to have a CompilerInvocation object set up or else it will crash later on.

    CompilerInvocation cinv;
    CompilerInvocation::CreateFromArgs(cinv, args, args + ARRAY_SIZE(args), ci.getDiagnostics());

    ci.setInvocation(&cinv);

You can either use that overload to set it up from arguments, or need to setup the different options manually.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.