mardi 14 mars 2023

Error calling a protected constructor of class clang::tooling::CommonOptionsParser

Following a basic Clang tutorial, I try to build an "out of the source" example and get a strange error using VSCode:

"calling a protected constructor of class 'clang::tooling::CommonOptionsParser'GCC"

(complete error details below)

when I try to compile this code:

#include <iostream>
#include "clang/Driver/Options.h"
#include "clang/AST/AST.h"
#include "clang/AST/ASTContext.h"
#include "clang/AST/ASTConsumer.h"
#include "clang/AST/RecursiveASTVisitor.h"
#include "clang/Frontend/ASTConsumers.h"
#include "clang/Frontend/FrontendActions.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Tooling/CommonOptionsParser.h"
#include "clang/Tooling/Tooling.h"
#include "clang/Rewrite/Core/Rewriter.h"

using namespace std;
using namespace clang;
using namespace clang::driver;
using namespace clang::tooling;
using namespace llvm;

Rewriter rewriter;
int numFunctions = 0;

int main( int argc, const char **argv ) {
    static llvm::cl::OptionCategory optionCategory( "tool options" );
    // parse the command-line args passed to your code
    Expected<tooling::CommonOptionsParser> expectedParser = clang::tooling::CommonOptionsParser::create(argc, argv, optionCategory);
    if (!expectedParser) {
        llvm::errs() << expectedParser.takeError();
        return 1;
    }
    CommonOptionsParser &optionsParser = expectedParser.get();
    // create a new Clang Tool instance (a LibTooling environment)
    ClangTool Tool( optionsParser.getCompilations(), optionsParser.getSourcePathList() );
 
    // run the Clang Tool, creating a new FrontendAction
    int result = Tool.run( newFrontendActionFactory<WrapperFrontendAction>().get() );

    errs() << "\nFound " << numFunctions << " functions.\n\n";
    // print out the rewritten source code ("rewriter" is a global var.)
    rewriter.getEditBuffer(rewriter.getSourceMgr().getMainFileID()).write(errs());
    return result;
}

I understand I should call create to get a new instance instead of referencing the constructor name, however, the CommonOptionsParser is declared as follows:

class CommonOptionsParser {

protected:
  /// Parses command-line, initializes a compilation database.
  ///
  /// This constructor can change argc and argv contents, e.g. consume
  /// command-line options used for creating FixedCompilationDatabase.
  ///
  /// All options not belonging to \p Category become hidden.
  ///
  /// It also allows calls to set the required number of positional parameters.
  CommonOptionsParser(
      int &argc, const char **argv, llvm::cl::OptionCategory &Category,
      llvm::cl::NumOccurrencesFlag OccurrencesFlag = llvm::cl::OneOrMore,
      const char *Overview = nullptr);

public:
  /// A factory method that is similar to the above constructor, except
  /// this returns an error instead exiting the program on error.
  static llvm::Expected<CommonOptionsParser>
  create(int &argc, const char **argv, llvm::cl::OptionCategory &Category,
         llvm::cl::NumOccurrencesFlag OccurrencesFlag = llvm::cl::OneOrMore,
         const char *Overview = nullptr);

  /// Returns a reference to the loaded compilations database.
  CompilationDatabase &getCompilations() {
    return *Compilations;
  }

This way specifying the namespace is "overriding" the create call and then the compiler gets confused?

Of course if I remove "clang::tooling::CommonOptionsParser::" then create is not found (Use of undeclared identifier 'create')

This is my compile_commands.json:

[
{
  "directory": "/Users/user/prjs/clang-tryout-3/build",
  "command": "/opt/homebrew/opt/llvm/bin/clang++ -I/opt/homebrew/opt/llvm/include -g -arch arm64 -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX13.1.sdk -o CMakeFiles/Hello.dir/main.cpp.o -c /Users/user/prjs/clang-tryout-3/main.cpp",
  "file": "/Users/user/prjs/clang-tryout-3/main.cpp"
}
]

Complete error details:

[{
    "resource": "/Users/user/prjs/clang-tryout-3/main.cpp",
    "owner": "cmake-build-diags",
    "severity": 8,
    "message": "calling a protected constructor of class 'clang::tooling::CommonOptionsParser'",
    "source": "GCC",
    "startLineNumber": 27,
    "startColumn": 22,
    "endLineNumber": 27,
    "endColumn": 1000,
    "relatedInformation": [
        {
            "startLineNumber": 76,
            "startColumn": 3,
            "endLineNumber": 76,
            "endColumn": 1000,
            "message": "declared protected here",
            "resource": "/opt/homebrew/opt/llvm/include/clang/Tooling/CommonOptionsParser.h"
        }
    ]
}]

Aucun commentaire:

Enregistrer un commentaire