View on
Daniel Pfeiffer > makepp > Mpp::Scanner



Annotate this POD


View/Report Bugs


Mpp::Scanner - Base class for makepp file scanners


  use Mpp::Scanner;
  our @ISA = qw/Mpp::Scanner/;
  sub xscan_file {
    my( $self, $cp, $tag, $finfo, $conditional, $fh ) = @_;
    while(<$fh>) {
      # ...


Mpp::Scanner is a base class for makepp(1) file scanners. It supports both conditional scanning and unconditional scanning.

The scanner for a particular language should derive from this class. An object of this type is normally instantiated by an object of type Mpp::CommandParser.

If unconditional scanning is used, then every include statement is considered active. This has the advantage that scanning is simpler, and that include directives are easily cached. However, it has the disadvantage of possibly creating incorrect dependencies. For example, in C the following lines should not affect the dependencies:

  #if 0
  # include "foo.h"

Also, the following should imply a dependency on foo.h, which should itself be scanned:

  #define NAME "foo.h"
  #include NAME

The reason that the include directives cannot easily be cached in conditional scanning is that the same file might include different things depending on what preceded it and what was defined on the command line. For the same reason, it is not possible to scan include files for the same translation in parallel if conditional scanning is employed (although that isn't done with unconditional scanning either right now).



  my $scanner = Mpp::Scanner->new($rule, $dir, $conditional);

Returns a new Mpp::Scanner for rule $rule. $dir is the directory name (relative to the CWD of $rule's Makefile) in which files are to be interpreted. $conditional is TRUE if conditional scanning is to be used, FALSE if not, and undef if it is to be determined from the rule options. The subclass may override this method, and may have different parameters.


  $scanner->add_include_dir($tag, $path);

Add directory $path (relative to $self->{DIR}) to the include path for tag $tag. Tags can be anything you want, as long as they are lowercase, for example "user" and "sys" for the C double-quote include path and angle-bracket include path, respectively.

An undefined $path represents the directory in which the file containing the include directive resides. A path matching /^in :/ is followed by an alternate tag to look in. A path matching /^in ;?/ is followed by an environment variable to look in. The optional ';' means to split on that (even when running under Cygwin).


  $scanner->add_include_suffix($tag, $suffix);

Append $suffix to the list of suffixes to append to the string naming the file to be included (i.e. the $name parameter of the include() method). The list starts as an empty array, but it defaults to ('') if a file is sought before any suffix is added. If the first element is '/' it means to try the other suffixes in the list only for a name that doesn't have one (embedded SQL/C). Directory search order takes precedence over suffix search order. [This is used by VCS, as well as a number of other Verilog-related tools.]



Add include path tag $tag to the list of tags that should generate a warning if the requested file is not found. Not calling this is useful, for example, for C angle-bracket includes, wherein it is likely that the file won't be found because we don't know the entire standard system include path. It is assumed that files included from the system include path would not be included with double quotes, to avoid false warnings.


  $scanner->info_string( $hashref );

Sets the build_info_string name associated with includes using the $tag include path to $name. For example, in C, "user" maps to "INCLUDES" and "sys" maps to "SYSTEM_INCLUDES".

NOTE: If you assign an info_string to any tag, then you need to assign info_string's to all the tags, or else the tags that aren't assigned will be ignored when the cached includes are used.


  $scanner->dont_scan($finfo, $absname);

Returns 1 if the file $finfo should be scanned. $absname must be $finfo's absolute filename.

Files that shouldn't be scanned typically are the system include files, as well as any files that are in a directory that can't be written by the current user. It is assumed that such files don't include other files that change between clean builds. This makes scanning go faster, and reduces the amount of "noise" emitted by makepp.


  $scanner->scan_file($command_parser, $tag, $name);

Check for cached dependencies of the file given by $name (relative to the current directory). If they are found, then add them to the dependency list, and either scan them recursively, or add them to the list of dependencies to scan later. Otherwise, delegate to $self->xscan_file().

Before this method returns successfully, all of the files that are included (possibly recursively) by the file will have been scanned.

The return value is TRUE on success, FALSE if any files to be scanned failed to build, or if scanning failed.



Scan pending files. This is useful if include() is called after the last scan_file() call. Returns TRUE if and only if any files were scanned. Returns undef if and only if a file to be scanned failed to build, or if scanning failed.


  $scanner->xscan_file($command_parser, $tag, $finfo, $conditional, $fh);

The derived class should define this to scan file handle $fh and report implicit dependencies by calling the include method.

If $conditional is FALSE, then the following methods may not be called:


Also, is_active() is guaranteed to return TRUE, and include() is guaranteed not to call scan_file() recursively.

$fh is a file handle that is initially opened for reading at the beginning of the file to be scanned.

This method should be called only from scan_file() and not directly, because otherwise you have to check for cached includes yourself. Implemented by classes which inherit from this.

A FALSE return value is interpreted as a scanning failure.


  $scanner->find($tag, $name, $src_dir);

Returns the Mpp::File object associated with the location where $name is found, or undef if $name is not found. $tag is the include path tag, and $src is the source Mpp::File, if any, or the source directory Mpp::File.


  $scanner->include($command_parser, $tag, $name, $src);

Signals that an include file whose name is $name and whose type is $tag was requested, but only if $self->{ACTIVE} is TRUE. If the file is found, then a dependency is added to $self->rule and the Mpp::File object of the file is returned. Otherwise, 1 is returned if the file is not found, or undef is returned if there was an error generating it or some other nested include file.

Whether this returns immediately or calls $self->xscan_file() recursively is not guaranteed. Currently, it is if and only if $self->{CONDITIONAL} is TRUE.

Whether this sets the build_info_string INCLUDE's of $name is not guaranteed. Currently, it does if and only if $self->{CONDITIONAL} is FALSE.


  my $rule=$scanner->rule;

Returns the rule object.


  my $dir=$scanner->dir;

Returns the dir name.


  my $finfo=$scanner->get_file_info($name);

Returns the Mpp::File object associated with $name relative to dir().


  my $context=$scanner->get_context;

Returns a scalar representing the current scanning context, i.e. the variable settings and enclosing scopes. The format of this scalar is not guaranteed, but you can pass it to reset().



Denotes the beginning of a new translation unit. Reset all variables, and sets $self->{ACTIVE} to TRUE. A Mpp::Scanner object is guaranteed to be in the reset state on return from new(). If $context (from get_context()) is specified, then reset to that context instead of clearing everything.


  $scanner->set_var($name, $value);

Sets the variable whose name in $name to $value, but only if $self->{ACTIVE} is TRUE.


  my $value=$scanner->get_var($name);

Returns the value of the variable whose name is $name.



Push a new scope, and set $self->{ACTIVE} to $act if $self->{ACTIVE} is TRUE. A scope is typically introduced when a preprocessor conditional (e.g. #ifdef) is encountered, and the scope is inactive if its conditional evaluated false.



Reverse the most recent push_scope(). A scope is typically closed when a preprocessor conditional is terminated (e.g. with #endif). Returns $self->{ACTIVE} before popping.


  my $act=$scanner->is_active;

Returns $self->{ACTIVE}. The current scope is active if and only if all enclosing scopes are active.


One way to cache the scanning of include files with conditional scanning in force is to generate a rendition of the source file that has only the preprocessor directives in it, and scan that instead. That file can be kept up-to-date using makepp in the usual way (more or less). Then the amount of I/O required for scanning is then much less when the source file hasn't been modified. It may or may not be practical to store this in the build info structure.

syntax highlighting: