Clang tutorial

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view

Clang tutorial

Matthias Bahne
Hi All,
for my diploma-thesis i'm working on a static analysis of  
c-applications in order to do a semi-automatic configuration  
of the configurable operation-system eCos.
I think clang is a good basis for the analysis.
I'm trying to get involved with clang using this tutorial:
This tutorial was mentioned 10 month before:
I have modified the files, so that they works with the actual  
clang version. I compiled and linked the files with the commands:
 g++ -I [LLVM_PATH]/llvm/tools/clang/include   \
  `[LLVM_PATH]/llvm/Debug/bin/llvm-config --cxxflags`  \
  -fno-rtti -c tut02_pp.cpp
 g++ -o tut02 tut02_pp.o `[LLVM_PATH]/llvm/Debug/bin/llvm-config  \
  --ldflags` -lCIndex    
When I try to use tut02 on an input-file it ends with:
 equal '='
 input01.c:5:13: warning: '/*' within block comment
 tut02: TextDiagnosticPrinter.cpp:282: void clang::TextDiagnosticPrinter::
  EmitCaretDiagnostic(clang::SourceLocation, clang::SourceRange*,  
  unsigned int, clang::SourceManager&, const clang::CodeModificationHint*,  
  unsigned int, unsigned int): Assertion `LangOpts &&  
  "Unexpected diagnostic outside source file processing"' failed.
What did I wrong?
Is there a better way to start with clang?
thank, Matthias

int main() {
  int a = 4 + 5;

  int b =
  /* Nested /* comments */ 12; // are handled correctly

  int c =
#ifdef __APPLE__

#ifndef PP_CONTEXT
#define PP_CONTEXT

#include <string>

#include "llvm/Config/config.h"

#include "clang/Basic/Diagnostic.h"
#include "clang/Basic/TargetInfo.h"
#include "clang/Basic/SourceManager.h"
#include "clang/Basic/FileManager.h"
#include "clang/Basic/TargetOptions.h"

#include "clang/Lex/HeaderSearch.h"
#include "clang/Lex/Preprocessor.h"

#include "clang/Frontend/TextDiagnosticPrinter.h"
#include "clang/Frontend/DiagnosticOptions.h"

struct PPContext {
  // Takes ownership of client.
  PPContext(llvm::raw_ostream &out_stream,
            clang::TargetOptions targetOpts,
            clang::DiagnosticClient* client = 0,
            const std::string& triple = LLVM_HOSTTRIPLE)
    : diagClient(client == 0?new clang::TextDiagnosticPrinter(out_stream, diagOptions):client),
      target(clang::TargetInfo::CreateTargetInfo(diags, targetOpts)),
      pp(diags, opts, *target, sm, headers)
    // Configure warnings to be similar to what command-line `clang` outputs
    // (see tut03).
    // XXX: move warning initialization to libDriver
    using namespace clang;
   // diags.setDiagnosticMapping(diag::warn_pp_undef_identifier,diag::MAP_IGNORE);

    delete diagClient;
    delete target;

  clang::DiagnosticOptions diagOptions;
  clang::DiagnosticClient* diagClient;
  clang::Diagnostic diags;
  clang::LangOptions opts;
  clang::TargetInfo* target;
  clang::SourceManager sm;
  clang::FileManager fm;
  clang::HeaderSearch headers;
  clang::Preprocessor pp;


#include "PPContext.h"
#include "llvm/Support/raw_ostream.h"

clang::TargetOptions targetOpts;

int main()
  const std::string& triple = LLVM_HOSTTRIPLE;
  targetOpts.Triple = triple;
  PPContext context(llvm::errs(),targetOpts);
#include <iostream>
#include "llvm/Support/raw_ostream.h"
using namespace std;

#include "PPContext.h"
using namespace clang;
using namespace llvm;

clang::TargetOptions targetOpts;

int main(int argc, char* argv[])
  if (argc != 2) {
    cerr << "No filename given" << endl;
    return EXIT_FAILURE;

  // Create Preprocessor object
  const std::string& triple = LLVM_HOSTTRIPLE;
  targetOpts.Triple = triple;
  PPContext context(llvm::errs(),targetOpts);

  // Add input file
  const FileEntry* File =[1]);
  if (!File) {
    cerr << "Failed to open \'" << argv[1] << "\'";
    return EXIT_FAILURE;
  }, SourceLocation());

  // Parse it
  Token Tok;
  do {
    if (context.diags.hasErrorOccurred())
    cerr << endl;
  } while (Tok.isNot(tok::eof));


cfe-dev mailing list
[hidden email]