深入研究Clang(五) Clang Lexer代码阅读笔记之Lexer

简介: 作者:史宁宁(snsn1984)Clang的Lexer(词法分析器)的源码的主要位置如下:clang/lib/Lex    这里是主要的Lexer的代码;clang/include/clang/Lex   这里是Lexer的头文件代码的位置;同时,Lexer还使用了clangBasic库,所以要分析Lexer的代码,clangBasic(clang/lib/Basic)的一些代码也会用到。

作者:史宁宁(snsn1984)


Clang的Lexer(词法分析器)的源码的主要位置如下:

clang/lib/Lex    这里是主要的Lexer的代码;

clang/include/clang/Lex   这里是Lexer的头文件代码的位置;

同时,Lexer还使用了clangBasic库,所以要分析Lexer的代码,clangBasic(clang/lib/Basic)的一些代码也会用到。


首先从Lexer入手。


clang/include/clang/Lex/Lexer.h

clang::Lexer:

00057   //===--------------------------------------------------------------------===//
00058   // Context-specific lexing flags set by the preprocessor.
00059   //
00060
00061   /// ExtendedTokenMode - The lexer can optionally keep comments and whitespace
00062   /// and return them as tokens.  This is used for -C and -CC modes, and
00063   /// whitespace preservation can be useful for some clients that want to lex
00064   /// the file in raw mode and get every character from the file.
00065   ///
00066   /// When this is set to 2 it returns comments and whitespace.  When set to 1
00067   /// it returns comments, when it is set to 0 it returns normal tokens only.
00068   unsigned char ExtendedTokenMode;
00069
00070   //===--------------------------------------------------------------------===//
这个成员变量保存词法分析的一个状态,根据它的值的不同:0、1、2,分别对应只返回正常的token,返回comments
和正常的token,返回空格、comments和正常的token。
下面是几个操作这个成员变量的函数,基本上都是获取值、设置值和重设值。代码不复杂,
00162   /// isKeepWhitespaceMode - Return true if the lexer should return tokens for
00163   /// every character in the file, including whitespace and comments.  This
00164   /// should only be used in raw mode, as the preprocessor is not prepared to
00165   /// deal with the excess tokens.
00166   bool isKeepWhitespaceMode() const {
00167     return ExtendedTokenMode > 1;
00168   }
00169
00170   /// SetKeepWhitespaceMode - This method lets clients enable or disable
00171   /// whitespace retention mode.
00172   void SetKeepWhitespaceMode(bool Val) {
00173     assert((!Val || LexingRawMode || LangOpts.TraditionalCPP) &&
00174            "Can only retain whitespace in raw mode or -traditional-cpp");
00175     ExtendedTokenMode = Val ? 2 : 0;
00176   }
00177
00178   /// inKeepCommentMode - Return true if the lexer should return comments as
00179   /// tokens.
00180   bool inKeepCommentMode() const {
00181     return ExtendedTokenMode > 0;
00182   }
00183
00184   /// SetCommentRetentionMode - Change the comment retention mode of the lexer
00185   /// to the specified mode.  This is really only useful when lexing in raw
00186   /// mode, because otherwise the lexer needs to manage this.
00187   void SetCommentRetentionState(bool Mode) {
00188     assert(!isKeepWhitespaceMode() &&
00189            "Can't play with comment retention state when retaining whitespace");
00190     ExtendedTokenMode = Mode ? 1 : 0;
00191   }
00192
00193   /// Sets the extended token mode back to its initial value, according to the
00194   /// language options and preprocessor. This controls whether the lexer
00195   /// produces comment and whitespace tokens.
00196   ///
00197   /// This requires the lexer to have an associated preprocessor. A standalone
00198   /// lexer has nothing to reset to.
00199   void resetExtendedTokenMode();
关于raw mode:
raw mode的时候,ExtendedTokenMode = 2,Lexer会输出包含空格、comments和正常tokens在内的所有
字符。在Lexer的父类:clang::PreprocessorLexer类中(),有一个成员变量:
00049   /// \brief True if in raw mode.
00050   ///
00051   /// Raw mode disables interpretation of tokens and is a far faster mode to
00052   /// lex in than non-raw-mode.  This flag:
00053   ///  1. If EOF of the current lexer is found, the include stack isn't popped.
00054   ///  2. Identifier information is not looked up for identifier tokens.  As an
00055   ///     effect of this, implicit macro expansion is naturally disabled.
00056   ///  3. "#" tokens at the start of a line are treated as normal tokens, not
00057   ///     implicitly transformed by the lexer.
00058   ///  4. All diagnostic messages are disabled.
00059   ///  5. No callbacks are made into the preprocessor.
00060   ///
00061   /// Note that in raw mode that the PP pointer may be null.
00062   bool LexingRawMode;
它可以表明Lexer是否在raw mode下。同时,这里的注释也说明了raw model的作用。

从clang::Lexer的定义可以看出,它是clang::PreprocessorLexer的子类,上面raw model的部分也引用了clang::PreprocessorLexer类的代码,下面看下clang::PreprocessorLexer的代码。

