crange: clang based tool to index and cross-reference c/c++ source code

38
CRANGE CROSS-REFERENCING SOURCE CODE WITH CLANG Anurag Patel Red Hat github.com/crange/crange 0

Upload: anurag-patel

Post on 03-Jul-2015

821 views

Category:

Data & Analytics


2 download

DESCRIPTION

Crange is a tool to index and cross-reference C/C++ source code. It can be used to generate tags database that can help with: * Identifier definitions * Identifier declaraions * References * Expressions * Operators * Symbols * Source range The source metadata collected by Crange can help with building tools to provide cross-referencing, syntax highlighting, code folding and deep source code search.

TRANSCRIPT

Page 1: Crange: Clang based tool to index and cross-reference C/C++ source code

CRANGECROSS-REFERENCING SOURCE CODE WITH CLANG

— — Anurag Patel Red Hat github.com/crange/crange

0

Page 2: Crange: Clang based tool to index and cross-reference C/C++ source code

TABLE OF CONTENTS1. Tools considered2. Clang and AST3. Indexing with libclang4. Crange5. Challenges6. Summary

Page 3: Crange: Clang based tool to index and cross-reference C/C++ source code

SOURCE CODE#include <stdio.h>int Fibonacci(int);int Fibonacci(int n) { if (n == 0) return 0; else if (n == 1) return 1; else return (Fibonacci(n-1) + Fibonacci(n-2));}

int main() { int n = 12, i = 0, c; for ( c = 1 ; c <= n ; c++ ) { printf("%d\n", Fibonacci(i)); i++; } return 0;}

Fibonacci series

Page 4: Crange: Clang based tool to index and cross-reference C/C++ source code

TOOLS CONSIDERED

Page 5: Crange: Clang based tool to index and cross-reference C/C++ source code

CTAGS+ Fast, comprehensive language support

- Definitions only

Page 6: Crange: Clang based tool to index and cross-reference C/C++ source code

DOXYGEN+ X-Ref, XML output

- Slow

Page 7: Crange: Clang based tool to index and cross-reference C/C++ source code

GNU GLOBAL+ Fast, definitions, X-Ref

Page 8: Crange: Clang based tool to index and cross-reference C/C++ source code

ANTLR+ Seems powerful, LL(*) parser

- Java, grammar files, complicated

Page 9: Crange: Clang based tool to index and cross-reference C/C++ source code

FLEX+BISON+ Is powerful

- CS 143 - Compilers

Page 10: Crange: Clang based tool to index and cross-reference C/C++ source code

CLANG+ Declarations, definitions, X-Ref, range, statements, operators,

expressions, literals

Page 11: Crange: Clang based tool to index and cross-reference C/C++ source code

CLANG

Page 12: Crange: Clang based tool to index and cross-reference C/C++ source code

CLANGCompiler front end and static analyzer for C, C++, Objective-C.

Converts programs to AST and allows AST manipulation.

Page 13: Crange: Clang based tool to index and cross-reference C/C++ source code

AST[snip]|-FunctionDecl 0x610a090 <fibonacci.c:2:1, col:18> Fibonacci 'int (int)'| ̀-ParmVarDecl 0x6109fd0 <col:15> 'int'|-FunctionDecl 0x610a1d0 prev 0x610a090 <line:3:1, line:10:1> Fibonacci 'int (int)'| |-ParmVarDecl 0x610a150 <line:3:15, col:19> n 'int'| ̀-CompoundStmt 0x6153a70 <col:22, line:10:1>| ̀-IfStmt 0x6153a40 <line:4:3, line:9:44>| |-<<<NULL>>>| |-BinaryOperator 0x610a2d8 <line:4:7, col:12> 'int' '=='| | |-ImplicitCastExpr 0x610a2c0 <col:7> 'int' <LValueToRValue>| | | ̀-DeclRefExpr 0x610a278 <col:7> 'int' lvalue ParmVar 0x610a150 'n' 'int'| | ̀-IntegerLiteral 0x610a2a0 <col:12> 'int' 0| |-ReturnStmt 0x610a320 <line:5:5, col:12>| | ̀-IntegerLiteral 0x610a300 <col:12> 'int' 0| ̀-IfStmt 0x6153a10 <line:6:8, line:9:44>[snip]

$ clang -cc1 -ast-dump fibonacci.c

Page 14: Crange: Clang based tool to index and cross-reference C/C++ source code

LIBCLANG

Page 15: Crange: Clang based tool to index and cross-reference C/C++ source code

LIBCLANGLibrary for processing source code.Translate source to ASTResolve identifiers and symbolsExpand macrosSyntax highlightingCode completion

Page 16: Crange: Clang based tool to index and cross-reference C/C++ source code

INDEXING AND X-REF (0/6)#include <stdio.h>

int Fibonacci(int);int Fibonacci(int n) { if (n == 0) return 0; else if (n == 1) return 1; else return (Fibonacci(n-1) + Fibonacci(n-2));}

int main() { int n = 12, i = 0, c; for ( c = 1 ; c <= n ; c++ ) { printf("%d\n", Fibonacci(i)); i++; } return 0;}

Walk the AST

Page 17: Crange: Clang based tool to index and cross-reference C/C++ source code

INDEXING AND X-REF (1/6)int Fibonacci(int);int Fibonacci(int n) { if (n == 0) return 0; else if (n == 1) return 1; else return (Fibonacci(n-1) + Fibonacci(n-2));}

int main() { int n = 12, i = 0, c; for ( c = 1 ; c <= n ; c++ ) { printf("%d\n", Fibonacci(i)); i++; } return 0;}

#include <stdio.h>

Macros

Page 18: Crange: Clang based tool to index and cross-reference C/C++ source code

INDEXING AND X-REF (2/6)#include <stdio.h>

