Guillem Marpons | 17 May 02:14
Picon
Picon
Favicon

Crisp: Coding rule checking using clang and LLVM

Hi,

I've been working the last months on a coding rule validation add-on
for clang/LLVM, called Crisp:

    https://github.com/gmarpons/Crisp

Coding Rules constrain admissible constructs of a language to help
produce better code (improving reliability, portability,
maintainability, etc.). Some well-known coding rule sets are:

- MISRA-C/C++ (no public access available)
- High Integrity C++ Coding Standard (HICPP): http://www.codingstandard.com/
- CERT's Secure Coding Standards: http://www.cert.org/secure-coding/

Coding rule sets can include style conventions but they go typically
further. Rules range from purely syntactic properties (e.g. "Do not
use the ‘inline’ keyword for member functions") to those that need
deep static analyses to be automated (e.g. "Do not return non-const
handles to class data from const member functions", both examples are
from HICPP).

There are some tools that can be used to define and enforce coding
rules on C/C++ code. Some distinctive features of our tool are:

- Rules (i.e., user checks) are going to be defined using a high-level
declarative Domain Specific Language. This language, called CRISP, is
not implemented yet. CRISP is based on first order logic, and rule
definitions are expected to be very concise and easy to read (see
below). The use of CRISP to formally define rules should avoid the
(Continue reading)

Slav | 16 May 22:38
Picon

Obtaining C++ AST.

Hello.
I am traying to get AST of specified file and iterate trough it. Here is my code:


class MyDiagnosticConsumer: public clang::DiagnosticConsumer
{
public:
    clang::DiagnosticConsumer* clone(clang::DiagnosticsEngine &Diags) const
    {
        return new MyDiagnosticConsumer;
    }
};

class MyModuleLoader: public clang::ModuleLoader
{
public:
    clang::Module* loadModule(clang::SourceLocation importLoc, clang::ModuleIdPath path, clang::Module::NameVisibilityKind visibility, bool isInclusionDirective)
    {
        //just do some parsing, forget about modules:
        return NULL;
    }
};

class MyASTConsumer: public clang::SemaConsumer
{
public:

    bool HandleTopLevelDecl(clang::DeclGroupRef D)
    {
        //never get here...
       
        return true;
    }

    void HandleTranslationUnit(clang::ASTContext &Ctx)
    {
        //not here either...
    }
};


int main()
{
    clang::IntrusiveRefCntPtr<clang::DiagnosticIDs> diags;
    MyDiagnosticConsumer dc;
    clang::DiagnosticsEngine de(diags, &dc, false);
    clang::Diagnostic d(&de);

    clang::TargetOptions to;
    to.Triple = llvm::Triple::getArchTypeName(llvm::Triple::x86);
    clang::TargetInfo* ti = clang::TargetInfo::CreateTargetInfo(de, to);

    clang::FileSystemOptions fso;
    clang::FileManager fm(fso);
    clang::SourceManager sm(de, fm);

    clang::LangOptions lo;
    clang::HeaderSearch hs(fm, de, lo, ti);

    MyModuleLoader ml;

    clang::Preprocessor p(de, lo, ti, sm, hs, ml);

    const clang::FileEntry* file = fm.getFile("test.cpp");
    sm.createMainFileID(file);
    p.EnterMainSourceFile();

    clang::IdentifierTable it(lo);
    clang::SelectorTable st;
    clang::Builtin::Context b_c;
    clang::ASTContext astContext(lo, sm, ti, it, st, b_c, 0);
    MyASTConsumer astConsumer;
    clang::Sema s(p, astContext, astConsumer);
    clang::Parser parser(p, s, false);

    parser.Initialize();
    clang::Parser::DeclGroupPtrTy result;
    while(!parser.ParseTopLevelDecl(result))
    {
        if ( result.get().isSingleDecl() )
        {
            clang::Decl* decl = result.get().getSingleDecl();

            clang::FunctionDecl* fd = clang::dyn_cast_or_null<clang::FunctionDecl>(decl);

            if ( fd->hasBody() )
            {
                clang::Stmt* s = fd->getBody();
                //don't know what to do with obtained statement...
            }
        }
        else if ( result.get().isDeclGroup() )
        {
            clang::DeclGroup& declGroup = result.get().getDeclGroup();
        }
    }

    getchar();

    return 0;
}


test.cpp is:
int main()
{
    return 5;
}

Am I on a wrong way? How is it need to parse files and iterate trough resulting AST?
Thank you.
_______________________________________________
cfe-dev mailing list
cfe-dev@...
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
Thomas Hauth | 16 May 14:49
Picon
Picon

Using scan-build with custom checker plugins

Hello,
we implemented some custom clang Static Analyzer checkers for our needs 
and compiled them as external modules.

When calling clang directly, one can use "-Xclang" to pass the command 
to load and run the checkers:

clang++ -Xclang -load -Xclang ./MainCallChecker.so -Xclang 
-analyzer-checker=example.MainCallChecker --analyze global_static.cpp

This variant works fine.

Is there a way, to still load the custom plugins when using the 
scan-build tool on regular make files?

As a workaround, I implemented a new environment variable named 
"CCC_ANALYZER_PLUGINS" which is read by the c++-analyzer script and used 
to pass the plugins to load to the clang call.

Thanks,
Thomas	
Lukhnos Liu | 16 May 05:05
Gravatar

Getting the same begin and end locations from TypeLoc?

Hi,

In what circumstances would one get the same location by calling the getBeginLoc() and getEndLoc() of a
TypeLoc instance?

My setup is like this: I have a SemaConsumer, and from the HandleTranslationUnit method, I descend (via the
passed ASTContext's getTranslationUnitDecl()) to the Decl I'm interested in:

  // I is an iterator from a DeclContext's decls_begin()
  if (const DeclaratorDecl *DD = dyn_cast<DeclaratorDecl>(*I)) {
    TypeSourceInfo *TSI = DD->getTypeSourceInfo();
    TypeLoc TL = TSI->getTypeLoc();
    SourceLocation B = TL.getBeginLoc();
    SourceLocation E = TL.getEndLoc();

    // want to do something with B and E, e.g. get the characterData from the source manager
  }

And by calling their print(), it turns out that SourceLocations B and E always point to the same place of the
source code -- same line, same column, though I expected E to be further down in the source. Did I miss
anything in using TypeLoc?

Thanks,
Lukhnos
Sai Charan | 16 May 03:07
Gravatar

Integrating LLVM pass into Clang

Hi,
I have an LLVM pass that I am able to run with 'opt'. I now want this pass to be available for invocation via Clang. Any pointers on if and how to achieve this?

I have already looked through the document on writing a pass; I did not find this information on that page.

Sai Charan,
CSE, UC Riverside.


_______________________________________________
cfe-dev mailing list
cfe-dev@...
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
Anna Zaks | 15 May 20:08
Picon
Favicon

Re: Did Clang support Incremental Static Analysis ????

As David Chisnall pointed out to me, there is some inherent support of incremental build due to the way scan-build works. "The standard way of running the static analyser is via the scan-build script, which runs as part of your normal build system.  If you run it as part of an incremental build, then you will get incremental analysis results.  You will not, however, get these merged with the previous set."

It would be relatively easy to merge the results in this setting. Note that, this happens to work well because the analyzer only deals with one file at a time, which does limit its power. Having whole program analyzes is something we are interested in doing in the future and it would greatly complicate the merging. Another problem with relying on this approach is that this would only give you file granularity. In principle, one might not need to reanalyze the whole file if one line in it changed. Coming up with a more general solution is a much more difficult problem.

Another feature that is somewhat related to this is identifying issues and tracking issues over time so that we could detect new warnings that just started appearing after a bunch of changes. This one is important to solve because many workflows involve looking at newly introduced issues. Hopefully, we'll get to this one soon.

If incremental analysis is something you would love to see in the analyzer, you can chime in by filing an enhancement request on llvm Bugzilla.

Cheers,
Anna.

On May 14, 2012, at 11:19 PM, Umesh Kalappa wrote:

Thanks anna for the clarification and any idea about adding  this feature in future ???

~Umesh

On Mon, May 14, 2012 at 11:51 PM, Anna Zaks <ganna-2kanFRK1NckAvxtiuMwx3w@public.gmane.org> wrote:
Hi Umesh,

No, currently, the Clang Static Analyzer does not have any support for incremental analysis.

Cheers,
Anna.
On May 14, 2012, at 3:01 AM, Umesh Kalappa wrote:

> Hi All,
>
> My Apologies if the below question was ended up in  the wrong forum.
>
> Like to know that did Clang Static Analysis(CSA)  has the feature called incremental analysis ???,When i say incremental here ,I mean when i kick off the CSA on the code  base for the second time with set of files changed then CSA  will use the analysis that was done for  the last time along the doing CSA  for changed files.
>
>
> Thanks
> ~Umesh
>
> _______________________________________________
> cfe-dev mailing list
> cfe-dev-Tmj1lob9twqVc3sceRu5cw@public.gmane.org
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev



_______________________________________________
cfe-dev mailing list
cfe-dev@...
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
Jonny Yu | 15 May 05:29
Picon

-fno-implicit-templates in clang?

Hello,

does clang 3.1 support -fno-implicit-templates option?
If not, is there any workarounds? 

Thanks,
-Jonny
Picon
Gravatar

Clang 3.1 on MacOSX xcode?

Hi,


I have been working on a mac with XCode for 2 months now and I'd like to be able to use CLang 3.1 because of C++11 features that would help me a lot writing less verbose code.
I am not a unix/bsd-like massive user so I lack a lot of knowledge about how to connect things in my work environnement.
Here are some questions:

 1. are there official plans or release date for Apple to upgrade XCode to Clang 3.1 once it's released? I assume that NO because I couldn't find anything online.
 2. assuming that I get clang 3.1 from the release package or from sources, once built, how to make XCode use this version?

I'm sorry for such noob question, I'm a bit lost in this new environnement...
But I am not sure if it's the good way to do it, as I need to be able to set the new compiler for a new project but keep the default compiler for previous projects...

Joel Lamotte
_______________________________________________
cfe-dev mailing list
cfe-dev@...
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
Sai Charan | 15 May 02:59
Gravatar

Converting pointers to fat pointers

Hi,
I am looking at using LLVM/Clang to automatically convert pointer declarations to fat pointers & the corresponding dereferences to something appropriate. I am looking for guidance on doing this. Will an LLVM pass be better suited to this or would this be better handled using Clang. Any guidance on getting started would be helpful.

Sai Charan,
CSE, UC Riverside.

_______________________________________________
cfe-dev mailing list
cfe-dev@...
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
Lukhnos Liu | 15 May 02:52
Gravatar

Tokens for rewriting C++ constructor initializers

Hi,

I'm writing a refactoring tool as a way to learn Clang internals. A problem I run into is: how do you get the
locations of the tokens that are not present in the AST?

What I'm doing is like this: I have an ASTConsumer, which I call ParseAST to run. In the consumer, I look for
CXXConstructorDecl with the TraverseDecl method, then I want to rewrite the method (for example, to move
the function body from the header to an implementation file).

Getting the function body is easy, but I'm not sure what to do with the initializers. For each item in the
initializer list that I get from init_begin() and init_end(), the source range encompasses the item
itself, but not the preceding colon or the in-between commas. Is there a way to get those parts from the AST,
or do I need to get those lex info elsewhere?

Thank you,
Lukhnos
Chad Rosier | 15 May 02:31
Picon
Favicon

-no-implicit-float for ARM

I would like to add a command line option, -no-implicit-float, that will cause the noimplicitfloat attribute to be added to functions when targeting ARM. This attribute disables implicit floating point instructions. X86 already utilizes this attribute when the -msoft-float option is used. Unfortunately, we can't use -msoft-float on ARM, because it's already defined to mean something else.  Therefore, I would like to propose we allow the driver to accept the -no-implicit-float option directly (i.e., basically forwarding the -no-implocit-float option to cc1).  Does anyone have any opinions, comments or suggestions? Regards, Chad

_______________________________________________
cfe-dev mailing list
cfe-dev@...
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev

Gmane