71
libclang: Thinking Beyond the Compiler 1 Monday, November 29, 2010

libclang: Thinking Beyond the Compiler - LLVM · 2019-10-30 · Clang is not just a great compiler... • Clang is also a library for processing source code – Translates text into

  • Upload
    others

  • View
    13

  • Download
    0

Embed Size (px)

Citation preview

Page 1: libclang: Thinking Beyond the Compiler - LLVM · 2019-10-30 · Clang is not just a great compiler... • Clang is also a library for processing source code – Translates text into

libclang: Thinking Beyond the Compiler

1Monday, November 29, 2010

Page 2: libclang: Thinking Beyond the Compiler - LLVM · 2019-10-30 · Clang is not just a great compiler... • Clang is also a library for processing source code – Translates text into

Clang is not just a great compiler...

2Monday, November 29, 2010

Page 3: libclang: Thinking Beyond the Compiler - LLVM · 2019-10-30 · Clang is not just a great compiler... • Clang is also a library for processing source code – Translates text into

Clang is not just a great compiler...

• Clang is also a library for processing source code– Translates text into Abstract Syntax Trees– Resolves identifiers and symbols– Expands macros– Makes implicit information explicit

2Monday, November 29, 2010

Page 4: libclang: Thinking Beyond the Compiler - LLVM · 2019-10-30 · Clang is not just a great compiler... • Clang is also a library for processing source code – Translates text into

Clang is not just a great compiler...

• Clang is also a library for processing source code– Translates text into Abstract Syntax Trees– Resolves identifiers and symbols– Expands macros– Makes implicit information explicit

• Clang obsessively tracks source-level location information

2Monday, November 29, 2010

Page 5: libclang: Thinking Beyond the Compiler - LLVM · 2019-10-30 · Clang is not just a great compiler... • Clang is also a library for processing source code – Translates text into

Introducing libclang

3Monday, November 29, 2010

Page 6: libclang: Thinking Beyond the Compiler - LLVM · 2019-10-30 · Clang is not just a great compiler... • Clang is also a library for processing source code – Translates text into

Introducing libclang

• Parsing

3Monday, November 29, 2010

Page 7: libclang: Thinking Beyond the Compiler - LLVM · 2019-10-30 · Clang is not just a great compiler... • Clang is also a library for processing source code – Translates text into

Introducing libclang

• Parsing• Indexing and cross-referencing

3Monday, November 29, 2010

Page 8: libclang: Thinking Beyond the Compiler - LLVM · 2019-10-30 · Clang is not just a great compiler... • Clang is also a library for processing source code – Translates text into

Introducing libclang

• Parsing• Indexing and cross-referencing• Syntax highlighting

3Monday, November 29, 2010

Page 9: libclang: Thinking Beyond the Compiler - LLVM · 2019-10-30 · Clang is not just a great compiler... • Clang is also a library for processing source code – Translates text into

Introducing libclang

• Parsing• Indexing and cross-referencing• Syntax highlighting• Code completion

3Monday, November 29, 2010

Page 10: libclang: Thinking Beyond the Compiler - LLVM · 2019-10-30 · Clang is not just a great compiler... • Clang is also a library for processing source code – Translates text into

Parsing Source Code

4Monday, November 29, 2010

Page 11: libclang: Thinking Beyond the Compiler - LLVM · 2019-10-30 · Clang is not just a great compiler... • Clang is also a library for processing source code – Translates text into

Parsing Source Code

int main(int argc, char *argv[]) { CXIndex Index = clang_createIndex(0, 0); CXTranslationUnit TU = clang_parseTranslationUnit(Index, 0,

argv, argc, 0, 0, CXTranslationUnit_None); for (unsigned I = 0, N = clang_getNumDiagnostics(TU); I != N; ++I) { CXDiagnostic Diag = clang_getDiagnostic(TU, I); CXString String = clang_formatDiagnostic(Diag, clang_defaultDiagnosticDisplayOptions()); fprintf(stderr, "%s\n", clang_getCString(String)); clang_disposeString(String); } clang_disposeTranslationUnit(TU); clang_disposeIndex(Index); return 0;}

5Monday, November 29, 2010

