[cfe-dev] Problem installing PPCallbacks

David Hammer via cfe-dev cfe-dev at lists.llvm.org
Fri Nov 1 10:38:52 PDT 2019


I'm having difficulty adding PPCallbacks to the preprocessor.  Only the
EndOfMainFile() callback is being invoked by Clang.  The others are not
invoked.

 

I created a struct that derives from clang::PPCallbacks, and overrides
MacroExpands(), MacroDefined(), FileChanged(), and EndOfMainFile() as
follows:

 

struct PPActions : public clang::PPCallbacks {

  static int nextSN; // Next serial number

  int sn;            // this s/n

  PPActions() { 

    sn = nextSN++;

    std::cout << "Default PPActions C'tor - instance #" << sn << std::endl;

  }

  void MacroExpands(const Token &MacroNameTok, const MacroDefinition &MD, 

                    SourceRange Range, const MacroArgs *Args) override {

    using std::cout; using std::endl;

    cout << "PPActions::MacroExpands() called!" << endl;

  }

  void MacroDefined(const Token &MacroNameTok, 

                    const MacroDirective *MD) override {

    using std::cout; using std::endl;

    cout << "PPActions::MacroDefined() called" << endl;

  }

  void FileChanged(SourceLocation Loc, FileChangeReason Reason,

                    SrcMgr::CharacteristicKind FileType, FileID PrevFID) 

                    override {

    using std::cout; using std::endl;

    cout << "PPActions::FileChanged() called" << endl;

  }

  // This is the only Callback actually being called.

  void EndOfMainFile()  override {

    using std::cout; using std::endl;

   cout << "PPActions::EndOfMainFile() called, instance #" << sn << endl;

  }

};

 

I created a class derived from clang:: PreprocessorFrontendAction with the
following ExecuteAction() function.

  void ExecuteAction() {

    using std::cout; using std::endl;

    cout << "ToolCppAction::ExecuteAction() called" << endl;

    if (compiler->hasPreprocessor()) {

      cout << "CompilerInstance has a preprocessor" << endl;

      Preprocessor& cpp = compiler->getPreprocessor();

      cout << "Installing PPCallbacks..." << endl;

      cpp.addPPCallbacks(std::make_unique<PPActions>() );

    }

 

I've also tried installing the callbacks in the BeginSourceFileAction().
The output of that version shows two instances of the callback struct
created, and the EndOfMainFile() of both being invoked (but none of the
other callbacks).

 

The problem I'm working involves code analysis on programs using a library
whose API consists mostly of macros, so being able to examine macros and
their arguments is extremely important.

Any help or insight into this issue would be appreciated.

Best regards,

David Hammer
Principal Developer I
O: 937.433.0477 ext. 140 | M: 937.520.2936 



CONFIDENTIALITY NOTICE: The information transmitted is intended only for the
person or entity to which it is addressed and may contain company
proprietary and/or privileged material. Any review, retransmission,
dissemination or other use of, or taking of any action in reliance upon,
this information by persons or entities other than the intended recipient is
prohibited. If you received this in error, please contact the sender and
destroy any copies of this document.

 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20191101/64f92e6f/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image001.png
Type: image/png
Size: 12626 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20191101/64f92e6f/attachment.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 3844 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20191101/64f92e6f/attachment.bin>


More information about the cfe-dev mailing list