int (int);int (int ) { if (n == 0) return 0; else if (n == 1) return 1; else return (Fibonacci(n-1) + Fibonacci(n-2));}

int () { int = 12, = 0, ; for ( c = 1 ; c <= n ; c++ ) { printf("%d\n", Fibonacci(i)); i++; } return 0;}

FibonacciFibonacci n

mainn i c

Declarations

Page 19: Crange: Clang based tool to index and cross-reference C/C++ source code

INDEXING AND X-REF (3/6)#include <stdio.h>

int Fibonacci(int);int Fibonacci(int n) { if ( == 0) return 0; else if ( == 1) return 1; else return ( (-1) + (-2));}

int main() { int n = 12, i = 0, c; for ( = 1 ; <= ; ++ ) { printf("%d\n", ()); ++; } return 0;}

n

n

Fibonaccin Fibonaccin

c c n cFibonaccii i

References

Page 20: Crange: Clang based tool to index and cross-reference C/C++ source code

INDEXING AND X-REF (4/6)#include <stdio.h>

int Fibonacci(int);int Fibonacci(int n) { if (n == 0) ; else if (n == 1) ; else ;}

int main() { int n = 12, i = 0, c;

;}

return 0

return 1

return (Fibonacci(n-1) + Fibonacci(n-2))

for ( c = 1 ; c <= n ; c++ ) { printf("%d\n", Fibonacci(i)); i++; }return 0

Statements

Page 21: Crange: Clang based tool to index and cross-reference C/C++ source code

INDEXING AND X-REF (5/6)#include <stdio.h>

int Fibonacci(int);int Fibonacci(int n) { if (n == 0) return 0; else if (n == 1) return 1; else return ( + );}

int main() { int n = 12, i = 0, c; for ( c = 1 ; c <= n ; c++ ) { ("%d\n", ); i++; } return 0;}

Fibonacci(n-1) Fibonacci(n-2)

printf Fibonacci(i)

Expressions

Page 22: Crange: Clang based tool to index and cross-reference C/C++ source code

INDEXING AND X-REF (6/6)#include <stdio.h>

int Fibonacci(int);int Fibonacci(int n) { if (n 0) return 0; else if (n 1) return 1; else return (Fibonacci(n1) Fibonacci(n2));}

int main() { int n = 12, i = 0, c; for ( c 1 ; c n ; c ) { printf("%d\n", Fibonacci(i)); i ; } return 0;}

==

==

- + -

= <= ++++

Operators

Page 23: Crange: Clang based tool to index and cross-reference C/C++ source code

CRANGE

Page 24: Crange: Clang based tool to index and cross-reference C/C++ source code

FRONTENDC++libclang + Pythonlibclang + Python + multiprocessingcrtags: generating tag dbcrange: query

Page 25: Crange: Clang based tool to index and cross-reference C/C++ source code

DATASTOREJSONHDF5BerkeleyDBSQLite

Page 26: Crange: Clang based tool to index and cross-reference C/C++ source code

MULTIPROCESSING MODEL1 DB worker1 multiprocessing.Queuen-1 AST workers*

Page 27: Crange: Clang based tool to index and cross-reference C/C++ source code

MULTIPROCESSING.POOL()

1 async DB worker, n-1 sync AST workers

Page 28: Crange: Clang based tool to index and cross-reference C/C++ source code

CRTAGS$ cd linux-3.13.5$ time crtags -v -j 32 .Parsing fs/xfs/xfs_bmap_btree.c (count: 1)Indexing fs/xfs/xfs_bmap_btree.c (nodes: 379, qsize: 0)...Parsing sound/soc/codecs/ak4641.h (count: 34348)Generating indexes

real 415m10.974suser 8108m52.241ssys 72m51.554s$

Generates 22GB tags file in ~7 hours

16GB RAM & 32 CPUs

Page 29: Crange: Clang based tool to index and cross-reference C/C++ source code

CURL DEMO$ time crtags -v -j 32 curl-7.35.0/

Page 30: Crange: Clang based tool to index and cross-reference C/C++ source code

CHALLENGES

Page 31: Crange: Clang based tool to index and cross-reference C/C++ source code

INCLUDE PATHS$ clang -I/usr/include -Iinclude -I`llvm-config --cflags`

Page 32: Crange: Clang based tool to index and cross-reference C/C++ source code

MACROS-D_DEBUG -D_GNU_SOURCE -D_CONSTANT_MACROS

Page 33: Crange: Clang based tool to index and cross-reference C/C++ source code

MAKEFILE/CMAKE[ { "directory": "/home/anurag/llvm/build", "command": "/usr/bin/clang++ -Irelative -DSOMEDEF=\"With spaces, quotes and \\-es.\" -c -o MachineVerifier.o MachineVerifier.cpp" "file": "MachineVerifier.cpp" }, ...]

Page 34: Crange: Clang based tool to index and cross-reference C/C++ source code

PYTHON VS C++

Page 35: Crange: Clang based tool to index and cross-reference C/C++ source code

SUMMARY

Page 36: Crange: Clang based tool to index and cross-reference C/C++ source code

CRANGE CAN PROVIDEDefinitionsDeclarationsReferencesExpressionsOperatorsSymbolsSource rangeFancy searchCode folding*Find similar code*

Page 37: Crange: Clang based tool to index and cross-reference C/C++ source code

FURTHER READINGhttps://github.com/crange/crangehttp://clang.llvm.org/get_started.htmlgithub.com/trolldbois/python-clang/tree/master/tests/cindexhttps://docs.python.org/2/library/multiprocessing.htmlhttp://clang.llvm.org/doxygen/group__CINDEX.html

Page 38: Crange: Clang based tool to index and cross-reference C/C++ source code

END