[cfe-dev] -emit-html example

Argiris Kirtzidis akyrtzi at gmail.com
Sun Apr 20 16:20:28 PDT 2008


Chris Lattner wrote:
> One of the spiffy things that Ted is doing with his static analysis 
> stuff is having it emit reports in HTML.  A required part of this is 
> just being able to turn code itself into HTML.  I think that the stuff 
> clang is doing is pretty cool, so I thought I'd show an example.
>
> [.....]
>
> Anyway, give -emit-html a try, if you have ideas for making it better, 
> it's really easy to improve: for example, the code to do the macro 
> expansions is ~70 lines of commented code at the end of HTMLRewrite.

Hey, this is awesome!

I had an idea to generalize it a bit to allow for other uses of source 
annotation. The attached patch adds a 'Annotate' library which provides 
interfaces for source file annotation.
There are an 'Annotator' class (derived from ASTConsumer) that traverses 
the AST and dispatches annotations to a 'AnnotationClient' object.
The AnnotationClient is like this:

-------------------------------------------------------------------------------------------------------------------
/// AnnotationClient - This is an abstract interface that should be 
implemented
/// by clients that receive and handle annotations for a source file. The
/// annotations are provided by an Annotator object that receives a 
particular
/// AnnotationClient and calls its methods.
class AnnotationClient {
public:
  virtual ~AnnotationClient();

  virtual void Initialize(SourceManager &SM, unsigned FileID) {}

  virtual void HighlightComments(unsigned Begin, unsigned End) {}

  virtual void HighlightDirective(unsigned Begin, unsigned End) {}

  virtual void HighlightKeyword(unsigned Begin, unsigned End,
                                tok::TokenKind Kind) {}

  virtual void HighlightMacro(unsigned Begin, unsigned End,
                              std::string& Expansion) {}

  /// HighlightVar - isDeclared is true if this is a new variable 
declaration,
  /// false if its a use of a variable name. Receives all file/local/param
  /// variables.
  virtual void HighlightVar(unsigned Begin, unsigned End,
                            VarDecl *VD, bool isDeclared) {}
};
-------------------------------------------------------------------------------------------------------------------

HTMLPrinter becomes a specialized AnnotationClient that puts out a html 
file and uses the annotations. Another kind of AnnotationClient could be 
used by an IDE
to syntax-colorize the displayed source file.

As you can see from AnnotationClient interface, I added a HighlightVar 
annotation. HTMLPrinter currently uses it to colorize and create links 
on variable names that point to
the variable declaration line. I attached gcc.html so you can see how it 
works on 'gcc.c'.

Let me know what you think!


-Argiris

-------------- next part --------------
A non-text attachment was scrubbed...
Name: libannotate.patch
Type: text/x-diff
Size: 29026 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20080420/5db84ee4/attachment.patch>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: gcc.zip
Type: application/octet-stream
Size: 151498 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20080420/5db84ee4/attachment.obj>


More information about the cfe-dev mailing list