[cfe-dev] Clang: Pass by const-ref C++0x

Lewis, Jason jason.lewis at gentex.com
Wed Jun 6 09:42:42 PDT 2012


Hello,


I've been working with Clang for a couple of days and I can't help feeling like I am missing something. When I parse out the following:

void HandleBlah(std::string const& in, std::string& out)
{
    // do something
}

It doesn't seem to recognize the qualifier const. However if I omit the reference it seems as though it works. It doesn't seem as if there is any indication either in ParmVarDecl or Qualifiers or QualType that a developer can deduce if the parameter is a pointer or reference type. How can I detect whether or not a parameter is a reference? I suspect it is something to do in how I have set up my project. I am using a SemaConsumer being passed into the Process function and I only care about top level function declarations. Thank you in advance!


-JR


To summarize the setup:

/**
*            To use:
*            0) Create compiler instance
*            1) Create a stream with NewOutputStream
 *            2) Call Setup with obvious parameters.
*            3) Create your consumer object.
*            4) Call Process with your consumer.
*
 */
#ifndef CLANGUTILS_CLANGSETUP_H
#define CLANGUTILS_CLANGSETUP_H

// clangutils dependencies
#include "clang-utils/ClangHeaders.h"
#include "clang-utils/ClangLogger.h"
#include "clang-utils/ClangUtils.h"

// std dependencies
#include <sstream>
#include <string>
#include <vector>


namespace clangutils
{

typedef llvm::raw_fd_ostream* OutputStream;

/**
* Stream all of the errors related to parsing/semantic analyzing to a
 * file called b.out. The parsing/semantic analyzing is all built in and
 * there is guaranteed to be errors so just do this.
* @param compInst The compiler instance.
* @param name The name of the file to create.
* @return a newly allocated output stream.
*/
inline OutputStream NewOutputStream(clang::CompilerInstance& compInst, std::string const& name)
{
    OutputStream outputStream = compInst.createOutputFile(name);
    return outputStream;
}


/**
* The setup for the clang parser.
*
* @param compInst The compiler instance we want to set up.
* @param includePaths The container of include paths to add in.
* @param outputStream A stream object being used by this diagnostic.
*/
inline void Setup( int argc
                 , char** argv
                 , clang::CompilerInstance& compInst
                 , std::vector<std::string> const& includePaths
                 , OutputStream outputStream
                 , std::string const& filename )
{
    // This is an important line because it sets up the compiler for C++.
    compInst.getLangOpts().CPlusPlus = 1;
    compInst.getLangOpts().CPlusPlus0x = 1;

    clang::DiagnosticOptions& diagnosticOptions = compInst.getDiagnosticOpts();

    clang::FileSystemOptions fileSystemOptions;
    compInst.createFileManager();
    clang::FileManager& fileManager = compInst.getFileManager();

    clang::TextDiagnosticPrinter* printer =
        new clang::TextDiagnosticPrinter( *outputStream
                                        , diagnosticOptions
                                        , false );

    compInst.createDiagnostics(argc, argv, printer);

    clang::TargetOptions targetOptions;
    targetOptions.Triple = llvm::sys::getDefaultTargetTriple();
    clang::TargetInfo *pTargetInfo =
        clang::TargetInfo::CreateTargetInfo( compInst.getDiagnostics()
                                           , targetOptions);

    compInst.setTarget(pTargetInfo);
    compInst.createSourceManager(compInst.getFileManager());
    compInst.createPreprocessor();

    // Obtain the preprocessor. This is needed so we can modify its search
    // path and its predefines.
    clang::Preprocessor& preprocessor = compInst.getPreprocessor();

    // Use the header search path and add directories to it.
    clang::HeaderSearch& headerSearch = preprocessor.getHeaderSearchInfo();

    // Grab the default source manager.
    clang::SourceManager& sourceManager = compInst.getSourceManager();

    for(size_t i=0; i<includePaths.size(); ++i)
    {
        clang::DirectoryEntry const* directoryEntry = fileManager.getDirectory(includePaths[i]);
        clang::DirectoryLookup lookup(directoryEntry, clang::SrcMgr::C_System, false, false);
        headerSearch.AddSearchPath(lookup, true);
        headerSearch.AddSearchPath(lookup, false);
    }

    const clang::FileEntry *pFile = fileManager.getFile(filename);
    sourceManager.createMainFileID(pFile);

    clang::TargetInfo const &targetInfo = *pTargetInfo;
    compInst.createASTContext();
}


/**
* Perform a tear down operation.
* @param compInst The compiler instance.
* @param outputStream the output stream to use.
* @param consumer The consumer.
*/
template <typename T>
void Process( clang::CompilerInstance& compInst
            , OutputStream outputStream
            , T& consumer )
{
    // Obtain the preprocessor. This is needed so we can modify its search
    // path and its predefines.
    clang::Preprocessor& preprocessor = compInst.getPreprocessor();
    preprocessor.AddCommentHandler(&consumer);

    clang::Sema* sema = new clang::Sema( preprocessor, compInst.getASTContext(), consumer);
    compInst.setSema(sema);
    consumer.InitializeSema(*sema);

    compInst.getDiagnosticClient().BeginSourceFile(compInst.getLangOpts(), &preprocessor);
    clang::ParseAST(preprocessor, &consumer, compInst.getASTContext());
    compInst.getDiagnosticClient().EndSourceFile();

    outputStream->close();
    delete outputStream;
}

}

#endif





THE INFORMATION CONTAINED IN THIS E-MAIL MESSAGE AND ANY ATTACHMENTS SENT FROM GENTEX CORPORATION IS GENTEX CONFIDENTIAL INFORMATION INTENDED ONLY FOR THE PERSONAL USE OF THE INDIVIDUAL OR ENTITY NAMED ABOVE. If you are not the intended recipient, you are hereby notified that any review, distribution, or copying of this communication is strictly prohibited.  If you have received this communication in error, please immediately notify the sender by return e-mail, and delete this e-mail message and any attachments from your computer.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20120606/775fd083/attachment.html>


More information about the cfe-dev mailing list