0
Clang:
A New Tool for Old Languages
        MORITA Hajime <omo@dodgson.org>
                    http://steps.dodgson.org/
...
//===----------------------------------------------------------------------===//
// C Language Family Front-end
//===-----...
• Clang       ?

•         ?




                  3
• Clang       ?

•         ?




                  4
//===----------------------------------------------------------------------===//
// C Language Family Front-end
//===-----...
//===----------------------------------------------------------------------===//
// C Language Family Front-end
//===-----...
C/C++/Obj-C

• LLVM.org
• 2006
• written in C++
                   6
C/C++/Obj-C


•             LLVM

• Apple
•                    (as LLVM)

•                (as LLVM)
                     ...
LLVM

•
•
•
• JIT
               8
Apple

•
    • gcc    (           )

    • MSVC       (   )

•
                             9
•
    •
    •
    • AST
•
•           10
Frontend
                Lex


                                         Driver
Basic           Parse
                     ...
Core 1/5:
clang/Basic
•         I/O (+          )

•
•
    •
•       basic      LLVM

                              12
Core 2/5:                                  Token


 clang/Lex                   Preprocessor
                             ...
Core 3/5:
clang/Parse
                                    Token


• Parser, Action, ....
• Parser:            Preprocessor...
Core 4/5:
    clang/Sema
                       Parser     Action


