Background technology
Code analysis claims again code scans or code check, mainly checks that source program code is to the degree of following of standard, readable height, the correctness of code logic expression, the aspects such as rationality of code structure.By code analysis, run counter to the problem of programming standard in can discovery procedure, and dangerous, indefinite and fuzzy part in program, find out and in program, repeat or portable part not.Code analysis comprises the 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 adopting 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 the regular formulation of inspection and the difference of analyzer algorithm.
There is following shortcoming in existing code analysis tool:
(1) low, the consuming time length of analysis efficiency, cannot 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 code analysis need of work and again does;
(3) as the code change of analyzing, can only be again execution analyzer from the beginning, be therefore difficult to the analysis of code while frequently changing;
(4) statistical study of more applicable single or a small amount of code file, in the face of huge code library, cannot do the comparative analysis between file and file, such as function call situation, duplicated code rate, code reuse situation, Module Division situation etc.
Summary of the invention
The object of the embodiment of the present invention is to provide a kind of code analysis methods, is intended to solve that existing code analysis efficiency is low, flexibility ratio is poor, is not suitable with the problem of enterprise-level code analysis.
The embodiment of the present 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 to code analysis, output code analysis result.
Another object of the embodiment of the present invention is to provide a kind of code analysis system, and described system comprises:
Indexing units, for collecting code information, sets up index database, and query interface is provided;
Code profiler, for according to the inspection rule of configuration, inquires about described index database by described query interface, obtains Query Result, Query Result is carried out to code analysis, output code analysis result; And
Code analysis display unit, for the code analysis result of code profiler output described in output display.
The embodiment of the present invention is collected code, set up index, for symbol lookup result, analyze, having realized code reads with code analysis and is separated, efficiency 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 object of the present invention, technical scheme and advantage clearer, below in conjunction with drawings and Examples, the present invention is further elaborated.Should be appreciated that specific embodiment described herein, only in order to explain the present invention, is not intended to limit the present invention.
The embodiment of the present invention is collected processing to code, sets up index database, utilizes high speed query interface to inquire about, and Query Result is carried out to code analysis, can greatly improve efficiency and the flexibility ratio of code analysis, realizes the analysis management of enterprise-level code.
Fig. 2 shows the realization flow of the code analysis methods that the embodiment of the present 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 self-developing.Search engine can be collected code information according to certain strategy, and after code information is organized and processed, the code information after output is processed, for rear end code analysis provides retrieval service.
Search engine in the embodiment of the present invention is full-text search engine, extracts the code information in each software program, sets up index data base, and can retrieve the record matching with querying condition, by certain putting in order, returns to Query Result, and detailed process is as follows:
1, the code information in capture program
Search engine has corresponding code capture program (Web Crawler), can from huge enterprise-level program code storehouse, capture continuously the code information in each program.
2, process the code capturing, set up index database
Search engine is caught after code information, do a large amount of pre-service work, and most important is exactly the keyword extracting in code information, sets up index database.
As one embodiment of the present of invention, search engine can monitor the code variations situation of target program easily, even if code changes, only need to again read and analyze the code section of variation, therefore can adapt to the code analysis under the frequent change conditions of code.
In step S102, according to the inspection rule of configuration, search index storehouse, obtains Query Result;
In embodiments of the present invention, during code analysis, directly code is not analyzed, but the query interface providing based on search engine, according to the inspection rule input inquiry condition of configuration, search index storehouse, the Query Result that search engine is returned carries out statistical study, can greatly promote the efficiency of statistics and comparative analysis.
In step S103, Query Result is analyzed to output analysis result.
In embodiments of the present invention, according to the Query Result of search engine, can realize the comparative analysis between code in huge source code library, such as finding out in whole project or enterprise the called situation of certain function etc., can also do high-rise logic analysis to code, such as duplicated code rate, code reuse situation, Module Division situation etc.
The embodiment of the present invention is by carrying out combination by 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 code, according to keywords create index, so just possessed the full-text search ability to code library.During code analysis, directly code is not analyzed, but the query interface providing based on search engine, Query Result is carried out to statistical study, can greatly promote the efficiency of statistics and comparative analysis, for example can first retrieve the code file that comprises certain function definition, again these code files are analyzed, judge that whether these function inside are also identical, to find out quickly and easily duplicated code.
Fig. 3 shows the structure of the code analysis system that the embodiment of the present invention provides, and for convenience of explanation, only shows the part relevant to the embodiment of the present 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, using search engine as indexing units, search engine can utilize existing search engine, also can self-developing.
Code information in searcher 11 discoveries and acquisition program.
Index 12 is processed the code information that searcher 11 searches, and therefrom extracts index entry, according to keywords creates index.
The index that index database 13 storage indexs 12 create, for example concordance list.
Searcher 14 provides query interface to code profiler 2, the querying condition search index storehouse 13 providing according to code profiler 2, output Query Result.
Code profiler 2 is regular according to the inspection of configuration, the query interface providing by indexing units 1, and search index storehouse, obtains Query Result, and Query Result is carried out to code analysis, output code analysis result.
In embodiments of the present invention, during code profiler 2 code analysis, directly code is not analyzed, but the query interface providing based on indexing units 1, according to the inspection rule input inquiry condition of configuration, search index storehouse, the Query Result that indexing units 1 is returned carries out statistical study, can greatly promote the efficiency 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 the query interface input inquiry condition that generates and provide to code search engine 1, receives the Query Result that code search engine 1 returns.
22 pairs of Query Results of code analysis logic carry out code analysis, 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 code in huge source code library, such as finding out in whole project or enterprise the called situation of certain function etc., can also do high-rise logic analysis to code, such as duplicated code rate, code reuse situation, Module Division situation etc.
The code analysis result of code analysis display unit 3 output display code profiler 2 outputs.
The embodiment of the present invention is collected code, set up index, for symbol lookup result, analyze, having realized code reads with code analysis and is separated, efficiency 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 present 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 separated with code analysis, if check rule change, only need again do code analysis.Meanwhile, based on search inquiry interface at a high speed, make code analysis efficient, rapid;
(3) can conveniently monitor the change conditions of object code, even if code changes, only need to again read and analyze changing unit, the analysis while can code frequently changing;
(4) expand the scope of application of code analysis, be particularly useful for the mega project that code quantity is huge, or analysis and monitoring to the whole codes of whole enterprise, can also realize spanned item destination code compare of analysis.
The foregoing is only preferred embodiment of the present invention, not in order to limit the present invention, all any modifications of doing within the spirit and principles in the present invention, be equal to and replace and improvement etc., within all should being included in protection scope of the present invention.