Background technology
Code analysis claims code scans or code check again, mainly checks the aspects such as rationality of source program code to the degree of following of standard, readable height, code logic correctness of expression, code structure.Run counter to the problem of programming standard in can discovery procedure by code analysis, and dangerous, indeterminate and fuzzy part in the program, find out and repeat in the program or portable part not.Code analysis comprises contents such as variable inspection, name and type examination, programmed logic examination, program syntax inspection and program structure inspection.
At present, the instrument of code analysis has a lot, the technical method that adopts is similar, as shown in Figure 1, generally be divided into fetch program, analyzer and form and show three parts, most of instrument can self-defined inspections rule, and main difference concentrates on checks the different of regular formulation and analyzer algorithm.
There is following shortcoming in existing code analysis tool:
(1) low, the consuming time length of analysis efficiency, can't be competent at the code administration of enterprise-level;
(2) code reads and analysis is coupled, and after checking that rule changes, code reads with the code analysis need of work and does again;
(3) the code change as analyzing, can only be once more execution analyzer from the beginning, therefore be difficult to the analysis of code when frequently changing;
(4) statistical study of more suitable single or a small amount of code file in the face of huge code library, can't be done the comparative analysis between file and the file, for example function call situation, duplicated code rate, code reuse situation, Module Division situation etc.
Summary of the invention
The purpose of the embodiment of the invention is to provide a kind of code analysis methods, is intended to solve that existing code analysis efficient is low, flexibility ratio is poor, the problem of incompatibility enterprise-level code analysis.
The embodiment of the invention is achieved in that a kind of code analysis methods, and described method comprises the steps:
Collect code information, set up index database;
According to the inspection rule of configuration, inquire about described index database, obtain Query Result;
Described Query Result is carried out code analysis, the output code analysis result.
Another purpose of the embodiment of the invention is to provide a kind of code analysis system, and described system comprises:
Indexing units is used to collect code information, sets up index database, and query interface is provided;
Code profiler is used for the inspection rule according to configuration, inquires about described index database by described query interface, obtains Query Result, Query Result is carried out code analysis, the output code analysis result; And
The code analysis display unit is used to export the code analysis result who shows described code profiler output.
The embodiment of the invention is collected code, set up index, the result analyzes at symbol lookup, having realized that code reads with code analysis is separated, the efficient and the flexibility ratio of code analysis have greatly been promoted, can realize the analysis management of enterprise-level code, code is collected with index and can be realized by search engine.
Embodiment
In order to make purpose of the present invention, technical scheme and advantage clearer,, the present invention is further elaborated below in conjunction with drawings and Examples.Should be appreciated that specific embodiment described herein only in order to explanation the present invention, and be not used in qualification the present invention.
The embodiment of the invention is collected processing to code, sets up index database, utilizes the high speed query interface to inquire about, and Query Result is carried out code analysis, can improve the efficient and the flexibility ratio of code analysis greatly, realizes the analysis management of enterprise-level code.
Fig. 2 shows the realization flow of the code analysis methods that the embodiment of the invention provides, and details are as follows:
In step S101, collect code information, set up index database;
As one embodiment of the present of invention, utilize search engine to collect code information, set up index database.Search engine can utilize existing search engine, also can develop voluntarily.Search engine can be collected code information according to certain strategy, and after code information was organized and handled, the code information after output is handled was for the rear end code analysis provides retrieval service.
Search engine in the embodiment of the invention is the full-text search engine, extracts the code information in each software program, sets up index data base, and can retrieve the record that is complementary with querying condition, returns Query Result by certain arrangement sequence, and detailed process is as follows:
1, the code information in the capture program
Search engine has corresponding code capture program (Web Crawler), can grasp the code information in each program from huge enterprise-level program code storehouse continuously.
2, handle the code that grasps, set up index database
After search engine is caught code information, do a large amount of pre-service work, most important is exactly the keyword that extracts in the code information, sets up index database.
As one embodiment of the present of invention, search engine is the code variations situation of monitored object program easily, even code changes, only need read and analyze the code section of variation again, therefore can adapt to the code analysis under the frequent change conditions of code.
In step S102, according to the inspection rule of configuration, the search index storehouse obtains Query Result;
In embodiments of the present invention, directly code is not analyzed during code analysis, and be based on the query interface that search engine provides, inspection rule input inquiry condition according to configuration, the search index storehouse, the Query Result that search engine is returned carries out statistical study, can greatly promote the efficient of statistics and comparative analysis.
In step S103, Query Result is analyzed the output analysis result.
In embodiments of the present invention, Query Result according to search engine, can realize the comparative analysis between the code in the huge source code library, for example find out in whole project or the enterprise certain function situation etc. that is called, can also make high-rise logic analysis, for example duplicated code rate, code reuse situation, Module Division situation etc. to code.
The embodiment of the invention is by carrying out combination with search engine and two kinds of proven technique of code profiler, the ability of utilizing search engine to collect text message is collected code information, preserve the necessary information in the code, according to keywords create index, so just possessed full-text search ability code library.Directly code is not analyzed during code analysis, and be based on the query interface that search engine provides, Query Result is carried out statistical study, can greatly promote the efficient of statistics and comparative analysis, for example can retrieve the code file that comprises certain function definition earlier, again these code files are analyzed, judge whether these function inside are also identical, to find out duplicated code quickly and easily.
Fig. 3 shows the structure of the code analysis system that the embodiment of the invention provides, and for convenience of explanation, only shows the part relevant with the embodiment of the invention.
Indexing units 1 is collected code information, sets up index database, and query interface is provided.
As one embodiment of the present of invention, as indexing units, search engine can utilize existing search engine with search engine, also can develop voluntarily.
Code information in searcher 11 discoveries and the acquisition program.
Index 12 is handled the code information that searcher 11 is searched, and therefrom extracts index entry, according to keywords creates index.
The index that index database 13 storage indexs 12 are created, for example concordance list.
Searcher 14 provides query interface to code profiler 2, according to the querying condition search index storehouse 13 that code profiler 2 provides, and the output Query Result.
Code profiler 2 is according to the inspection rule of configuration, and by the query interface that indexing units 1 provides, the search index storehouse obtains Query Result, and Query Result is carried out code analysis, the output code analysis result.
In embodiments of the present invention, directly code is not analyzed during code profiler 2 code analysis, and be based on the query interface that indexing units 1 provides, inspection rule input inquiry condition according to configuration, the search index storehouse, the Query Result that indexing units 1 is returned carries out statistical study, can greatly promote the efficient of statistics and comparative analysis.
As one embodiment of the present of invention, symbol lookup logic 21 is according to the inspection rule of configuration, and generation and the query interface input inquiry condition that provides to code search engine 1 receive the Query Result that code search engine 1 returns.
22 pairs of Query Results of code analysis logic carry out code analysis, the output code analysis result.
In embodiments of the present invention, code analysis logic 22 is according to the Query Result of search engine, can realize the comparative analysis between the code in the huge source code library, for example find out in whole project or the enterprise certain function situation etc. that is called, can also make high-rise logic analysis, for example duplicated code rate, code reuse situation, Module Division situation etc. to code.
The code analysis result of code analysis display unit 3 output reveal codes analyzers 2 outputs.
The embodiment of the invention is collected code, set up index, the result analyzes at symbol lookup, having realized that code reads with code analysis is separated, the efficient and the flexibility ratio of code analysis have greatly been promoted, can realize the analysis management of enterprise-level code, code is collected with index and can be realized by search engine.
Particularly, the embodiment of the invention has the following advantages:
(1) huge code library can be analyzed, the analysis management of enterprise-level code can be realized;
(2) code is read with code analysis separate,, only need do code analysis again if check the rule change.Simultaneously, based on search inquiry interface at a high speed, make code analysis efficient, rapid;
(3) can make things convenient for the change conditions of monitored object code,, only need read and analyze changing unit again, the analysis when can code frequently changing even code changes;
(4) enlarged the scope of application of code analysis, be particularly useful for the huge mega project of code quantity,, can also realize spanned item destination code compare of analysis perhaps to the analysis and the monitoring of the whole codes of whole enterprise.
The above only is preferred embodiment of the present invention, not in order to restriction the present invention, all any modifications of being done within the spirit and principles in the present invention, is equal to and replaces and improvement etc., all should be included within protection scope of the present invention.