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

作者:史宁宁(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类中(clang/include/clang/Lex/PreprocessorLexer.h),有一个成员变量:

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的作用。

时间: 2024-08-06 03:51:19

深入研究Clang(五) Clang Lexer代码阅读笔记的相关文章

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

clang/include/clang/Lex/Preprocesser.h 这个文件是包含clang::Preprocesser类的定义的头文件.它是类C语言(C.C++.Object C)的预处理的头文件.也就是说,类C语言的预处理都会用到此处的代码. 00082 /// \brief Context in which macro name is used. 00083 enum MacroUse { 00084 MU_Other = 0, // other than #define or

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

作者:史宁宁(snsn1984) 源码位置:clang/lib/Lexer.cpp 源码网络地址:http://clang.llvm.org/doxygen/Lexer_8cpp_source.html Lexer.cpp这个文件,是Clang这个前端的词法分析器的主要文件,它的内容是对Lexer这个类的具体实现,原文件的注释中:"This file implements the Lexer and Token interfaces." 这么解释这个文件的,但是Token只有两个简单函

梦断代码阅读笔记有感之二

08梦断代码阅读笔记有感之二 在梦断代码的一开始我们就学会了如何去写代码,如何成功的去做一个软件工程师. 在现在人的严重,也许软件工程师写出的代码只是让人在玩游戏,在用一些简单的用代码写出的软件.只是认为工程师在不断地重复着一个动作:写代码.但我只能说你们大错特错,就像在文章中说的那样,其实软件工程师是在:“改变世界”,他们利用他们的手用键盘在电脑上打出一行一行的代码,程序产生了,一个新的软件也就产生了.而且,众所周知的,工程师做一个软件,总是在无限制的更新他的内容,让我们的软件更加的先进化,就

梦断代码阅读笔记有感之三

09梦断代码阅读笔记之三 这是最后一篇的阅读笔记,我发现时间真的过的好快好快. 想想以前,我们总是在应付一切的差事,但是真正的到最后,我们才发现,到最后吃亏的还是我们自己. 从前的我们,我们总是对自己大脑中的东西一片一片的特别的混乱.其实我们就像作者所说的,我们就像放任胡乱的cd随地的乱放,到最后不知道哪是哪.我们应该学会分类去放置,例如:我们可以根据歌唱者的名字,音乐的类型等等,其实这些东西对于我们写代码也同样的试用,刚开始的上大一时候,我们第一次的学习C++,我们只是随时随地的在写代码,并不

socketserver.py代码阅读笔记

socketserver.py源码阅读笔记 前言 一直想弄清楚一个http server和Web框架的工作原理.但以我目前的实力,阅读一个http server或web框架代码还是太难了.后来又对异步IO.并发产生的兴趣.前几天做一个大作业需要写几个各种不同并发模型的TCP Server,写完才想起Python有现成的socketsever模块可以用,完全不需要自己写.于是对比了一下我写的代码和socketsever.py,发现我写的真没socketsever写的好.我的代码经验还是太少了.于是

梦断代码阅读笔记之一

最近阅读了罗森伯格的<梦断代码>,算是近距离观察了十几年前软件开发的状态.这本书是作者对OSAF主持的Chandler项目进行田野调查  而写的一本书.本书是在讲一事,也是在讲百千事:是写一软件,也是在写千百软件.在描述Chandler项目的过程当中亦提出了很多观点,带给我们很多思考.让我们这些软件工程专业的学生对软件开发有了一个更深层次的认知. 在本书第一章,作者为我们介绍了一个布鲁克斯法则:"往已延误的项目里补充人力,只会使其继续延误". 布鲁克斯曾是IBM的资深程序经

梦断代码阅读笔记二(4-7章)

在上一周<梦断代码>读完了第七章,全书已经过半,对于这本书有了更深的体会,对于软件开发之难也更加理解.      乐高王国一章中引出了一个代码世界或者说程序员世界里的美好设想——程序将由可复用的部件组合而成,软件部件将在全球范围内提供,软件工程将从编程的窠臼中解放出来.软件组件就像乐高积木一样,细小.不能再分.可被替代.可以自由组合.这是代码复用的概念,这会省去编写代码的麻烦,但是也存在不少问题,诸如大型可复用组件的稀少,有些程序员不愿拾人牙慧等等.其实我认为这是一个不错的设想,也是一个值得努

梦断代码阅读笔记(5-7)

第五章--管束奇客和狗 用代码的多少去衡量一个程序员的好坏是最白痴的做法,一段精彩的代码或许仅仅是小小的一段创新,而那种做法只会让越来越多的人敲出越来越杂乱无章繁琐的代码.在变成当中我们可以用一部分时间来寻求简洁的方法路径,磨刀不误砍柴工,但是我们不能把这段时间过多的放大.就像我本来做饭而去超市买醋,却看到了旁边的衣服,逛了半天商场,忘记了本事是要来买醋的. 第六章--完成设计方案 通过对这一章节的阅读,让我认识到了对程序备份的重要性.或许我们可以足够的相信自己,但是我们也要为所有的结果预留合适

&lt;&lt;梦断代码&gt;&gt;阅读笔记三

看完了这最后三分之一的<梦断代码>,意味着这本软件行业的著作已经被我粗略地过了一遍. 在这最后三分之一的内容中,我深入了解了在大型软件项目的运作过程中存在的困难和艰辛.一个大型软件项目的成功代表着这团队所付出的所有心血,以及那不为 人知的无数个‘人月’.而联想自己的专业,产生了一点迷惘,这就是我今后要走的道路么,我能走得多远,我能否像书中所提到的那些人一样百折不挠,这一切我 都无从得知.但是我只能向前走,别无选择,没有人会承认自己不如别人,哪怕现在不如,但总会寄托于未来,未来是未知的,但又是现