•   ParseAST(Preprocessor &PP,               AST
     ...
Core 5/5:
                     Translation      AST
                        Unit         Context



clang/AST             ...
Decl
                                            Type

              NamedDecl

                                          ...
App libraries
• Analysis:
 •
• Rewrite:
 •             (Rope, DeltaTree)

 • Obj-C → C
 • HTML
• AST
                     ...
• Clang       ?

•         ?




                  19
•
    (       )

•
    (   )



                20
•
                         OK: Driver/clang.cpp

•
    • ASTConsumer
    • DiagnosticClient
•
                            ...
:   main()
               :
- #include-s
-




                            22
#include-s:
              #include   <vector>
              #include   <string>
              #include   <iosfwd>
        ...
: ASTConsumer

• Driver/ASTConsumers.cpp
  • ASTDumper:
  • DeclContextPrinter: ( )
•                     :
  ↑

         ...
class DeclContextPrinter : public ASTConsumer {
  llvm::raw_ostream& Out;
public:
  DeclContextPrinter() : Out(llvm::errs(...
void DeclContextPrinter::PrintDeclContext(const DeclContext* DC,
                                          unsigned Indent...
AST
•                 /
    (better doxygen, GLOBAL, LXR, ...)
•
    (better/worse RPC, Serializer, SWIG, ...)
•          ...
•                (   FS)

•       (                   )

•       (Lexer       )

• C++
• ...
                           28
5       C/Objc-C/C++

    •
    •             +

    •                       ?

    •                 (ry

               ...
Clang

• C++   C++                         !!!

•                       !!!

•        Apple!!!

  •                       ...
For more:
•   http://clang.llvm.org/get_started.html


•   http://llvm.org/devmtg/2008-08/Naroff_Clang.pdf


•   trunk/Dri...
Upcoming SlideShare
Loading in...5
×

clang-intro

4,076

Published on

an unofficial, non-gentle introduction to clang compiler.

Published in: Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
4,076
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
67
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Transcript of "clang-intro"

  1. 1. Clang: A New Tool for Old Languages MORITA Hajime <omo@dodgson.org> http://steps.dodgson.org/ 2009/03/21 1
  2. 2. //===----------------------------------------------------------------------===// // C Language Family Front-end //===----------------------------------------------------------------------===// Chris Lattner I. Introduction: clang: noun 1. A loud, resonant, metallic sound. 2. The strident call of a crane or goose. 3. C-language family front-end toolkit. http://llvm.org/svn/llvm-project/cfe/trunk/README.txt
  3. 3. • Clang ? • ? 3
  4. 4. • Clang ? • ? 4
  5. 5. //===----------------------------------------------------------------------===// // C Language Family Front-end //===----------------------------------------------------------------------===// Chris Lattner I. Introduction: clang: noun 1. A loud, resonant, metallic sound. 2. The strident call of a crane or goose. 3. C-language family front-end toolkit. http://llvm.org/svn/llvm-project/cfe/trunk/README.txt
  6. 6. //===----------------------------------------------------------------------===// // C Language Family Front-end //===----------------------------------------------------------------------===// Chris Lattner I. Introduction: clang: noun 1. A loud, resonant, metallic sound. 2. The strident call of a crane or goose. 3. C-language family front-end toolkit. http://llvm.org/svn/llvm-project/cfe/trunk/README.txt
  7. 7. C/C++/Obj-C • LLVM.org • 2006 • written in C++ 6
  8. 8. C/C++/Obj-C • LLVM • Apple • (as LLVM) • (as LLVM) 7
  9. 9. LLVM • • • • JIT 8
  10. 10. Apple • • gcc ( ) • MSVC ( ) • 9
  11. 11. • • • • AST • • 10
  12. 12. Frontend Lex Driver Basic Parse CodeGen LLVM/ Sema LLVM System Analysis AST Rewrite Core App 11
  13. 13. Core 1/5: clang/Basic • I/O (+ ) • • • • basic LLVM 12
  14. 14. Core 2/5: Token clang/Lex Preprocessor Preprocessor Lexer • Lexer, Token, Preprocessor, ... • Lexer: Lexer PTHLexer • pull style: Lexer::Lex(Token& result); • hand-written • (: ) • PTH(Preprocessed-Token-Header) 13
  15. 15. Core 3/5: clang/Parse Token • Parser, Action, .... • Parser: Preprocessor Parser Action • push-style • C/C++/Obj-C (!) 14
  16. 16. Core 4/5: clang/Sema Parser Action • ParseAST(Preprocessor &PP, AST ASTConsumer *Consumer, Consumer TranslationUnit *TU, Sema bool PrintStats); Translation • Sema : public Action Unit • callback+Token→AST 15
  17. 17. Core 5/5: Translation AST Unit Context clang/AST Type Decl Expr • Decl : • VarDecl, FunctionDecl, CXXRecordDecl, ... • Type : • QualType, BuiltinType, ComplexType, ... • Expr : • DeclRefExpr, IntegerLiteral, UnaryOperator, ... 16
  18. 18. Decl Type NamedDecl QualType DeclContext ValueDecl ParmVar Decl FunctionDecl Stmt Expr CXX MethodDecl CXX RecordDecl 17
  19. 19. App libraries • Analysis: • • Rewrite: • (Rope, DeltaTree) • Obj-C → C • HTML • AST 18
  20. 20. • Clang ? • ? 19
  21. 21. • ( ) • ( ) 20
  22. 22. • OK: Driver/clang.cpp • • ASTConsumer • DiagnosticClient • 21
  23. 23. : main() : - #include-s - 22
  24. 24. #include-s: #include <vector> #include <string> #include <iosfwd> #include <llvm/Support/raw_ostream.h> #include <clang/Frontend/CompileOptions.h> #include <clang/Frontend/PathDiagnosticClients.h> #include <clang/Frontend/InitHeaderSearch.h> #include <clang/Frontend/TextDiagnosticBuffer.h> #include <clang/Frontend/TextDiagnosticPrinter.h> #include <clang/Analysis/PathDiagnostic.h> #include <clang/AST/Decl.h> #include <clang/AST/DeclCXX.h> #include <clang/AST/TranslationUnit.h> #include <clang/CodeGen/ModuleBuilder.h> #include <clang/Sema/ParseAST.h> #include <clang/Sema/SemaDiagnostic.h> #include <clang/AST/ASTConsumer.h> #include <clang/Parse/Parser.h> #include <clang/Lex/HeaderSearch.h> #include <clang/Lex/LexDiagnostic.h> #include <clang/Basic/FileManager.h> • #include <clang/Basic/SourceManager.h> #include <clang/Basic/TargetInfo.h> #include <clang/Basic/Diagnostic.h> #include <llvm/ADT/OwningPtr.h> #include <llvm/ADT/SmallPtrSet.h> #include <llvm/ADT/StringExtras.h> #include <llvm/Config/config.h> #include <llvm/Support/CommandLine.h> #include <llvm/Support/ManagedStatic.h> #include <llvm/Support/MemoryBuffer.h> #include <llvm/Support/PluginLoader.h> #include <llvm/Support/PrettyStackTrace.h> #include <llvm/Support/Timer.h> #include <llvm/System/Host.h> #include <llvm/System/Path.h> #include <llvm/System/Signals.h> #include <iostream> 23
  25. 25. : ASTConsumer • Driver/ASTConsumers.cpp • ASTDumper: • DeclContextPrinter: ( ) • : ↑ 24
  26. 26. class DeclContextPrinter : public ASTConsumer { llvm::raw_ostream& Out; public: DeclContextPrinter() : Out(llvm::errs()) {} void HandleTranslationUnit(TranslationUnit& TU) { TranslationUnitDecl* TUD = TU.getContext().getTranslationUnitDecl(); PrintDeclContext(TUD, 4); } void PrintDeclContext(const DeclContext* DC, unsigned Indentation) }; 25
  27. 27. void DeclContextPrinter::PrintDeclContext(const DeclContext* DC, unsigned Indentation) { // Print DeclContext name. switch (DC->getDeclKind()) { case Decl::TranslationUnit: Out << quot;[translation unit] quot; << DC; break; case Decl::Namespace: { Out << quot;[namespace] quot;; const NamespaceDecl* ND = cast<NamespaceDecl>(DC); Out << ND->getNameAsString(); break; } ... case Decl::CXXRecord: { const CXXRecordDecl* RD = cast<CXXRecordDecl>(DC); if (RD->isDefinition()) Out << quot;[class] quot;; else Out << quot;<class> quot;; Out << RD->getNameAsString() << quot; quot; << DC; break; } ... 26
  28. 28. AST • / (better doxygen, GLOBAL, LXR, ...) • (better/worse RPC, Serializer, SWIG, ...) • , AST • AOP ( , , ....) • http://people.imendio.com/richard/gtk-rewriter/ 27
  29. 29. • ( FS) • ( ) • (Lexer ) • C++ • ... 28
  30. 30. 5 C/Objc-C/C++ • • + • ? • (ry 29
  31. 31. Clang • C++ C++ !!! • !!! • Apple!!! • !!! 30
  32. 32. For more: • http://clang.llvm.org/get_started.html • http://llvm.org/devmtg/2008-08/Naroff_Clang.pdf • trunk/Driver/ clang • http://www.flickr.com/photos/darrenhunter/2918246464/ 31
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×