PageRenderTime 49ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/netbeans-7.3/cnd.dwarfdiscovery/src/org/netbeans/modules/cnd/dwarfdiscovery/provider/BaseDwarfProvider.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 789 lines | 703 code | 29 blank | 57 comment | 244 complexity | c9e641f9251d64213e7a5f93701c2544 MD5 | raw file
  1. /*
  2. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
  3. *
  4. * Copyright 1997-2010 Oracle and/or its affiliates. All rights reserved.
  5. *
  6. * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
  7. * Other names may be trademarks of their respective owners.
  8. *
  9. * The contents of this file are subject to the terms of either the GNU
  10. * General Public License Version 2 only ("GPL") or the Common
  11. * Development and Distribution License("CDDL") (collectively, the
  12. * "License"). You may not use this file except in compliance with the
  13. * License. You can obtain a copy of the License at
  14. * http://www.netbeans.org/cddl-gplv2.html
  15. * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
  16. * specific language governing permissions and limitations under the
  17. * License. When distributing the software, include this License Header
  18. * Notice in each file and include the License file at
  19. * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
  20. * particular file as subject to the "Classpath" exception as provided
  21. * by Oracle in the GPL Version 2 section of the License file that
  22. * accompanied this code. If applicable, add the following below the
  23. * License Header, with the fields enclosed by brackets [] replaced by
  24. * your own identifying information:
  25. * "Portions Copyrighted [year] [name of copyright owner]"
  26. *
  27. * Contributor(s):
  28. *
  29. * The Original Software is NetBeans. The Initial Developer of the Original
  30. * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
  31. * Microsystems, Inc. All Rights Reserved.
  32. *
  33. * If you wish your version of this file to be governed by only the CDDL
  34. * or only the GPL Version 2, indicate your decision by adding
  35. * "[Contributor] elects to include this software in this distribution
  36. * under the [CDDL or GPL Version 2] license." If you do not indicate a
  37. * single choice of license, a recipient has the option to distribute
  38. * your version of this file under either the CDDL, the GPL Version 2 or
  39. * to extend the choice of license to its licensees as provided above.
  40. * However, if you add GPL Version 2 code and therefore, elected the GPL
  41. * Version 2 license, then the option applies only if the new code is
  42. * made subject to such option by the copyright holder.
  43. */
  44. package org.netbeans.modules.cnd.dwarfdiscovery.provider;
  45. import java.io.File;
  46. import java.io.FileNotFoundException;
  47. import java.io.IOException;
  48. import java.util.ArrayList;
  49. import java.util.HashMap;
  50. import java.util.List;
  51. import java.util.Map;
  52. import java.util.Set;
  53. import java.util.TreeMap;
  54. import java.util.concurrent.ConcurrentHashMap;
  55. import java.util.concurrent.CountDownLatch;
  56. import java.util.concurrent.atomic.AtomicBoolean;
  57. import java.util.concurrent.atomic.AtomicInteger;
  58. import java.util.logging.Level;
  59. import org.netbeans.api.project.Project;
  60. import org.netbeans.modules.cnd.api.remote.RemoteFileUtil;
  61. import org.netbeans.modules.cnd.discovery.api.ApplicableImpl;
  62. import org.netbeans.modules.cnd.discovery.api.DiscoveryExtensionInterface.Position;
  63. import org.netbeans.modules.cnd.discovery.api.DiscoveryProvider;
  64. import org.netbeans.modules.cnd.discovery.api.DiscoveryUtils;
  65. import org.netbeans.modules.cnd.discovery.api.ItemProperties;
  66. import org.netbeans.modules.cnd.discovery.api.Progress;
  67. import org.netbeans.modules.cnd.discovery.api.ProjectProxy;
  68. import org.netbeans.modules.cnd.discovery.api.ProviderProperty;
  69. import org.netbeans.modules.cnd.discovery.api.SourceFileProperties;
  70. import org.netbeans.modules.cnd.discovery.wizard.api.support.ProjectBridge;
  71. import org.netbeans.modules.cnd.dwarfdiscovery.provider.RelocatablePathMapper.FS;
  72. import org.netbeans.modules.cnd.dwarfdiscovery.provider.RelocatablePathMapper.ResolvedPath;
  73. import org.netbeans.modules.cnd.dwarfdump.CompilationUnit;
  74. import org.netbeans.modules.cnd.dwarfdump.CompilationUnitInterface;
  75. import org.netbeans.modules.cnd.dwarfdump.Dwarf;
  76. import org.netbeans.modules.cnd.dwarfdump.dwarf.DwarfEntry;
  77. import org.netbeans.modules.cnd.dwarfdump.dwarfconsts.LANG;
  78. import org.netbeans.modules.cnd.dwarfdump.dwarfconsts.TAG;
  79. import org.netbeans.modules.cnd.dwarfdump.exception.WrongFileFormatException;
  80. import org.netbeans.modules.cnd.dwarfdump.reader.ElfReader.SharedLibraries;
  81. import org.netbeans.modules.cnd.makeproject.api.configurations.Item;
  82. import org.netbeans.modules.cnd.utils.CndUtils;
  83. import org.netbeans.modules.cnd.utils.cache.CndFileUtils;
  84. import org.netbeans.modules.dlight.libs.common.PathUtilities;
  85. import org.openide.filesystems.FileObject;
  86. import org.openide.filesystems.FileSystem;
  87. import org.openide.util.Exceptions;
  88. import org.openide.util.NbBundle;
  89. import org.openide.util.RequestProcessor;
  90. /**
  91. *
  92. * @author Alexander Simon
  93. */
  94. public abstract class BaseDwarfProvider implements DiscoveryProvider {
  95. public static final String RESTRICT_SOURCE_ROOT = "restrict_source_root"; // NOI18N
  96. public static final String RESTRICT_COMPILE_ROOT = "restrict_compile_root"; // NOI18N
  97. protected AtomicBoolean isStoped = new AtomicBoolean(false);
  98. private RelocatablePathMapperImpl mapper;
  99. private CompilerSettings myCommpilerSettings;
  100. private Map<String,GrepEntry> grepBase = new ConcurrentHashMap<String, GrepEntry>();
  101. public BaseDwarfProvider() {
  102. }
  103. public final void init(ProjectProxy project) {
  104. myCommpilerSettings = new CompilerSettings(project);
  105. mapper = new RelocatablePathMapperImpl(project);
  106. }
  107. @Override
  108. public boolean isApplicable(ProjectProxy project) {
  109. return true;
  110. }
  111. @Override
  112. public void stop() {
  113. isStoped.set(true);
  114. }
  115. protected List<SourceFileProperties> getSourceFileProperties(String[] objFileName, Progress progress, ProjectProxy project, Set<String> dlls, CompileLineStorage storage){
  116. CountDownLatch countDownLatch = new CountDownLatch(objFileName.length);
  117. RequestProcessor rp = new RequestProcessor("Parallel analyzing", CndUtils.getNumberCndWorkerThreads()); // NOI18N
  118. try{
  119. Map<String,SourceFileProperties> map = new ConcurrentHashMap<String,SourceFileProperties>();
  120. for (String file : objFileName) {
  121. MyRunnable r = new MyRunnable(countDownLatch, file, map, progress, project, dlls, storage);
  122. rp.post(r);
  123. }
  124. try {
  125. countDownLatch.await();
  126. } catch (InterruptedException ex) {
  127. Exceptions.printStackTrace(ex);
  128. }
  129. List<SourceFileProperties> list = new ArrayList<SourceFileProperties>();
  130. list.addAll(map.values());
  131. return list;
  132. } finally {
  133. PathCache.dispose();
  134. grepBase.clear();
  135. grepBase = new ConcurrentHashMap<String, GrepEntry>();
  136. getCommpilerSettings().dispose();
  137. }
  138. }
  139. protected FileSystem getFileSystem(ProjectProxy project) {
  140. if (project != null) {
  141. Project p = project.getProject();
  142. if (p != null) {
  143. return RemoteFileUtil.getProjectSourceFileSystem(p);
  144. }
  145. }
  146. return CndFileUtils.getLocalFileSystem();
  147. }
  148. protected RelocatablePathMapper getRelocatablePathMapper() {
  149. return mapper;
  150. }
  151. protected FileObject resolvePath(ProjectProxy project, String buildArtifact, final FileSystem fileSystem, SourceFileProperties f, String name) {
  152. FileObject fo = fileSystem.findResource(name);
  153. if (!(f instanceof Relocatable)) {
  154. return fo;
  155. }
  156. FS fs = new RelocatablePathMapperImpl.FS() {
  157. @Override
  158. public boolean exists(String path) {
  159. FileObject fo = fileSystem.findResource(path);
  160. if (fo != null && fo.isValid()) {
  161. return true;
  162. }
  163. return false;
  164. }
  165. };
  166. String sourceRoot = null;
  167. if (project != null) {
  168. sourceRoot = project.getSourceRoot();
  169. if (sourceRoot != null && sourceRoot.length() < 2) {
  170. sourceRoot = null;
  171. }
  172. }
  173. if (sourceRoot == null) {
  174. sourceRoot = PathUtilities.getDirName(buildArtifact);
  175. if (sourceRoot != null && sourceRoot.length() < 2) {
  176. sourceRoot = null;
  177. }
  178. }
  179. if (fo == null || !fo.isValid()) {
  180. ResolvedPath resolvedPath = mapper.getPath(name);
  181. if (resolvedPath == null) {
  182. if (sourceRoot != null) {
  183. if (mapper.discover(fs, sourceRoot, name)) {
  184. resolvedPath = mapper.getPath(name);
  185. fo = fileSystem.findResource(resolvedPath.getPath());
  186. if (fo != null && fo.isValid() && fo.isData()) {
  187. ((Relocatable) f).resetItemPath(resolvedPath, mapper, fs);
  188. return fo;
  189. }
  190. }
  191. }
  192. } else {
  193. fo = fileSystem.findResource(resolvedPath.getPath());
  194. if (fo != null && fo.isValid() && fo.isData()) {
  195. ((Relocatable) f).resetItemPath(resolvedPath, mapper, fs);
  196. return fo;
  197. }
  198. }
  199. }
  200. if (fo != null && fo.isData()) {
  201. name = fo.getPath();
  202. ResolvedPath resolvedPath = mapper.getPath(name);
  203. if (resolvedPath == null) {
  204. if (sourceRoot != null) {
  205. if (!name.startsWith(sourceRoot)) {
  206. if (mapper.discover(fs, sourceRoot, name)) {
  207. resolvedPath = mapper.getPath(name);
  208. FileObject resolved = fileSystem.findResource(resolvedPath.getPath());
  209. if (resolved != null && resolved.isValid() && resolved.isData()) {
  210. ((Relocatable) f).resetItemPath(resolvedPath, mapper, fs);
  211. return resolved;
  212. }
  213. }
  214. }
  215. }
  216. } else {
  217. FileObject resolved = fileSystem.findResource(resolvedPath.getPath());
  218. if (resolved != null && resolved.isValid() && resolved.isData()) {
  219. ((Relocatable) f).resetItemPath(resolvedPath, mapper, fs);
  220. return resolved;
  221. }
  222. }
  223. sourceRoot = null;
  224. if (project != null) {
  225. sourceRoot = project.getSourceRoot();
  226. if (sourceRoot != null && sourceRoot.length() < 2) {
  227. sourceRoot = null;
  228. }
  229. }
  230. if (sourceRoot == null) {
  231. sourceRoot = PathUtilities.getBaseName(name);
  232. if (sourceRoot != null && sourceRoot.length() < 2) {
  233. sourceRoot = null;
  234. }
  235. }
  236. if (sourceRoot != null) {
  237. ((Relocatable) f).resolveIncludePaths(sourceRoot, mapper, fs);
  238. }
  239. return fo;
  240. }
  241. return null;
  242. }
  243. private boolean processObjectFile(String file, Map<String, SourceFileProperties> map, Progress progress, ProjectProxy project, Set<String> dlls, CompileLineStorage storage) {
  244. if (isStoped.get()) {
  245. return true;
  246. }
  247. ProjectBridge bridge = null;
  248. if (project.getProject() != null) {
  249. bridge = new ProjectBridge(project.getProject());
  250. }
  251. String restrictSourceRoot = null;
  252. ProviderProperty p = getProperty(RESTRICT_SOURCE_ROOT);
  253. if (p != null) {
  254. String s = (String) p.getValue();
  255. if (s.length() > 0) {
  256. restrictSourceRoot = CndFileUtils.normalizeFile(new File(s)).getAbsolutePath();
  257. }
  258. }
  259. String restrictCompileRoot = null;
  260. p = getProperty(RESTRICT_COMPILE_ROOT);
  261. if (p != null) {
  262. String s = (String) p.getValue();
  263. if (s.length() > 0) {
  264. restrictCompileRoot = CndFileUtils.normalizeFile(new File(s)).getAbsolutePath();
  265. }
  266. }
  267. FileSystem fileSystem = getFileSystem(project);
  268. for (SourceFileProperties f : getSourceFileProperties(file, map, project, dlls, storage)) {
  269. if (isStoped.get()) {
  270. break;
  271. }
  272. String name = f.getItemPath();
  273. if (name == null) {
  274. continue;
  275. }
  276. if (restrictSourceRoot != null) {
  277. if (!name.startsWith(restrictSourceRoot)) {
  278. continue;
  279. }
  280. }
  281. FileObject fo = resolvePath(project, file, fileSystem, f, name);
  282. if (fo == null) {
  283. if (DwarfSource.LOG.isLoggable(Level.FINE)) {
  284. DwarfSource.LOG.log(Level.FINE, "Not Exist {0}", name); // NOI18N
  285. }
  286. continue;
  287. }
  288. boolean skip = false;
  289. if (restrictCompileRoot != null) {
  290. if (f.getCompilePath() != null && !f.getCompilePath().startsWith(restrictCompileRoot)) {
  291. skip = true;
  292. if (bridge != null) {
  293. String relPath = bridge.getRelativepath(fo.getPath());
  294. Item item = bridge.getProjectItem(relPath);
  295. if (item != null) {
  296. skip = false;
  297. }
  298. }
  299. }
  300. }
  301. if (skip) {
  302. if (DwarfSource.LOG.isLoggable(Level.FINE)) {
  303. DwarfSource.LOG.log(Level.FINE, "Skiped {0}", name); // NOI18N
  304. }
  305. continue;
  306. }
  307. name = fo.getPath();
  308. SourceFileProperties existed = map.get(name);
  309. if (existed == null) {
  310. map.put(name, f);
  311. } else {
  312. // Duplicated
  313. if (existed.getUserInludePaths().size() < f.getUserInludePaths().size()) {
  314. map.put(name, f);
  315. } else if (existed.getUserInludePaths().size() == f.getUserInludePaths().size()) {
  316. if (existed.getUserMacros().size() < f.getUserMacros().size()) {
  317. map.put(name, f);
  318. } else if (existed.getUserMacros().size() == f.getUserMacros().size()) {
  319. if (macrosWeight(existed) < macrosWeight(f)) {
  320. map.put(name, f);
  321. } else {
  322. // ignore
  323. }
  324. } else {
  325. // ignore
  326. }
  327. } else {
  328. // ignore
  329. }
  330. }
  331. }
  332. if (progress != null) {
  333. synchronized(progress) {
  334. progress.increment(file);
  335. }
  336. }
  337. return false;
  338. }
  339. private int macrosWeight(SourceFileProperties f) {
  340. int sum = 0;
  341. for(String m : f.getUserMacros().keySet()) {
  342. for(int i = 0; i < m.length(); i++) {
  343. sum += m.charAt(i);
  344. }
  345. }
  346. return sum;
  347. }
  348. protected ApplicableImpl sizeComilationUnit(String objFileName, Set<String> dlls, boolean findMain){
  349. int res = 0;
  350. int sunStudio = 0;
  351. Dwarf dump = null;
  352. Position position = null;
  353. List<String> errors = new ArrayList<String>();
  354. List<String> searchPaths = new ArrayList<String>();
  355. TreeMap<String,AtomicInteger> realRoots = new TreeMap<String,AtomicInteger>();
  356. TreeMap<String,AtomicInteger> roots = new TreeMap<String,AtomicInteger>();
  357. int foundDebug = 0;
  358. Map<String, AtomicInteger> compilers = new HashMap<String, AtomicInteger>();
  359. try{
  360. dump = new Dwarf(objFileName);
  361. Dwarf.CompilationUnitIterator iterator = dump.iteratorCompilationUnits();
  362. while (iterator.hasNext()) {
  363. CompilationUnitInterface cu = iterator.next();
  364. if (cu != null) {
  365. if (cu.getSourceFileName() == null) {
  366. continue;
  367. }
  368. String lang = cu.getSourceLanguage();
  369. if (lang == null) {
  370. continue;
  371. }
  372. foundDebug++;
  373. String path = cu.getSourceFileAbsolutePath();
  374. incrementRoot(path, realRoots);
  375. path = DiscoveryUtils.normalizeAbsolutePath(path);
  376. if (!CndFileUtils.isExistingFile(path)) {
  377. String fileFinder = Dwarf.fileFinder(objFileName, path);
  378. if (fileFinder != null) {
  379. fileFinder = DiscoveryUtils.normalizeAbsolutePath(fileFinder);
  380. if (!CndFileUtils.isExistingFile(fileFinder)) {
  381. continue;
  382. } else {
  383. path = fileFinder;
  384. }
  385. } else {
  386. continue;
  387. }
  388. }
  389. ItemProperties.LanguageKind language;
  390. if (LANG.DW_LANG_C.toString().equals(lang) ||
  391. LANG.DW_LANG_C89.toString().equals(lang) ||
  392. LANG.DW_LANG_C99.toString().equals(lang)) {
  393. language = ItemProperties.LanguageKind.C;
  394. res++;
  395. } else if (LANG.DW_LANG_C_plus_plus.toString().equals(lang)) {
  396. language = ItemProperties.LanguageKind.CPP;
  397. res++;
  398. } else if (LANG.DW_LANG_Fortran77.toString().equals(lang) ||
  399. LANG.DW_LANG_Fortran90.toString().equals(lang) ||
  400. LANG.DW_LANG_Fortran95.toString().equals(lang)) {
  401. language = ItemProperties.LanguageKind.Fortran;
  402. res++;
  403. } else {
  404. continue;
  405. }
  406. incrementRoot(path, roots);
  407. String compilerName = DwarfSource.extractCompilerName(cu, language);
  408. if (compilerName != null) {
  409. AtomicInteger count = compilers.get(compilerName);
  410. if (count == null) {
  411. count = new AtomicInteger();
  412. compilers.put(compilerName, count);
  413. }
  414. count.incrementAndGet();
  415. }
  416. if (DwarfSource.isSunStudioCompiler(cu)) {
  417. sunStudio++;
  418. }
  419. if (findMain && position == null) {
  420. if (cu.hasMain()) {
  421. if (cu instanceof CompilationUnit) {
  422. List<DwarfEntry> topLevelEntries = ((CompilationUnit)cu).getTopLevelEntries();
  423. for(DwarfEntry entry : topLevelEntries) {
  424. if (entry.getKind() == TAG.DW_TAG_subprogram) {
  425. if ("main".equals(entry.getName())) { // NOI18N
  426. if (entry.isExternal()) {
  427. //VIS visibility = entry.getVisibility();
  428. //if (visibility == VIS.DW_VIS_exported) {
  429. position = new MyPosition(path, entry.getLine());
  430. //}
  431. }
  432. }
  433. }
  434. }
  435. } else {
  436. position = new MyPosition(path, 1);
  437. }
  438. }
  439. }
  440. }
  441. }
  442. if (dlls != null) {
  443. SharedLibraries pubNames = dump.readPubNames();
  444. synchronized (dlls) {
  445. for (String dll : pubNames.getDlls()) {
  446. dlls.add(dll);
  447. }
  448. searchPaths.addAll(pubNames.getPaths());
  449. }
  450. }
  451. } catch (FileNotFoundException ex) {
  452. errors.add(NbBundle.getMessage(BaseDwarfProvider.class, "FileNotFoundException", objFileName)); // NOI18N
  453. if (DwarfSource.LOG.isLoggable(Level.FINE)) {
  454. DwarfSource.LOG.log(Level.FINE, "File not found {0}: {1}", new Object[]{objFileName, ex.getMessage()}); // NOI18N
  455. }
  456. } catch (WrongFileFormatException ex) {
  457. errors.add(NbBundle.getMessage(BaseDwarfProvider.class, "WrongFileFormatException", objFileName)); // NOI18N
  458. if (DwarfSource.LOG.isLoggable(Level.FINE)) {
  459. DwarfSource.LOG.log(Level.FINE, "Unsuported format of file {0}: {1}", new Object[]{objFileName, ex.getMessage()}); // NOI18N
  460. }
  461. } catch (IOException ex) {
  462. errors.add(NbBundle.getMessage(BaseDwarfProvider.class, "IOException", objFileName, ex.toString())); // NOI18N
  463. DwarfSource.LOG.log(Level.INFO, "Exception in file " + objFileName, ex); // NOI18N
  464. } catch (Exception ex) {
  465. errors.add(NbBundle.getMessage(BaseDwarfProvider.class, "Exception", objFileName, ex.toString())); // NOI18N
  466. DwarfSource.LOG.log(Level.INFO, "Exception in file " + objFileName, ex); // NOI18N
  467. } finally {
  468. if (dump != null) {
  469. dump.dispose();
  470. }
  471. }
  472. int max = 0;
  473. String top = "";
  474. for(Map.Entry<String, AtomicInteger> entry : compilers.entrySet()){
  475. if (entry.getValue().get() > max) {
  476. max = entry.getValue().get();
  477. top = entry.getKey();
  478. }
  479. }
  480. ArrayList<String> dllResult = null;
  481. if (dlls != null) {
  482. dllResult = new ArrayList<String>(dlls);
  483. }
  484. ArrayList<String> pathsResult = null;
  485. if (dlls != null) {
  486. pathsResult = new ArrayList<String>(searchPaths);
  487. }
  488. String commonRoot = getRoot(roots);
  489. if (res > 0) {
  490. return new ApplicableImpl(true, errors, top, res, sunStudio > res/2, dllResult, pathsResult, commonRoot, position);
  491. } else {
  492. if (errors.isEmpty()) {
  493. if (foundDebug > 0) {
  494. String notFoundRoot = getRoot(realRoots);
  495. errors.add(NbBundle.getMessage(BaseDwarfProvider.class, "BadDebugInformation", notFoundRoot)); // NOI18N
  496. } else {
  497. errors.add(NbBundle.getMessage(BaseDwarfProvider.class, "NotFoundDebugInformation", objFileName)); // NOI18N
  498. }
  499. }
  500. return new ApplicableImpl(false, errors, top, res, sunStudio > res/2, dllResult, pathsResult, commonRoot, position);
  501. }
  502. }
  503. private void incrementRoot(String path, Map<String,AtomicInteger> roots) {
  504. path = path.replace('\\', '/');
  505. int i = path.lastIndexOf('/');
  506. if (i >= 0) {
  507. String folder = path.substring(0, i);
  508. AtomicInteger val = roots.get(folder);
  509. if (val == null) {
  510. val = new AtomicInteger();
  511. roots.put(folder, val);
  512. }
  513. val.incrementAndGet();
  514. }
  515. }
  516. private String getCommonPart(String path, String commonRoot) {
  517. String[] splitPath = path.split("/"); // NOI18N
  518. ArrayList<String> list1 = new ArrayList<String>();
  519. boolean isUnixPath = false;
  520. for (int i = 0; i < splitPath.length; i++) {
  521. if (!splitPath[i].isEmpty()) {
  522. list1.add(splitPath[i]);
  523. } else {
  524. if (i == 0) {
  525. isUnixPath = true;
  526. }
  527. }
  528. }
  529. String[] splitRoot = commonRoot.split("/"); // NOI18N
  530. ArrayList<String> list2 = new ArrayList<String>();
  531. boolean isUnixRoot = false;
  532. for (int i = 0; i < splitRoot.length; i++) {
  533. if (!splitRoot[i].isEmpty()) {
  534. list2.add(splitRoot[i]);
  535. } else {
  536. if (i == 0) {
  537. isUnixRoot = true;
  538. }
  539. }
  540. }
  541. if (isUnixPath != isUnixRoot) {
  542. return "";
  543. }
  544. StringBuilder buf = new StringBuilder();
  545. if (isUnixPath) {
  546. buf.append('/');
  547. }
  548. for (int i = 0; i < Math.min(list1.size(), list2.size()); i++) {
  549. if (list1.get(i).equals(list2.get(i))) {
  550. if (i > 0) {
  551. buf.append('/');
  552. }
  553. buf.append(list1.get(i));
  554. } else {
  555. break;
  556. }
  557. }
  558. return buf.toString();
  559. }
  560. private String getRoot(TreeMap<String,AtomicInteger> roots) {
  561. ArrayList<String> res = new ArrayList<String>();
  562. ArrayList<AtomicInteger> resCount = new ArrayList<AtomicInteger>();
  563. String current = null;
  564. AtomicInteger currentCount = null;
  565. for(Map.Entry<String,AtomicInteger> entry : roots.entrySet()) {
  566. if (current == null) {
  567. current = entry.getKey();
  568. currentCount = new AtomicInteger(entry.getValue().get());
  569. continue;
  570. }
  571. String s = getCommonPart(entry.getKey(), current);
  572. String[] split = s.split("/"); // NOI18N
  573. int length = (split.length > 0 && split[0].isEmpty()) ? split.length - 1 : split.length;
  574. if (length >= 2) {
  575. current = s;
  576. currentCount.addAndGet(entry.getValue().get());
  577. } else {
  578. res.add(current);
  579. resCount.add(currentCount);
  580. current = entry.getKey();
  581. currentCount = new AtomicInteger(entry.getValue().get());
  582. }
  583. }
  584. if (current != null) {
  585. res.add(current);
  586. resCount.add(currentCount);
  587. }
  588. TreeMap<String,AtomicInteger> newRoots = new TreeMap<String, AtomicInteger>();
  589. String bestRoot = null;
  590. int bestCount = 0;
  591. for(int i = 0; i < res.size(); i++) {
  592. newRoots.put(res.get(i), resCount.get(i));
  593. if (bestRoot == null) {
  594. bestRoot = res.get(i);
  595. bestCount = resCount.get(i).get();
  596. } else {
  597. if (bestCount < resCount.get(i).get()) {
  598. bestRoot = res.get(i);
  599. bestCount = resCount.get(i).get();
  600. }
  601. }
  602. }
  603. return bestRoot;
  604. }
  605. protected List<SourceFileProperties> getSourceFileProperties(String objFileName, Map<String, SourceFileProperties> map, ProjectProxy project, Set<String> dlls, CompileLineStorage storage) {
  606. List<SourceFileProperties> list = new ArrayList<SourceFileProperties>();
  607. Dwarf dump = null;
  608. try {
  609. if (DwarfSource.LOG.isLoggable(Level.FINE)) {
  610. DwarfSource.LOG.log(Level.FINE, "Process file {0}", objFileName); // NOI18N
  611. }
  612. dump = new Dwarf(objFileName);
  613. Dwarf.CompilationUnitIterator iterator = dump.iteratorCompilationUnits();
  614. while (iterator.hasNext()) {
  615. CompilationUnitInterface cu = iterator.next();
  616. if (cu != null) {
  617. if (isStoped.get()) {
  618. break;
  619. }
  620. if (cu.getSourceFileName() == null) {
  621. if (DwarfSource.LOG.isLoggable(Level.FINE)) {
  622. DwarfSource.LOG.log(Level.FINE, "Compilation unit has broken name in file {0}", objFileName); // NOI18N
  623. }
  624. continue;
  625. }
  626. String lang = cu.getSourceLanguage();
  627. if (lang == null) {
  628. if (DwarfSource.LOG.isLoggable(Level.FINE)) {
  629. DwarfSource.LOG.log(Level.FINE, "Compilation unit has unresolved language in file {0}for {1}", new Object[]{objFileName, cu.getSourceFileName()}); // NOI18N
  630. }
  631. continue;
  632. }
  633. DwarfSource source = null;
  634. if (LANG.DW_LANG_C.toString().equals(lang)) {
  635. source = new DwarfSource(cu, ItemProperties.LanguageKind.C, ItemProperties.LanguageStandard.C, getCommpilerSettings(), grepBase, storage);
  636. } else if (LANG.DW_LANG_C89.toString().equals(lang)) {
  637. source = new DwarfSource(cu, ItemProperties.LanguageKind.C, ItemProperties.LanguageStandard.C89, getCommpilerSettings(), grepBase, storage);
  638. } else if (LANG.DW_LANG_C99.toString().equals(lang)) {
  639. source = new DwarfSource(cu, ItemProperties.LanguageKind.C, ItemProperties.LanguageStandard.C99, getCommpilerSettings(), grepBase, storage);
  640. } else if (LANG.DW_LANG_C_plus_plus.toString().equals(lang)) {
  641. source = new DwarfSource(cu, ItemProperties.LanguageKind.CPP, ItemProperties.LanguageStandard.Unknown, getCommpilerSettings(), grepBase, storage);
  642. } else if (LANG.DW_LANG_Fortran77.toString().equals(lang)) {
  643. source = new DwarfSource(cu, ItemProperties.LanguageKind.Fortran, ItemProperties.LanguageStandard.F77, getCommpilerSettings(), grepBase, storage);
  644. } else if (LANG.DW_LANG_Fortran90.toString().equals(lang)) {
  645. source = new DwarfSource(cu, ItemProperties.LanguageKind.Fortran, ItemProperties.LanguageStandard.F90, getCommpilerSettings(), grepBase, storage);
  646. } else if (LANG.DW_LANG_Fortran95.toString().equals(lang)) {
  647. source = new DwarfSource(cu, ItemProperties.LanguageKind.Fortran, ItemProperties.LanguageStandard.F95, getCommpilerSettings(), grepBase, storage);
  648. } else {
  649. if (DwarfSource.LOG.isLoggable(Level.FINE)) {
  650. DwarfSource.LOG.log(Level.FINE, "Unknown language: {0}", lang); // NOI18N
  651. }
  652. // Ignore other languages
  653. }
  654. if (source != null) {
  655. if (source.getCompilePath() == null) {
  656. if (DwarfSource.LOG.isLoggable(Level.FINE)) {
  657. DwarfSource.LOG.log(Level.FINE, "Compilation unit has NULL compile path in file {0}", objFileName); // NOI18N
  658. }
  659. continue;
  660. }
  661. String name = source.getItemPath();
  662. SourceFileProperties old = map.get(name);
  663. if (old != null && old.getUserInludePaths().size() > 0) {
  664. if (DwarfSource.LOG.isLoggable(Level.FINE)) {
  665. DwarfSource.LOG.log(Level.FINE, "Compilation unit already exist. Skip {0}", name); // NOI18N
  666. }
  667. // do not process processed item
  668. continue;
  669. }
  670. source.process(cu);
  671. list.add(source);
  672. }
  673. }
  674. }
  675. if (dlls != null) {
  676. SharedLibraries pubNames = dump.readPubNames();
  677. synchronized(dlls) {
  678. for(String dll : pubNames.getDlls()) {
  679. dlls.add(dll);
  680. }
  681. }
  682. }
  683. } catch (FileNotFoundException ex) {
  684. // Skip Exception
  685. if (DwarfSource.LOG.isLoggable(Level.FINE)) {
  686. DwarfSource.LOG.log(Level.FINE, "File not found {0}: {1}", new Object[]{objFileName, ex.getMessage()}); // NOI18N
  687. }
  688. } catch (WrongFileFormatException ex) {
  689. if (DwarfSource.LOG.isLoggable(Level.FINE)) {
  690. DwarfSource.LOG.log(Level.FINE, "Unsuported format of file {0}: {1}", new Object[]{objFileName, ex.getMessage()}); // NOI18N
  691. }
  692. } catch (IOException ex) {
  693. DwarfSource.LOG.log(Level.INFO, "Exception in file " + objFileName, ex); // NOI18N
  694. } catch (Exception ex) {
  695. DwarfSource.LOG.log(Level.INFO, "Exception in file " + objFileName, ex); // NOI18N
  696. } finally {
  697. if (dump != null) {
  698. dump.dispose();
  699. }
  700. }
  701. return list;
  702. }
  703. public CompilerSettings getCommpilerSettings(){
  704. return myCommpilerSettings;
  705. }
  706. public static class GrepEntry {
  707. ArrayList<String> includes = new ArrayList<String>();
  708. String firstMacro = null;
  709. int firstMacroLine = -1;
  710. }
  711. private class MyRunnable implements Runnable {
  712. private final String file;
  713. private final Map<String, SourceFileProperties> map;
  714. private final Progress progress;
  715. private final CountDownLatch countDownLatch;
  716. private final ProjectProxy project;
  717. private final Set<String> dlls;
  718. private final CompileLineStorage storage;
  719. private MyRunnable(CountDownLatch countDownLatch, String file, Map<String, SourceFileProperties> map, Progress progress, ProjectProxy project, Set<String> dlls, CompileLineStorage storage){
  720. this.file = file;
  721. this.map = map;
  722. this.progress = progress;
  723. this.countDownLatch = countDownLatch;
  724. this.project = project;
  725. this.dlls = dlls;
  726. this.storage = storage;
  727. }
  728. @Override
  729. public void run() {
  730. try {
  731. if (!isStoped.get()) {
  732. Thread.currentThread().setName("Parallel analyzing "+file); // NOI18N
  733. processObjectFile(file, map, progress, project, dlls, storage);
  734. }
  735. } finally {
  736. countDownLatch.countDown();
  737. }
  738. }
  739. }
  740. private static class MyPosition implements Position {
  741. private final String path;
  742. private final int line;
  743. private MyPosition(String path, int line){
  744. this.path = path;
  745. this.line = line;
  746. }
  747. @Override
  748. public String getFilePath() {
  749. return path;
  750. }
  751. @Override
  752. public int getLine() {
  753. return line;
  754. }
  755. @Override
  756. public String toString() {
  757. return path+":"+line; //NOI18N
  758. }
  759. }
  760. }