Page 12: libclang: Thinking Beyond the Compiler - LLVM · 2019-10-30 · Clang is not just a great compiler... • Clang is also a library for processing source code – Translates text into

Parsing Source Code

int main(int argc, char *argv[]) { CXIndex Index = clang_createIndex(0, 0); CXTranslationUnit TU = clang_parseTranslationUnit(Index, 0,

argv, argc, 0, 0, CXTranslationUnit_None); for (unsigned I = 0, N = clang_getNumDiagnostics(TU); I != N; ++I) { CXDiagnostic Diag = clang_getDiagnostic(TU, I); CXString String = clang_formatDiagnostic(Diag, clang_defaultDiagnosticDisplayOptions()); fprintf(stderr, "%s\n", clang_getCString(String)); clang_disposeString(String); } clang_disposeTranslationUnit(TU); clang_disposeIndex(Index); return 0;}

6Monday, November 29, 2010

Page 13: libclang: Thinking Beyond the Compiler - LLVM · 2019-10-30 · Clang is not just a great compiler... • Clang is also a library for processing source code – Translates text into

int main(int argc, char *argv[]) { CXIndex Index = clang_createIndex(0, 0); CXTranslationUnit TU = clang_parseTranslationUnit(Index, 0,

argv, argc, 0, 0, CXTranslationUnit_None); for (unsigned I = 0, N = clang_getNumDiagnostics(TU); I != N; ++I) { CXDiagnostic Diag = clang_getDiagnostic(TU, I); CXString String = clang_formatDiagnostic(Diag, clang_defaultDiagnosticDisplayOptions()); fprintf(stderr, "%s\n", clang_getCString(String)); clang_disposeString(String); } clang_disposeTranslationUnit(TU); clang_disposeIndex(Index); return 0;}

Parsing Source Code

7Monday, November 29, 2010

Page 14: libclang: Thinking Beyond the Compiler - LLVM · 2019-10-30 · Clang is not just a great compiler... • Clang is also a library for processing source code – Translates text into

int main(int argc, char *argv[]) { CXIndex Index = clang_createIndex(0, 0); CXTranslationUnit TU = clang_parseTranslationUnit(Index, 0,

argv, argc, 0, 0, CXTranslationUnit_None); for (unsigned I = 0, N = clang_getNumDiagnostics(TU); I != N; ++I) { CXDiagnostic Diag = clang_getDiagnostic(TU, I); CXString String = clang_formatDiagnostic(Diag, clang_defaultDiagnosticDisplayOptions()); fprintf(stderr, "%s\n", clang_getCString(String)); clang_disposeString(String); } clang_disposeTranslationUnit(TU); clang_disposeIndex(Index); return 0;}

Parsing Source Code

7Monday, November 29, 2010

Page 15: libclang: Thinking Beyond the Compiler - LLVM · 2019-10-30 · Clang is not just a great compiler... • Clang is also a library for processing source code – Translates text into

Parsing Source Code

int main(int argc, char *argv[]) { CXIndex Index = clang_createIndex(0, 0); CXTranslationUnit TU = clang_parseTranslationUnit(Index, 0,

argv, argc, 0, 0, CXTranslationUnit_None); for (unsigned I = 0, N = clang_getNumDiagnostics(TU); I != N; ++I) { CXDiagnostic Diag = clang_getDiagnostic(TU, I); CXString String = clang_formatDiagnostic(Diag, clang_defaultDiagnosticDisplayOptions()); fprintf(stderr, "%s\n", clang_getCString(String)); clang_disposeString(String); } clang_disposeTranslationUnit(TU); clang_disposeIndex(Index); return 0;}

8Monday, November 29, 2010

Page 16: libclang: Thinking Beyond the Compiler - LLVM · 2019-10-30 · Clang is not just a great compiler... • Clang is also a library for processing source code – Translates text into

• Given list.c: struct List { ... }; int sum(union List *L) { /* ... */ }

• Run our syntax-checker:$ syntax-check -I../../ list.c

list.c:2:9: error: use of 'List' with tag type that does not match previous declarationlist.c:1:8: note: previous use is here

Parsing Source Code—Results

$ syntax-check -I../../ list.c