clang/include/clang/Lex/PreprocessorLexer.h

00022 namespace clang {
00023 
00024 class FileEntry;
00025 class Preprocessor;
从这里可以看出clang::PreprocessorLexer使用了上面两个类,而在头文件中的具体位置就是:

00027 class PreprocessorLexer {
00028   virtual void anchor();
00029 protected:
00030   Preprocessor *PP;              // Preprocessor object controlling lexing.
以及

00164   /// getFileEntry - Return the FileEntry corresponding to this FileID.  Like
00165   /// getFileID(), this only works for lexers with attached preprocessors.
00166   const FileEntry *getFileEntry() const;

从代码中可以看出,这两个类,一个是作为成员变量,一个是作为了一个成员函数的返回类型来使用的。我们跟踪代码去看下这两个类的具体实现。这两个类的具体实现,FileEntry较为简单,很容易看出到底内容;而Preprocessor类较为复杂,牵涉内容较多,在这里暂且不作分析。后续继续分析。






目录
相关文章
|
Linux
Win或Linux系统下用conda安装Open Babel
Win或Linux系统下用conda安装Open Babel
2107 0
Win或Linux系统下用conda安装Open Babel
|
12月前
|
Rust 前端开发 JavaScript
前端技术新探索:从React到WebAssembly的高效之路
前端技术新探索:从React到WebAssembly的高效之路
339 2
|
3月前
|
人工智能 数据可视化 前端开发
蚂蚁的可视化图表 MCP 首发上线!支持超过 25 种的可视化图表生成,也支持生成路书!
蚂蚁 AntV 团队推出可视化图表 MCP 插件,支持 25+ 种图表类型,涵盖统计图、关系图及地图标注、路径地图等,用户只需输入文本即可生成炫酷图表,大幅简化数据可视化流程,提升数据展示效率。
461 23
|
11月前
|
人工智能 Rust 前端开发
前端界的未来趋势:掌握这些新技术,让你的作品走在时代前沿!
【10月更文挑战第30天】前端开发正以前所未有的速度发展,新技术层出不穷。本文探讨了AI助手(如GitHub Copilot)、低代码/无代码平台、跨平台技术(如React Native)和WebAssembly等未来主流技术,并附上示例代码,帮助你走在时代前沿。
441 1
|
7月前
|
运维 Kubernetes 调度
《探秘Kubernetes核心:Pod设计理念与调度基石》
Kubernetes作为容器编排领域的事实标准,其核心调度单元Pod在应用运行与管理中占据重要地位。Pod通过封装紧密协作的容器,实现资源共享与生命周期统一管理,贴近应用本质并简化部署运维。作为资源分配与隔离的最佳粒度,Pod适应复杂应用架构,支持弹性伸缩,提升系统性能与可靠性。其设计理念推动了容器编排技术发展,促进了云原生应用普及,对云计算领域影响深远。
156 11
|
9月前
|
Dart 前端开发 Android开发
【02】写一个注册页面以及配置打包选项打包安卓apk测试—开发完整的社交APP-前端客户端开发+数据联调|以优雅草商业项目为例做开发-flutter开发-全流程-商业应用级实战开发-优雅草央千澈
【02】写一个注册页面以及配置打包选项打包安卓apk测试—开发完整的社交APP-前端客户端开发+数据联调|以优雅草商业项目为例做开发-flutter开发-全流程-商业应用级实战开发-优雅草央千澈
210 1
【02】写一个注册页面以及配置打包选项打包安卓apk测试—开发完整的社交APP-前端客户端开发+数据联调|以优雅草商业项目为例做开发-flutter开发-全流程-商业应用级实战开发-优雅草央千澈
|
开发者 API Windows
从怀旧到革新:看WinForms如何在保持向后兼容性的前提下,借助.NET新平台的力量实现自我进化与应用现代化,让经典桌面应用焕发第二春——我们的WinForms应用转型之路深度剖析
【8月更文挑战第31天】在Windows桌面应用开发中,Windows Forms(WinForms)依然是许多开发者的首选。尽管.NET Framework已演进至.NET 5 及更高版本,WinForms 仍作为核心组件保留,支持现有代码库的同时引入新特性。开发者可将项目迁移至.NET Core,享受性能提升和跨平台能力。迁移时需注意API变更,确保应用平稳过渡。通过自定义样式或第三方控件库,还可增强视觉效果。结合.NET新功能,WinForms 应用不仅能延续既有投资,还能焕发新生。 示例代码展示了如何在.NET Core中创建包含按钮和标签的基本窗口,实现简单的用户交互。
272 0
|
Java Linux Shell
docker 打包 springboot 项目快速入门
docker 打包 springboot 项目快速入门
189 0
|
移动开发 搜索推荐 UED
HTML标签的语义化:为何重要及其实践
HTML标签的语义化:为何重要及其实践
217 0
|
存储 程序员
【汇编】内存的读写与地址空间、寄存器及数据存储
【汇编】内存的读写与地址空间、寄存器及数据存储
1091 1
【汇编】内存的读写与地址空间、寄存器及数据存储