Archive

Posts Tagged ‘preprocessing’

Semantic pattern matching (Coccinelle)

January 8th, 2009 Derek-Jones No comments

I have just discovered Coccinelle a tool that claims to fill a remarkable narrow niche (providing semantic patch functionality; I have no idea how the name is pronounced) but appears to have a lot of other uses. The functionality required of a semantic patch is the ability to write source code patterns and a set of transformation rules that convert the input source into the desired output. What is so interesting about Coccinelle is its pattern matching ability and the ability to output what appears to be unpreprocessed source (it has to be told the usual compile time stuff about include directory paths and macros defined via the command line; it would be unfair of me to complain that it needs to build a symbol table).

Creating a pattern requires defining identifiers to have various properties (eg, an expression in the following example) followed by various snippets of code that specify the pattern to match (in the following <… …> represents a bracketed (in the C compound statement sense) don’t care sequence of code and the lines starting with +/- have the usual patch meaning (ie, add/delete line)). The tool builds an abstract syntax tree so urb is treated as a complete expression that needs to be mapped over to the added line).

@@
expression lock, flags;
expression urb;
@@
 
  spin_lock_irqsave(lock, flags);
  &lt;...
- usb_submit_urb(urb)
+ usb_submit_urb(urb, GFP_ATOMIC)
  ...&gt;
  spin_unlock_irqrestore(lock, flags);

Coccinelle comes with a bunch of predefined equivalence relations (they are called isomophisms) so that constructs such as if (x), if (x != NULL) and if (NULL != x) are known to be equivalent, which reduces the combinatorial explosion that often occurs when writing patterns that can handle real-world code.

It is written in OCaml (I guess there had to be some fly in the ointment) and so presumably borrows a lot from CIL, perhaps in this case a version number of 0.1.3 is not as bad as it might sound.

My main interest is in counting occurrences of various kinds of patterns in source code. A short-term hack is to map the sought-for pattern to some unique character sequence and pipe the output through grep and wc. There does not seem to be any option to output a count of the matched patterns … yet :-)

Parsing without a symbol table

December 19th, 2008 Derek-Jones No comments

When processing C/C++ source for the first time through a compiler or static analysis tool there are invariably errors caused by missing header files (often because the search path has not been set) or incorrectly defined, or not defined, macro names. One solution to this configuration problem is to be able to process source without handling preprocessing directives (e.g., skipping them, such as not reading the contents of header files or working out which arm of a conditional directive is applicable). Developers can do it, why not machines?

A few years ago GLR support was added to Bison, enabling it to process ambiguous grammars, and I decided to create a C parser that simply skipped all preprocessing directives. I knew that at least one reasonably common usage would generate a syntax error:

func_call(a,
#if SOME_FLAG
b_1);
#else
b_2);
#endif

c);
and wanted to minimize its consequences (i.e., cascading syntax errors to the end of the file). The solution chosen was to parse the source a single statement or declaration at a time, so any syntax error would be localized to a single statement or declaration.

Systems for parsing ambiguous grammars work on the basis that while the input may be locally ambiguous, once enough tokens have been seen the number of possible parses will be reduced to one. In C (and even more so in C++) there are some situations where it is impossible to resolve which of several possible parses apply without declaration information on one or more of the identifiers involved (a traditional parser would maintain a symbol table where this information could be obtained when needed). For instance, x * y; could be a declaration of the identifier y to have type x or an expression statement that multiplies x and y. My parser did not have a symbol table and even if it did the lack of header file processing meant that its contents would only contain a partial set of the declared identifiers. The ambiguity resolution strategy I adopted was to pick the most likely case, which in the example is the declaration parse.

Other constructs where the common case (chosen by me and I have yet to get around to actually verifying via measurement) was used to resolve an ambiguity deadlock included:

f(p);      // Very common, 
            // confidently picked function call as the common case
(m)*p;   // Not rare,
            // confidently picked multiplication as the common case
(s) - t;      // Quiet rare,
               // picked binary operator as the common case
(r) + (s) - t; // Very rare,
                  //an iteration on the case above

At the moment I am using the parser to measure language usage, so less than 100% correctness can be tolerated. Some of the constructs that cause a syntax error to be generated every few hundred statement/declarations include:

offsetof(struct tag, field_name)  // Declarators cannot be 
                                            //function arguments
int f(p, q)
int p;     // Tries to reduce this as a declaration without handling
char q;   // it as part of an old style function definition
{
 
MACRO(+); // Preprocessing expands to something meaningful

Some of these can be handled by extensions to the grammar, while others could be handled by an error recovery mechanism that recognized likely macro usage and inserted something appropriate (e.g., a dummy expression in the MACRO(x) case).

www.wenn.com
FireStats icon Powered by FireStatswww.tinynibbles.com cialis 100 mg generic

buy viagra mexico

once a day viagra

get propecia online pharmacy

cialis transdermal

bestellen levitra online

cialis price 100 mg

best way to use cialis

buy dosages levitra

buy levitra uk

buy propecia online prescription

cost of propecia

canadian drugs propecia

buy cheap generic levitra

levitra canadian

best price for generic cialis

generic cialis from india

cheap fast levitra

cialis professional 20 mg

next day delivery cialis

cialis alternative

daily dosage cialis

buy viagra germany canadian meds

genuine cialis pills

cialis vs levitra

cialis price in canada

cheap levitra

brand name cialis overnight

cialis quick shipment

buy now propecia

name brand cialis

buy propecia prescriptions online

online generic cialis 100 mg

canadian healthcare

lowest propecia 1 mg

buy viagra on line

canada viagra

buy viagra online cheap us

discount propecia rx

cialis soft pills

order cheapest propecia online

i need to buy propecia

levitra online sales

generic propecia finasteride

canadian viagra

cheapest propecia prescription

cialis and canada custom

buying generic propecia

how to get viagra

cheap propecia 5mg

generic cialis sale

indian cialis generic

buy cialis fedex shipping

buying cialis next day delivery

does generic cialis work

natural viagra

buy generic levitra

generic viagra india

indian viagra

generic viagra 100 mg

once daily cialis

cialis 5 mg italia

buy cialis once daily

overnight delivery cialis

buy propecia online

cheap levitra prescription

buy levitra vardenafil

how strong is 5 mg of cialis

can i get viagra in mexico

levitra in canada

lowest propecia prices in canada

online levitra

cialis from mexico

brand cialis for sale

buy propecia in the uk

cheap levitra uk

buy generic viagra india rx

buying propecia online

canadian healthcare viagra

drug generic propecia

cialis cheap us pharmacy

buying generic cialis mexico rx

levitra pill

buy propecia cheap

buy cialis canada

generic propecia for sale

canadian online pharmacy cialis

lowest cost levitra

cialis to buy

cialis overnight delivery

how much to buy viagra in pounds

buy cialis online canada

canada levitra

online propecia uk

generic propecia alternative

cheap viagra online

mexico pharmacy cialis

levitra 10mg

cialis generic 100 mg

canadian viagra 50mg

cialis one a day

buy cheapest propecia

buy propecia online from usa pharmacy

buy propecia generic

how much does cialis cost

ganeric cialis

levitra tabs

canada generic propecia

brand name cialis

cialis 5 mg

overnight delivery viagra

buy propecia on line

bio viagra herbal

cialis overnight

cialis daily in canada

how to get cialis in canada

buy cialis 5 mg

buy canada levitra

obtain viagra without prescription

generic propecia 5mg

cheapest prices for viagra

cheapest viagra usa

online propecia prescription

best price for propecia

cialis professional no prescription

buying propecia

cheapest overnight cialis

generic viagra made in india

buy propecia online pharmacy

order cheap levitra

get levitra

canadian healthcare pharmacy

discount generic propecia

generic viagra made in usa

cheapest propecia sale uk

cialis discounts

order cheap propecia

cialis in mexico

buy can from i propecia who

5 mg daily cialis

buy real viagra online

lowest price propecia

get levitra online

buy online prescription propecia

how to buy cialis in canada

combine cialis and levitra

buying online propecia

cialis dosage mg

ordering propecia online

internet pharmacy propecia

generic levitra vardenafil

best price cialis

mail online order propecia

buying cialis

levitra cheap fast

cialis tablets foreign

low cost propecia

generic viagra online

generic levitra cheap

order prescription propecia

mexico levitra

cialis discount

buy propecia canada

canada propecia prescription

cialis buy overnight

generic propecia sale

mail order propecia

canadian propecia rx

buy cialis cannada

cheap levitra without prescription

lowest price propecia best

getting cialis from canada

discount levitra rx

how much is viagra

buy propecia now

cheap propecia no prescription

discount cialis india

cialis uk

discount levitra purchase

buy levitra overnight

levitra mg

discount drug propecia

buy generic cialis

cialis delivered overnight

get cialis online

generic viagra canada

levitra next day delivery

levitra prescription

cheap viagra from uk

buying levitra online

herbal propecia

buy cialis without prescription

best price levitra

cialis pharmacy

levitra viagra cialis

cialis 20 mg

buy cialis online uk

cost of daily cialis

buy levitra online no prescription

discount us propecia

generic levitra purchase

canada online pharmacy levitra

levitra sales uk

cialis en mexico

cialis 5 mg buy

buying cialis soft tabs 100 mg

cheap prescription propecia

buy discount viagra

best price propecia

gele viagra

hydrochlorothiazide cialis

generic cialis next day shipping

cheapest propecia uk

indian cialis

info levitra

lowest price for propecia

cialis cheap

for sale levitra

buying viagra in canada

buy cialis usa

low cost levitra

levitra online no prescription

fda levitra

brand viagra over the net

cialis fast delivery

buy propecia without prescription

online propecia prescriptions

50 mg cialis

cheap propecia online prescription

generic cialis soft tabs

levitra viagra online

cialis refractory

levitra cost

cheap propecia uk

online pharmacy propecia viagra

generic propecia effective

canadian pharmacy discount code viagra

buy real cialis

cheapest price propecia cheap

cialis and diarrhea

cialis from canada

get cialis

canadian viagra india

discount propecia online

cheap cialis from india

discount levitra online

buy cialis in usa

canadian pharmacy viagra

lowest price levitra

canadian pharmacies cialis

levitra order prescription

generic levitra overnight delivery

canada viagra pharmacies scam

cheap discount levitra

buy 5 mg cialis

online viagra gel to buy

levitra online

cialis purchase

discount propecia propecia

levitra where to buy

cialis 50 mg

mail order levitra

cialis no prescription

cialis online

canadian pharmacy

canadian viagra and healthcare

cialis 100 mg

buy viagra online

order generic levitra

levitra low price

cialis prescription

low cost canadian viagra

cialis price

buy propecia where

order viagra or levitra

cialis by mail

buy viagra china

buy cheap levitra

cialis profesional

buy levitra us

best viagra

cheap cialis soft

cialis daily

generic propecia fda approved

levitra discount

cost of viagra

levitra online prescription

levitra mail order

buy cialis for daily use

cialis daily dosage pharmacy

lowest price on non generic levitra

best price generic propecia

order levitra online

canada online pharmacy propecia

order propecia

levitra buy online

cheap levitra tablets

cheap order prescription propecia

buy levitra online viagra

buy cheap generic propecia