list.c:2:9: error: use of 'List' with tag type that does not match previous declarationlist.c:1:8: note: previous use is here

9Monday, November 29, 2010

Page 17: libclang: Thinking Beyond the Compiler - LLVM · 2019-10-30 · Clang is not just a great compiler... • Clang is also a library for processing source code – Translates text into

• Given list.c: struct List { ... }; int sum(union List *L) { /* ... */ }

• Run our syntax-checker:$ syntax-check -I../../ list.c

list.c:2:9: error: use of 'List' with tag type that does not match previous declarationlist.c:1:8: note: previous use is here

Parsing Source Code—Results

$ syntax-check -I../../ list.c

list.c:2:9: error: use of 'List' with tag type that does not match previous declarationlist.c:1:8: note: previous use is here

list.c:2:9: error: use of 'List' with tag type that does not match previous declarationint sum(union List *Node) { ^~~~~ structlist.c:1:8: note: previous use is herestruct List { ^

9Monday, November 29, 2010

Page 18: libclang: Thinking Beyond the Compiler - LLVM · 2019-10-30 · Clang is not just a great compiler... • Clang is also a library for processing source code – Translates text into

Dissecting Diagnostics

10Monday, November 29, 2010

Page 19: libclang: Thinking Beyond the Compiler - LLVM · 2019-10-30 · Clang is not just a great compiler... • Clang is also a library for processing source code – Translates text into

Dissecting Diagnostics

• Core diagnostic information:– enum CXDiagnosticSeverity

clang_getDiagnosticSeverity(CXDiagnostic Diag);– CXSourceLocation

clang_getDiagnosticLocation(CXDiagnostic Diag);– CXString clang_getDiagnosticSpelling(CXDiagnostic Diag);

10Monday, November 29, 2010

Page 20: libclang: Thinking Beyond the Compiler - LLVM · 2019-10-30 · Clang is not just a great compiler... • Clang is also a library for processing source code – Translates text into

Dissecting Diagnostics

• Core diagnostic information:– enum CXDiagnosticSeverity

clang_getDiagnosticSeverity(CXDiagnostic Diag);– CXSourceLocation

clang_getDiagnosticLocation(CXDiagnostic Diag);– CXString clang_getDiagnosticSpelling(CXDiagnostic Diag);

• Highlighted source ranges

10Monday, November 29, 2010

Page 21: libclang: Thinking Beyond the Compiler - LLVM · 2019-10-30 · Clang is not just a great compiler... • Clang is also a library for processing source code – Translates text into

Dissecting Diagnostics

• Core diagnostic information:– enum CXDiagnosticSeverity

clang_getDiagnosticSeverity(CXDiagnostic Diag);– CXSourceLocation

clang_getDiagnosticLocation(CXDiagnostic Diag);– CXString clang_getDiagnosticSpelling(CXDiagnostic Diag);

• Highlighted source ranges• Fix-its:

– unsigned clang_getDiagnosticNumFixIts(CXDiagnostic Diag);– CXString clang_getDiagnosticFixIt(CXDiagnostic Diag,

unsigned FixIt, CXSourceRange *ReplacementRange);

10Monday, November 29, 2010

Page 22: libclang: Thinking Beyond the Compiler - LLVM · 2019-10-30 · Clang is not just a great compiler... • Clang is also a library for processing source code – Translates text into

Indexing & Cross-Referencing

11Monday, November 29, 2010

Page 23: libclang: Thinking Beyond the Compiler - LLVM · 2019-10-30 · Clang is not just a great compiler... • Clang is also a library for processing source code – Translates text into

Indexing and Cross-Referencing

struct List { int Data; struct List *Next;};

int sum(struct List *Node) { int result = 0; for (; Node; Node = Node->Next) result = result + Node->Data; return result;}

12Monday, November 29, 2010

Page 24: libclang: Thinking Beyond the Compiler - LLVM · 2019-10-30 · Clang is not just a great compiler... • Clang is also a library for processing source code – Translates text into

• Walk the Abstract Syntax Tree

Indexing and Cross-Referencing

struct List { int Data; struct List *Next;};

int sum(struct List *Node) { int result = 0; for (; Node; Node = Node->Next) result = result + Node->Data; return result;}

12Monday, November 29, 2010

Page 25: libclang: Thinking Beyond the Compiler - LLVM · 2019-10-30 · Clang is not just a great compiler... • Clang is also a library for processing source code – Translates text into

• Walk the Abstract Syntax Tree– Declarations

Indexing and Cross-Referencing

struct List { int Data; struct List *Next;};

int sum(struct List *Node) { int result = 0; for (; Node; Node = Node->Next) result = result + Node->Data; return result;}

12Monday, November 29, 2010

Page 26: libclang: Thinking Beyond the Compiler - LLVM · 2019-10-30 · Clang is not just a great compiler... • Clang is also a library for processing source code – Translates text into

• Walk the Abstract Syntax Tree– Declarations– References

Indexing and Cross-Referencing

struct List { int Data; struct List *Next;};

int sum(struct List *Node) { int result = 0; for (; Node; Node = Node->Next) result = result + Node->Data; return result;}

12Monday, November 29, 2010

Page 27: libclang: Thinking Beyond the Compiler - LLVM · 2019-10-30 · Clang is not just a great compiler... • Clang is also a library for processing source code – Translates text into

• Walk the Abstract Syntax Tree– Declarations– References– Statements &

expressions

Indexing and Cross-Referencing

struct List { int Data; struct List *Next;};

int sum(struct List *Node) { int result = 0; for (; Node; Node = Node->Next) result = result + Node->Data; return result;}

12Monday, November 29, 2010

Page 28: libclang: Thinking Beyond the Compiler - LLVM · 2019-10-30 · Clang is not just a great compiler... • Clang is also a library for processing source code – Translates text into

• Walk the Abstract Syntax Tree– Declarations– References– Statements &

expressions– Macro definitions &

instantiations

Indexing and Cross-Referencing

struct List { int Data; struct List *Next;};

int sum(struct List *Node) { int result = 0; for (; Node; Node = Node->Next) result = result + Node->Data; return result;}

12Monday, November 29, 2010

Page 29: libclang: Thinking Beyond the Compiler - LLVM · 2019-10-30 · Clang is not just a great compiler... • Clang is also a library for processing source code – Translates text into

CXCursor: AST Node, Simplified

• typedef struct { ... } CXCursor;

• Unifies AST nodes (declaration, reference, expression, statement, etc.)– Source location and extent– Name and symbol resolution– Type– Child nodes

13Monday, November 29, 2010

Page 30: libclang: Thinking Beyond the Compiler - LLVM · 2019-10-30 · Clang is not just a great compiler... • Clang is also a library for processing source code – Translates text into

Example CXCursor: struct List

struct List { int Data; struct List *Next;};

14Monday, November 29, 2010

Page 31: libclang: Thinking Beyond the Compiler - LLVM · 2019-10-30 · Clang is not just a great compiler... • Clang is also a library for processing source code – Translates text into

• Top-level cursor C for List:– clang_getCursorKind(C)

== CXCursor_StructDecl– clang_getCursorSpelling(C)

== “List”

Example CXCursor: struct List

struct List { int Data; struct List *Next;};

14Monday, November 29, 2010

Page 32: libclang: Thinking Beyond the Compiler - LLVM · 2019-10-30 · Clang is not just a great compiler... • Clang is also a library for processing source code – Translates text into

• Top-level cursor C for List:– clang_getCursorKind(C)

== CXCursor_StructDecl– clang_getCursorSpelling(C)

== “List”– clang_getCursorLocation(C)

Example CXCursor: struct List

struct List { int Data; struct List *Next;};

14Monday, November 29, 2010

Page 33: libclang: Thinking Beyond the Compiler - LLVM · 2019-10-30 · Clang is not just a great compiler... • Clang is also a library for processing source code – Translates text into

• Top-level cursor C for List:– clang_getCursorKind(C)

== CXCursor_StructDecl– clang_getCursorSpelling(C)

== “List”– clang_getCursorLocation(C)– clang_getCursorExtent(C)

Example CXCursor: struct List

struct List { int Data; struct List *Next;};

14Monday, November 29, 2010

Page 34: libclang: Thinking Beyond the Compiler - LLVM · 2019-10-30 · Clang is not just a great compiler... • Clang is also a library for processing source code – Translates text into

• Top-level cursor C for List:– clang_getCursorKind(C)

== CXCursor_StructDecl– clang_getCursorSpelling(C)

== “List”– clang_getCursorLocation(C)– clang_getCursorExtent(C)– clang_visitChildren(C, …);

Example CXCursor: struct List

struct List { int Data; struct List *Next;};

14Monday, November 29, 2010

Page 35: libclang: Thinking Beyond the Compiler - LLVM · 2019-10-30 · Clang is not just a great compiler... • Clang is also a library for processing source code – Translates text into

Reference Cursors

struct List { int Data; struct List *Next;};

15Monday, November 29, 2010

Page 36: libclang: Thinking Beyond the Compiler - LLVM · 2019-10-30 · Clang is not just a great compiler... • Clang is also a library for processing source code – Translates text into

Reference Cursors

• Reference cursor R for List:– clang_getCursorKind(R)

== CXCursor_TypeRef– clang_getCursorSpelling(R)

== “List”– clang_getCursorLocation(R)

struct List { int Data; struct List *Next;};

15Monday, November 29, 2010

Page 37: libclang: Thinking Beyond the Compiler - LLVM · 2019-10-30 · Clang is not just a great compiler... • Clang is also a library for processing source code – Translates text into

Reference Cursors

• Reference cursor R for List:– clang_getCursorKind(R)

== CXCursor_TypeRef– clang_getCursorSpelling(R)

== “List”– clang_getCursorLocation(R)– clang_getCursorExtent(R)

struct List { int Data; struct List *Next;};

15Monday, November 29, 2010

Page 38: libclang: Thinking Beyond the Compiler - LLVM · 2019-10-30 · Clang is not just a great compiler... • Clang is also a library for processing source code – Translates text into

Reference Cursors

• Reference cursor R for List:– clang_getCursorKind(R)

== CXCursor_TypeRef– clang_getCursorSpelling(R)

== “List”– clang_getCursorLocation(R)– clang_getCursorExtent(R)– clang_getCursorReferenced(R) == C

struct List { int Data; struct List *Next;};

15Monday, November 29, 2010

Page 39: libclang: Thinking Beyond the Compiler - LLVM · 2019-10-30 · Clang is not just a great compiler... • Clang is also a library for processing source code – Translates text into

Local Renaming of Declarations

struct List { int Data; struct List *Next;};

16Monday, November 29, 2010

Page 40: libclang: Thinking Beyond the Compiler - LLVM · 2019-10-30 · Clang is not just a great compiler... • Clang is also a library for processing source code – Translates text into

• Walk all cursors in the AST, recursively:

Local Renaming of Declarations

struct List { int Data; struct List *Next;};

16Monday, November 29, 2010

Page 41: libclang: Thinking Beyond the Compiler - LLVM · 2019-10-30 · Clang is not just a great compiler... • Clang is also a library for processing source code – Translates text into

• Walk all cursors in the AST, recursively:– Identify the cursor C corresponding to the declaration we want

to rename

Local Renaming of Declarations

struct List { int Data; struct List *Next;};

16Monday, November 29, 2010

Page 42: libclang: Thinking Beyond the Compiler - LLVM · 2019-10-30 · Clang is not just a great compiler... • Clang is also a library for processing source code – Translates text into

• Walk all cursors in the AST, recursively:– Identify the cursor C corresponding to the declaration we want

to rename– Find each cursor R: clang_getCursorReferenced(R) = C

Local Renaming of Declarations

struct List { int Data; struct List *Next;};

16Monday, November 29, 2010

Page 43: libclang: Thinking Beyond the Compiler - LLVM · 2019-10-30 · Clang is not just a great compiler... • Clang is also a library for processing source code – Translates text into

• Walk all cursors in the AST, recursively:– Identify the cursor C corresponding to the declaration we want

to rename– Find each cursor R: clang_getCursorReferenced(R) = C

• Perform textual replacement of the name at the locations of C and each R

Local Renaming of Declarations

struct MyList { int Data; struct MyList *Next;};

16Monday, November 29, 2010

Page 44: libclang: Thinking Beyond the Compiler - LLVM · 2019-10-30 · Clang is not just a great compiler... • Clang is also a library for processing source code – Translates text into

Spanning Translation Units

• Unified Symbol Resolutions provide a stable name for declarations– Every entity with external linkage has a USR– USRs are stable across translation units, time

CXString clang_getCursorUSR(CXCursor C);

17Monday, November 29, 2010

Page 45: libclang: Thinking Beyond the Compiler - LLVM · 2019-10-30 · Clang is not just a great compiler... • Clang is also a library for processing source code – Translates text into

Syntax Coloring

18Monday, November 29, 2010

Page 46: libclang: Thinking Beyond the Compiler - LLVM · 2019-10-30 · Clang is not just a great compiler... • Clang is also a library for processing source code – Translates text into

What’s Under My Cursor?

struct List { int Data; struct List *Next;};

int sum(struct List *Node) { int result = 0; for (; Node; Node = Node->Next) result = result + Node->Data; return result;}

19Monday, November 29, 2010

Page 47: libclang: Thinking Beyond the Compiler - LLVM · 2019-10-30 · Clang is not just a great compiler... • Clang is also a library for processing source code – Translates text into

What’s Under My Cursor?

struct List { int Data; struct List *Next;};

int sum(struct List *Node) { int result = 0; for (; Node; Node = Node->Next) result = result + Node->Data; return result;}

19Monday, November 29, 2010

Page 48: libclang: Thinking Beyond the Compiler - LLVM · 2019-10-30 · Clang is not just a great compiler... • Clang is also a library for processing source code – Translates text into

What’s Under My Cursor?

• Kind: CXCursor_DeclRefExpr• Type: int• References: variable

declaration “result”struct List { int Data; struct List *Next;};

int sum(struct List *Node) { int result = 0; for (; Node; Node = Node->Next) result = result + Node->Data; return result;}

19Monday, November 29, 2010

Page 49: libclang: Thinking Beyond the Compiler - LLVM · 2019-10-30 · Clang is not just a great compiler... • Clang is also a library for processing source code – Translates text into

What’s Under My Cursor?

• Kind: CXCursor_DeclRefExpr• Type: int• References: variable

declaration “result”struct List { int Data; struct List *Next;};

int sum(struct List *Node) { int result = 0; for (; Node; Node = Node->Next) result = result + Node->Data; return result;}

CXSourceLocation clang_getLocation(CXTranslationUnit TU, CXFile File, unsigned Line, unsigned Column);

CXCursor clang_getCursor(CXTranslationUnit TU, CXSourceLocation Where);

19Monday, November 29, 2010

Page 50: libclang: Thinking Beyond the Compiler - LLVM · 2019-10-30 · Clang is not just a great compiler... • Clang is also a library for processing source code – Translates text into

What’s Under My Cursor?

struct List { int Data; struct List *Next;};

int sum(struct List *Node) { int result = 0; for (; Node; Node = Node->Next) result = result + Node->Data; return result;}

• Kind: CXCursor_UnexposedExpr

• Type: int

19Monday, November 29, 2010

Page 51: libclang: Thinking Beyond the Compiler - LLVM · 2019-10-30 · Clang is not just a great compiler... • Clang is also a library for processing source code – Translates text into

What’s Under My Cursor?

struct List { int Data; struct List *Next;};

int sum(struct List *Node) { int result = 0; for (; Node; Node = Node->Next) result = result + Node->Data; return result;}

• Kind: CXCursor_MemberRefExpr

• Type: struct List *• References: field

declaration “Next”

19Monday, November 29, 2010

Page 52: libclang: Thinking Beyond the Compiler - LLVM · 2019-10-30 · Clang is not just a great compiler... • Clang is also a library for processing source code – Translates text into

struct MyList { int Data; struct MyList *Next;};

Syntax Coloring

20Monday, November 29, 2010

Page 53: libclang: Thinking Beyond the Compiler - LLVM · 2019-10-30 · Clang is not just a great compiler... • Clang is also a library for processing source code – Translates text into

struct MyList { int Data; struct MyList *Next;};

Syntax Coloring

20Monday, November 29, 2010

Page 54: libclang: Thinking Beyond the Compiler - LLVM · 2019-10-30 · Clang is not just a great compiler... • Clang is also a library for processing source code – Translates text into

struct MyList { int Data; struct MyList *Next;};

• void clang_tokenize(CXTranslationUnit, CXSourceRange, CXToken **Tokens, unsigned *NumTokens);

Syntax Coloring

20Monday, November 29, 2010

Page 55: libclang: Thinking Beyond the Compiler - LLVM · 2019-10-30 · Clang is not just a great compiler... • Clang is also a library for processing source code – Translates text into

struct MyList { int Data; struct MyList *Next;};

• void clang_tokenize(CXTranslationUnit, CXSourceRange, CXToken **Tokens, unsigned *NumTokens);

– Keyword [1:1-1:7]: “struct”– Identifier [1:8-1:14]: “MyList”– Punctuation [1:15-1:16]: “{“– Keyword [2:3-2:6]: “int”– Identifier [2:7-2:11]: “Data”

– Punctuation [2:11-2:12]: “;”– Keyword [3:3-3:9]: “struct”– Identifier [3:10-3:16]: “MyList”

Syntax Coloring

20Monday, November 29, 2010

Page 56: libclang: Thinking Beyond the Compiler - LLVM · 2019-10-30 · Clang is not just a great compiler... • Clang is also a library for processing source code – Translates text into

struct MyList { int Data; struct MyList *Next;};

• void clang_tokenize(CXTranslationUnit, CXSourceRange, CXToken **Tokens, unsigned *NumTokens);

– Keyword [1:1-1:7]: “struct”– Identifier [1:8-1:14]: “MyList”– Punctuation [1:15-1:16]: “{“– Keyword [2:3-2:6]: “int”– Identifier [2:7-2:11]: “Data”

– Punctuation [2:11-2:12]: “;”– Keyword [3:3-3:9]: “struct”– Identifier [3:10-3:16]: “MyList”

• void clang_annotateTokens(CXTranslationUnit, CXToken *Tokens, unsigned NumTokens, CXCursor *Cursors);

Syntax Coloring

20Monday, November 29, 2010

Page 57: libclang: Thinking Beyond the Compiler - LLVM · 2019-10-30 · Clang is not just a great compiler... • Clang is also a library for processing source code – Translates text into

Syntax Coloring

• void clang_annotateTokens(CXTranslationUnit, CXToken *Tokens, unsigned NumTokens, CXCursor *Cursors);– Keyword [1:1-1:7]: “struct” – Identifier [1:8-1:14]: “MyList” (MyList struct declaration)– Punctuation [1:15-1:16]: “{“– Keyword [2:3-2:6]: “int”– Identifier [2:7-2:11]: “Data”

(Data member declaration)– Punctuation [2:11-2:12]: “;”– Keyword [3:3-3:9]: “struct”– Identifier [3:10-3:16]: “MyList” (MyList type reference)

struct MyList { int Data; struct MyList *Next;};

21Monday, November 29, 2010

Page 58: libclang: Thinking Beyond the Compiler - LLVM · 2019-10-30 · Clang is not just a great compiler... • Clang is also a library for processing source code – Translates text into

Code Completion

22Monday, November 29, 2010

Page 59: libclang: Thinking Beyond the Compiler - LLVM · 2019-10-30 · Clang is not just a great compiler... • Clang is also a library for processing source code – Translates text into

What Can I Do <here>?

23Monday, November 29, 2010

Page 60: libclang: Thinking Beyond the Compiler - LLVM · 2019-10-30 · Clang is not just a great compiler... • Clang is also a library for processing source code – Translates text into

What Can I Do <here>?

• Code completion suggests what we can type, e.g.– “Data”– “Next”

int sum(struct List *Node) { int result = 0; for (; Node; Node = Node->Next) result = result + Node->

23Monday, November 29, 2010

Page 61: libclang: Thinking Beyond the Compiler - LLVM · 2019-10-30 · Clang is not just a great compiler... • Clang is also a library for processing source code – Translates text into

What Can I Do <here>?

• Code completion suggests what we can type, e.g.– “Data”– “Next”

• Implemented in the parser:– Insert token in the lexer– Parse token and call context-sensitive callback function– Form completion results

int sum(struct List *Node) { int result = 0; for (; Node; Node = Node->Next) result = result + Node->

23Monday, November 29, 2010

Page 62: libclang: Thinking Beyond the Compiler - LLVM · 2019-10-30 · Clang is not just a great compiler... • Clang is also a library for processing source code – Translates text into

What Can I Do <here>? (Cont.)

24Monday, November 29, 2010

Page 63: libclang: Thinking Beyond the Compiler - LLVM · 2019-10-30 · Clang is not just a great compiler... • Clang is also a library for processing source code – Translates text into

What Can I Do <here>? (Cont.)

int sum(struct List *Node) { int result = 0; for (; Node; Node = Node->Next) result = result +

24Monday, November 29, 2010

Page 64: libclang: Thinking Beyond the Compiler - LLVM · 2019-10-30 · Clang is not just a great compiler... • Clang is also a library for processing source code – Translates text into

What Can I Do <here>? (Cont.)

– Reference a local variable (result, Node)– Call a function (e.g., sum)– Use any C expression (e.g., sizeof)– Use any defined macro (e.g., NULL)– In C++, name any type (e.g., List)– In C++, refer into a namespace (e.g., std::)

int sum(struct List *Node) { int result = 0; for (; Node; Node = Node->Next) result = result +

24Monday, November 29, 2010

Page 65: libclang: Thinking Beyond the Compiler - LLVM · 2019-10-30 · Clang is not just a great compiler... • Clang is also a library for processing source code – Translates text into

Semantic Strings

25Monday, November 29, 2010

Page 66: libclang: Thinking Beyond the Compiler - LLVM · 2019-10-30 · Clang is not just a great compiler... • Clang is also a library for processing source code – Translates text into

Semantic Strings

• Code completion results are composed of “chunks”:– Typed Text, which the user types and is inserted into the file– Text, which is inserted into the buffer– Informative Text, which is shown but not inserted– Placeholders, which indicate code the user should modify– Result Type, which indicates the type of the result– Cursor Kind, which is the kind of entity in the completion

25Monday, November 29, 2010

Page 67: libclang: Thinking Beyond the Compiler - LLVM · 2019-10-30 · Clang is not just a great compiler... • Clang is also a library for processing source code – Translates text into

Semantic Strings

• Code completion results are composed of “chunks”:– Typed Text, which the user types and is inserted into the file– Text, which is inserted into the buffer– Informative Text, which is shown but not inserted– Placeholders, which indicate code the user should modify– Result Type, which indicates the type of the result– Cursor Kind, which is the kind of entity in the completion

• Example completion: int sum(struct List *Node)– Cursor kind: CXCursor_FunctionDecl

Result Typeint

Typed Textsum

Text(

Text)

Placeholderstruct List *Node

25Monday, November 29, 2010

Page 68: libclang: Thinking Beyond the Compiler - LLVM · 2019-10-30 · Clang is not just a great compiler... • Clang is also a library for processing source code – Translates text into

26Monday, November 29, 2010

Page 69: libclang: Thinking Beyond the Compiler - LLVM · 2019-10-30 · Clang is not just a great compiler... • Clang is also a library for processing source code – Translates text into

Wrap-Up

27Monday, November 29, 2010

Page 70: libclang: Thinking Beyond the Compiler - LLVM · 2019-10-30 · Clang is not just a great compiler... • Clang is also a library for processing source code – Translates text into

Getting Started with libclang

• User-level tools– Xcode 4 Developer Preview– Emacs code completion “demo” (see clang/utils/clang-completion-mode.el)

– Vim code completion (see llvm/utils/vim/)• Developer-centric resources

– c-index-test provides a command-line interface to the API

– Doxygen documentation at http://clang.llvm.org

28Monday, November 29, 2010

Page 71: libclang: Thinking Beyond the Compiler - LLVM · 2019-10-30 · Clang is not just a great compiler... • Clang is also a library for processing source code – Translates text into

libclang Supports Development Tools

• Simple C API covers a variety of features– Parsing– Indexing– Cross-referencing– Mapping between source code

and ASTs– Syntax coloring– Code completion

• Great basis for development tools

29Monday, November 29, 2010