You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

PreprocessorHandler.java 4.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. MIT License
  3. Copyright (c) 2018-2019 Gang ZHANG
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in all
  11. copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  18. SOFTWARE.
  19. */
  20. package depends.extractor.cpp.cdt;
  21. import java.io.File;
  22. import java.io.IOException;
  23. import java.util.ArrayList;
  24. import java.util.HashSet;
  25. import java.util.List;
  26. import org.eclipse.cdt.core.dom.ast.IASTFileLocation;
  27. import org.eclipse.cdt.core.dom.ast.IASTPreprocessorIncludeStatement;
  28. import org.eclipse.cdt.core.dom.ast.IASTPreprocessorStatement;
  29. import org.eclipse.cdt.internal.core.parser.scanner.ScannerUtility;
  30. import multilang.depends.util.file.FileTraversal;
  31. import multilang.depends.util.file.FileTraversal.IFileVisitor;
  32. import multilang.depends.util.file.FileUtil;
  33. public class PreprocessorHandler {
  34. private List<String> includePaths;
  35. private String inputSrcPath;
  36. private HashSet<String> allFiles = new HashSet<>();
  37. public PreprocessorHandler(String inputSrcPath, List<String> includePaths){
  38. this.inputSrcPath = inputSrcPath;
  39. this.includePaths = includePaths;
  40. buildAllFiles();
  41. }
  42. class AllFileVisitor implements IFileVisitor{
  43. @Override
  44. public void visit(File file) {
  45. try {
  46. allFiles.add(file.getCanonicalPath());
  47. } catch (IOException e) {
  48. }
  49. }
  50. }
  51. private void buildAllFiles() {
  52. allFiles = new HashSet<>();
  53. AllFileVisitor v = new AllFileVisitor();
  54. if (inputSrcPath!=null) {
  55. FileTraversal ft = new FileTraversal(v,false,true);
  56. ft.travers(inputSrcPath);
  57. }
  58. for (String includePath:includePaths) {
  59. FileTraversal ft = new FileTraversal(v,false,true);
  60. ft.travers(includePath);
  61. }
  62. }
  63. private boolean existFile(String checkPath) {
  64. checkPath = FileUtil.uniformPath(checkPath);
  65. return allFiles.contains(checkPath);
  66. }
  67. public List<String> getDirectIncludedFiles(IASTPreprocessorStatement[] statements, String fileLocation) {
  68. ArrayList<String> includedFullPathNames = new ArrayList<>();
  69. for (int statementIndex=0;statementIndex<statements.length;statementIndex++) {
  70. if (statements[statementIndex] instanceof IASTPreprocessorIncludeStatement)
  71. {
  72. IASTPreprocessorIncludeStatement incl = (IASTPreprocessorIncludeStatement)(statements[statementIndex]);
  73. if (!incl.getFileLocation().getFileName().equals(fileLocation))
  74. continue;
  75. String path = resolveInclude(incl);
  76. if (!existFile(path)) {
  77. continue;
  78. }
  79. if (FileUtil.isDirectory(path)) {
  80. continue;
  81. }
  82. includedFullPathNames.add(path);
  83. }
  84. }
  85. return includedFullPathNames;
  86. }
  87. private String resolveInclude(IASTPreprocessorIncludeStatement incl) {
  88. String path = incl.toString();
  89. int pos = path.indexOf(' ');
  90. path = path.substring(pos+1).trim();
  91. if (path.startsWith("\"") || path.startsWith("<")){
  92. path = path.substring(1);
  93. path = path.substring(0,path.length()-1);
  94. }
  95. //First search in local directory
  96. IASTFileLocation location = incl.getFileLocation();
  97. String locationDir = FileUtil.getLocatedDir(location.getFileName());
  98. ArrayList<String> searchPath = new ArrayList<>();
  99. searchPath.add(locationDir);
  100. searchPath.addAll(includePaths);
  101. for (String includePath:searchPath) {
  102. String checkPath = ScannerUtility.createReconciledPath(includePath,path);
  103. if (existFile(checkPath)) {
  104. return FileUtil.uniqFilePath(checkPath);
  105. }
  106. }
  107. return "";
  108. }
  109. public List<String> getIncludePaths() {
  110. return includePaths;
  111. }
  112. }

基于Vue的大屏可视化设计器,前后端一体化解决方案,几十种炫酷图表,支持多种数据来源接入,适用于大屏、低代码、BI场景,使用简单,代码完全开源。

Contributors (2)