Clang Preprocessor 类的创建
参考:
Create a working compiler with the LLVM framework, Part 2
How to parse C programs with Clang: A tutorial
[Clang,libClang] exercise1 : FileManager
注意:此篇笔记各类之构造函数源码来自 llvm/clang doxygen 5.0.0 svn,与现行版本(此时为 3.9.1)有一定出入,其中主要是现行版本有些参数是 llvm::IntrusiveRefCntPtr(llvm 实现的 smart pointer)而不是 std::shared_ptr
一、Preprocessor 的构造函数
前端由许多部分组成,其中第一部分通常是一个 lexer,clang 中 Preprocessor 类是 lexer 的 main interface。出于性能考虑,clang 没有独立的预处理器程序,而是在 lexing 的过程中进行预处理。
Preprocessor 的构造函数如下所示:
1 Preprocessor::Preprocessor(
2 std::shared_ptr<PreprocessorOptions> PPOpts, // constructor: PreprocessorOptions()
3 DiagnosticsEngine& diags,
4 LangOptions& opts, // constructor: LangOptions()
5 // Keep track of the various options that can be enabled,
6 // which controls the dialect of C or C++ that is accepted
7 SourceManager& SM,
8 HeaderSearch& Headers,
9 ModuleLoader& TheModuleLoader,
10 IdentifierInfoLookup* IILookup = nullptr,
11 bool OwnsHeaderSearch = false,
12 TranslationUnitKind TUKind = TU_Complete
13 )
Preprocessor 的构造函数
DiagnosticsEngine : 用来给用户报告错误和警告信息。构造函数如下:
1 DiagnosticsEngine::DiagnosticsEngine(
2 IntrusiveRefCntPtr<DiagnosticIDs> Diags, // constructor: DiagnosticIDs()
3 // used for handling and querying diagnostic IDs
4 DiagnosticOptions* DiagOpts, // constructor: DiagnosticOptions()
5 // Options for controlling the compiler diagnostics engine
6 DiagnosticConsumer* client = nullptr,
7 bool ShouldOwnClient = true
8 )
DiagnosticsEngine 的构造函数
其中 DiagnosticConsumer 是一个抽象接口,由前端的 clients 实现,用来 formats and prints fully processed diagnostics。clang 内置一个 TextDiagnosticsConsumer 类,将错误和警告信息写到 console 上,clang binary 用的 DiagnosticConsumer 也是这个类。TextDiagnosticsConsumer 的构造函数如下:
1 TextDiagnosticPrinter::TextDiagnosticPrinter(
2 raw_ostream& os, // llvm::outs() returns a reference to a raw_ostream for standard output
3 DiagnosticOptions* diags,
4 bool OwnsOutputStream = false // within destructor:(OS is the member, initialized with os)
5 // if (OwnsOutputStream) delete &OS
6 )
TextDiagnosticPrinter 的构造函数
SourceManager :handles loading and caching of source files into memory。构造函数如下:
1 SourceManager::SourceManager(vim
2 DiagnosticsEngine& Diag,
3 FileManager& FileMgr,
4 bool UserFilesAreVolatile = false
5 )
SourceManager 的构造函数
FileManager :实现了对文件系统查找、文件系统缓存、目录查找管理的支持。构造函数如下:
1 FileManager::FileManager(
2 const FileSystemOptions& FileSystemOpts, // use default constructor
3 IntrusiveRefCntPtr<vfs::FileSystem> FS = nullptr
4 )
FileManager 的构造函数
HeaderSearch :Encapsulates the information needed to find the file referenced by a #include or #include_next, (sub-)framework lookup, etc。构造函数如下:
1 HeaderSearch::HeaderSearch(
2 std::shared_ptr<HeaderSearchOptions> HSOpts, // constructor: HeaderSearchOptions::HeaderSearchOptions(StringRef _Sysroot = "/")
3 SourceManager& SourceMgr,
4 DiagnosticsEngine& Diags,
5 const LangOptions& LangOpts,
6 const TargetInfo* Target
7 )
HeaderSearch 的构造函数
TargetInfo :Exposes information about the current target。 其构造函数为 protected,因此需要调用工厂函数 static TargetInfo* TargetInfo::CreateTargetInfo(DiagnosticsEngine &Diags, const std::shared_ptr<TargetOptions>& Opts) ,其中 TargetOptions 类包含 target 的相关信息,如 CPU、ABI 等。类中有一个属性 Triple 用以定义 target 的架构。Triple 是一个 string,形如 i386-apple-darwin,通过 llvm::sys::getDefaultTargetTriple() 可以获得编译 llvm 的机器的 host triple。
ModuleLoader:描述了 module loader 的抽象接口。Module loader 负责解析一个 module name(如“std”),将其与实际的 module file 联系起来,并加载该 module。CompilerInstance 便是一个实现了该接口的 module loader。
二、通过 CompilerInstance 创建 Preprocessor
比起手写 Preprocessor,CompilerInstance 更加实用一些。CompilerInstance 主要有两个作用:(1)管理运行编译器所必须的各个对象,如 preprocessor、target information、AST context 等;(2)提供创建和操作常用 Clang 对象的有用方法。下面是其类定义的一部分:
1 class CompilerInstance : public ModuleLoader {
2 /// The options used in this compiler instance.
3 std::shared_ptr<CompilerInvocation> Invocation;
4 /// The diagnostics engine instance.
5 IntrusiveRefCntPtr<DiagnosticsEngine> Diagnostics;
6 /// The target being compiled for.
7 IntrusiveRefCntPtr<TargetInfo> Target;
8 /// The file manager.
9 IntrusiveRefCntPtr<FileManager> FileMgr;
10 /// The source manager.
11 IntrusiveRefCntPtr<SourceManager> SourceMgr;
12 /// The preprocessor.
13 std::shared_ptr<Preprocessor> PP;
14 /// The AST context.
15 IntrusiveRefCntPtr<ASTContext> Context;
16 /// An optional sema source that will be attached to sema.
17 IntrusiveRefCntPtr<ExternalSemaSource> ExternalSemaSrc;
18 /// The AST consumer.
19 std::unique_ptr<ASTConsumer> Consumer;
20 /// The semantic analysis object.
21 std::unique_ptr<Sema> TheSema;
22 /// ...
23 };
CompilerInstance 类定义的一部分
下列代码通过 CompilerInstance 来创建 Preprocessor:
1 #include <memory>
2
3 #include "clang/Basic/LangOptions.h"
4 #include "clang/Basic/TargetInfo.h"
5 #include "clang/Frontend/CompilerInstance.h"
6
7 int main() {
8 clang::CompilerInstance ci;
9
10 ci.createDiagnostics();
11
12 std::shared_ptr<clang::TargetOptions> pTargetOptions =
13 std::make_shared<clang::TargetOptions>();
14 pTargetOptions->Triple = llvm::sys::getDefaultTargetTriple();
15 clang::TargetInfo *pTargetInfo =
16 clang::TargetInfo::CreateTargetInfo(ci.getDiagnostics(), pTargetOptions);
17 ci.setTarget(pTargetInfo);
18
19 ci.createFileManager();
20 ci.createSourceManager(ci.getFileManager());
21 ci.createPreprocessor(clang::TU_Complete);
22
23 return 0;
24 }
Use CompilerInstance to construct Preprocessor
首先创建 DiagnosticsEngine(通过 createDiagnostics()),然后创建并设置 TargetInfo,然后依次创建 FileManager(通过 createFileManager()),SourceManager(通过 createSourceManager (FileManager &FileMgr)),最后创建 Preprocessor(createPreprocessor(TranslationUnitKind))。
三、FileManager 与 SourceManager
FileManager:
p, li { white-space: pre-wrap }
Clang Preprocessor 类的创建的更多相关文章
- C# 根据类名称创建类示例
//获得类所在的程序集名称(此处我选择当前程序集) string bllName = System.IO.Path.GetFileNameWithoutExtension(System.Reflect ...
- php简单实用的操作文件工具类(创建、移动、复制、删除)
php简单实用好用的文件及文件夹复制函数和工具类(创建.移动.复制.删除) function recurse_copy($src,$dst) { // 原目录,复制到的目录 $dir = opend ...
- 李洪强iOS开发之OC[013] -类的创建的练习
// // main.m // 12 - 类的创建练习 // // Created by vic fan on 16/7/9. // Copyright © 2016年 李洪强. All ri ...
- C++:类的创建
类的创建 #include<iostream> #include<cmath> using namespace std; class Complex //声明一个名为Compl ...
- 2--OC -- 类的创建与实例化
2.OC -- 类的创建与实例化 一.OC类的简述 1.OC类分为2个文件:.h文件用于类的声明,.m文件用于实现.h的函数: 2.类是声明使用关键字:@interface.@end : 3.类是 ...
- JAVA类的创建: 创建JAVA的类 ,JAVA的字段,JAVA类的方法
1. 创建Java的类 如果说Java的一切都是对象,那么类型就是决定了某一类对象的外观与行为.可是类型的关键字不是type,而是class,创建一个新的类型要用下面的代码: 1 2 3 class ...
- python 通过元类控制类的创建
一.python中如何创建类? 1. 直接定义类 class A: a = 'a' 2. 通过type对象创建 在python中一切都是对象 在上面这张图中,A是我们平常在python中写的类,它可以 ...
- Day 5-7 exec 和元类的创建.
exec方法 元类 exec(str_command,globals,locals)参数1:字符串形式的命令参数2:全局作用域(字典形式). 如果不指定,默认globals参数3:局部作用(字典形式) ...
- Egret 类的创建和继承--TypeScript
class test extends egret.DisplayObjectContainer { /** * 类的创建 */ //属性 name: string; age: number; ts: ...
- 快速创建SpringBoot2.x应用之工具类自动创建web应用、SpringBoot2.x的依赖默认Maven版本
快速创建SpringBoot2.x应用之工具类自动创建web应用简介:使用构建工具自动生成项目基本架构 1.工具自动创建:http://start.spring.io/ 2.访问地址:http://l ...
随机推荐
- JS 保姆级贴心,从零教你手写实现一个防抖debounce方法
壹 ❀ 引 防抖在前端开发中算一个基础但很实用的开发技巧,在对于一些高频操作例如监听输入框值变化触发更新之类,会有奇效.除了实际开发,在面试中我们也可能偶遇手写防抖节流的问题,鉴于不同公司考核要求不一 ...
- NC22598 Rinne Loves Edges
题目链接 题目 题目描述 Rinne 最近了解了如何快速维护可支持插入边删除边的图,并且高效的回答一下奇妙的询问. 她现在拿到了一个 n 个节点 m 条边的无向连通图,每条边有一个边权 \(w_i\) ...
- SATA学习笔记——Transport Layer 概述
一.故事前传 在之前的文章中,我们有提到SATA主要包括:应用层(Application Layer), 传输层(Transport Layer),链路层(Link Layer)以及物理层(Physi ...
- [技术选型与调研] 流程引擎/工作流引擎:Activiti、Flowable、Camunda
1 概述:流程与流程引擎 低代码平台.办公自动化(OA).BPM平台.工作流系统均需要流程引擎功能 [工作流引擎的三大功能] 1)验证当前过程状态:在给定当前状态的情况下,检查是否有效执行任务. 2) ...
- Java I/O 教程(四) FileInputStream 类
Java FileInputStream class 从一个文件读取字节数据. 用于从图像,音频,视频等文件中读取字节类型数据. 类定义 public class FileInputStream ex ...
- golang 打隧道和端口转发
`package main import ( "golang.org/x/crypto/ssh" "io" "log" "net& ...
- windbg 学习
常用的 windbg 命令 .ecxr 用来切换到异常发生时的上下文,主要用在分析静态 dump 文件的时候.当我们使用 .reload 命令去强制加载库的 pdb 文件后,需要执行 .ecxr 命令 ...
- virtualapp安装应用流程源码分析
1. HomeActivity 为处理的入口 @Override protected void onActivityResult(int requestCode, int resultCode, In ...
- 【Java复健指南13】OOP高级04【告一段落】-四大内部类
四大内部类 一个类的内部又完整的嵌套了另一个类结构. class Outer{ //外部类 class lnner{ //内部类 } } class Other{//外部其他类 } 被嵌套的类称为内部 ...
- ZYNQ 裸机模式下修改默认uart端口
## 背景 调试ZYNQ 裸机code, 调用 printf()后在UART端口无法看到打印信息输出,查看原理图后发现,板子用的UART 1作为默认串口调试接口,UART 0分配给了RS485使用,因 ...