⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 metre.doc

📁 这是一个C程序分析工具
💻 DOC
📖 第 1 页 / 共 3 页
字号:
Copyright (c) 1993-1995 by Paul Long All rights reserved.Internet:   Paul_Long@ortel.org or plong@perf.com          April 3, 1995CompuServe: 72607,1506                                Metre v2.3Introduction------------Metre is a Standard C (ANSI/ISO) parser whos behavior is determined by aset of rules. It is portable across operating systems, Standard Ccompilers, and various flavors of lex and yacc. However, lex and yaccare generally only needed if you modify the parser, which is not verylikely, or want to run the tools on a non-ASCII system, which is notvery common.When the parser is linked with the rules specified in the accompanyingfile, metrules.c, it becomes Metre, a software-metrics tool forStandard C. When linked with trerules.c, it becomes Mtree, a tool thatprints call trees. See the sections, Metre Metrics Tool and MtreeCall-Tree Tool, respectively, for tool-specific information. All othersections apply to any tool built with Metre. For an explanation of howto construct other tools, see the section, Modifying Metre, in theaccompanying install.txt file.Note: The term, "Metre," is overloaded in that it is used to refer tothis entire product including the parser and the provided sets of rulesand to refer to the software-metrics tool. In other words, Metre is usedto build the Metre software-metrics tool, but it can also be used tobuild other tools that have nothing to do with metrics, such as theMtree call-tree tool. "Metre" is overloaded because of its history--itwas originally intended to be solely a metrics tool.Common CapabilitiesThere are capabilities common to all tools built with Metre. From thecommand line, you can define identifers, provide substitute input-filenames, suppress warnings, provide name of file from which to readcommand-line arguments, specify name of output file, interleave inputwith output, and print help information. Input is taken from a list offile names on the command line or from standard input if no files arespecified. These are described in more detail, below.Input-----Metre does not perform preprocessing, so you should typically run yoursource through a preprocessor first and then run the output of thepreprocessor into Metre. For example, with Borland's MS-DOS tool set:         cpp -P- myfile.c(The -P- tells the preprocessor not to include its non-Standard-C linedirectives in the output.) In this case, the output would be calledmyfile.i. This is the file that should be used with Metre, as in thefollowing:         metre myfile.iTo facilitate piping, Metre takes its input from stdin if no file nameis specified on the command line. This way, if your preprocessor canwrite its output to stdout, you do not need an intermediate file, as in,cpp | metre.Since Metre output refers to the input file by name and this may not bethe name of the original source file (for example, you ran thepreprocessor on it but the preprocessor did not generate line directiveswhich would contain the name of the original file), the -S option can beused to specify the name of the original file, as in -Smyfile.c. Notethat this option can be repeated so that each one specifies a substitutefile name for each input file, respectively. If you entered thefollowing line:         metre -Sa.c -Sb.c a.i b.ithe Metre output would use the name, "a.c", for the first file (a.i)and "b.c" for the second file (b.i).Metre refers to lines by number in its input file unless it encounters aline directive, e.g., #line 5 "startup.c" or # 503 "main.c" 2. Itreplaces the current line number and file name with whatever isspecified in the line directive. Some preprocessors can place linedirectives in the output file that relate back to the original sourcefile. If yours does, you probably want to use this option.While Metre recognizes C comments, for example, /* a comment */, it doesnot recognize C++ comments--those that begin with //. Until Metre does,there is a utility you can download in source form that converts C++comments to C comments. I have not used it, so I cannot personally vouchfor it. It was written by Lars Wirzen (Lars.Wirzenius@helsinki.fi). Ifyou have WWW access, see        http://www.cs.helsinki.fi/~wirzeniu/misc/ccmtcnvt.art,or get it from the comp.sources.misc archives (ftp.uu.net, as/usenet/comp.sources.misc/volume43/ccmtcnvt).Standard C requires there to be one or more external declarations in atranslation unit. However, header files often contain preprocessordirectives but no external declarations. So that header files can beparsed independent of any source files that may include them, Metremakes external declarations optional in a translation unit.Metre reads all command-line options from a "response file" if you usethe -f command-line option. For example, -fmetre.cmd, causes Metre toignore any other arguments on the command line and use whateverarguments are in the file, metre.cmd. The arguments in this file can bespecified on one or more lines. Any input or output redirection mustcontinue to be specified on the command line, however. A response fileis useful when Metre is executed over and over with a large number ofcommand-line arguments, such as file names or -D options.Output------Use the -C command-line option to cause the parser to copy its input toits output, interspersing its reporting information with your sourcefile. This option only works if Metre is built with an MKS or AT&T lexerand not a flex or pclex lexer. The lexer distributed with Metre,lex_yy.c, is an MKS lexer.Use the -o option to redirect output to a file, as in: -oout.txt. Thisis provided for environments that do not have command-line redirection,for example, VMS.To suppress warning diagnostics, use the -w command-line option. To bepresented with help for a Metre tool use the -h command-line option.Adaptibility-------------Metre ignores preprocessor directives (from # to end-of-line) other thanline directives, so if your preprocessor passes any through, such as#pragmas, everything should be okay. This could also be useful if youonly rely on the preprocessor for simple substitution. If so, you mightbe able to get away with running your C source file directly into Metrewithout being preprocessed. This is done in the example in the CertifiedOutput subsection of the Building Metre section in the accompanyinginstall.txt file.Some C compilers, such as Intel's iRMX C compiler, accept the dollarsign ($) as a valid identifier character. Because of this and becausethe dollar sign is not otherwise defined in C, Metre accepts identifierswith a dollar sign. They are valid anywhere a letter is, for example,$100, any$more, and mo$ are all valid identifiers to Metre. If you donot want to allow it, change scan.l, run it through lex, and rebuild theexecutable.The define command-line option allows you to perform simplesubstitutions within the input file. This may be enough to convert theextensions used by your compiler to something that Metre understands asStandard C syntax. For example, since Borland uses cdecl in its stdio.hfile, I have to use the following command to get Metre to accept it:         metre -Dcdecl= metrules.iThis defines cdecl as a null string, effectively removing alloccurrences from Metre's input stream. There may also be a situationwhere a lexeme used in a declaration could be converted to another, asin the following:         metre -Dselector=int foo.iThe lexeme, selector, is a type specifier recognized by Intel's iRMX Ccompiler. Other examples include the storage-class specifiers, globaldefand globalref, recognized by DEC's VAX C compiler. In this case, theyshould be replaced by extern with the following:         metre "-Dglobalref=extern" "-Dglobaldef=extern" foo.iNote that when running Metre under DEC's VMS operating system, thecommand-line options must be quoted because VMS's command-lineinterpreter normally upshifts all command-line arguments. If not quoted,Metre would see -DGLOBALREF=EXTERN and -DGLOBALDEF=EXTERN. In C,GLOBALREF, GLOBALDEF, and EXTERN are not the same keywords as globalref,globaldef, and extern. Therefore, globaldef and globalref would not bereplaced by extern.Depending on which command-line interpreter, or shell, you are using,such as MS-DOS' COMMAND.COM, quoting a definition option may also allowyou to specify expansion text that contains imbedded spaces, such as"-Dulong=unsigned long". I have never needed to do this, but just keepit in the back of your mind. It may come in handy someday.Now I need to mention how to handle a situation about which severalpeople have asked me. "How can a get Metre to ignore the expansion ofthe stdio.h macros, such as getc()?"  One answer is to modify yourstdio.h file so that if the manifest constant, Metre, is defined, itprovides function declarations instead of macro definitions. Thenpreprocess your source files with "Metre" defined on the command line,as in -DMetre. This is a trick that I found in Sun's stdio.h forhandling lint--apparently lint needlessly complains about these defines.For example, Borland provides macros for the ferror(), feof(), getc(),and putc() standard-I/O functions. The expanded code containsexpressions that could make your function appear to be more complex thanit is. Modify stdio.h with the following directives and functiondeclarations then run the preprocessor against your input file with the-DMetre command-line option. Et voila, Metre is able to calculate a moreaccurate complexity index.   #ifdef Metre   int ferror(FILE *);   int feof(FILE *);   int getc(FILE *);   int putc(int, FILE *);   #else   ...   (The original defines for these functions go here.)   #endifMetre Metrics Tool------------------The set of rules in metrules.c defines the software-metrics tool, Metre.Metre reports the following metrics at the function, module, and projectlevel, where applicable, and for each metric it reports the minimum,maximum, average, and total, again where applicable.         McCabe's cyclomatic complexity (cyc)         Myers' extended complexity (exc)         Halstead's program length (pln), program vocabulary (pvc),                program volume (pvl), program level (plv), programming                effort (pef), intelligence content (inc), programming                time (in hours) (ptm), and language level (llv)         Number of source (scl), code (cdl), comment (cml), and blank                lines (bll)         Number of executable (exs), declaration (dcs), and preprocessor                (pps) statements         Backfired function points (fnp)         Maximum control depth (mcd)         Identifier count (idc) and length (idl)         Number of functions (fnc) and modules (mdc)The abbreviation in parentheses following the metric is the name thatone can use in the -m command-line option. See the section, ControllingMetre's Output, for more information.Specifically, Metre reports these metrics for each function:      Cyclomatic:      total        Extended:      total      Halstead Length: total        Vocabulary:    total        Volume:        total        Level:         total        Effort:        total        Intelligence:  total        Time (hours)   total        Lang Level:    total      Lines:           total        Code:          total        Comment:       total        Blank:         total      Exec Statements: total      Decl Statements: total      Max Depth:       total      Identifiers:     total        Length:        <min >max ~avgthese for each module:      -Function-      Cyclomatic:      <min >max ~avg        Extended:      <min >max ~avg      Halstead Length: <min >max ~avg        Vocabulary:    <min >max ~avg        Volume:        <min >max ~avg        Level:         <min >max ~avg        Effort:        <min >max ~avg        Intelligence:  <min >max ~avg        Time (hours)   <min >max ~avg total        Lang Level:    <min >max ~avg      Lines:           <min >max ~avg total        Code:          <min >max ~avg total        Comment:       <min >max ~avg total        Blank:         <min >max ~avg total      Exec Statements: <min >max ~avg      Decl Statements: <min >max ~avg total      Max Depth:       <min >max ~avg      -Module-      Lines:           total        Code:          total        Comment:       total        Blank:         total      Exec Statements: total      Decl Statements: total      PP Statements:   total      Function Points: total      Identifiers:     total        Length:        <min >max ~avg      Functions:       totaland these for each project if there is more than one module:   -Function-   Cyclomatic:      <min >max ~avg     Extended:      <min >max ~avg   Halstead Length: <min >max ~avg     Vocabulary:    <min >max ~avg     Volume:        <min >max ~avg     Level:         <min >max ~avg     Effort:        <min >max ~avg     Intelligence:  <min >max ~avg     Time (hours)   <min >max ~avg total     Lang Level:    <min >max ~avg   Lines:           <min >max ~avg total     Code:          <min >max ~avg total     Comment:       <min >max ~avg total     Blank:         <min >max ~avg total   Exec Statements: <min >max ~avg   Decl Statements: <min >max ~avg total   Max Depth:       <min >max ~avg   -Module-   Lines:           <min >max ~avg     Code:          <min >max ~avg     Comment:       <min >max ~avg     Blank:         <min >max ~avg   Exec Statements: <min >max ~avg   Decl Statements: <min >max ~avg   PP Statements:   <min >max ~avg   Functions:       <min >max ~avg   -Project-   Lines:           total     Code:          total     Comment:       total     Blank:         total   Exec Statements: total   Decl Statements: total   PP Statements:   total   Function Points: total   Identifiers:     total     Length:        <min >max ~avg   Functions:       total   Modules:         totalCyclomatic is McCabe's cyclomatic complexity index; Extended is GlenfordMyers' extension to cyclomatic complexity; Lang Level refers toHalstead's Language Level metric; Max Depth refers to the maximumcontrol depth; Exec, Decl, and PP Statements refer to executable,declaration, and preprocessor statements.Controlling Metre's OutputThe abbreviations in parentheses following the metrics listed at the

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -