[aspectc-user] Aspect C++ question:how to control the output methods?

Olaf Spinczyk Olaf.Spinczyk at informatik.uni-erlangen.de
Tue Oct 14 17:48:28 CEST 2003


Hello Gati,

Michael, G. wrote:
> Dear Sir,
> 
> I am doing a project using the AspectC++ system.
> Curently, I am trying to define an aspect that prints the name of the method on the screen at the start and exit of a method.
> Below I include an example of the advice that I have tried.
> However, it yields output only after one of the methods called in my program.
> 
> I seem to remember that there is an example program (maybe in one of the papers on AspectC++) that illustrates this type of functionality.
> 
> Could you please send this example to me and/or tell me how to code such an aspect?
> Kind regards,
> 
> 
>   Gati Michael.
> 
> ---------
> The aspect code looks like this:
> 	#ifndef __mytest_h__
> 	#define __mytest_h__
> 	#include <stdio.h>
> 	aspect mytest {
> 
> 	advice execution((" void %::%()"))&&: before()  
> 	{ printf(" Function ... has been found\n");
> 	    }
> 	advice execution((" void %::%()"))&&: after()  
> 	{printf(" Function ... has been exit\n");
> 	    }
> 	};
> 	#endif // __mytest_h__
> 
> The program code looks like this:
> #include <stdio.h>
> #include <stdlib.h>
> class PBase
>  {
>    public:
>       void abar(int, int) {
>         printf("PBase::abar()\n");
>          cbar(2,5);  }
> 
> void cbar(int, int) {
>         printf("PBase::cbar()\n");
>       }
>  };
> 
> class Test : public PBase {
>   public:
>     void foo() {
>       	printf("Test::foo()\n");
> 	abar(1,2);
> }   void abar(int, int) {
>    printf("Test::abar()\n");
>     }
>     void bbar(int, int) {
>         printf("Test::bbar()\n");}
> };
> class subTest : public Test
>  {
>     void abar(int, int) {
>         printf("subTest::abar()\n");
>     }
>  };
> 
> int main()
>  {
>    subTest test;
>   test.foo();
>   PBase od;
>  od.abar (3, 4);
>  PBase nr;  
>  nr.cbar(4,6);
>   return 0;
>  }
> 
> Here  is the result:
> 
> Function ... has been found
> Test::foo()
> Test::abar()
> Function... has been exit
> PBase::abar()
> PBase::cbar()
> PBase::cbar()
> 
> How can I control the  results such that I get all five prints after each method?

The problem is that your match expression (" void %::%()") only matches 
member functions without arguments (only foo() in your case). To match 
functions with any number of arguments use "void %::%(...)".

The && in your pointcut expression is invalid. Did ac++ really transform 
your code or is this just a cut&paste artefact?

Some more advice:

- be careful with space characters in match expressions, e.g. use
   "void..." and *not* " void..." as in your code.
- limit the scope of your pointcut, e.g. use
   'execution("void %::%(...)")' together with
   '&& within(derived("PBase"))'.

I hope this helps,

Olaf




More information about the aspectc-user mailing list